| 1 |
// Note: adapted from SDL2 |
| 2 |
#include <sys/time.h> |
| 3 |
#include <ctype.h> |
| 4 |
|
| 5 |
#include <X11/Xatom.h> |
| 6 |
|
| 7 |
#include "allegro5/allegro.h" |
| 8 |
#include "allegro5/platform/aintunix.h" |
| 9 |
#include "allegro5/internal/aintern_x.h" |
| 10 |
#include "allegro5/internal/aintern_xdisplay.h" |
| 11 |
#include "allegro5/internal/aintern_xevents.h" |
| 12 |
#include "allegro5/internal/aintern_xsystem.h" |
| 13 |
#include "allegro5/internal/aintern_xdnd.h" |
| 14 |
|
| 15 |
ALLEGRO_DEBUG_CHANNEL("xdnd") |
| 16 |
|
| 17 |
typedef struct { |
| 18 |
unsigned char *data; |
| 19 |
int format, count; |
| 20 |
Atom type; |
| 21 |
} Property; |
| 22 |
|
| 23 |
static int last_drop_x, last_drop_y; |
| 24 |
|
| 25 |
void _al_display_xglx_init_dnd_atoms(ALLEGRO_SYSTEM_XGLX *s) |
| 26 |
{ |
| 27 |
#define GET_ATOM(name) s->dnd_info.name = XInternAtom(s->x11display, #name, False) |
| 28 |
GET_ATOM(XdndEnter); |
| 29 |
GET_ATOM(XdndPosition); |
| 30 |
GET_ATOM(XdndStatus); |
| 31 |
GET_ATOM(XdndTypeList); |
| 32 |
GET_ATOM(XdndActionCopy); |
| 33 |
GET_ATOM(XdndDrop); |
| 34 |
GET_ATOM(XdndFinished); |
| 35 |
GET_ATOM(XdndSelection); |
| 36 |
GET_ATOM(XdndLeave); |
| 37 |
GET_ATOM(PRIMARY); |
| 38 |
} |
| 39 |
|
| 40 |
/* Read property |
| 41 |
* Must call X11_XFree on results |
| 42 |
*/ |
| 43 |
static void read_property(Property *p, Display *disp, Window w, Atom prop) |
| 44 |
{ |
| 45 |
unsigned char *ret = NULL; |
| 46 |
Atom type; |
| 47 |
int fmt; |
| 48 |
unsigned long count; |
| 49 |
unsigned long bytes_left; |
| 50 |
int bytes_fetch = 0; |
| 51 |
|
| 52 |
do { |
| 53 |
if (ret != 0) XFree(ret); |
| 54 |
XGetWindowProperty(disp, w, prop, 0, bytes_fetch, false, |
| 55 |
AnyPropertyType, &type, &fmt, &count, &bytes_left, &ret); |
| 56 |
bytes_fetch += bytes_left; |
| 57 |
} while (bytes_left != 0); |
| 58 |
|
| 59 |
p->data = ret; |
| 60 |
p->format = fmt; |
| 61 |
p->count = count; |
| 62 |
p->type = type; |
| 63 |
} |
| 64 |
|
| 65 |
/* Find text-uri-list in a list of targets and return it's atom |
| 66 |
* if available, else return None |
| 67 |
*/ |
| 68 |
static Atom pick_target(Display *disp, Atom list[], int list_count) |
| 69 |
{ |
| 70 |
Atom request = None; |
| 71 |
char *name; |
| 72 |
int i; |
| 73 |
for (i = 0; i < list_count && request == None; i++) { |
| 74 |
name = XGetAtomName(disp, list[i]); |
| 75 |
if ((strcmp("text/uri-list", name) == 0) || (strcmp("text/plain", name) == 0)) { |
| 76 |
request = list[i]; |
| 77 |
} |
| 78 |
XFree(name); |
| 79 |
} |
| 80 |
return request; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/* Wrapper for pick_target for a maximum of three targets, a special |
| 85 |
* case in the Xdnd protocol |
| 86 |
*/ |
| 87 |
static Atom pick_target_from_atoms(Display *disp, Atom a0, Atom a1, Atom a2) |
| 88 |
{ |
| 89 |
int count = 0; |
| 90 |
Atom atom[3]; |
| 91 |
if (a0 != None) atom[count++] = a0; |
| 92 |
if (a1 != None) atom[count++] = a1; |
| 93 |
if (a2 != None) atom[count++] = a2; |
| 94 |
return pick_target(disp, atom, count); |
| 95 |
} |
| 96 |
|
| 97 |
static void x11_reply(ALLEGRO_SYSTEM_XGLX *s, ALLEGRO_DISPLAY_XGLX *allegro_display, |
| 98 |
Window window, Atom message_type) |
| 99 |
{ |
| 100 |
XClientMessageEvent m; |
| 101 |
memset(&m, 0, sizeof(XClientMessageEvent)); |
| 102 |
m.type = ClientMessage; |
| 103 |
m.display = s->x11display; |
| 104 |
m.window = window; |
| 105 |
m.message_type = message_type; |
| 106 |
m.format = 32; |
| 107 |
m.data.l[0] = allegro_display->window; |
| 108 |
if (message_type == s->dnd_info.XdndStatus) { |
| 109 |
/* See https://freedesktop.org/wiki/Specifications/XDND/ |
| 110 |
* |
| 111 |
* data.l[0] contains the XID of the target window. |
| 112 |
* data.l[1]: |
| 113 |
* Bit 0 is set if the current target will accept the drop. |
| 114 |
* data.l[2,3] contains a rectangle in root coordinates that |
| 115 |
* means "don't send another XdndPosition message until the mouse |
| 116 |
* moves out of here". |
| 117 |
* data.l[4] contains the action accepted by the target |
| 118 |
*/ |
| 119 |
m.data.l[1] = s->dnd_info.xdnd_req != None ? 3 : 0; |
| 120 |
m.data.l[4] = s->dnd_info.XdndActionCopy; |
| 121 |
} |
| 122 |
if (message_type == s->dnd_info.XdndFinished) { |
| 123 |
/* See https://freedesktop.org/wiki/Specifications/XDND/ |
| 124 |
* |
| 125 |
* data.l[0] contains the XID of the target window. |
| 126 |
* data.l[1]: |
| 127 |
* Bit 0 is set if the current target accepted the drop |
| 128 |
* data.l[2] contains the action performed by the target. |
| 129 |
*/ |
| 130 |
m.data.l[1] = s->dnd_info.xdnd_req != None; |
| 131 |
m.data.l[2] = s->dnd_info.xdnd_req != None ? s->dnd_info.XdndActionCopy : None; |
| 132 |
} |
| 133 |
XSendEvent(s->x11display, window, false, NoEventMask, (XEvent*)&m); |
| 134 |
XSync(s->x11display, 0); |
| 135 |
} |
| 136 |
|
| 137 |
static void _send_event(ALLEGRO_DISPLAY_XGLX *allegro_display, char *text, |
| 138 |
bool is_file, int row, bool is_complete) |
| 139 |
{ |
| 140 |
ALLEGRO_EVENT_SOURCE *es = &allegro_display->display.es; |
| 141 |
_al_event_source_lock(es); |
| 142 |
|
| 143 |
if (_al_event_source_needs_to_generate_event(es)) { |
| 144 |
ALLEGRO_EVENT event; |
| 145 |
event.drop.type = ALLEGRO_EVENT_DROP; |
| 146 |
event.drop.timestamp = al_get_time(); |
| 147 |
event.drop.x = last_drop_x; |
| 148 |
event.drop.y = last_drop_y; |
| 149 |
event.drop.text = text; |
| 150 |
event.drop.is_file = is_file; |
| 151 |
event.drop.row = row; |
| 152 |
event.drop.is_complete = is_complete; |
| 153 |
_al_event_source_emit_event(es, &event); |
| 154 |
} |
| 155 |
_al_event_source_unlock(es); |
| 156 |
} |
| 157 |
|
| 158 |
/* Return true if the event is handled as a DND related event else |
| 159 |
* false. |
| 160 |
*/ |
| 161 |
bool _al_display_xglx_handle_drag_and_drop(ALLEGRO_SYSTEM_XGLX *s, |
| 162 |
ALLEGRO_DISPLAY_XGLX *allegro_display, XEvent *xevent) |
| 163 |
{ |
| 164 |
if (!(allegro_display->display.flags & ALLEGRO_DRAG_AND_DROP)) |
| 165 |
return false; |
| 166 |
if (xevent->xclient.message_type == s->dnd_info.XdndEnter) { |
| 167 |
ALLEGRO_DEBUG("Xdnd entered\n"); |
| 168 |
bool use_list = xevent->xclient.data.l[1] & 1; |
| 169 |
s->dnd_info.xdnd_source = xevent->xclient.data.l[0]; |
| 170 |
|
| 171 |
if (use_list) { |
| 172 |
Property p; |
| 173 |
read_property(&p, s->x11display, s->dnd_info.xdnd_source, |
| 174 |
s->dnd_info.XdndTypeList); |
| 175 |
s->dnd_info.xdnd_req = pick_target(s->x11display, (Atom*)p.data, p.count); |
| 176 |
XFree(p.data); |
| 177 |
} else { |
| 178 |
s->dnd_info.xdnd_req = pick_target_from_atoms(s->x11display, |
| 179 |
xevent->xclient.data.l[2], xevent->xclient.data.l[3], |
| 180 |
xevent->xclient.data.l[4]); |
| 181 |
} |
| 182 |
if (s->dnd_info.xdnd_req == None) { |
| 183 |
ALLEGRO_WARN("Xdnd action unsupported\n"); |
| 184 |
} |
| 185 |
return true; |
| 186 |
} |
| 187 |
else if (xevent->xclient.message_type == s->dnd_info.XdndPosition) { |
| 188 |
int root_x, root_y, window_x, window_y; |
| 189 |
Window ChildReturn; |
| 190 |
root_x = xevent->xclient.data.l[2] >> 16; |
| 191 |
root_y = xevent->xclient.data.l[2] & 0xffff; |
| 192 |
|
| 193 |
XTranslateCoordinates(s->x11display, DefaultRootWindow(s->x11display), |
| 194 |
allegro_display->window, |
| 195 |
root_x, root_y, &window_x, &window_y, &ChildReturn); |
| 196 |
|
| 197 |
// we don't send the text yet to avoid all the string allocations |
| 198 |
last_drop_x = window_x; |
| 199 |
last_drop_y = window_y; |
| 200 |
_send_event(allegro_display, NULL, false, 0, false); |
| 201 |
x11_reply(s, allegro_display, xevent->xclient.data.l[0], s->dnd_info.XdndStatus); |
| 202 |
return true; |
| 203 |
} |
| 204 |
else if (xevent->xclient.message_type == s->dnd_info.XdndDrop) { |
| 205 |
if (s->dnd_info.xdnd_req == None) { |
| 206 |
x11_reply(s, allegro_display, xevent->xclient.data.l[0], s->dnd_info.XdndFinished); |
| 207 |
_send_event(allegro_display, NULL, false, 0, true); |
| 208 |
ALLEGRO_DEBUG("Xdnd aborted\n"); |
| 209 |
} else { |
| 210 |
ALLEGRO_DEBUG("Xdnd received, converting to selection\n"); |
| 211 |
int xdnd_version = xevent->xclient.data.l[1] >> 24; |
| 212 |
if(xdnd_version >= 1) { |
| 213 |
XConvertSelection(s->x11display, s->dnd_info.XdndSelection, |
| 214 |
s->dnd_info.xdnd_req, s->dnd_info.PRIMARY, allegro_display->window, |
| 215 |
xevent->xclient.data.l[2]); |
| 216 |
} else { |
| 217 |
XConvertSelection(s->x11display, s->dnd_info.XdndSelection, |
| 218 |
s->dnd_info.xdnd_req, s->dnd_info.PRIMARY, allegro_display->window, CurrentTime); |
| 219 |
} |
| 220 |
} |
| 221 |
return true; |
| 222 |
} |
| 223 |
else if (xevent->xclient.message_type == s->dnd_info.XdndLeave) { |
| 224 |
_send_event(allegro_display, NULL, false, 0, true); |
| 225 |
ALLEGRO_DEBUG("Xdnd cancelled\n"); |
| 226 |
} |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
// Replaces "%xx" with the hex character and "+" with a space. |
| 231 |
static void _urldecode(char *url) |
| 232 |
{ |
| 233 |
char const *src = url; |
| 234 |
char *dst = url; |
| 235 |
char a, b; |
| 236 |
while (*src) { |
| 237 |
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && |
| 238 |
(isxdigit(a) && isxdigit(b))) { |
| 239 |
if (a >= 'a') a = a - 'a' + 'A'; |
| 240 |
if (a >= 'A') a = a - 'A' + 10; |
| 241 |
else a -= '0'; |
| 242 |
if (b >= 'a') b = b - 'a' + 'A'; |
| 243 |
if (b >= 'A') b = b - 'A' + 10; |
| 244 |
else b -= '0'; |
| 245 |
*dst++ = 16 * a + b; |
| 246 |
src += 3; |
| 247 |
} else if (*src == '+') { |
| 248 |
*dst++ = ' '; |
| 249 |
src++; |
| 250 |
} else { |
| 251 |
*dst++ = *src++; |
| 252 |
} |
| 253 |
} |
| 254 |
*dst++ = '\0'; |
| 255 |
} |
| 256 |
|
| 257 |
// in any of these cases: |
| 258 |
// - /path |
| 259 |
// - file:/path |
| 260 |
// - file:///path |
| 261 |
// - file://hostname/path |
| 262 |
// we will return just /path |
| 263 |
static char *_uri_to_filename(char *uri) |
| 264 |
{ |
| 265 |
char *uri2 = uri; |
| 266 |
if (memcmp(uri, "file://", 7) == 0) { |
| 267 |
uri2 = strstr(uri + 7, "/"); |
| 268 |
} |
| 269 |
else if (memcmp(uri, "file:/", 6) == 0) { |
| 270 |
uri2 += 5; |
| 271 |
} |
| 272 |
else if (strstr(uri, ":/") != NULL) { |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
memmove(uri, uri2, strlen(uri2) + 1); |
| 276 |
_urldecode(uri); |
| 277 |
return uri; |
| 278 |
} |
| 279 |
|
| 280 |
bool _al_display_xglx_handle_drag_and_drop_selection(ALLEGRO_SYSTEM_XGLX *s, |
| 281 |
ALLEGRO_DISPLAY_XGLX *allegro_display, XEvent *xevent) |
| 282 |
{ |
| 283 |
if (!(allegro_display->display.flags & ALLEGRO_DRAG_AND_DROP)) |
| 284 |
return false; |
| 285 |
Atom target = xevent->xselection.target; |
| 286 |
|
| 287 |
if (target == s->dnd_info.xdnd_req) { |
| 288 |
Property p; |
| 289 |
read_property(&p, s->x11display, allegro_display->window, s->dnd_info.PRIMARY); |
| 290 |
if (p.format == 8) { |
| 291 |
ALLEGRO_USTR *text = al_ustr_new((char *)p.data); |
| 292 |
char *name = XGetAtomName(s->x11display, target); |
| 293 |
if (name) { |
| 294 |
ALLEGRO_INFO("Xdnd data received, format '%s'\n", name); |
| 295 |
int pos = 0; |
| 296 |
int row = 0; |
| 297 |
bool complete = false; |
| 298 |
while (!complete) { |
| 299 |
int pos2 = al_ustr_find_set_cstr(text, pos, "\r\n"); |
| 300 |
if (pos2 == -1) pos2 = al_ustr_size(text); |
| 301 |
ALLEGRO_USTR *token = al_ustr_dup_substr(text, pos, pos2); |
| 302 |
if (al_ustr_get(text, pos2) == '\r') al_ustr_next(text, &pos2); |
| 303 |
if (al_ustr_get(text, pos2) == '\n') al_ustr_next(text, &pos2); |
| 304 |
if (al_ustr_get(text, pos2) == -1) complete = true; |
| 305 |
pos = pos2; |
| 306 |
|
| 307 |
if (strcmp(name, "text/plain") == 0) { |
| 308 |
_send_event(allegro_display, al_cstr_dup(token), false, row, complete); |
| 309 |
} |
| 310 |
else if (strcmp(name, "text/uri-list") == 0) { |
| 311 |
char *filename = _uri_to_filename(al_cstr_dup(token)); |
| 312 |
_send_event(allegro_display, filename, true, row, complete); |
| 313 |
} |
| 314 |
else { |
| 315 |
ALLEGRO_WARN("Attempt to drop unknown format '%s'\n", name); |
| 316 |
} |
| 317 |
al_ustr_free(token); |
| 318 |
row++; |
| 319 |
} |
| 320 |
XFree(name); |
| 321 |
} |
| 322 |
al_ustr_free(text); |
| 323 |
} |
| 324 |
else { |
| 325 |
s->dnd_info.xdnd_req = None; |
| 326 |
} |
| 327 |
if (p.data) XFree(p.data); |
| 328 |
x11_reply(s, allegro_display, s->dnd_info.xdnd_source, |
| 329 |
s->dnd_info.XdndFinished); |
| 330 |
ALLEGRO_DEBUG("Xdnd completed\n"); |
| 331 |
return true; |
| 332 |
} |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
void _al_xwin_accept_drag_and_drop(ALLEGRO_DISPLAY *display, bool accept) |
| 337 |
{ |
| 338 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 339 |
ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display; |
| 340 |
|
| 341 |
Atom XdndAware = XInternAtom(system->x11display, "XdndAware", False); |
| 342 |
|
| 343 |
if (accept) { |
| 344 |
Atom xdnd_version = 5; |
| 345 |
XChangeProperty(system->x11display, glx->window, XdndAware, XA_ATOM, 32, |
| 346 |
PropModeReplace, (unsigned char*)&xdnd_version, 1); |
| 347 |
} else { |
| 348 |
XDeleteProperty(system->x11display, glx->window, XdndAware); |
| 349 |
} |
| 350 |
} |