Code Monkey home page Code Monkey logo

gdclip's Introduction

GDClip: Clip + Godot

Status: Unsupported as a similar feature now exists in Godot 4.2!

Version: v0.2

Language: GDNative (C++)

Tested on Godot Engine v3.4.x (Windows + Linux/X11 + macOS)

GDClip is a Windows/macOS/Linux copy/paste clipboard library wrapper of clip for Godot programs.

This repo also includes a small demo featuring paste functionality and unit tests.

Dependencies

  • Common: make, scons, git
  • Linux: gcc, libx11-dev/libx11-devel, libpng-dev/libpng-devel
  • macOS: Command Line Tools for Xcode
  • Windows: MSYS2, mingw-w64-cross-gcc

Building and Running Demo

  1. Install required dependencies for your platform.
# Windows w/ MSYS2
pacman -S mingw-w64-cross-gcc scons make git
# macOS w/ Command Line Tools for Xcode
pip3 install scons
# Arch Linux
sudo pacman -S gcc libx11 libpng git make scons
  1. Clone repo and build for your platform:
git clone https://github.com/hansemro/GDClip
cd GDClip
make PLATFORM=<linux|osx|windows> build
  1. Open demo/project.godot in Godot.

  2. Copy an image into clipboard before running demo.

If successful, you should see the image pasted in the center of the demo window.

If cross-compiling, see Wiki for more information.

GDClip API

get_version

String get_version()

Returns GDClip library version as Godot String.

clear

bool clear()

Clears clipboard content and returns true if successful.

get_text

String get_text()

Returns text content from clipboard as Godot String.

If there is no text in the clipboard, then an empty String is returned.

set_text

bool set_text(String text)

Set text in clipboard and returns true if successful.

If text is empty, then this function should return true.

has_image

bool has_image()

Returns true if clipboard contains an image and false otherwise.

get_image_size

PoolIntArray get_image_size()

Returns [width, height] of image in clipboard as PoolIntArray.

If there is no image, then this function returns [0, 0].

get_image_as_pbarray

PoolByteArray get_image_as_pbarray()

Returns PoolByteArray containing RGBA8888 values of the image in the clipboard.

If there is no image in the clipboard, then this function returns an empty PoolByteArray.

set_image_from_pbarray

bool set_image_from_pbarray(PoolByteArray image, uint32_t width, uint32_t height)

Set image in clipboard from PoolByteArray and returns true if successful.

Returns false if width or height is zero, or if PoolByteArray image size is less than width*height*sizeof(uint32_t).

Usage in GDScript Project

  1. Clone repo inside Godot project and build library file(s) at GDClip/bin
git clone https://github.com/hansemro/GDClip
cd GDClip
make PLATFORM=<linux|osx|windows> build
  1. Create gdclip.gdnlib (in GDClip/bin/) containing the following:
[general]

singleton=true
load_once=true
symbol_prefix="godot_"
reloadable=false

[entry]

X11.64="res://GDClip/bin/x11/libgdclip.so"
Windows.64="res://GDClip/bin/win64/libgdclip.dll"
OSX.64="res://GDClip/bin/osx/libgdclip.dylib"

[dependencies]

X11.64=[]
Windows.64=[]
OSX.64=[]

Update library paths if necessary.

  1. Create gdclip.gdns file (in GDClip/bin/) containing the following:
[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="res://GDClip/bin/gdclip.gdnlib" type="GDNativeLibrary" id=1]

[resource]
resource_name = "gdclip"
class_name = "GDClip"
library = ExtResource( 1 )

Update path if necessary.

  1. Load gdclip.gdns as a resource in an appropriate GDScript file.
onready var gdclip = preload("res://GDClip/bin/gdclip.gdns").new()
  1. Access GDClip API functions as needed.
# get text from clipboard
var text = gdclip.get_text()

# write text to clipboard
gdclip.set_text("Hello World")

# clear clipboard
gdclip.clear()

# get image and image size from clipboard and add to scene
var pbarray = gdclip.get_image_as_pbarray()
var size = gdclip.get_image_size()
var image = Image.new()
image.create_from_data(size[0], size[1], false, Image.FORMAT_RGBA8, pbarray)
var texture = ImageTexture.new()
texture.create_from_image(image)
var spr = Sprite.new()
spr.set_texture(texture)
spr.position = Vector2(100, 100)
SomeScene.add_child(spr)

License

gdclip's People

Contributors

hansemro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gdclip's Issues

Some pasted images are transparent

  • OS: Manjaro
  • Desktop Environment: KDE (X11)
  • Image viewer used for copying image: Gnome Image Viewer (eog)

Issue: Some images copied from EOG become transparent in GDClip demo

clip library expects image data to be non-premultiplied (alpha). However, if the image is premultiplied, then GDClip sends out a transparent image. <- Incorrect guess.

GDClip assumes image spec from clip is correct and does not check to see if alpha shift & mask are valid. In particular, when handling RGB images without alpha channel, clip will set bits_per_pixel to 32 and alpha_{mask,shift} to 0 which yield 0-alpha-valued RGBA images (fully transparent).

Issue seems to be isolated to Linux. Both sample images below work fine on Windows.

Steps to replicate

  1. Download pixel samples containing 1x1 RGB8 and red pixel RGBA8 png images (named broken_red_pixel.png and working_red_pixel.png, respectively).
    red_pixel_test.zip

  2. Copy image and run GDClip demo

Only RGBA8 red pixel image works.

Workaround options

Here are some potential solutions:

1. Ignore problem since all PNGs should be non-premultiplied.
2. Ignore alpha channel values and let image be opaque.

https://www.adriancourreges.com/blog/2017/05/09/beware-of-transparent-pixels/
https://developer.nvidia.com/content/alpha-blending-pre-or-not-pre
https://nigeltao.github.io/blog/2022/premultiplied-alpha.html
https://github.com/Tom94/tev/blob/94218f2e4bfb92c0ff69cb5ba65ec0af72c27741/src/imageio/ClipboardImageLoader.cpp#L82-L100

godot-cpp fails to build in MSYS2 environment

  • OS: Windows 10 Pro
  • Shell/Environment: MSYS2 downloaded from https://www.msys2.org/
  • MSYS2 packages installed with pacman -S mingw-w64-cross-gcc scons make git:
$ pacman -S mingw-w64-cross-gcc scons make git
resolving dependencies...
looking for conflicting packages...

Packages (45) heimdal-7.7.0-3  isl-0.22.1-1  libcbor-0.9.0-1  libfido2-1.10.0-1
              mingw-w64-cross-binutils-2.37-1
              mingw-w64-cross-crt-git-9.0.0.6158.1c773877-1
              mingw-w64-cross-headers-git-9.0.0.6158.1c773877-1
              mingw-w64-cross-windows-default-manifest-6.4-2
              mingw-w64-cross-winpthreads-git-9.0.0.6158.1c773877-1
              mpc-1.2.1-1  mpdecimal-2.5.0-1  openssh-8.9p1-3
              perl-Authen-SASL-2.16-2  perl-Clone-0.45-2
              perl-Convert-BinHex-1.125-1  perl-Encode-Locale-1.05-1
              perl-Error-0.17029-1  perl-File-Listing-6.14-1
              perl-HTML-Parser-3.76-1  perl-HTML-Tagset-3.20-2
              perl-HTTP-Cookies-6.10-1  perl-HTTP-Daemon-6.12-1
              perl-HTTP-Date-6.05-1  perl-HTTP-Message-6.36-1
              perl-HTTP-Negotiate-6.01-2  perl-IO-HTML-1.004-1
              perl-IO-Socket-SSL-2.074-1  perl-IO-Stringy-2.113-1
              perl-LWP-MediaTypes-6.04-1  perl-MIME-tools-5.509-1
              perl-MailTools-2.21-1  perl-Net-HTTP-6.22-1
              perl-Net-SMTP-SSL-1.04-1  perl-Net-SSLeay-1.90-1
              perl-TermReadKey-2.38-2  perl-TimeDate-2.33-1
              perl-Try-Tiny-0.31-1  perl-URI-5.10-1  perl-WWW-RobotRules-6.02-2
              perl-libwww-6.57-1  python-3.9.9-2  git-2.35.1-3  make-4.3-3
              mingw-w64-cross-gcc-10.2.0-1  scons-3.1.2-7

Issue: godot-cpp/SConstruct fails to recognize MSYS2 environment as a valid platform and raises an error.

In MSYS2-provided python3.9, sys.platform reports cygwin which does not fall under any category to set host_platform in godot-cpp/SConstruct.

Error:

$ make PLATFORM=windows build
cd godot-cpp && scons use_mingw=true platform=windows target=debug generate_bindings=yes -j4
scons: Reading SConscript files ...
ValueError: Could not detect platform automatically, please specify with platform=<platform>:
  File "/c/Users/Hansem Ro/Documents/GDClip/godot-cpp/SConstruct", line 80:
    raise ValueError("Could not detect platform automatically, please specify with platform=<platform>")
make: *** [Makefile:24: godot-cpp/bin/libgodot-cpp.windows.debug.64.a] Error 2

Workaround: Treat cygwin as linux host platform:

Replace if sys.platform.startswith("linux"): (line 71) in godot-cpp/SConstruct with if sys.platform.startswith("linux") or sys.platform == "cygwin":

With this workaround, gdclip library successfully builds and works perfectly in GDClip demo.

Comments

I am unsure why sys.platform reports "cygwin" when other users report "msys".

set_image_from_pbarray crashes demo

OS: Windows, Linux

Issue: Godot demo stalls and then closes when attempting to set large images to clipboard.

Issue is caused by failing to allocate correct number of entries to store image.

Comments

Setting 10x10 image works unreliably. Sometimes it works, but many times it causes demo to crash.

[Linux/X11] Image paste does not work after GDClip demo closes

OS: Manjaro
Desktop Environment: KDE, XFCE

Issue: After GDClip demo closes in KDE, clipboard manager fails to store clipboard image from the demo.

The image that gets copied before the demo's execution should be restored after demo finishes its tests (which overwrites clipboard data). The restored image can be pasted while the demo application is running, but fails to work when the demo closes. According to clip_x11 source, Manager hands off clipboard data to a clipboard manager when it is deconstructed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.