Debian Codegraph @

gdk/x11/gdkdnd-x11.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include "config.h"
26
27 #include "gdkx11dnd.h"
28 #include "gdkdndprivate.h"
29
30 #include "gdkmain.h"
31 #include "gdkinternals.h"
32 #include "gdkasync.h"
33 #include "gdkproperty.h"
34 #include "gdkprivate-x11.h"
35 #include "gdkscreen-x11.h"
36 #include "gdkdisplay-x11.h"
37
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xatom.h>
41 #include <X11/extensions/shape.h>
42 #ifdef HAVE_XCOMPOSITE
43 #include <X11/extensions/Xcomposite.h>
44 #endif
45
46 #include <string.h>
47
48 typedef enum {
49 GDK_DRAG_STATUS_DRAG,
50 GDK_DRAG_STATUS_MOTION_WAIT,
51 GDK_DRAG_STATUS_ACTION_WAIT,
52 GDK_DRAG_STATUS_DROP
53 } GtkDragStatus;
54
55 typedef struct {
56 guint32 xid;
57 gint x, y, width, height;
58 gboolean mapped;
59 gboolean shape_selected;
60 gboolean shape_valid;
61 cairo_region_t *shape;
62 } GdkCacheChild;
63
64 typedef struct {
65 GList *children;
66 GHashTable *child_hash;
67 guint old_event_mask;
68 GdkScreen *screen;
69 gint ref_count;
70 } GdkWindowCache;
71
72
73 struct _GdkX11DragContext
74 {
75 GdkDragContext context;
76
77 gint start_x; /* Where the drag started */
78 gint start_y;
79 guint16 last_x; /* Coordinates from last event */
80 guint16 last_y;
81 GdkDragAction old_action; /* The last action we sent to the source */
82 GdkDragAction old_actions; /* The last actions we sent to the source */
83 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
84 guint version; /* Xdnd protocol version */
85
86 GSList *window_caches;
87
88 GdkWindow *drag_window;
89
90 GdkWindow *ipc_window;
91 GdkCursor *cursor;
92 GdkSeat *grab_seat;
93 GdkDragAction actions;
94 GdkDragAction current_action;
95
96 gint hot_x;
97 gint hot_y;
98
99 Window dest_xid; /* The last window we looked up */
100 Window drop_xid; /* The (non-proxied) window that is receiving drops */
101 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
102 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
103 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
104 guint drag_status : 4; /* current status of drag */
105 guint drop_failed : 1; /* Whether the drop was unsuccessful */
106 };
107
108 struct _GdkX11DragContextClass
109 {
110 GdkDragContextClass parent_class;
111 };
112
113 typedef struct {
114 gint keysym;
115 gint modifiers;
116 } GrabKey;
117
118 static GrabKey grab_keys[] = {
119 { XK_Escape, 0 },
120 { XK_space, 0 },
121 { XK_KP_Space, 0 },
122 { XK_Return, 0 },
123 { XK_KP_Enter, 0 },
124 { XK_Up, 0 },
125 { XK_Up, Mod1Mask },
126 { XK_Down, 0 },
127 { XK_Down, Mod1Mask },
128 { XK_Left, 0 },
129 { XK_Left, Mod1Mask },
130 { XK_Right, 0 },
131 { XK_Right, Mod1Mask },
132 { XK_KP_Up, 0 },
133 { XK_KP_Up, Mod1Mask },
134 { XK_KP_Down, 0 },
135 { XK_KP_Down, Mod1Mask },
136 { XK_KP_Left, 0 },
137 { XK_KP_Left, Mod1Mask },
138 { XK_KP_Right, 0 },
139 { XK_KP_Right, Mod1Mask }
140 };
141
142 /* Forward declarations */
143
144 static GdkWindowCache *gdk_window_cache_get (GdkScreen *screen);
145 static GdkWindowCache *gdk_window_cache_ref (GdkWindowCache *cache);
146 static void gdk_window_cache_unref (GdkWindowCache *cache);
147
148 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
149 GdkEvent *event,
150 gpointer data);
151 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
152 GdkEvent *event,
153 gpointer data);
154 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
155 GdkEvent *event,
156 gpointer data);
157 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
158 GdkEvent *event,
159 gpointer data);
160 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
161 GdkEvent *event,
162 gpointer data);
163 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
164 GdkEvent *event,
165 gpointer data);
166
167 static void xdnd_manage_source_filter (GdkDragContext *context,
168 GdkWindow *window,
169 gboolean add_filter);
170
171 gboolean gdk_x11_drag_context_handle_event (GdkDragContext *context,
172 const GdkEvent *event);
173 void gdk_x11_drag_context_action_changed (GdkDragContext *context,
174 GdkDragAction action);
175
176 static GList *contexts;
177 static GSList *window_caches;
178
179 static const struct {
180 const char *atom_name;
181 GdkFilterFunc func;
182 } xdnd_filters[] = {
183 { "XdndEnter", xdnd_enter_filter },
184 { "XdndLeave", xdnd_leave_filter },
185 { "XdndPosition", xdnd_position_filter },
186 { "XdndStatus", xdnd_status_filter },
187 { "XdndFinished", xdnd_finished_filter },
188 { "XdndDrop", xdnd_drop_filter },
189 };
190
191
192 G_DEFINE_TYPE (GdkX11DragContext, gdk_x11_drag_context, GDK_TYPE_DRAG_CONTEXT)
193
194 static void
195 gdk_x11_drag_context_init (GdkX11DragContext *context)
196 {
197 contexts = g_list_prepend (contexts, context);
198 }
199
200 static void gdk_x11_drag_context_finalize (GObject *object);
201 static GdkWindow * gdk_x11_drag_context_find_window (GdkDragContext *context,
202 GdkWindow *drag_window,
203 GdkScreen *screen,
204 gint x_root,
205 gint y_root,
206 GdkDragProtocol *protocol);
207 static gboolean gdk_x11_drag_context_drag_motion (GdkDragContext *context,
208 GdkWindow *dest_window,
209 GdkDragProtocol protocol,
210 gint x_root,
211 gint y_root,
212 GdkDragAction suggested_action,
213 GdkDragAction possible_actions,
214 guint32 time);
215 static void gdk_x11_drag_context_drag_status (GdkDragContext *context,
216 GdkDragAction action,
217 guint32 time_);
218 static void gdk_x11_drag_context_drag_abort (GdkDragContext *context,
219 guint32 time_);
220 static void gdk_x11_drag_context_drag_drop (GdkDragContext *context,
221 guint32 time_);
222 static void gdk_x11_drag_context_drop_reply (GdkDragContext *context,
223 gboolean accept,
224 guint32 time_);
225 static void gdk_x11_drag_context_drop_finish (GdkDragContext *context,
226 gboolean success,
227 guint32 time_);
228 static gboolean gdk_x11_drag_context_drop_status (GdkDragContext *context);
229 static GdkAtom gdk_x11_drag_context_get_selection (GdkDragContext *context);
230 static GdkWindow * gdk_x11_drag_context_get_drag_window (GdkDragContext *context);
231 static void gdk_x11_drag_context_set_hotspot (GdkDragContext *context,
232 gint hot_x,
233 gint hot_y);
234 static void gdk_x11_drag_context_drop_done (GdkDragContext *context,
235 gboolean success);
236 static gboolean gdk_x11_drag_context_manage_dnd (GdkDragContext *context,
237 GdkWindow *window,
238 GdkDragAction actions);
239 static void gdk_x11_drag_context_set_cursor (GdkDragContext *context,
240 GdkCursor *cursor);
241 static void gdk_x11_drag_context_cancel (GdkDragContext *context,
242 GdkDragCancelReason reason);
243 static void gdk_x11_drag_context_drop_performed (GdkDragContext *context,
244 guint32 time);
245
246 static void
247 gdk_x11_drag_context_class_init (GdkX11DragContextClass *klass)
248 {
249 GObjectClass *object_class = G_OBJECT_CLASS (klass);
250 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
251
252 object_class->finalize = gdk_x11_drag_context_finalize;
253
254 context_class->find_window = gdk_x11_drag_context_find_window;
255 context_class->drag_status = gdk_x11_drag_context_drag_status;
256 context_class->drag_motion = gdk_x11_drag_context_drag_motion;
257 context_class->drag_abort = gdk_x11_drag_context_drag_abort;
258 context_class->drag_drop = gdk_x11_drag_context_drag_drop;
259 context_class->drop_reply = gdk_x11_drag_context_drop_reply;
260 context_class->drop_finish = gdk_x11_drag_context_drop_finish;
261 context_class->drop_status = gdk_x11_drag_context_drop_status;
262 context_class->get_selection = gdk_x11_drag_context_get_selection;
263 context_class->get_drag_window = gdk_x11_drag_context_get_drag_window;
264 context_class->set_hotspot = gdk_x11_drag_context_set_hotspot;
265 context_class->drop_done = gdk_x11_drag_context_drop_done;
266 context_class->manage_dnd = gdk_x11_drag_context_manage_dnd;
267 context_class->set_cursor = gdk_x11_drag_context_set_cursor;
268 context_class->cancel = gdk_x11_drag_context_cancel;
269 context_class->drop_performed = gdk_x11_drag_context_drop_performed;
270 context_class->handle_event = gdk_x11_drag_context_handle_event;
271 context_class->action_changed = gdk_x11_drag_context_action_changed;
272 }
273
274 static void
275 gdk_x11_drag_context_finalize (GObject *object)
276 {
277 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
278 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (object);
279 GdkWindow *drag_window;
280
281 if (context->source_window)
282 {
283 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
284 xdnd_manage_source_filter (context, context->source_window, FALSE);
285 }
286
287 g_slist_free_full (x11_context->window_caches, (GDestroyNotify)gdk_window_cache_unref);
288 x11_context->window_caches = NULL;
289
290 contexts = g_list_remove (contexts, context);
291
292 drag_window = context->drag_window;
293
294 G_OBJECT_CLASS (gdk_x11_drag_context_parent_class)->finalize (object);
295
296 if (drag_window)
297 gdk_window_destroy (drag_window);
298 }
299
300 /* Drag Contexts */
301
302 static GdkDragContext *
303 gdk_drag_context_find (GdkDisplay *display,
304 gboolean is_source,
305 Window source_xid,
306 Window dest_xid)
307 {
308 GList *tmp_list;
309 GdkDragContext *context;
310 GdkX11DragContext *context_x11;
311 Window context_dest_xid;
312
313 for (tmp_list = contexts; tmp_list; tmp_list = tmp_list->next)
314 {
315 context = (GdkDragContext *)tmp_list->data;
316 context_x11 = (GdkX11DragContext *)context;
317
318 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
319 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
320 continue;
321
322 context_dest_xid = context->dest_window
323 ? (context_x11->drop_xid
324 ? context_x11->drop_xid
325 : GDK_WINDOW_XID (context->dest_window))
326 : None;
327
328 if ((!context->is_source == !is_source) &&
329 ((source_xid == None) || (context->source_window &&
330 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
331 ((dest_xid == None) || (context_dest_xid == dest_xid)))
332 return context;
333 }
334
335 return NULL;
336 }
337
338 static void
339 precache_target_list (GdkDragContext *context)
340 {
341 if (context->targets)
342 {
343 GPtrArray *targets = g_ptr_array_new ();
344 GList *tmp_list;
345 int i;
346
347 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
348 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
349
350 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
351 (const gchar **)targets->pdata,
352 targets->len);
353
354 for (i =0; i < targets->len; i++)
355 g_free (targets->pdata[i]);
356
357 g_ptr_array_free (targets, TRUE);
358 }
359 }
360
361 /* Utility functions */
362
363 static void
364 free_cache_child (GdkCacheChild *child,
365 GdkDisplay *display)
366 {
367 if (child->shape)
368 cairo_region_destroy (child->shape);
369
370 if (child->shape_selected && display)
371 {
372 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
373
374 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
375 }
376
377 g_free (child);
378 }
379
380 static void
381 gdk_window_cache_add (GdkWindowCache *cache,
382 guint32 xid,
383 gint x,
384 gint y,
385 gint width,
386 gint height,
387 gboolean mapped)
388 {
389 GdkCacheChild *child = g_new (GdkCacheChild, 1);
390
391 child->xid = xid;
392 child->x = x;
393 child->y = y;
394 child->width = width;
395 child->height = height;
396 child->mapped = mapped;
397 child->shape_selected = FALSE;
398 child->shape_valid = FALSE;
399 child->shape = NULL;
400
401 cache->children = g_list_prepend (cache->children, child);
402 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
403 cache->children);
404 }
405
406 static GdkFilterReturn
407 gdk_window_cache_shape_filter (GdkXEvent *xev,
408 GdkEvent *event,
409 gpointer data)
410 {
411 XEvent *xevent = (XEvent *)xev;
412 GdkWindowCache *cache = data;
413
414 GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
415
416 if (display->have_shapes &&
417 xevent->type == display->shape_event_base + ShapeNotify)
418 {
419 XShapeEvent *xse = (XShapeEvent*)xevent;
420 GList *node;
421
422 node = g_hash_table_lookup (cache->child_hash,
423 GUINT_TO_POINTER (xse->window));
424 if (node)
425 {
426 GdkCacheChild *child = node->data;
427 child->shape_valid = FALSE;
428 if (child->shape)
429 {
430 cairo_region_destroy (child->shape);
431 child->shape = NULL;
432 }
433 }
434
435 return GDK_FILTER_REMOVE;
436 }
437
438 return GDK_FILTER_CONTINUE;
439 }
440
441 static GdkFilterReturn
442 gdk_window_cache_filter (GdkXEvent *xev,
443 GdkEvent *event,
444 gpointer data)
445 {
446 XEvent *xevent = (XEvent *)xev;
447 GdkWindowCache *cache = data;
448
449 switch (xevent->type)
450 {
451 case CirculateNotify:
452 break;
453 case ConfigureNotify:
454 {
455 XConfigureEvent *xce = &xevent->xconfigure;
456 GList *node;
457
458 node = g_hash_table_lookup (cache->child_hash,
459 GUINT_TO_POINTER (xce->window));
460 if (node)
461 {
462 GdkCacheChild *child = node->data;
463 child->x = xce->x;
464 child->y = xce->y;
465 child->width = xce->width;
466 child->height = xce->height;
467 if (xce->above == None && (node->next))
468 {
469 GList *last = g_list_last (cache->children);
470 cache->children = g_list_remove_link (cache->children, node);
471 last->next = node;
472 node->next = NULL;
473 node->prev = last;
474 }
475 else
476 {
477 GList *above_node = g_hash_table_lookup (cache->child_hash,
478 GUINT_TO_POINTER (xce->above));
479 if (above_node && node->next != above_node)
480 {
481 /* Put the window above (before in the list) above_node */
482 cache->children = g_list_remove_link (cache->children, node);
483 node->prev = above_node->prev;
484 if (node->prev)
485 node->prev->next = node;
486 else
487 cache->children = node;
488 node->next = above_node;
489 above_node->prev = node;
490 }
491 }
492 }
493 break;
494 }
495 case CreateNotify:
496 {
497 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
498
499 if (!g_hash_table_lookup (cache->child_hash,
500 GUINT_TO_POINTER (xcwe->window)))
501 gdk_window_cache_add (cache, xcwe->window,
502 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
503 FALSE);
504 break;
505 }
506 case DestroyNotify:
507 {
508 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
509 GList *node;
510
511 node = g_hash_table_lookup (cache->child_hash,
512 GUINT_TO_POINTER (xdwe->window));
513 if (node)
514 {
515 GdkCacheChild *child = node->data;
516
517 g_hash_table_remove (cache->child_hash,
518 GUINT_TO_POINTER (xdwe->window));
519 cache->children = g_list_remove_link (cache->children, node);
520 /* window is destroyed, no need to disable ShapeNotify */
521 free_cache_child (child, NULL);
522 g_list_free_1 (node);
523 }
524 break;
525 }
526 case MapNotify:
527 {
528 XMapEvent *xme = &xevent->xmap;
529 GList *node;
530
531 node = g_hash_table_lookup (cache->child_hash,
532 GUINT_TO_POINTER (xme->window));
533 if (node)
534 {
535 GdkCacheChild *child = node->data;
536 child->mapped = TRUE;
537 }
538 break;
539 }
540 case ReparentNotify:
541 break;
542 case UnmapNotify:
543 {
544 XMapEvent *xume = &xevent->xmap;
545 GList *node;
546
547 node = g_hash_table_lookup (cache->child_hash,
548 GUINT_TO_POINTER (xume->window));
549 if (node)
550 {
551 GdkCacheChild *child = node->data;
552 child->mapped = FALSE;
553 }
554 break;
555 }
556 default:
557 return GDK_FILTER_CONTINUE;
558 }
559 return GDK_FILTER_REMOVE;
560 }
561
562 static GdkWindowCache *
563 gdk_window_cache_new (GdkScreen *screen)
564 {
565 XWindowAttributes xwa;
566 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
567 GdkWindow *root_window = gdk_screen_get_root_window (screen);
568 GdkChildInfoX11 *children;
569 guint nchildren, i;
570 #ifdef HAVE_XCOMPOSITE
571 Window cow;
572 #endif
573
574 GdkWindowCache *result = g_new (GdkWindowCache, 1);
575
576 result->children = NULL;
577 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
578 result->screen = screen;
579 result->ref_count = 1;
580
581 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
582 result->old_event_mask = xwa.your_event_mask;
583
584 if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_X11_SCREEN (screen)->display)->trusted_client))
585 {
586 GList *toplevel_windows, *list;
587 GdkWindow *window;
588 GdkWindowImplX11 *impl;
589 gint x, y, width, height;
590
591 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
592 for (list = toplevel_windows; list; list = list->next)
593 {
594 window = GDK_WINDOW (list->data);
595 impl = GDK_WINDOW_IMPL_X11 (window->impl);
596 gdk_window_get_geometry (window, &x, &y, &width, &height);
597 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
598 x * impl->window_scale, y * impl->window_scale,
599 width * impl->window_scale,
600 height * impl->window_scale,
601 gdk_window_is_visible (window));
602 }
603 g_list_free (toplevel_windows);
604 return result;
605 }
606
607 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
608 result->old_event_mask | SubstructureNotifyMask);
609 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
610 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
611
612 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
613 GDK_WINDOW_XID (root_window),
614 FALSE, NULL,
615 &children, &nchildren))
616 return result;
617
618 for (i = 0; i < nchildren ; i++)
619 {
620 gdk_window_cache_add (result, children[i].window,
621 children[i].x, children[i].y, children[i].width, children[i].height,
622 children[i].is_mapped);
623 }
624
625 g_free (children);
626
627 #ifdef HAVE_XCOMPOSITE
628 /*
629 * Add the composite overlay window to the cache, as this can be a reasonable
630 * Xdnd proxy as well.
631 * This is only done when the screen is composited in order to avoid mapping
632 * the COW. We assume that the CM is using the COW (which is true for pretty
633 * much any CM currently in use).
634 */
635 if (gdk_screen_is_composited (screen))
636 {
637 gdk_x11_display_error_trap_push (GDK_X11_SCREEN (screen)->display);
638 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
639 gdk_window_cache_add (result, cow, 0, 0,
640 gdk_x11_screen_get_width (screen) * GDK_X11_SCREEN(screen)->window_scale,
641 gdk_x11_screen_get_height (screen) * GDK_X11_SCREEN(screen)->window_scale,
642 TRUE);
643 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
644 gdk_x11_display_error_trap_pop_ignored (GDK_X11_SCREEN (screen)->display);
645 }
646 #endif
647
648 return result;
649 }
650
651 static void
652 gdk_window_cache_destroy (GdkWindowCache *cache)
653 {
654 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
655 GdkDisplay *display;
656
657 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
658 GDK_WINDOW_XID (root_window),
659 cache->old_event_mask);
660 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
661 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
662
663 display = gdk_screen_get_display (cache->screen);
664
665 gdk_x11_display_error_trap_push (display);
666 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
667 gdk_x11_display_error_trap_pop_ignored (display);
668
669 g_list_free (cache->children);
670 g_hash_table_destroy (cache->child_hash);
671
672 g_free (cache);
673 }
674
675 static GdkWindowCache *
676 gdk_window_cache_ref (GdkWindowCache *cache)
677 {
678 cache->ref_count += 1;
679
680 return cache;
681 }
682
683 static void
684 gdk_window_cache_unref (GdkWindowCache *cache)
685 {
686 g_assert (cache->ref_count > 0);
687
688 cache->ref_count -= 1;
689
690 if (cache->ref_count == 0)
691 {
692 window_caches = g_slist_remove (window_caches, cache);
693 gdk_window_cache_destroy (cache);
694 }
695 }
696
697 GdkWindowCache *
698 gdk_window_cache_get (GdkScreen *screen)
699 {
700 GSList *list;
701 GdkWindowCache *cache;
702
703 for (list = window_caches; list; list = list->next)
704 {
705 cache = list->data;
706 if (cache->screen == screen)
707 return gdk_window_cache_ref (cache);
708 }
709
710 cache = gdk_window_cache_new (screen);
711
712 window_caches = g_slist_prepend (window_caches, cache);
713
714 return cache;
715 }
716
717 static gboolean
718 is_pointer_within_shape (GdkDisplay *display,
719 GdkCacheChild *child,
720 gint x_pos,
721 gint y_pos)
722 {
723 if (!child->shape_selected)
724 {
725 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
726
727 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
728 child->shape_selected = TRUE;
729 }
730 if (!child->shape_valid)
731 {
732 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
733 cairo_region_t *input_shape;
734
735 child->shape = NULL;
736 if (gdk_display_supports_shapes (display))
737 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
738 child->xid, 1, ShapeBounding);
739 #ifdef ShapeInput
740 input_shape = NULL;
741 if (gdk_display_supports_input_shapes (display))
742 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
743 child->xid, 1, ShapeInput);
744
745 if (child->shape && input_shape)
746 {
747 cairo_region_intersect (child->shape, input_shape);
748 cairo_region_destroy (input_shape);
749 }
750 else if (input_shape)
751 {
752 child->shape = input_shape;
753 }
754 #endif
755
756 child->shape_valid = TRUE;
757 }
758
759 return child->shape == NULL ||
760 cairo_region_contains_point (child->shape, x_pos, y_pos);
761 }
762
763 static Window
764 get_client_window_at_coords_recurse (GdkDisplay *display,
765 Window win,
766 gboolean is_toplevel,
767 gint x,
768 gint y)
769 {
770 GdkChildInfoX11 *children;
771 unsigned int nchildren;
772 int i;
773 gboolean found_child = FALSE;
774 GdkChildInfoX11 child = { 0, };
775 gboolean has_wm_state = FALSE;
776
777 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
778 is_toplevel? &has_wm_state : NULL,
779 &children, &nchildren))
780 return None;
781
782 if (has_wm_state)
783 {
784 g_free (children);
785
786 return win;
787 }
788
789 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
790 {
791 GdkChildInfoX11 *cur_child = &children[i];
792
793 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
794 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
795 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
796 {
797 x -= cur_child->x;
798 y -= cur_child->y;
799 child = *cur_child;
800 found_child = TRUE;
801 }
802 }
803
804 g_free (children);
805
806 if (found_child)
807 {
808 if (child.has_wm_state)
809 return child.window;
810 else
811 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
812 }
813 else
814 return None;
815 }
816
817 static Window
818 get_client_window_at_coords (GdkWindowCache *cache,
819 Window ignore,
820 gint x_root,
821 gint y_root)
822 {
823 GList *tmp_list;
824 Window retval = None;
825 GdkDisplay *display;
826
827 display = gdk_screen_get_display (cache->screen);
828
829 gdk_x11_display_error_trap_push (display);
830
831 tmp_list = cache->children;
832
833 while (tmp_list && !retval)
834 {
835 GdkCacheChild *child = tmp_list->data;
836
837 if ((child->xid != ignore) && (child->mapped))
838 {
839 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
840 (y_root >= child->y) && (y_root < child->y + child->height))
841 {
842 if (!is_pointer_within_shape (display, child,
843 x_root - child->x,
844 y_root - child->y))
845 {
846 tmp_list = tmp_list->next;
847 continue;
848 }
849
850 retval = get_client_window_at_coords_recurse (display,
851 child->xid, TRUE,
852 x_root - child->x,
853 y_root - child->y);
854 if (!retval)
855 retval = child->xid;
856 }
857 }
858 tmp_list = tmp_list->next;
859 }
860
861 gdk_x11_display_error_trap_pop_ignored (display);
862
863 if (retval)
864 return retval;
865 else
866 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
867 }
868
869 #ifdef G_ENABLE_DEBUG
870 static void
871 print_target_list (GList *targets)
872 {
873 while (targets)
874 {
875 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
876 g_message ("\t%s", name);
877 g_free (name);
878 targets = targets->next;
879 }
880 }
881 #endif /* G_ENABLE_DEBUG */
882
883 /*************************************************************
884 ***************************** XDND **************************
885 *************************************************************/
886
887 /* Utility functions */
888
889 static struct {
890 const gchar *name;
891 GdkAtom atom;
892 GdkDragAction action;
893 } xdnd_actions_table[] = {
894 { "XdndActionCopy", None, GDK_ACTION_COPY },
895 { "XdndActionMove", None, GDK_ACTION_MOVE },
896 { "XdndActionLink", None, GDK_ACTION_LINK },
897 { "XdndActionAsk", None, GDK_ACTION_ASK },
898 { "XdndActionPrivate", None, GDK_ACTION_COPY },
899 };
900
901 static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
902 static gboolean xdnd_actions_initialized = FALSE;
903
904 static void
905 xdnd_initialize_actions (void)
906 {
907 gint i;
908
909 xdnd_actions_initialized = TRUE;
910 for (i = 0; i < xdnd_n_actions; i++)
911 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
912 }
913
914 static GdkDragAction
915 xdnd_action_from_atom (GdkDisplay *display,
916 Atom xatom)
917 {
918 GdkAtom atom;
919 gint i;
920
921 if (xatom == None)
922 return 0;
923
924 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
925
926 if (!xdnd_actions_initialized)
927 xdnd_initialize_actions();
928
929 for (i = 0; i < xdnd_n_actions; i++)
930 if (atom == xdnd_actions_table[i].atom)
931 return xdnd_actions_table[i].action;
932
933 return 0;
934 }
935
936 static Atom
937 xdnd_action_to_atom (GdkDisplay *display,
938 GdkDragAction action)
939 {
940 gint i;
941
942 if (!xdnd_actions_initialized)
943 xdnd_initialize_actions();
944
945 for (i = 0; i < xdnd_n_actions; i++)
946 if (action == xdnd_actions_table[i].action)
947 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
948
949 return None;
950 }
951
952 /* Source side */
953
954 static GdkFilterReturn
955 xdnd_status_filter (GdkXEvent *xev,
956 GdkEvent *event,
957 gpointer data)
958 {
959 GdkDisplay *display;
960 XEvent *xevent = (XEvent *)xev;
961 guint32 dest_window = xevent->xclient.data.l[0];
962 guint32 flags = xevent->xclient.data.l[1];
963 Atom action = xevent->xclient.data.l[4];
964 GdkDragContext *context;
965
966 if (!event->any.window ||
967 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
968 return GDK_FILTER_CONTINUE; /* Not for us */
969
970 GDK_NOTE (DND,
971 g_message ("XdndStatus: dest_window: %#x action: %ld",
972 dest_window, action));
973
974 display = gdk_window_get_display (event->any.window);
975 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
976
977 if (context)
978 {
979 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
980 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
981 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
982
983 event->dnd.send_event = FALSE;
984 event->dnd.type = GDK_DRAG_STATUS;
985 event->dnd.context = context;
986 gdk_event_set_device (event, gdk_drag_context_get_device (context));
987 g_object_ref (context);
988
989 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
990 if (!(action != 0) != !(flags & 1))
991 {
992 GDK_NOTE (DND,
993 g_warning ("Received status event with flags not corresponding to action!"));
994 action = 0;
995 }
996
997 context->action = xdnd_action_from_atom (display, action);
998
999 return GDK_FILTER_TRANSLATE;
1000 }
1001
1002 return GDK_FILTER_REMOVE;
1003 }
1004
1005 static GdkFilterReturn
1006 xdnd_finished_filter (GdkXEvent *xev,
1007 GdkEvent *event,
1008 gpointer data)
1009 {
1010 GdkDisplay *display;
1011 XEvent *xevent = (XEvent *)xev;
1012 guint32 dest_window = xevent->xclient.data.l[0];
1013 GdkDragContext *context;
1014 GdkX11DragContext *context_x11;
1015
1016 if (!event->any.window ||
1017 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1018 return GDK_FILTER_CONTINUE; /* Not for us */
1019
1020 GDK_NOTE (DND,
1021 g_message ("XdndFinished: dest_window: %#x", dest_window));
1022
1023 display = gdk_window_get_display (event->any.window);
1024 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1025
1026 if (context)
1027 {
1028 context_x11 = GDK_X11_DRAG_CONTEXT (context);
1029 if (context_x11->version == 5)
1030 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
1031
1032 event->dnd.type = GDK_DROP_FINISHED;
1033 event->dnd.context = context;
1034 gdk_event_set_device (event, gdk_drag_context_get_device (context));
1035 g_object_ref (context);
1036
1037 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1038
1039 return GDK_FILTER_TRANSLATE;
1040 }
1041
1042 return GDK_FILTER_REMOVE;
1043 }
1044
1045 static void
1046 xdnd_set_targets (GdkX11DragContext *context_x11)
1047 {
1048 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1049 Atom *atomlist;
1050 GList *tmp_list = context->targets;
1051 gint i;
1052 gint n_atoms = g_list_length (context->targets);
1053 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1054
1055 atomlist = g_new (Atom, n_atoms);
1056 i = 0;
1057 while (tmp_list)
1058 {
1059 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
1060 tmp_list = tmp_list->next;
1061 i++;
1062 }
1063
1064 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1065 GDK_WINDOW_XID (context->source_window),
1066 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
1067 XA_ATOM, 32, PropModeReplace,
1068 (guchar *)atomlist, n_atoms);
1069
1070 g_free (atomlist);
1071
1072 context_x11->xdnd_targets_set = 1;
1073 }
1074
1075 static void
1076 xdnd_set_actions (GdkX11DragContext *context_x11)
1077 {
1078 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1079 Atom *atomlist;
1080 gint i;
1081 gint n_atoms;
1082 guint actions;
1083 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1084
1085 if (!xdnd_actions_initialized)
1086 xdnd_initialize_actions();
1087
1088 actions = context->actions;
1089 n_atoms = 0;
1090 for (i = 0; i < xdnd_n_actions; i++)
1091 {
1092 if (actions & xdnd_actions_table[i].action)
1093 {
1094 actions &= ~xdnd_actions_table[i].action;
1095 n_atoms++;
1096 }
1097 }
1098
1099 atomlist = g_new (Atom, n_atoms);
1100
1101 actions = context->actions;
1102 n_atoms = 0;
1103 for (i = 0; i < xdnd_n_actions; i++)
1104 {
1105 if (actions & xdnd_actions_table[i].action)
1106 {
1107 actions &= ~xdnd_actions_table[i].action;
1108 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1109 n_atoms++;
1110 }
1111 }
1112
1113 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1114 GDK_WINDOW_XID (context->source_window),
1115 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
1116 XA_ATOM, 32, PropModeReplace,
1117 (guchar *)atomlist, n_atoms);
1118
1119 g_free (atomlist);
1120
1121 context_x11->xdnd_actions_set = TRUE;
1122 context_x11->xdnd_actions = context->actions;
1123 }
1124
1125 static void
1126 send_client_message_async_cb (Window window,
1127 gboolean success,
1128 gpointer data)
1129 {
1130 GdkDragContext *context = data;
1131 GDK_NOTE (DND,
1132 g_message ("Got async callback for #%lx, success = %d",
1133 window, success));
1134
1135 /* On failure, we immediately continue with the protocol
1136 * so we don't end up blocking for a timeout
1137 */
1138 if (!success &&
1139 context->dest_window &&
1140 window == GDK_WINDOW_XID (context->dest_window))
1141 {
1142 GdkEvent *temp_event;
1143 GdkX11DragContext *context_x11 = data;
1144
1145 g_object_unref (context->dest_window);
1146 context->dest_window = NULL;
1147 context->action = 0;
1148
1149 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1150
1151 temp_event = gdk_event_new (GDK_DRAG_STATUS);
1152 temp_event->dnd.window = g_object_ref (context->source_window);
1153 temp_event->dnd.send_event = TRUE;
1154 temp_event->dnd.context = g_object_ref (context);
1155 temp_event->dnd.time = GDK_CURRENT_TIME;
1156 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
1157
1158 gdk_event_put (temp_event);
1159
1160 gdk_event_free (temp_event);
1161 }
1162
1163 g_object_unref (context);
1164 }
1165
1166
1167 static GdkDisplay *
1168 gdk_drag_context_get_display (GdkDragContext *context)
1169 {
1170 if (context->source_window)
1171 return GDK_WINDOW_DISPLAY (context->source_window);
1172 else if (context->dest_window)
1173 return GDK_WINDOW_DISPLAY (context->dest_window);
1174
1175 g_assert_not_reached ();
1176 return NULL;
1177 }
1178
1179 static void
1180 send_client_message_async (GdkDragContext *context,
1181 Window window,
1182 gboolean propagate,
1183 glong event_mask,
1184 XClientMessageEvent *event_send)
1185 {
1186 GdkDisplay *display = gdk_drag_context_get_display (context);
1187
1188 g_object_ref (context);
1189
1190 _gdk_x11_send_client_message_async (display, window,
1191 propagate, event_mask, event_send,
1192 send_client_message_async_cb, context);
1193 }
1194
1195 static gboolean
1196 xdnd_send_xevent (GdkX11DragContext *context_x11,
1197 GdkWindow *window,
1198 gboolean propagate,
1199 XEvent *event_send)
1200 {
1201 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1202 GdkDisplay *display = gdk_drag_context_get_display (context);
1203 Window xwindow;
1204 glong event_mask;
1205
1206 g_assert (event_send->xany.type == ClientMessage);
1207
1208 /* We short-circuit messages to ourselves */
1209 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
1210 {
1211 gint i;
1212
1213 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
1214 {
1215 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
1216 event_send->xclient.message_type)
1217 {
1218 GdkEvent *temp_event;
1219
1220 temp_event = gdk_event_new (GDK_NOTHING);
1221 temp_event->any.window = g_object_ref (window);
1222
1223 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
1224 gdk_event_put (temp_event);
1225
1226 gdk_event_free (temp_event);
1227
1228 return TRUE;
1229 }
1230 }
1231 }
1232
1233 xwindow = GDK_WINDOW_XID (window);
1234
1235 if (_gdk_x11_display_is_root_window (display, xwindow))
1236 event_mask = ButtonPressMask;
1237 else
1238 event_mask = 0;
1239
1240 send_client_message_async (context, xwindow, propagate, event_mask,
1241 &event_send->xclient);
1242
1243 return TRUE;
1244 }
1245
1246 static void
1247 xdnd_send_enter (GdkX11DragContext *context_x11)
1248 {
1249 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1250 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
1251 XEvent xev;
1252
1253 xev.xclient.type = ClientMessage;
1254 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
1255 xev.xclient.format = 32;
1256 xev.xclient.window = context_x11->drop_xid
1257 ? context_x11->drop_xid
1258 : GDK_WINDOW_XID (context->dest_window);
1259 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
1260 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
1261 xev.xclient.data.l[2] = 0;
1262 xev.xclient.data.l[3] = 0;
1263 xev.xclient.data.l[4] = 0;
1264
1265 GDK_NOTE(DND,
1266 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
1267 GDK_WINDOW_XID (context->source_window), context_x11->version));
1268 if (g_list_length (context->targets) > 3)
1269 {
1270 if (!context_x11->xdnd_targets_set)
1271 xdnd_set_targets (context_x11);
1272 xev.xclient.data.l[1] |= 1;
1273 }
1274 else
1275 {
1276 GList *tmp_list = context->targets;
1277 gint i = 2;
1278
1279 while (tmp_list)
1280 {
1281 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
1282 GDK_POINTER_TO_ATOM (tmp_list->data));
1283 tmp_list = tmp_list->next;
1284 i++;
1285 }
1286 }
1287
1288 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
1289 {
1290 GDK_NOTE (DND,
1291 g_message ("Send event to %lx failed",
1292 GDK_WINDOW_XID (context->dest_window)));
1293 g_object_unref (context->dest_window);
1294 context->dest_window = NULL;
1295 }
1296 }
1297
1298 static void
1299 xdnd_send_leave (GdkX11DragContext *context_x11)
1300 {
1301 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1302 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1303 XEvent xev;
1304
1305 xev.xclient.type = ClientMessage;
1306 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
1307 xev.xclient.format = 32;
1308 xev.xclient.window = context_x11->drop_xid
1309 ? context_x11->drop_xid
1310 : GDK_WINDOW_XID (context->dest_window);
1311 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
1312 xev.xclient.data.l[1] = 0;
1313 xev.xclient.data.l[2] = 0;
1314 xev.xclient.data.l[3] = 0;
1315 xev.xclient.data.l[4] = 0;
1316
1317 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
1318 {
1319 GDK_NOTE (DND,
1320 g_message ("Send event to %lx failed",
1321 GDK_WINDOW_XID (context->dest_window)));
1322 g_object_unref (context->dest_window);
1323 context->dest_window = NULL;
1324 }
1325 }
1326
1327 static void
1328 xdnd_send_drop (GdkX11DragContext *context_x11,
1329 guint32 time)
1330 {
1331 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1332 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1333 XEvent xev;
1334
1335 xev.xclient.type = ClientMessage;
1336 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
1337 xev.xclient.format = 32;
1338 xev.xclient.window = context_x11->drop_xid
1339 ? context_x11->drop_xid
1340 : GDK_WINDOW_XID (context->dest_window);
1341 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
1342 xev.xclient.data.l[1] = 0;
1343 xev.xclient.data.l[2] = time;
1344 xev.xclient.data.l[3] = 0;
1345 xev.xclient.data.l[4] = 0;
1346
1347 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
1348 {
1349 GDK_NOTE (DND,
1350 g_message ("Send event to %lx failed",
1351 GDK_WINDOW_XID (context->dest_window)));
1352 g_object_unref (context->dest_window);
1353 context->dest_window = NULL;
1354 }
1355 }
1356
1357 static void
1358 xdnd_send_motion (GdkX11DragContext *context_x11,
1359 gint x_root,
1360 gint y_root,
1361 GdkDragAction action,
1362 guint32 time)
1363 {
1364 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1365 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1366 XEvent xev;
1367
1368 xev.xclient.type = ClientMessage;
1369 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
1370 xev.xclient.format = 32;
1371 xev.xclient.window = context_x11->drop_xid
1372 ? context_x11->drop_xid
1373 : GDK_WINDOW_XID (context->dest_window);
1374 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
1375 xev.xclient.data.l[1] = 0;
1376 xev.xclient.data.l[2] = (x_root << 16) | y_root;
1377 xev.xclient.data.l[3] = time;
1378 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
1379
1380 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
1381 {
1382 GDK_NOTE (DND,
1383 g_message ("Send event to %lx failed",
1384 GDK_WINDOW_XID (context->dest_window)));
1385 g_object_unref (context->dest_window);
1386 context->dest_window = NULL;
1387 }
1388 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1389 }
1390
1391 static guint32
1392 xdnd_check_dest (GdkDisplay *display,
1393 Window win,
1394 guint *xdnd_version)
1395 {
1396 gboolean retval = FALSE;
1397 Atom type = None;
1398 int format;
1399 unsigned long nitems, after;
1400 guchar *data;
1401 Atom *version;
1402 Window *proxy_data;
1403 Window proxy;
1404 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
1405 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
1406
1407 proxy = None;
1408
1409 gdk_x11_display_error_trap_push (display);
1410 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1411 xdnd_proxy_atom, 0,
1412 1, False, AnyPropertyType,
1413 &type, &format, &nitems, &after,
1414 &data) == Success)
1415 {
1416 if (type != None)
1417 {
1418 proxy_data = (Window *)data;
1419
1420 if ((format == 32) && (nitems == 1))
1421 {
1422 proxy = *proxy_data;
1423 }
1424 else
1425 {
1426 GDK_NOTE (DND,
1427 g_warning ("Invalid XdndProxy property on window %ld", win));
1428 }
1429
1430 XFree (proxy_data);
1431 }
1432
1433 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
1434 xdnd_aware_atom, 0,
1435 1, False, AnyPropertyType,
1436 &type, &format, &nitems, &after,
1437 &data) == Success) &&
1438 type != None)
1439 {
1440 version = (Atom *)data;
1441
1442 if ((format == 32) && (nitems == 1))
1443 {
1444 if (*version >= 3)
1445 retval = TRUE;
1446 if (xdnd_version)
1447 *xdnd_version = *version;
1448 }
1449 else
1450 {
1451 GDK_NOTE (DND,
1452 g_warning ("Invalid XdndAware property on window %ld", win));
1453 }
1454
1455 XFree (version);
1456 }
1457 }
1458
1459 gdk_x11_display_error_trap_pop_ignored (display);
1460
1461 return retval ? (proxy ? proxy : win) : None;
1462 }
1463
1464 /* Target side */
1465
1466 static void
1467 xdnd_read_actions (GdkX11DragContext *context_x11)
1468 {
1469 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1470 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1471 Atom type;
1472 int format;
1473 gulong nitems, after;
1474 guchar *data;
1475 Atom *atoms;
1476 gint i;
1477
1478 context_x11->xdnd_have_actions = FALSE;
1479
1480 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
1481 {
1482 /* Get the XdndActionList, if set */
1483
1484 gdk_x11_display_error_trap_push (display);
1485 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
1486 GDK_WINDOW_XID (context->source_window),
1487 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
1488 0, 65536,
1489 False, XA_ATOM, &type, &format, &nitems,
1490 &after, &data) == Success &&
1491 type == XA_ATOM)
1492 {
1493 atoms = (Atom *)data;
1494
1495 context->actions = 0;
1496
1497 for (i = 0; i < nitems; i++)
1498 context->actions |= xdnd_action_from_atom (display, atoms[i]);
1499
1500 context_x11->xdnd_have_actions = TRUE;
1501
1502 #ifdef G_ENABLE_DEBUG
1503 if (GDK_DEBUG_CHECK (DND))
1504 {
1505 GString *action_str = g_string_new (NULL);
1506 if (context->actions & GDK_ACTION_MOVE)
1507 g_string_append(action_str, "MOVE ");
1508 if (context->actions & GDK_ACTION_COPY)
1509 g_string_append(action_str, "COPY ");
1510 if (context->actions & GDK_ACTION_LINK)
1511 g_string_append(action_str, "LINK ");
1512 if (context->actions & GDK_ACTION_ASK)
1513 g_string_append(action_str, "ASK ");
1514
1515 g_message("Xdnd actions = %s", action_str->str);
1516 g_string_free (action_str, TRUE);
1517 }
1518 #endif /* G_ENABLE_DEBUG */
1519
1520 }
1521
1522 if (data)
1523 XFree (data);
1524
1525 gdk_x11_display_error_trap_pop_ignored (display);
1526 }
1527 else
1528 {
1529 /* Local drag
1530 */
1531 GdkDragContext *source_context;
1532
1533 source_context = gdk_drag_context_find (display, TRUE,
1534 GDK_WINDOW_XID (context->source_window),
1535 GDK_WINDOW_XID (context->dest_window));
1536
1537 if (source_context)
1538 {
1539 context->actions = source_context->actions;
1540 context_x11->xdnd_have_actions = TRUE;
1541 }
1542 }
1543 }
1544
1545 /* We have to make sure that the XdndActionList we keep internally
1546 * is up to date with the XdndActionList on the source window
1547 * because we get no notification, because Xdnd wasn’t meant
1548 * to continually send actions. So we select on PropertyChangeMask
1549 * and add this filter.
1550 */
1551 static GdkFilterReturn
1552 xdnd_source_window_filter (GdkXEvent *xev,
1553 GdkEvent *event,
1554 gpointer cb_data)
1555 {
1556 XEvent *xevent = (XEvent *)xev;
1557 GdkX11DragContext *context_x11 = cb_data;
1558 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
1559
1560 if ((xevent->xany.type == PropertyNotify) &&
1561 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
1562 {
1563 xdnd_read_actions (context_x11);
1564
1565 return GDK_FILTER_REMOVE;
1566 }
1567
1568 return GDK_FILTER_CONTINUE;
1569 }
1570
1571 static void
1572 xdnd_manage_source_filter (GdkDragContext *context,
1573 GdkWindow *window,
1574 gboolean add_filter)
1575 {
1576 if (!GDK_WINDOW_DESTROYED (window) &&
1577 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
1578 {
1579 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
1580
1581 if (add_filter)
1582 {
1583 gdk_window_set_events (window,
1584 gdk_window_get_events (window) |
1585 GDK_PROPERTY_CHANGE_MASK);
1586 gdk_window_add_filter (window, xdnd_source_window_filter, context);
1587 }
1588 else
1589 {
1590 gdk_window_remove_filter (window,
1591 xdnd_source_window_filter,
1592 context);
1593 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
1594 * but we might want it for other reasons. (Like
1595 * INCR selection transactions).
1596 */
1597 }
1598
1599 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
1600 }
1601 }
1602
1603 static void
1604 base_precache_atoms (GdkDisplay *display)
1605 {
1606 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1607
1608 if (!display_x11->base_dnd_atoms_precached)
1609 {
1610 static const char *const precache_atoms[] = {
1611 "WM_STATE",
1612 "XdndAware",
1613 "XdndProxy"
1614 };
1615
1616 _gdk_x11_precache_atoms (display,
1617 precache_atoms, G_N_ELEMENTS (precache_atoms));
1618
1619 display_x11->base_dnd_atoms_precached = TRUE;
1620 }
1621 }
1622
1623 static void
1624 xdnd_precache_atoms (GdkDisplay *display)
1625 {
1626 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1627
1628 if (!display_x11->xdnd_atoms_precached)
1629 {
1630 static const gchar *const precache_atoms[] = {
1631 "XdndActionAsk",
1632 "XdndActionCopy",
1633 "XdndActionLink",
1634 "XdndActionList",
1635 "XdndActionMove",
1636 "XdndActionPrivate",
1637 "XdndDrop",
1638 "XdndEnter",
1639 "XdndFinished",
1640 "XdndLeave",
1641 "XdndPosition",
1642 "XdndSelection",
1643 "XdndStatus",
1644 "XdndTypeList"
1645 };
1646
1647 _gdk_x11_precache_atoms (display,
1648 precache_atoms, G_N_ELEMENTS (precache_atoms));
1649
1650 display_x11->xdnd_atoms_precached = TRUE;
1651 }
1652 }
1653
1654 static GdkFilterReturn
1655 xdnd_enter_filter (GdkXEvent *xev,
1656 GdkEvent *event,
1657 gpointer cb_data)
1658 {
1659 GdkDisplay *display;
1660 GdkX11Display *display_x11;
1661 XEvent *xevent = (XEvent *)xev;
1662 GdkDragContext *context;
1663 GdkX11DragContext *context_x11;
1664 GdkSeat *seat;
1665 gint i;
1666 Atom type;
1667 int format;
1668 gulong nitems, after;
1669 guchar *data;
1670 Atom *atoms;
1671 guint32 source_window;
1672 gboolean get_types;
1673 gint version;
1674
1675 if (!event->any.window ||
1676 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1677 return GDK_FILTER_CONTINUE; /* Not for us */
1678
1679 source_window = xevent->xclient.data.l[0];
1680 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
1681 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
1682
1683 display = GDK_WINDOW_DISPLAY (event->any.window);
1684 display_x11 = GDK_X11_DISPLAY (display);
1685
1686 xdnd_precache_atoms (display);
1687
1688 GDK_NOTE (DND,
1689 g_message ("XdndEnter: source_window: %#x, version: %#x",
1690 source_window, version));
1691
1692 if (version < 3)
1693 {
1694 /* Old source ignore */
1695 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
1696 return GDK_FILTER_REMOVE;
1697 }
1698
1699 if (display_x11->current_dest_drag != NULL)
1700 {
1701 g_object_unref (display_x11->current_dest_drag);
1702 display_x11->current_dest_drag = NULL;
1703 }
1704
1705 context_x11 = (GdkX11DragContext *)g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
1706 context = (GdkDragContext *)context_x11;
1707
1708 context->display = display;
1709 context->protocol = GDK_DRAG_PROTO_XDND;
1710 context_x11->version = version;
1711
1712 /* FIXME: Should extend DnD protocol to have device info */
1713 seat = gdk_display_get_default_seat (display);
1714 gdk_drag_context_set_device (context, gdk_seat_get_pointer (seat));
1715
1716 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1717 if (!context->source_window)
1718 {
1719 g_object_unref (context);
1720 return GDK_FILTER_REMOVE;
1721 }
1722 context->dest_window = event->any.window;
1723 g_object_ref (context->dest_window);
1724
1725 context->targets = NULL;
1726 if (get_types)
1727 {
1728 gdk_x11_display_error_trap_push (display);
1729 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
1730 source_window,
1731 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
1732 0, 65536,
1733 False, XA_ATOM, &type, &format, &nitems,
1734 &after, &data);
1735
1736 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
1737 {
1738 g_object_unref (context);
1739
1740 if (data)
1741 XFree (data);
1742
1743 return GDK_FILTER_REMOVE;
1744 }
1745
1746 atoms = (Atom *)data;
1747
1748 for (i = 0; i < nitems; i++)
1749 context->targets =
1750 g_list_append (context->targets,
1751 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
1752 atoms[i])));
1753
1754 XFree (atoms);
1755 }
1756 else
1757 {
1758 for (i = 0; i < 3; i++)
1759 if (xevent->xclient.data.l[2 + i])
1760 context->targets =
1761 g_list_append (context->targets,
1762 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
1763 xevent->xclient.data.l[2 + i])));
1764 }
1765
1766 #ifdef G_ENABLE_DEBUG
1767 if (GDK_DEBUG_CHECK (DND))
1768 print_target_list (context->targets);
1769 #endif /* G_ENABLE_DEBUG */
1770
1771 xdnd_manage_source_filter (context, context->source_window, TRUE);
1772 xdnd_read_actions (context_x11);
1773
1774 event->dnd.type = GDK_DRAG_ENTER;
1775 event->dnd.context = context;
1776 gdk_event_set_device (event, gdk_drag_context_get_device (context));
1777 g_object_ref (context);
1778
1779 display_x11->current_dest_drag = context;
1780
1781 return GDK_FILTER_TRANSLATE;
1782 }
1783
1784 static GdkFilterReturn
1785 xdnd_leave_filter (GdkXEvent *xev,
1786 GdkEvent *event,
1787 gpointer data)
1788 {
1789 XEvent *xevent = (XEvent *)xev;
1790 guint32 source_window = xevent->xclient.data.l[0];
1791 GdkDisplay *display;
1792 GdkX11Display *display_x11;
1793
1794 if (!event->any.window ||
1795 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1796 return GDK_FILTER_CONTINUE; /* Not for us */
1797
1798 GDK_NOTE (DND,
1799 g_message ("XdndLeave: source_window: %#x",
1800 source_window));
1801
1802 display = GDK_WINDOW_DISPLAY (event->any.window);
1803 display_x11 = GDK_X11_DISPLAY (display);
1804
1805 xdnd_precache_atoms (display);
1806
1807 if ((display_x11->current_dest_drag != NULL) &&
1808 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
1809 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
1810 {
1811 event->dnd.type = GDK_DRAG_LEAVE;
1812 /* Pass ownership of context to the event */
1813 event->dnd.context = display_x11->current_dest_drag;
1814 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
1815
1816 display_x11->current_dest_drag = NULL;
1817
1818 return GDK_FILTER_TRANSLATE;
1819 }
1820 else
1821 return GDK_FILTER_REMOVE;
1822 }
1823
1824 static GdkFilterReturn
1825 xdnd_position_filter (GdkXEvent *xev,
1826 GdkEvent *event,
1827 gpointer data)
1828 {
1829 GdkWindowImplX11 *impl;
1830 XEvent *xevent = (XEvent *)xev;
1831 guint32 source_window = xevent->xclient.data.l[0];
1832 gint16 x_root = xevent->xclient.data.l[2] >> 16;
1833 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
1834 guint32 time = xevent->xclient.data.l[3];
1835 Atom action = xevent->xclient.data.l[4];
1836 GdkDisplay *display;
1837 GdkX11Display *display_x11;
1838 GdkDragContext *context;
1839 GdkX11DragContext *context_x11;
1840
1841 if (!event->any.window ||
1842 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1843 return GDK_FILTER_CONTINUE; /* Not for us */
1844
1845 GDK_NOTE (DND,
1846 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
1847 source_window, x_root, y_root, time, action));
1848
1849 display = GDK_WINDOW_DISPLAY (event->any.window);
1850 display_x11 = GDK_X11_DISPLAY (display);
1851
1852 xdnd_precache_atoms (display);
1853
1854 context = display_x11->current_dest_drag;
1855
1856 if ((context != NULL) &&
1857 (context->protocol == GDK_DRAG_PROTO_XDND) &&
1858 (GDK_WINDOW_XID (context->source_window) == source_window))
1859 {
1860 impl = GDK_WINDOW_IMPL_X11 (event->any.window->impl);
1861
1862 context_x11 = GDK_X11_DRAG_CONTEXT (context);
1863
1864 event->dnd.type = GDK_DRAG_MOTION;
1865 event->dnd.context = context;
1866 gdk_event_set_device (event, gdk_drag_context_get_device (context));
1867 g_object_ref (context);
1868
1869 event->dnd.time = time;
1870
1871 context->suggested_action = xdnd_action_from_atom (display, action);
1872
1873 if (!context_x11->xdnd_have_actions)
1874 context->actions = context->suggested_action;
1875
1876 event->dnd.x_root = x_root / impl->window_scale;
1877 event->dnd.y_root = y_root / impl->window_scale;
1878
1879 context_x11->last_x = x_root / impl->window_scale;
1880 context_x11->last_y = y_root / impl->window_scale;
1881
1882 return GDK_FILTER_TRANSLATE;
1883 }
1884
1885 return GDK_FILTER_REMOVE;
1886 }
1887
1888 static GdkFilterReturn
1889 xdnd_drop_filter (GdkXEvent *xev,
1890 GdkEvent *event,
1891 gpointer data)
1892 {
1893 XEvent *xevent = (XEvent *)xev;
1894 guint32 source_window = xevent->xclient.data.l[0];
1895 guint32 time = xevent->xclient.data.l[2];
1896 GdkDisplay *display;
1897 GdkX11Display *display_x11;
1898 GdkDragContext *context;
1899 GdkX11DragContext *context_x11;
1900
1901 if (!event->any.window ||
1902 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1903 return GDK_FILTER_CONTINUE; /* Not for us */
1904
1905 GDK_NOTE (DND,
1906 g_message ("XdndDrop: source_window: %#x time: %d",
1907 source_window, time));
1908
1909 display = GDK_WINDOW_DISPLAY (event->any.window);
1910 display_x11 = GDK_X11_DISPLAY (display);
1911
1912 xdnd_precache_atoms (display);
1913
1914 context = display_x11->current_dest_drag;
1915
1916 if ((context != NULL) &&
1917 (context->protocol == GDK_DRAG_PROTO_XDND) &&
1918 (GDK_WINDOW_XID (context->source_window) == source_window))
1919 {
1920 context_x11 = GDK_X11_DRAG_CONTEXT (context);
1921 event->dnd.type = GDK_DROP_START;
1922
1923 event->dnd.context = context;
1924 gdk_event_set_device (event, gdk_drag_context_get_device (context));
1925 g_object_ref (context);
1926
1927 event->dnd.time = time;
1928 event->dnd.x_root = context_x11->last_x;
1929 event->dnd.y_root = context_x11->last_y;
1930
1931 gdk_x11_window_set_user_time (event->any.window, time);
1932
1933 return GDK_FILTER_TRANSLATE;
1934 }
1935
1936 return GDK_FILTER_REMOVE;
1937 }
1938
1939 GdkFilterReturn
1940 _gdk_x11_dnd_filter (GdkXEvent *xev,
1941 GdkEvent *event,
1942 gpointer data)
1943 {
1944 XEvent *xevent = (XEvent *) xev;
1945 GdkDisplay *display;
1946 int i;
1947
1948 if (!GDK_IS_X11_WINDOW (event->any.window))
1949 return GDK_FILTER_CONTINUE;
1950
1951 if (xevent->type != ClientMessage)
1952 return GDK_FILTER_CONTINUE;
1953
1954 display = GDK_WINDOW_DISPLAY (event->any.window);
1955
1956 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
1957 {
1958 if (xevent->xclient.message_type != gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name))
1959 continue;
1960
1961 return xdnd_filters[i].func (xev, event, data);
1962 }
1963
1964 return GDK_FILTER_CONTINUE;
1965 }
1966
1967 /* Source side */
1968
1969 static void
1970 gdk_drag_do_leave (GdkX11DragContext *context_x11,
1971 guint32 time)
1972 {
1973 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1974
1975 if (context->dest_window)
1976 {
1977 switch (context->protocol)
1978 {
1979 case GDK_DRAG_PROTO_XDND:
1980 xdnd_send_leave (context_x11);
1981 break;
1982 case GDK_DRAG_PROTO_ROOTWIN:
1983 case GDK_DRAG_PROTO_NONE:
1984 default:
1985 break;
1986 }
1987
1988 g_object_unref (context->dest_window);
1989 context->dest_window = NULL;
1990 }
1991 }
1992
1993 static GdkWindow *
1994 create_drag_window (GdkScreen *screen)
1995 {
1996 GdkWindowAttr attrs = { 0 };
1997 guint mask;
1998
1999 attrs.x = attrs.y = 0;
2000 attrs.width = attrs.height = 100;
2001 attrs.wclass = GDK_INPUT_OUTPUT;
2002 attrs.window_type = GDK_WINDOW_TEMP;
2003 attrs.type_hint = GDK_WINDOW_TYPE_HINT_DND;
2004 attrs.visual = gdk_screen_get_rgba_visual (screen);
2005 if (!attrs.visual)
2006 attrs.visual = gdk_screen_get_system_visual (screen);
2007
2008 mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
2009
2010 return gdk_window_new (gdk_screen_get_root_window (screen), &attrs, mask);
2011 }
2012
2013 GdkDragContext *
2014 _gdk_x11_window_drag_begin (GdkWindow *window,
2015 GdkDevice *device,
2016 GList *targets,
2017 gint x_root,
2018 gint y_root)
2019 {
2020 GdkDragContext *context;
2021
2022 context = (GdkDragContext *) g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
2023
2024 context->display = gdk_window_get_display (window);
2025 context->is_source = TRUE;
2026 context->source_window = window;
2027 g_object_ref (window);
2028
2029 context->targets = g_list_copy (targets);
2030 precache_target_list (context);
2031
2032 context->actions = 0;
2033
2034 gdk_drag_context_set_device (context, device);
2035
2036 GDK_X11_DRAG_CONTEXT (context)->start_x = x_root;
2037 GDK_X11_DRAG_CONTEXT (context)->start_y = y_root;
2038 GDK_X11_DRAG_CONTEXT (context)->last_x = x_root;
2039 GDK_X11_DRAG_CONTEXT (context)->last_y = y_root;
2040
2041 GDK_X11_DRAG_CONTEXT (context)->drag_window = create_drag_window (gdk_window_get_screen (window));
2042
2043 return context;
2044 }
2045
2046 Window
2047 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
2048 Window xid,
2049 GdkDragProtocol *protocol,
2050 guint *version)
2051
2052 {
2053 GdkWindow *window;
2054 Window retval;
2055
2056 base_precache_atoms (display);
2057
2058 /* Check for a local drag */
2059 window = gdk_x11_window_lookup_for_display (display, xid);
2060 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2061 {
2062 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2063 {
2064 *protocol = GDK_DRAG_PROTO_XDND;
2065 *version = 5;
2066 xdnd_precache_atoms (display);
2067 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", (guint) xid));
2068 return xid;
2069 }
2070 else if (_gdk_x11_display_is_root_window (display, xid))
2071 {
2072 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2073 GDK_NOTE (DND, g_message ("Entering root window\n"));
2074 return xid;
2075 }
2076 }
2077 else if ((retval = xdnd_check_dest (display, xid, version)))
2078 {
2079 *protocol = GDK_DRAG_PROTO_XDND;
2080 xdnd_precache_atoms (display);
2081 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", (guint) xid));
2082 return retval;
2083 }
2084 else
2085 {
2086 /* Check if this is a root window */
2087 gboolean rootwin = FALSE;
2088
2089 if (_gdk_x11_display_is_root_window (display, (Window) xid))
2090 rootwin = TRUE;
2091
2092 if (rootwin)
2093 {
2094 GDK_NOTE (DND, g_message ("Entering root window\n"));
2095 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2096 return xid;
2097 }
2098 }
2099
2100 *protocol = GDK_DRAG_PROTO_NONE;
2101
2102 return 0; /* a.k.a. None */
2103 }
2104
2105 static GdkWindowCache *
2106 drag_context_find_window_cache (GdkX11DragContext *context_x11,
2107 GdkScreen *screen)
2108 {
2109 GSList *list;
2110 GdkWindowCache *cache;
2111
2112 for (list = context_x11->window_caches; list; list = list->next)
2113 {
2114 cache = list->data;
2115 if (cache->screen == screen)
2116 return cache;
2117 }
2118
2119 cache = gdk_window_cache_get (screen);
2120 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
2121
2122 return cache;
2123 }
2124
2125 static GdkWindow *
2126 gdk_x11_drag_context_find_window (GdkDragContext *context,
2127 GdkWindow *drag_window,
2128 GdkScreen *screen,
2129 gint x_root,
2130 gint y_root,
2131 GdkDragProtocol *protocol)
2132 {
2133 GdkX11Screen *screen_x11 = GDK_X11_SCREEN(screen);
2134 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2135 GdkWindowCache *window_cache;
2136 GdkDisplay *display;
2137 Window dest;
2138 GdkWindow *dest_window;
2139
2140 display = GDK_WINDOW_DISPLAY (context->source_window);
2141
2142 window_cache = drag_context_find_window_cache (context_x11, screen);
2143
2144 dest = get_client_window_at_coords (window_cache,
2145 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
2146 GDK_WINDOW_XID (drag_window) : None,
2147 x_root * screen_x11->window_scale,
2148 y_root * screen_x11->window_scale);
2149
2150 if (context_x11->dest_xid != dest)
2151 {
2152 Window recipient;
2153 context_x11->dest_xid = dest;
2154
2155 /* Check if new destination accepts drags, and which protocol */
2156
2157 /* There is some ugliness here. We actually need to pass
2158 * _three_ pieces of information to drag_motion - dest_window,
2159 * protocol, and the XID of the unproxied window. The first
2160 * two are passed explicitly, the third implicitly through
2161 * protocol->dest_xid.
2162 */
2163 recipient = _gdk_x11_display_get_drag_protocol (display,
2164 dest,
2165 protocol,
2166 &context_x11->version);
2167
2168 if (recipient != None)
2169 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
2170 else
2171 dest_window = NULL;
2172 }
2173 else
2174 {
2175 dest_window = context->dest_window;
2176 if (dest_window)
2177 g_object_ref (dest_window);
2178 *protocol = context->protocol;
2179 }
2180
2181 return dest_window;
2182 }
2183
2184 static void
2185 move_drag_window (GdkDragContext *context,
2186 guint x_root,
2187 guint y_root)
2188 {
2189 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2190
2191 gdk_window_move (context_x11->drag_window,
2192 x_root - context_x11->hot_x,
2193 y_root - context_x11->hot_y);
2194 gdk_window_raise (context_x11->drag_window);
2195 }
2196
2197 static gboolean
2198 gdk_x11_drag_context_drag_motion (GdkDragContext *context,
2199 GdkWindow *dest_window,
2200 GdkDragProtocol protocol,
2201 gint x_root,
2202 gint y_root,
2203 GdkDragAction suggested_action,
2204 GdkDragAction possible_actions,
2205 guint32 time)
2206 {
2207 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2208 GdkWindowImplX11 *impl;
2209
2210 if (context_x11->drag_window)
2211 move_drag_window (context, x_root, y_root);
2212
2213 context_x11->old_actions = context->actions;
2214 context->actions = possible_actions;
2215
2216 if (context_x11->old_actions != possible_actions)
2217 context_x11->xdnd_actions_set = FALSE;
2218
2219 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
2220 {
2221 /* This ugly hack is necessary since GTK+ doesn't know about
2222 * the XDND protocol version, and in particular doesn't know
2223 * that gdk_drag_find_window_for_screen() has the side-effect
2224 * of setting context_x11->version, and therefore sometimes call
2225 * gdk_drag_motion() without a prior call to
2226 * gdk_drag_find_window_for_screen(). This happens, e.g.
2227 * when GTK+ is proxying DND events to embedded windows.
2228 */
2229 if (dest_window)
2230 {
2231 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
2232
2233 xdnd_check_dest (display,
2234 GDK_WINDOW_XID (dest_window),
2235 &context_x11->version);
2236 }
2237 }
2238
2239 /* When we have a Xdnd target, make sure our XdndActionList
2240 * matches the current actions;
2241 */
2242 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
2243 {
2244 if (dest_window)
2245 {
2246 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
2247 xdnd_set_actions (context_x11);
2248 else if (context->dest_window == dest_window)
2249 {
2250 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
2251 GdkDragContext *dest_context;
2252
2253 dest_context = gdk_drag_context_find (display, FALSE,
2254 GDK_WINDOW_XID (context->source_window),
2255 GDK_WINDOW_XID (dest_window));
2256
2257 if (dest_context)
2258 {
2259 dest_context->actions = context->actions;
2260 GDK_X11_DRAG_CONTEXT (dest_context)->xdnd_have_actions = TRUE;
2261 }
2262 }
2263 }
2264 }
2265
2266 if (context->dest_window != dest_window)
2267 {
2268 GdkEvent *temp_event;
2269
2270 /* Send a leave to the last destination */
2271 gdk_drag_do_leave (context_x11, time);
2272 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2273
2274 /* Check if new destination accepts drags, and which protocol */
2275
2276 if (dest_window)
2277 {
2278 context->dest_window = dest_window;
2279 context_x11->drop_xid = context_x11->dest_xid;
2280 g_object_ref (context->dest_window);
2281 context->protocol = protocol;
2282
2283 switch (protocol)
2284 {
2285 case GDK_DRAG_PROTO_XDND:
2286 xdnd_send_enter (context_x11);
2287 break;
2288
2289 case GDK_DRAG_PROTO_ROOTWIN:
2290 case GDK_DRAG_PROTO_NONE:
2291 default:
2292 break;
2293 }
2294 context_x11->old_action = suggested_action;
2295 context->suggested_action = suggested_action;
2296 context_x11->old_actions = possible_actions;
2297 }
2298 else
2299 {
2300 context->dest_window = NULL;
2301 context_x11->drop_xid = None;
2302 context->action = 0;
2303 }
2304
2305 /* Push a status event, to let the client know that
2306 * the drag changed
2307 */
2308 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2309 temp_event->dnd.window = g_object_ref (context->source_window);
2310 /* We use this to signal a synthetic status. Perhaps
2311 * we should use an extra field...
2312 */
2313 temp_event->dnd.send_event = TRUE;
2314
2315 temp_event->dnd.context = g_object_ref (context);
2316 temp_event->dnd.time = time;
2317 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2318
2319 gdk_event_put (temp_event);
2320 gdk_event_free (temp_event);
2321 }
2322 else
2323 {
2324 context_x11->old_action = context->suggested_action;
2325 context->suggested_action = suggested_action;
2326 }
2327
2328 /* Send a drag-motion event */
2329
2330 context_x11->last_x = x_root;
2331 context_x11->last_y = y_root;
2332
2333 if (context->dest_window)
2334 {
2335 impl = GDK_WINDOW_IMPL_X11 (context->dest_window->impl);
2336
2337 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
2338 {
2339 switch (context->protocol)
2340 {
2341 case GDK_DRAG_PROTO_XDND:
2342 xdnd_send_motion (context_x11, x_root * impl->window_scale, y_root * impl->window_scale, suggested_action, time);
2343 break;
2344
2345 case GDK_DRAG_PROTO_ROOTWIN:
2346 {
2347 GdkEvent *temp_event;
2348 /* GTK+ traditionally has used application/x-rootwin-drop,
2349 * but the XDND spec specifies x-rootwindow-drop.
2350 */
2351 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
2352 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
2353
2354 if (g_list_find (context->targets,
2355 GDK_ATOM_TO_POINTER (target1)) ||
2356 g_list_find (context->targets,
2357 GDK_ATOM_TO_POINTER (target2)))
2358 context->action = context->suggested_action;
2359 else
2360 context->action = 0;
2361
2362 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2363 temp_event->dnd.window = g_object_ref (context->source_window);
2364 temp_event->dnd.send_event = FALSE;
2365 temp_event->dnd.context = g_object_ref (context);
2366 temp_event->dnd.time = time;
2367 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2368
2369 gdk_event_put (temp_event);
2370 gdk_event_free (temp_event);
2371 }
2372 break;
2373 case GDK_DRAG_PROTO_NONE:
2374 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
2375 break;
2376 default:
2377 break;
2378 }
2379 }
2380 else
2381 return TRUE;
2382 }
2383
2384 return FALSE;
2385 }
2386
2387 static void
2388 gdk_x11_drag_context_drag_abort (GdkDragContext *context,
2389 guint32 time)
2390 {
2391 gdk_drag_do_leave (GDK_X11_DRAG_CONTEXT (context), time);
2392 }
2393
2394 static void
2395 gdk_x11_drag_context_drag_drop (GdkDragContext *context,
2396 guint32 time)
2397 {
2398 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2399
2400 if (context->dest_window)
2401 {
2402 switch (context->protocol)
2403 {
2404 case GDK_DRAG_PROTO_XDND:
2405 xdnd_send_drop (context_x11, time);
2406 break;
2407
2408 case GDK_DRAG_PROTO_ROOTWIN:
2409 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
2410 break;
2411 case GDK_DRAG_PROTO_NONE:
2412 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
2413 break;
2414 default:
2415 break;
2416 }
2417 }
2418 }
2419
2420 /* Destination side */
2421
2422 static void
2423 gdk_x11_drag_context_drag_status (GdkDragContext *context,
2424 GdkDragAction action,
2425 guint32 time_)
2426 {
2427 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2428 XEvent xev;
2429 GdkDisplay *display;
2430
2431 display = GDK_WINDOW_DISPLAY (context->source_window);
2432
2433 context->action = action;
2434
2435 if (context->protocol == GDK_DRAG_PROTO_XDND)
2436 {
2437 xev.xclient.type = ClientMessage;
2438 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
2439 xev.xclient.format = 32;
2440 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
2441
2442 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
2443 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
2444 xev.xclient.data.l[2] = 0;
2445 xev.xclient.data.l[3] = 0;
2446 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2447 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
2448 {
2449 GDK_NOTE (DND,
2450 g_message ("Send event to %lx failed",
2451 GDK_WINDOW_XID (context->source_window)));
2452 }
2453 }
2454
2455 context_x11->old_action = action;
2456 }
2457
2458 static void
2459 gdk_x11_drag_context_drop_reply (GdkDragContext *context,
2460 gboolean accepted,
2461 guint32 time_)
2462 {
2463 }
2464
2465 static void
2466 gdk_x11_drag_context_drop_finish (GdkDragContext *context,
2467 gboolean success,
2468 guint32 time)
2469 {
2470 if (context->protocol == GDK_DRAG_PROTO_XDND)
2471 {
2472 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2473 XEvent xev;
2474
2475 xev.xclient.type = ClientMessage;
2476 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
2477 xev.xclient.format = 32;
2478 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
2479
2480 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
2481 if (success)
2482 {
2483 xev.xclient.data.l[1] = 1;
2484 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
2485 context->action);
2486 }
2487 else
2488 {
2489 xev.xclient.data.l[1] = 0;
2490 xev.xclient.data.l[2] = None;
2491 }
2492 xev.xclient.data.l[3] = 0;
2493 xev.xclient.data.l[4] = 0;
2494
2495 if (!xdnd_send_xevent (GDK_X11_DRAG_CONTEXT (context), context->source_window, FALSE, &xev))
2496 {
2497 GDK_NOTE (DND,
2498 g_message ("Send event to %lx failed",
2499 GDK_WINDOW_XID (context->source_window)));
2500 }
2501 }
2502 }
2503
2504 void
2505 _gdk_x11_window_register_dnd (GdkWindow *window)
2506 {
2507 static const gulong xdnd_version = 5;
2508 GdkDisplay *display = gdk_window_get_display (window);
2509
2510 g_return_if_fail (window != NULL);
2511
2512 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
2513 return;
2514
2515 base_precache_atoms (display);
2516
2517 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2518 return;
2519 else
2520 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
2521
2522 /* Set XdndAware */
2523
2524 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
2525 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
2526 GDK_WINDOW_XID (window),
2527 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
2528 XA_ATOM, 32, PropModeReplace,
2529 (guchar *)&xdnd_version, 1);
2530 }
2531
2532 static GdkAtom
2533 gdk_x11_drag_context_get_selection (GdkDragContext *context)
2534 {
2535 if (context->protocol == GDK_DRAG_PROTO_XDND)
2536 return gdk_atom_intern_static_string ("XdndSelection");
2537 else
2538 return GDK_NONE;
2539 }
2540
2541 static gboolean
2542 gdk_x11_drag_context_drop_status (GdkDragContext *context)
2543 {
2544 return ! GDK_X11_DRAG_CONTEXT (context)->drop_failed;
2545 }
2546
2547 static GdkWindow *
2548 gdk_x11_drag_context_get_drag_window (GdkDragContext *context)
2549 {
2550 return GDK_X11_DRAG_CONTEXT (context)->drag_window;
2551 }
2552
2553 static void
2554 gdk_x11_drag_context_set_hotspot (GdkDragContext *context,
2555 gint hot_x,
2556 gint hot_y)
2557 {
2558 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2559
2560 x11_context->hot_x = hot_x;
2561 x11_context->hot_y = hot_y;
2562
2563 if (x11_context->grab_seat)
2564 {
2565 /* DnD is managed, update current position */
2566 move_drag_window (context, x11_context->last_x, x11_context->last_y);
2567 }
2568 }
2569
2570 static double
2571 ease_out_cubic (double t)
2572 {
2573 double p = t - 1;
2574 return p * p * p + 1;
2575 }
2576
2577
2578 #define ANIM_TIME 500000 /* half a second */
2579
2580 typedef struct _GdkDragAnim GdkDragAnim;
2581 struct _GdkDragAnim {
2582 GdkX11DragContext *context;
2583 GdkFrameClock *frame_clock;
2584 gint64 start_time;
2585 };
2586
2587 static void
2588 gdk_drag_anim_destroy (GdkDragAnim *anim)
2589 {
2590 g_object_unref (anim->context);
2591 g_slice_free (GdkDragAnim, anim);
2592 }
2593
2594 static gboolean
2595 gdk_drag_anim_timeout (gpointer data)
2596 {
2597 GdkDragAnim *anim = data;
2598 GdkX11DragContext *context = anim->context;
2599 GdkFrameClock *frame_clock = anim->frame_clock;
2600 gint64 current_time;
2601 double f;
2602 double t;
2603
2604 if (!frame_clock)
2605 return G_SOURCE_REMOVE;
2606
2607 current_time = gdk_frame_clock_get_frame_time (frame_clock);
2608
2609 f = (current_time - anim->start_time) / (double) ANIM_TIME;
2610
2611 if (f >= 1.0)
2612 return G_SOURCE_REMOVE;
2613
2614 t = ease_out_cubic (f);
2615
2616 gdk_window_show (context->drag_window);
2617 gdk_window_move (context->drag_window,
2618 (context->last_x - context->hot_x) +
2619 (context->start_x - context->last_x) * t,
2620 (context->last_y - context->hot_y) +
2621 (context->start_y - context->last_y) * t);
2622 gdk_window_set_opacity (context->drag_window, 1.0 - f);
2623
2624 return G_SOURCE_CONTINUE;
2625 }
2626
2627 static void
2628 gdk_x11_drag_context_drop_done (GdkDragContext *context,
2629 gboolean success)
2630 {
2631 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2632 GdkDragAnim *anim;
2633 cairo_surface_t *win_surface;
2634 cairo_surface_t *surface;
2635 cairo_pattern_t *pattern;
2636 cairo_t *cr;
2637
2638 if (success)
2639 {
2640 gdk_window_hide (x11_context->drag_window);
2641 return;
2642 }
2643
2644 win_surface = _gdk_window_ref_cairo_surface (x11_context->drag_window);
2645 surface = gdk_window_create_similar_surface (x11_context->drag_window,
2646 cairo_surface_get_content (win_surface),
2647 gdk_window_get_width (x11_context->drag_window),
2648 gdk_window_get_height (x11_context->drag_window));
2649 cr = cairo_create (surface);
2650 cairo_set_source_surface (cr, win_surface, 0, 0);
2651 cairo_paint (cr);
2652 cairo_destroy (cr);
2653 cairo_surface_destroy (win_surface);
2654
2655 pattern = cairo_pattern_create_for_surface (surface);
2656
2657 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2658 gdk_window_set_background_pattern (x11_context->drag_window, pattern);
2659 G_GNUC_END_IGNORE_DEPRECATIONS
2660
2661 cairo_pattern_destroy (pattern);
2662 cairo_surface_destroy (surface);
2663
2664 anim = g_slice_new0 (GdkDragAnim);
2665 anim->context = g_object_ref (x11_context);
2666 anim->frame_clock = gdk_window_get_frame_clock (x11_context->drag_window);
2667 anim->start_time = gdk_frame_clock_get_frame_time (anim->frame_clock);
2668
2669 gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 17,
2670 gdk_drag_anim_timeout, anim,
2671 (GDestroyNotify) gdk_drag_anim_destroy);
2672 }
2673
2674 static gboolean
2675 drag_context_grab (GdkDragContext *context)
2676 {
2677 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2678 GdkDevice *device = gdk_drag_context_get_device (context);
2679 GdkSeatCapabilities capabilities;
2680 GdkWindow *root;
2681 GdkSeat *seat;
2682 gint keycode, i;
2683 GdkCursor *cursor;
2684
2685 if (!x11_context->ipc_window)
2686 return FALSE;
2687
2688 root = gdk_screen_get_root_window (gdk_window_get_screen (x11_context->ipc_window));
2689 seat = gdk_device_get_seat (gdk_drag_context_get_device (context));
2690
2691 #ifdef XINPUT_2
2692 if (GDK_IS_X11_DEVICE_XI2 (device))
2693 capabilities = GDK_SEAT_CAPABILITY_ALL_POINTING;
2694 else
2695 #endif
2696 capabilities = GDK_SEAT_CAPABILITY_ALL;
2697
2698 cursor = gdk_drag_get_cursor (context, x11_context->current_action);
2699 g_set_object (&x11_context->cursor, cursor);
2700
2701 if (gdk_seat_grab (seat, x11_context->ipc_window,
2702 capabilities, FALSE,
2703 x11_context->cursor, NULL, NULL, NULL) != GDK_GRAB_SUCCESS)
2704 return FALSE;
2705
2706 g_set_object (&x11_context->grab_seat, seat);
2707
2708 gdk_x11_display_error_trap_push (gdk_window_get_display (x11_context->ipc_window));
2709
2710 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
2711 {
2712 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (x11_context->ipc_window),
2713 grab_keys[i].keysym);
2714 if (keycode == NoSymbol)
2715 continue;
2716
2717 #ifdef XINPUT_2
2718 if (GDK_IS_X11_DEVICE_XI2 (device))
2719 {
2720 gint deviceid = gdk_x11_device_get_id (gdk_seat_get_keyboard (seat));
2721 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
2722 XIGrabModifiers mods;
2723 XIEventMask evmask;
2724 gint num_mods;
2725
2726 memset (mask, 0, sizeof (mask));
2727 XISetMask (mask, XI_KeyPress);
2728 XISetMask (mask, XI_KeyRelease);
2729
2730 evmask.deviceid = deviceid;
2731 evmask.mask_len = sizeof (mask);
2732 evmask.mask = mask;
2733
2734 num_mods = 1;
2735 mods.modifiers = grab_keys[i].modifiers;
2736
2737 XIGrabKeycode (GDK_WINDOW_XDISPLAY (x11_context->ipc_window),
2738 deviceid,
2739 keycode,
2740 GDK_WINDOW_XID (root),
2741 GrabModeAsync,
2742 GrabModeAsync,
2743 False,
2744 &evmask,
2745 num_mods,
2746 &mods);
2747 }
2748 else
2749 #endif
2750 {
2751 XGrabKey (GDK_WINDOW_XDISPLAY (x11_context->ipc_window),
2752 keycode, grab_keys[i].modifiers,
2753 GDK_WINDOW_XID (root),
2754 FALSE,
2755 GrabModeAsync,
2756 GrabModeAsync);
2757 }
2758 }
2759
2760 gdk_x11_display_error_trap_pop_ignored (gdk_window_get_display (x11_context->ipc_window));
2761
2762 return TRUE;
2763 }
2764
2765 static void
2766 drag_context_ungrab (GdkDragContext *context)
2767 {
2768 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2769 GdkDevice *keyboard;
2770 GdkWindow *root;
2771 gint keycode, i;
2772
2773 if (!x11_context->grab_seat)
2774 return;
2775
2776 gdk_seat_ungrab (x11_context->grab_seat);
2777
2778 keyboard = gdk_seat_get_keyboard (x11_context->grab_seat);
2779 root = gdk_screen_get_root_window (gdk_window_get_screen (x11_context->ipc_window));
2780 g_clear_object (&x11_context->grab_seat);
2781
2782 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
2783 {
2784 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (x11_context->ipc_window),
2785 grab_keys[i].keysym);
2786 if (keycode == NoSymbol)
2787 continue;
2788
2789 #ifdef XINPUT_2
2790 if (GDK_IS_X11_DEVICE_XI2 (keyboard))
2791 {
2792 XIGrabModifiers mods;
2793 gint num_mods;
2794
2795 num_mods = 1;
2796 mods.modifiers = grab_keys[i].modifiers;
2797
2798 XIUngrabKeycode (GDK_WINDOW_XDISPLAY (x11_context->ipc_window),
2799 gdk_x11_device_get_id (keyboard),
2800 keycode,
2801 GDK_WINDOW_XID (root),
2802 num_mods,
2803 &mods);
2804 }
2805 else
2806 #endif /* XINPUT_2 */
2807 {
2808 XUngrabKey (GDK_WINDOW_XDISPLAY (x11_context->ipc_window),
2809 keycode, grab_keys[i].modifiers,
2810 GDK_WINDOW_XID (root));
2811 }
2812 }
2813 }
2814
2815 static gboolean
2816 gdk_x11_drag_context_manage_dnd (GdkDragContext *context,
2817 GdkWindow *ipc_window,
2818 GdkDragAction actions)
2819 {
2820 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2821
2822 if (x11_context->ipc_window)
2823 return FALSE;
2824
2825 context->protocol = GDK_DRAG_PROTO_XDND;
2826 x11_context->ipc_window = g_object_ref (ipc_window);
2827
2828 if (drag_context_grab (context))
2829 {
2830 x11_context->actions = actions;
2831 move_drag_window (context, x11_context->start_x, x11_context->start_y);
2832 return TRUE;
2833 }
2834 else
2835 {
2836 g_clear_object (&x11_context->ipc_window);
2837 return FALSE;
2838 }
2839 }
2840
2841 static void
2842 gdk_x11_drag_context_set_cursor (GdkDragContext *context,
2843 GdkCursor *cursor)
2844 {
2845 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2846
2847 if (!g_set_object (&x11_context->cursor, cursor))
2848 return;
2849
2850 if (x11_context->grab_seat)
2851 {
2852 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
2853 gdk_device_grab (gdk_seat_get_pointer (x11_context->grab_seat),
2854 x11_context->ipc_window,
2855 GDK_OWNERSHIP_APPLICATION, FALSE,
2856 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2857 cursor, GDK_CURRENT_TIME);
2858 G_GNUC_END_IGNORE_DEPRECATIONS;
2859 }
2860 }
2861
2862 static void
2863 gdk_x11_drag_context_cancel (GdkDragContext *context,
2864 GdkDragCancelReason reason)
2865 {
2866 drag_context_ungrab (context);
2867 gdk_drag_drop_done (context, FALSE);
2868 }
2869
2870 static void
2871 gdk_x11_drag_context_drop_performed (GdkDragContext *context,
2872 guint32 time_)
2873 {
2874 drag_context_ungrab (context);
2875 }
2876
2877 #define BIG_STEP 20
2878 #define SMALL_STEP 1
2879
2880 static void
2881 gdk_drag_get_current_actions (GdkModifierType state,
2882 gint button,
2883 GdkDragAction actions,
2884 GdkDragAction *suggested_action,
2885 GdkDragAction *possible_actions)
2886 {
2887 *suggested_action = 0;
2888 *possible_actions = 0;
2889
2890 if ((button == GDK_BUTTON_MIDDLE || button == GDK_BUTTON_SECONDARY) && (actions & GDK_ACTION_ASK))
2891 {
2892 *suggested_action = GDK_ACTION_ASK;
2893 *possible_actions = actions;
2894 }
2895 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
2896 {
2897 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
2898 {
2899 if (actions & GDK_ACTION_LINK)
2900 {
2901 *suggested_action = GDK_ACTION_LINK;
2902 *possible_actions = GDK_ACTION_LINK;
2903 }
2904 }
2905 else if (state & GDK_CONTROL_MASK)
2906 {
2907 if (actions & GDK_ACTION_COPY)
2908 {
2909 *suggested_action = GDK_ACTION_COPY;
2910 *possible_actions = GDK_ACTION_COPY;
2911 }
2912 }
2913 else
2914 {
2915 if (actions & GDK_ACTION_MOVE)
2916 {
2917 *suggested_action = GDK_ACTION_MOVE;
2918 *possible_actions = GDK_ACTION_MOVE;
2919 }
2920 }
2921 }
2922 else
2923 {
2924 *possible_actions = actions;
2925
2926 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
2927 *suggested_action = GDK_ACTION_ASK;
2928 else if (actions & GDK_ACTION_COPY)
2929 *suggested_action = GDK_ACTION_COPY;
2930 else if (actions & GDK_ACTION_MOVE)
2931 *suggested_action = GDK_ACTION_MOVE;
2932 else if (actions & GDK_ACTION_LINK)
2933 *suggested_action = GDK_ACTION_LINK;
2934 }
2935 }
2936
2937 static void
2938 gdk_drag_update (GdkDragContext *context,
2939 gdouble x_root,
2940 gdouble y_root,
2941 GdkModifierType mods,
2942 guint32 evtime)
2943 {
2944 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2945 GdkDragAction action, possible_actions;
2946 GdkWindow *dest_window;
2947 GdkDragProtocol protocol;
2948
2949 gdk_drag_get_current_actions (mods, GDK_BUTTON_PRIMARY, x11_context->actions,
2950 &action, &possible_actions);
2951
2952 gdk_drag_find_window_for_screen (context,
2953 x11_context->drag_window,
2954 gdk_display_get_default_screen (gdk_display_get_default ()),
2955 x_root, y_root, &dest_window, &protocol);
2956
2957 gdk_drag_motion (context, dest_window, protocol, x_root, y_root,
2958 action, possible_actions, evtime);
2959 }
2960
2961 static gboolean
2962 gdk_dnd_handle_motion_event (GdkDragContext *context,
2963 const GdkEventMotion *event)
2964 {
2965 GdkModifierType state;
2966
2967 if (!gdk_event_get_state ((GdkEvent *) event, &state))
2968 return FALSE;
2969
2970 gdk_drag_update (context, event->x_root, event->y_root, state,
2971 gdk_event_get_time ((GdkEvent *) event));
2972 return TRUE;
2973 }
2974
2975 static gboolean
2976 gdk_dnd_handle_key_event (GdkDragContext *context,
2977 const GdkEventKey *event)
2978 {
2979 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
2980 GdkModifierType state;
2981 GdkWindow *root_window;
2982 GdkDevice *pointer;
2983 gint dx, dy;
2984
2985 dx = dy = 0;
2986 state = event->state;
2987 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
2988
2989 if (event->type == GDK_KEY_PRESS)
2990 {
2991 switch (event->keyval)
2992 {
2993 case GDK_KEY_Escape:
2994 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_USER_CANCELLED);
2995 return TRUE;
2996
2997 case GDK_KEY_space:
2998 case GDK_KEY_Return:
2999 case GDK_KEY_ISO_Enter:
3000 case GDK_KEY_KP_Enter:
3001 case GDK_KEY_KP_Space:
3002 if ((gdk_drag_context_get_selected_action (context) != 0) &&
3003 (gdk_drag_context_get_dest_window (context) != NULL))
3004 {
3005 g_signal_emit_by_name (context, "drop-performed",
3006 gdk_event_get_time ((GdkEvent *) event));
3007 }
3008 else
3009 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_NO_TARGET);
3010
3011 return TRUE;
3012
3013 case GDK_KEY_Up:
3014 case GDK_KEY_KP_Up:
3015 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
3016 break;
3017
3018 case GDK_KEY_Down:
3019 case GDK_KEY_KP_Down:
3020 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
3021 break;
3022
3023 case GDK_KEY_Left:
3024 case GDK_KEY_KP_Left:
3025 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
3026 break;
3027
3028 case GDK_KEY_Right:
3029 case GDK_KEY_KP_Right:
3030 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
3031 break;
3032 }
3033 }
3034
3035 /* The state is not yet updated in the event, so we need
3036 * to query it here. We could use XGetModifierMapping, but
3037 * that would be overkill.
3038 */
3039 root_window = gdk_screen_get_root_window (gdk_window_get_screen (x11_context->ipc_window));
3040 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
3041
3042 if (dx != 0 || dy != 0)
3043 {
3044 x11_context->last_x += dx;
3045 x11_context->last_y += dy;
3046 gdk_device_warp (pointer,
3047 gdk_window_get_screen (x11_context->ipc_window),
3048 x11_context->last_x, x11_context->last_y);
3049 }
3050
3051 gdk_drag_update (context, x11_context->last_x, x11_context->last_y, state,
3052 gdk_event_get_time ((GdkEvent *) event));
3053
3054 return TRUE;
3055 }
3056
3057 static gboolean
3058 gdk_dnd_handle_grab_broken_event (GdkDragContext *context,
3059 const GdkEventGrabBroken *event)
3060 {
3061 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
3062
3063 /* Don't cancel if we break the implicit grab from the initial button_press.
3064 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
3065 * example, when changing the drag cursor.
3066 */
3067 if (event->implicit ||
3068 event->grab_window == x11_context->drag_window ||
3069 event->grab_window == x11_context->ipc_window)
3070 return FALSE;
3071
3072 if (gdk_event_get_device ((GdkEvent *) event) !=
3073 gdk_drag_context_get_device (context))
3074 return FALSE;
3075
3076 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_ERROR);
3077 return TRUE;
3078 }
3079
3080 static gboolean
3081 gdk_dnd_handle_button_event (GdkDragContext *context,
3082 const GdkEventButton *event)
3083 {
3084 #if 0
3085 /* FIXME: Check the button matches */
3086 if (event->button != x11_context->button)
3087 return FALSE;
3088 #endif
3089
3090 if ((gdk_drag_context_get_selected_action (context) != 0) &&
3091 (gdk_drag_context_get_dest_window (context) != NULL))
3092 {
3093 g_signal_emit_by_name (context, "drop-performed",
3094 gdk_event_get_time ((GdkEvent *) event));
3095 }
3096 else
3097 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_NO_TARGET);
3098
3099 return TRUE;
3100 }
3101
3102 gboolean
3103 gdk_dnd_handle_drag_status (GdkDragContext *context,
3104 const GdkEventDND *event)
3105 {
3106 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3107 GdkDragAction action;
3108
3109 if (context != event->context)
3110 return FALSE;
3111
3112 action = gdk_drag_context_get_selected_action (context);
3113
3114 if (action != context_x11->current_action)
3115 {
3116 context_x11->current_action = action;
3117 g_signal_emit_by_name (context, "action-changed", action);
3118 }
3119
3120 return TRUE;
3121 }
3122
3123 static gboolean
3124 gdk_dnd_handle_drop_finished (GdkDragContext *context,
3125 const GdkEventDND *event)
3126 {
3127 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
3128
3129 if (context != event->context)
3130 return FALSE;
3131
3132 g_signal_emit_by_name (context, "dnd-finished");
3133 gdk_drag_drop_done (context, !x11_context->drop_failed);
3134 return TRUE;
3135 }
3136
3137 gboolean
3138 gdk_x11_drag_context_handle_event (GdkDragContext *context,
3139 const GdkEvent *event)
3140 {
3141 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (context);
3142
3143 if (!context->is_source)
3144 return FALSE;
3145 if (!x11_context->grab_seat && event->type != GDK_DROP_FINISHED)
3146 return FALSE;
3147
3148 switch (event->type)
3149 {
3150 case GDK_MOTION_NOTIFY:
3151 return gdk_dnd_handle_motion_event (context, &event->motion);
3152 case GDK_BUTTON_RELEASE:
3153 return gdk_dnd_handle_button_event (context, &event->button);
3154 case GDK_KEY_PRESS:
3155 case GDK_KEY_RELEASE:
3156 return gdk_dnd_handle_key_event (context, &event->key);
3157 case GDK_GRAB_BROKEN:
3158 return gdk_dnd_handle_grab_broken_event (context, &event->grab_broken);
3159 case GDK_DRAG_STATUS:
3160 return gdk_dnd_handle_drag_status (context, &event->dnd);
3161 case GDK_DROP_FINISHED:
3162 return gdk_dnd_handle_drop_finished (context, &event->dnd);
3163 default:
3164 break;
3165 }
3166
3167 return FALSE;
3168 }
3169
3170 void
3171 gdk_x11_drag_context_action_changed (GdkDragContext *context,
3172 GdkDragAction action)
3173 {
3174 GdkCursor *cursor;
3175
3176 cursor = gdk_drag_get_cursor (context, action);
3177 gdk_drag_context_set_cursor (context, cursor);
3178 }