Verified Commit 3e1d6b16 authored by Thomas Bernard's avatar Thomas Bernard 💬

Copy support for X11

parent cd74f5da
This diff is collapsed.
......@@ -141,6 +141,7 @@ void Test_PNG(T_IO_Context *, FILE *);
void Load_PNG(T_IO_Context *);
void Save_PNG(T_IO_Context *);
void Load_PNG_Sub(T_IO_Context * context, FILE * file, const char * memory_buffer, unsigned long memory_buffer_size);
void Save_PNG_Sub(T_IO_Context * context, FILE * file, char * * buffer, unsigned long * buffer_size);
#endif
// -- INFO (Amiga ICONS) ----------------------------------------------------
......
......@@ -38,6 +38,7 @@
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#endif
#include "gfx2log.h"
......@@ -522,6 +523,60 @@ static int Handle_SelectionNotify(const XSelectionEvent* xselection)
}
return user_feedback_required;
}
static void Handle_SelectionRequest(const XSelectionRequestEvent* xselectionrequest)
{
XSelectionEvent xselection;
char * target_name;
char * property_name;
Atom png;
#if defined(SDL_VIDEO_DRIVER_X11)
Display * X11_display;
Window X11_window;
if (!GFX2_Get_X11_Display_Window(&X11_display, &X11_window))
{
GFX2_Log(GFX2_ERROR, "Failed to get X11 display and window\n");
return;
}
#endif
png = XInternAtom(X11_display, "image/png", False);
target_name = XGetAtomName(X11_display, xselectionrequest->target);
property_name = XGetAtomName(X11_display, xselectionrequest->property);
GFX2_Log(GFX2_DEBUG, "Handle_SelectionRequest target=%s property=%s\n", target_name, property_name);
XFree(target_name);
XFree(property_name);
xselection.type = SelectionNotify;
xselection.requestor = xselectionrequest->requestor;
xselection.selection = xselectionrequest->selection;
xselection.target = xselectionrequest->target;
xselection.property = xselectionrequest->property;
xselection.time = xselectionrequest->time;
if (xselectionrequest->target == XInternAtom(X11_display, "TARGETS", False))
{
Atom targets[1];
targets[0] = png;
XChangeProperty(X11_display, xselectionrequest->requestor, xselectionrequest->property,
XA_ATOM, 32, PropModeReplace,
(unsigned char *)targets, 1);
}
else if (xselectionrequest->target == png)
{
XChangeProperty(X11_display, xselectionrequest->requestor, xselectionrequest->property,
png, 8, PropModeReplace,
(unsigned char *)X11_clipboard, X11_clipboard_size);
}
else
{
xselection.property = None; // refuse
}
XSendEvent(X11_display, xselectionrequest->requestor, True, NoEventMask, (XEvent *)&xselection);
}
#endif
#if defined(USE_SDL)
......@@ -1351,6 +1406,18 @@ int Get_input(int sleep_time)
if (Handle_SelectionNotify(&(xevent.xselection)))
user_feedback_required = 1;
break;
case SelectionRequest:
Handle_SelectionRequest(&(xevent.xselectionrequest));
break;
case SelectionClear:
GFX2_Log(GFX2_DEBUG, "X11 SelectionClear\n");
if (X11_clipboard)
{
free(X11_clipboard);
X11_clipboard_size = 0;
}
SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
break;
case ButtonPress:
case ButtonRelease:
case MotionNotify:
......@@ -1687,6 +1754,23 @@ int Get_input(int sleep_time)
if (Handle_SelectionNotify(&event.xselection))
user_feedback_required = 1;
break;
case SelectionClear:
GFX2_Log(GFX2_DEBUG, "X11 SelectionClear\n");
if (X11_clipboard)
{
free(X11_clipboard);
X11_clipboard_size = 0;
}
break;
case SelectionRequest:
Handle_SelectionRequest(&event.xselectionrequest);
break;
case ReparentNotify:
GFX2_Log(GFX2_DEBUG, "X11 ReparentNotify\n");
break;
case MapNotify:
GFX2_Log(GFX2_DEBUG, "X11 MapNotify\n");
break;
default:
GFX2_Log(GFX2_INFO, "X11 event.type = %d not handled\n", event.type);
}
......
......@@ -1566,6 +1566,46 @@ static void Save_ClipBoard_Image(T_IO_Context * context)
}
}
CloseClipboard();
#elif defined(USE_X11) || (defined(SDL_VIDEO_DRIVER_X11) && !defined(NO_X11))
Atom selection;
#if defined(SDL_VIDEO_DRIVER_X11)
Display * X11_display;
Window X11_window;
//int old_wmevent_state;
if (!GFX2_Get_X11_Display_Window(&X11_display, &X11_window))
{
GFX2_Log(GFX2_ERROR, "Failed to get X11 display and window\n");
return;
}
if (X11_display == NULL)
{
#if defined(USE_SDL)
char video_driver_name[32];
GFX2_Log(GFX2_WARNING, "X11 display is NULL. X11 is needed for Copy/Paste. SDL video driver is currently %s\n", SDL_VideoDriverName(video_driver_name, sizeof(video_driver_name)));
#elif defined(USE_SDL2)
GFX2_Log(GFX2_WARNING, "X11 display is NULL. X11 is needed for Copy/Paste. SDL video driver is currently %s\n", SDL_GetCurrentVideoDriver());
#endif
return;
}
#endif
File_error = 0;
if (X11_clipboard != NULL)
{
free(X11_clipboard);
X11_clipboard = NULL;
X11_clipboard_size = 0;
}
Save_PNG_Sub(context, NULL, &X11_clipboard, &X11_clipboard_size);
if (!File_error)
{
#if defined(USE_SDL) || defined(USE_SDL2)
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
#endif
selection = XInternAtom(X11_display, "CLIPBOARD", False);
XSetSelectionOwner(X11_display, selection, X11_window, CurrentTime);
}
#else
GFX2_Log(GFX2_ERROR, "Save_ClipBoard_Image() not implemented on this platform yet\n");
File_error = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment