| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_//_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* X11 clipboard handling. |
| 12 |
* |
| 13 |
* By Beoran. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <X11/Xlib.h> |
| 19 |
#include <X11/Xatom.h> |
| 20 |
|
| 21 |
#include "allegro5/allegro.h" |
| 22 |
#include "allegro5/internal/aintern_bitmap.h" |
| 23 |
#include "allegro5/internal/aintern_x.h" |
| 24 |
#include "allegro5/internal/aintern_xclipboard.h" |
| 25 |
#include "allegro5/internal/aintern_xcursor.h" |
| 26 |
#include "allegro5/internal/aintern_xdisplay.h" |
| 27 |
#include "allegro5/internal/aintern_xsystem.h" |
| 28 |
|
| 29 |
#ifdef ALLEGRO_RASPBERRYPI |
| 30 |
#include "allegro5/internal/aintern_raspberrypi.h" |
| 31 |
#define ALLEGRO_SYSTEM_XGLX ALLEGRO_SYSTEM_RASPBERRYPI |
| 32 |
#define ALLEGRO_DISPLAY_XGLX ALLEGRO_DISPLAY_RASPBERRYPI |
| 33 |
#endif |
| 34 |
|
| 35 |
ALLEGRO_DEBUG_CHANNEL("clipboard") |
| 36 |
|
| 37 |
|
| 38 |
void _al_xwin_display_selection_notify(ALLEGRO_DISPLAY *display, XSelectionEvent *xselection) |
| 39 |
{ |
| 40 |
(void) display; (void) xselection; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
void _al_xwin_display_selection_request(ALLEGRO_DISPLAY *display, XSelectionRequestEvent *xselectionrequest) |
| 45 |
{ |
| 46 |
(void) display; |
| 47 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 48 |
Display *xdisplay = system->x11display; |
| 49 |
|
| 50 |
|
| 51 |
XSelectionRequestEvent *req; |
| 52 |
XEvent sevent = { 0 }; |
| 53 |
int seln_format; |
| 54 |
unsigned long nbytes; |
| 55 |
unsigned long overflow; |
| 56 |
unsigned char *seln_data; |
| 57 |
|
| 58 |
req = xselectionrequest; |
| 59 |
|
| 60 |
ALLEGRO_DEBUG("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", xdisplay, |
| 61 |
req->requestor, req->target); |
| 62 |
|
| 63 |
memset(&sevent, 0, sizeof(sevent)); |
| 64 |
sevent.xany.type = SelectionNotify; |
| 65 |
sevent.xselection.selection = req->selection; |
| 66 |
sevent.xselection.target = None; |
| 67 |
sevent.xselection.property = None; |
| 68 |
sevent.xselection.requestor = req->requestor; |
| 69 |
sevent.xselection.time = req->time; |
| 70 |
|
| 71 |
if (XGetWindowProperty(xdisplay, DefaultRootWindow(xdisplay), |
| 72 |
XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target, |
| 73 |
&sevent.xselection.target, &seln_format, &nbytes, |
| 74 |
&overflow, &seln_data) == Success) { |
| 75 |
Atom XA_TARGETS = XInternAtom(xdisplay, "TARGETS", 0); |
| 76 |
if (sevent.xselection.target == req->target) { |
| 77 |
XChangeProperty(xdisplay, req->requestor, req->property, |
| 78 |
sevent.xselection.target, seln_format, PropModeReplace, |
| 79 |
seln_data, nbytes); |
| 80 |
sevent.xselection.property = req->property; |
| 81 |
} else if (XA_TARGETS == req->target) { |
| 82 |
Atom SupportedFormats[] = { sevent.xselection.target, XA_TARGETS }; |
| 83 |
XChangeProperty(xdisplay, req->requestor, req->property, |
| 84 |
XA_ATOM, 32, PropModeReplace, |
| 85 |
(unsigned char *)SupportedFormats, |
| 86 |
sizeof(SupportedFormats)/sizeof(*SupportedFormats)); |
| 87 |
sevent.xselection.property = req->property; |
| 88 |
} |
| 89 |
XFree(seln_data); |
| 90 |
} |
| 91 |
XSendEvent(xdisplay, req->requestor, False, 0, &sevent); |
| 92 |
XSync(xdisplay, False); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/* Waits for a selection (copy/paste or DND event) */ |
| 97 |
static bool _al_display_xglx_await_selection_event(ALLEGRO_DISPLAY *d) |
| 98 |
{ |
| 99 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 100 |
ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)d; |
| 101 |
ALLEGRO_TIMEOUT timeout; |
| 102 |
|
| 103 |
ALLEGRO_DEBUG("Awaiting selection event\n"); |
| 104 |
|
| 105 |
XSync(system->x11display, False); |
| 106 |
|
| 107 |
/* Wait until the selection event is notified. |
| 108 |
* Don't wait forever if an event never comes. |
| 109 |
*/ |
| 110 |
al_init_timeout(&timeout, 1.0); |
| 111 |
if (_al_cond_timedwait(&glx->selectioned, &system->lock, &timeout) == -1) { |
| 112 |
ALLEGRO_ERROR("Timeout while waiting for selection event.\n"); |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
static bool xdpy_set_clipboard_text(ALLEGRO_DISPLAY *display, const char *text) |
| 121 |
{ |
| 122 |
|
| 123 |
ALLEGRO_DISPLAY_XGLX *glx = (void *)display; |
| 124 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 125 |
Display *xdisplay = system->x11display; |
| 126 |
Window xwindow = glx->window; |
| 127 |
|
| 128 |
Atom format; |
| 129 |
Atom XA_CLIPBOARD = XInternAtom(xdisplay, "CLIPBOARD", 0); |
| 130 |
|
| 131 |
/* Get the window that will own the selection */ |
| 132 |
if (xwindow == None) { |
| 133 |
ALLEGRO_DEBUG("Couldn't find a window to own the selection\n"); |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
/* Save the selection on the root window */ |
| 138 |
/* If you don't support UTF-8, you might use XA_STRING here */ |
| 139 |
format = XInternAtom(xdisplay, "UTF8_STRING", False); |
| 140 |
XChangeProperty(xdisplay, DefaultRootWindow(xdisplay), |
| 141 |
XA_CUT_BUFFER0, format, 8, PropModeReplace, |
| 142 |
(const unsigned char *)text, strlen(text)); |
| 143 |
|
| 144 |
if (XA_CLIPBOARD != None && |
| 145 |
XGetSelectionOwner(xdisplay, XA_CLIPBOARD) != xwindow) { |
| 146 |
XSetSelectionOwner(xdisplay, XA_CLIPBOARD, xwindow, CurrentTime); |
| 147 |
} |
| 148 |
|
| 149 |
if (XGetSelectionOwner(xdisplay, XA_PRIMARY) != xwindow) { |
| 150 |
XSetSelectionOwner(xdisplay, XA_PRIMARY, xwindow, CurrentTime); |
| 151 |
} |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
static char *xdpy_get_clipboard_text(ALLEGRO_DISPLAY *display) |
| 158 |
{ |
| 159 |
ALLEGRO_DISPLAY_XGLX *glx = (void *)display; |
| 160 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 161 |
Display *xdisplay = system->x11display; |
| 162 |
Window xwindow = glx->window; |
| 163 |
|
| 164 |
Atom format; |
| 165 |
Window owner; |
| 166 |
Atom selection; |
| 167 |
Atom seln_type; |
| 168 |
int seln_format; |
| 169 |
unsigned long nbytes; |
| 170 |
unsigned long overflow; |
| 171 |
unsigned char *src; |
| 172 |
char *text = NULL; |
| 173 |
|
| 174 |
Atom XA_CLIPBOARD = XInternAtom(xdisplay, "CLIPBOARD", 0); |
| 175 |
if (XA_CLIPBOARD == None) { |
| 176 |
ALLEGRO_DEBUG("Couldn't access X clipboard\n"); |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
/* Get the window that holds the selection */ |
| 181 |
format = XInternAtom(xdisplay, "UTF8_STRING", 0); |
| 182 |
owner = XGetSelectionOwner(xdisplay, XA_CLIPBOARD); |
| 183 |
if ((owner == None) || (owner == xwindow)) { |
| 184 |
owner = DefaultRootWindow(xdisplay); |
| 185 |
selection = XA_CUT_BUFFER0; |
| 186 |
} else { |
| 187 |
/* Request that the selection owner copy the data to our window. */ |
| 188 |
owner = xwindow; |
| 189 |
selection = XInternAtom(xdisplay, "ALLEGRO_SELECTION", False); |
| 190 |
XConvertSelection(xdisplay, XA_CLIPBOARD, format, selection, owner, |
| 191 |
CurrentTime); |
| 192 |
|
| 193 |
_al_mutex_lock(&system->lock); |
| 194 |
glx->is_selectioned = false; |
| 195 |
bool result = _al_display_xglx_await_selection_event(display); |
| 196 |
_al_mutex_unlock(&system->lock); |
| 197 |
if (!result || !glx->is_selectioned) |
| 198 |
return NULL; |
| 199 |
} |
| 200 |
|
| 201 |
if (XGetWindowProperty(xdisplay, owner, selection, 0, INT_MAX/4, False, |
| 202 |
format, &seln_type, &seln_format, &nbytes, &overflow, &src) |
| 203 |
== Success) { |
| 204 |
if (seln_type == format) { |
| 205 |
text = (char *)al_malloc(nbytes+1); |
| 206 |
if (text) { |
| 207 |
memcpy(text, src, nbytes); |
| 208 |
text[nbytes] = '\0'; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
XFree(src); |
| 213 |
return text; |
| 214 |
} |
| 215 |
|
| 216 |
static bool xdpy_has_clipboard_text(ALLEGRO_DISPLAY *display) |
| 217 |
{ |
| 218 |
char *text = xdpy_get_clipboard_text(display); |
| 219 |
|
| 220 |
if (!text) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
al_free(text); |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
void _al_xwin_add_clipboard_functions(ALLEGRO_DISPLAY_INTERFACE *vt) |
| 231 |
{ |
| 232 |
vt->set_clipboard_text = xdpy_set_clipboard_text; |
| 233 |
vt->get_clipboard_text = xdpy_get_clipboard_text; |
| 234 |
vt->has_clipboard_text = xdpy_has_clipboard_text; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/* vim: set sts=3 sw=3 et: */ |