mirror of
https://github.com/raysan5/raylib.git
synced 2026-04-26 16:47:25 -04:00
[platform][glfw][rgfw] Implementing clipboard image linux (#5603)
* Testing linux implementation * Add implementation for ClipboardImage on Linux * Adding another check to make sure that only X11 include X11 libs * Adding some comments to explain the magic numbers
This commit is contained in:
@ -1043,6 +1043,11 @@ const char *GetClipboardText(void)
|
|||||||
return glfwGetClipboardString(platform.handle);
|
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
|
// Get clipboard image
|
||||||
Image GetClipboardImage(void)
|
Image GetClipboardImage(void)
|
||||||
{
|
{
|
||||||
@ -1059,6 +1064,51 @@ Image GetClipboardImage(void)
|
|||||||
|
|
||||||
if (bmpData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data.");
|
if (bmpData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data.");
|
||||||
else image = LoadImageFromMemory(".bmp", (const unsigned char *)bmpData, (int)dataSize);
|
else image = LoadImageFromMemory(".bmp", (const unsigned char *)bmpData, (int)dataSize);
|
||||||
|
|
||||||
|
#elif defined(__linux__) && defined(_GLFW_X11)
|
||||||
|
|
||||||
|
// Implementation based on 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
|
||||||
|
XEvent ev;
|
||||||
|
XNextEvent(dpy, &ev);
|
||||||
|
|
||||||
|
Atom actualType;
|
||||||
|
int actualFormat;
|
||||||
|
unsigned long nitems, bytesAfter;
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (data != NULL) {
|
||||||
|
image = LoadImageFromMemory(".png", data, (int)nitems);
|
||||||
|
XFree(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
XDestroyWindow(dpy, win);
|
||||||
|
XCloseDisplay(dpy);
|
||||||
#else
|
#else
|
||||||
TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
|
TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
|
||||||
#endif // defined(_WIN32)
|
#endif // defined(_WIN32)
|
||||||
|
|||||||
@ -999,6 +999,9 @@ const char *GetClipboardText(void)
|
|||||||
#define WINBASE_ALREADY_INCLUDED
|
#define WINBASE_ALREADY_INCLUDED
|
||||||
#define WINGDI_ALREADY_INCLUDED
|
#define WINGDI_ALREADY_INCLUDED
|
||||||
#include "../external/win32_clipboard.h"
|
#include "../external/win32_clipboard.h"
|
||||||
|
#elif defined(__linux__) && defined(DRGFW_X11)
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
#include <X11/Xatom.h>
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1006,6 +1009,7 @@ const char *GetClipboardText(void)
|
|||||||
Image GetClipboardImage(void)
|
Image GetClipboardImage(void)
|
||||||
{
|
{
|
||||||
Image image = { 0 };
|
Image image = { 0 };
|
||||||
|
|
||||||
#if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES
|
#if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
@ -1018,6 +1022,51 @@ Image GetClipboardImage(void)
|
|||||||
|
|
||||||
if (fileData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data");
|
if (fileData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data");
|
||||||
else image = LoadImageFromMemory(".bmp", (const unsigned char *)fileData, dataSize);
|
else image = LoadImageFromMemory(".bmp", (const unsigned char *)fileData, dataSize);
|
||||||
|
|
||||||
|
#elif defined(__linux__) && defined(DRGFW_X11)
|
||||||
|
|
||||||
|
// Implementation based on 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
|
||||||
|
XEvent ev;
|
||||||
|
XNextEvent(dpy, &ev);
|
||||||
|
|
||||||
|
Atom actualType;
|
||||||
|
int actualFormat;
|
||||||
|
unsigned long nitems, bytesAfter;
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (data != NULL) {
|
||||||
|
image = LoadImageFromMemory(".png", data, (int)nitems);
|
||||||
|
XFree(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
XDestroyWindow(dpy, win);
|
||||||
|
XCloseDisplay(dpy);
|
||||||
#else
|
#else
|
||||||
TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_DESKTOP_RGFW doesn't implement GetClipboardImage() for this OS");
|
TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_DESKTOP_RGFW doesn't implement GetClipboardImage() for this OS");
|
||||||
#endif // defined(_WIN32)
|
#endif // defined(_WIN32)
|
||||||
|
|||||||
Reference in New Issue
Block a user