Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions src/platforms/rcore_desktop_glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,6 @@ const char *GetClipboardText(void)
return glfwGetClipboardString(platform.handle);
}

#if SUPPORT_CLIPBOARD_IMAGE && defined(__linux__) && defined(_GLFW_X11)
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif

// Get clipboard image
Image GetClipboardImage(void)
{
Expand All @@ -1066,51 +1061,46 @@ Image GetClipboardImage(void)
else image = LoadImageFromMemory(".bmp", (const unsigned char *)bmpData, (int)dataSize);

#elif defined(__linux__) && defined(_GLFW_X11)
Comment thread
raysan5 marked this conversation as resolved.

// REF: https://github.com/ColleagueRiley/Clipboard-Copy-Paste/blob/main/x11.c
Display *dpy = XOpenDisplay(NULL);
if (!dpy) return image;

Window root = DefaultRootWindow(dpy);
Window win = XCreateSimpleWindow(
dpy, // The connection to the X Server
root, // The 'Parent' window (usually the desktop/root)
0, 0, // X and Y position on the screen
1, 1, // Width and Height (1x1 pixel)
0, // Border width
0, // Border color
0 // Background color
);

Atom clipboard = XInternAtom(dpy, "CLIPBOARD", False);
Atom targetType = XInternAtom(dpy, "image/png", False); // Ask for PNG
Atom property = XInternAtom(dpy, "RAYLIB_CLIPBOARD_MANAGER", False);

// Request the data: "Convert whatever is in CLIPBOARD to image/png and put it in RAYLIB_CLIPBOARD_MANAGER"
XConvertSelection(dpy, clipboard, targetType, property, win, CurrentTime);

// Wait for the SelectionNotify event

static Atom clipboard = 0;
static Atom targetType = 0;
static Atom property = 0;

Display *display = glfwGetX11Display();
XID window = glfwGetX11Window(platform.handle);

// Lazy-load X11 atoms
if(clipboard == 0)
{
clipboard = XInternAtom(display, "CLIPBOARD", False);
targetType = XInternAtom(display, "image/png", False);
property = XInternAtom(display, "RAYLIB_CLIPBOARD_MANAGER", False);
}

XConvertSelection(display, clipboard, targetType, property, window, CurrentTime);
XSync(display, 0);

XEvent ev = { 0 };
XNextEvent(dpy, &ev);

// Keep calling until we get SelectionNotify
while (XCheckTypedEvent(display, SelectionNotify, &ev) == False);

Atom actualType = { 0 };
int actualFormat = 0;
unsigned long nitems = 0;
unsigned long bytesAfter = 0;
unsigned char *data = NULL;

// Read the data from our ghost window's property
XGetWindowProperty(dpy, win, property, 0, ~0L, False, AnyPropertyType,
&actualType, &actualFormat, &nitems, &bytesAfter, &data);
XGetWindowProperty(display, window, property, 0, ~0L, False, AnyPropertyType,
&actualType, &actualFormat, &nitems, &bytesAfter, &data);

if (data != NULL)
{
image = LoadImageFromMemory(".png", data, (int)nitems);
XFree(data);
}

XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
#else
TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
#endif // _WIN32
Expand Down