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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27 #include "config.h"
28
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xatom.h>
32 #include <X11/extensions/shape.h>
33 #ifdef HAVE_XCOMPOSITE
34 #include <X11/extensions/Xcomposite.h>
35 #endif
36
37 #include <string.h>
38
39 #include "gdk.h" /* For gdk_flush() */
40 #include "gdkx.h"
41 #include "gdkasync.h"
42 #include "gdkdnd.h"
43 #include "gdkproperty.h"
44 #include "gdkprivate-x11.h"
45 #include "gdkinternals.h"
46 #include "gdkscreen-x11.h"
47 #include "gdkdisplay-x11.h"
48 #include "gdkalias.h"
49
50 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
51
52 typedef enum {
53 GDK_DRAG_STATUS_DRAG,
54 GDK_DRAG_STATUS_MOTION_WAIT,
55 GDK_DRAG_STATUS_ACTION_WAIT,
56 GDK_DRAG_STATUS_DROP
57 } GtkDragStatus;
58
59 typedef struct {
60 guint32 xid;
61 gint x, y, width, height;
62 gboolean mapped;
63 gboolean shape_selected;
64 gboolean shape_valid;
65 GdkRegion *shape;
66 } GdkCacheChild;
67
68 typedef struct {
69 GList *children;
70 GHashTable *child_hash;
71 guint old_event_mask;
72 GdkScreen *screen;
73 gint ref_count;
74 } GdkWindowCache;
75
76 /* Structure that holds information about a drag in progress.
77 * this is used on both source and destination sides.
78 */
79 struct _GdkDragContextPrivateX11 {
80 GdkDragContext context;
81
82 Atom motif_selection;
83 guint ref_count;
84
85 guint16 last_x; /* Coordinates from last event */
86 guint16 last_y;
87 GdkDragAction old_action; /* The last action we sent to the source */
88 GdkDragAction old_actions; /* The last actions we sent to the source */
89 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
90
91 Window dest_xid; /* The last window we looked up */
92 Window drop_xid; /* The (non-proxied) window that is receiving drops */
93 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
94 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
95 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
96 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
97 guint drag_status : 4; /* current status of drag */
98
99 guint drop_failed : 1; /* Whether the drop was unsuccessful */
100 guint version; /* Xdnd protocol version */
101
102 GSList *window_caches;
103 };
104
105 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
106
107 /* Forward declarations */
108
109 static GdkWindowCache *gdk_window_cache_get (GdkScreen *screen);
110 static GdkWindowCache *gdk_window_cache_ref (GdkWindowCache *cache);
111 static void gdk_window_cache_unref (GdkWindowCache *cache);
112
113 static void motif_read_target_table (GdkDisplay *display);
114
115 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
116 GdkEvent *event,
117 gpointer data);
118
119 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
120 GdkEvent *event,
121 gpointer data);
122 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
123 GdkEvent *event,
124 gpointer data);
125 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
126 GdkEvent *event,
127 gpointer data);
128 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
129 GdkEvent *event,
130 gpointer data);
131 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
132 GdkEvent *event,
133 gpointer data);
134 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
135 GdkEvent *event,
136 gpointer data);
137
138 static void xdnd_manage_source_filter (GdkDragContext *context,
139 GdkWindow *window,
140 gboolean add_filter);
141
142 static void gdk_drag_context_finalize (GObject *object);
143
144 static GList *contexts;
145 static GSList *window_caches;
146
147 static const struct {
148 const char *atom_name;
149 GdkFilterFunc func;
150 } xdnd_filters[] = {
151 { "XdndEnter", xdnd_enter_filter },
152 { "XdndLeave", xdnd_leave_filter },
153 { "XdndPosition", xdnd_position_filter },
154 { "XdndStatus", xdnd_status_filter },
155 { "XdndFinished", xdnd_finished_filter },
156 { "XdndDrop", xdnd_drop_filter },
157 };
158
159 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
160
161 static void
162 gdk_drag_context_init (GdkDragContext *dragcontext)
163 {
164 GdkDragContextPrivateX11 *private;
165
166 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
167 GDK_TYPE_DRAG_CONTEXT,
168 GdkDragContextPrivateX11);
169
170 dragcontext->windowing_data = private;
171
172 contexts = g_list_prepend (contexts, dragcontext);
173 }
174
175 static void
176 gdk_drag_context_class_init (GdkDragContextClass *klass)
177 {
178 GObjectClass *object_class = G_OBJECT_CLASS (klass);
179
180 object_class->finalize = gdk_drag_context_finalize;
181
182 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
183 }
184
185 static void
186 gdk_drag_context_finalize (GObject *object)
187 {
188 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
189 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
190
191 g_list_free (context->targets);
192
193 if (context->source_window)
194 {
195 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
196 !context->is_source)
197 xdnd_manage_source_filter (context, context->source_window, FALSE);
198
199 g_object_unref (context->source_window);
200 }
201
202 if (context->dest_window)
203 g_object_unref (context->dest_window);
204
205 g_slist_free_full (private->window_caches, (GDestroyNotify)gdk_window_cache_unref);
206 private->window_caches = NULL;
207
208 contexts = g_list_remove (contexts, context);
209
210 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
211 }
212
213 /* Drag Contexts */
214
215 /**
216 * gdk_drag_context_new:
217 *
218 * Creates a new #GdkDragContext.
219 *
220 * Return value: the newly created #GdkDragContext.
221 *
222 * Deprecated: 2.24: This function is not useful, you always
223 * obtain drag contexts by gdk_drag_begin() or similar.
224 **/
225 GdkDragContext *
226 gdk_drag_context_new (void)
227 {
228 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
229 }
230
231 /**
232 * gdk_drag_context_ref:
233 * @context: a #GdkDragContext.
234 *
235 * Deprecated function; use g_object_ref() instead.
236 *
237 * Deprecated: 2.2: Use g_object_ref() instead.
238 **/
239 void
240 gdk_drag_context_ref (GdkDragContext *context)
241 {
242 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
243
244 g_object_ref (context);
245 }
246
247 /**
248 * gdk_drag_context_unref:
249 * @context: a #GdkDragContext.
250 *
251 * Deprecated function; use g_object_unref() instead.
252 *
253 * Deprecated: 2.2: Use g_object_unref() instead.
254 **/
255 void
256 gdk_drag_context_unref (GdkDragContext *context)
257 {
258 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
259
260 g_object_unref (context);
261 }
262
263 static GdkDragContext *
264 gdk_drag_context_find (GdkDisplay *display,
265 gboolean is_source,
266 Window source_xid,
267 Window dest_xid)
268 {
269 GList *tmp_list = contexts;
270 GdkDragContext *context;
271 GdkDragContextPrivateX11 *private;
272 Window context_dest_xid;
273
274 while (tmp_list)
275 {
276 context = (GdkDragContext *)tmp_list->data;
277 private = PRIVATE_DATA (context);
278
279 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
280 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
281 continue;
282
283 context_dest_xid = context->dest_window ?
284 (private->drop_xid ?
285 private->drop_xid :
286 GDK_DRAWABLE_XID (context->dest_window)) :
287 None;
288
289 if ((!context->is_source == !is_source) &&
290 ((source_xid == None) || (context->source_window &&
291 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
292 ((dest_xid == None) || (context_dest_xid == dest_xid)))
293 return context;
294
295 tmp_list = tmp_list->next;
296 }
297
298 return NULL;
299 }
300
301 static void
302 precache_target_list (GdkDragContext *context)
303 {
304 if (context->targets)
305 {
306 GPtrArray *targets = g_ptr_array_new ();
307 GList *tmp_list;
308 int i;
309
310 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
311 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
312
313 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
314 (const gchar **)targets->pdata,
315 targets->len);
316
317 for (i =0; i < targets->len; i++)
318 g_free (targets->pdata[i]);
319
320 g_ptr_array_free (targets, TRUE);
321 }
322 }
323
324 /* Utility functions */
325
326 static void
327 free_cache_child (GdkCacheChild *child,
328 GdkDisplay *display)
329 {
330 if (child->shape)
331 gdk_region_destroy (child->shape);
332
333 if (child->shape_selected && display)
334 {
335 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
336
337 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
338 }
339
340 g_free (child);
341 }
342
343 static void
344 gdk_window_cache_add (GdkWindowCache *cache,
345 guint32 xid,
346 gint x, gint y, gint width, gint height,
347 gboolean mapped)
348 {
349 GdkCacheChild *child = g_new (GdkCacheChild, 1);
350
351 child->xid = xid;
352 child->x = x;
353 child->y = y;
354 child->width = width;
355 child->height = height;
356 child->mapped = mapped;
357 child->shape_selected = FALSE;
358 child->shape_valid = FALSE;
359 child->shape = NULL;
360
361 cache->children = g_list_prepend (cache->children, child);
362 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
363 cache->children);
364 }
365
366 static GdkFilterReturn
367 gdk_window_cache_shape_filter (GdkXEvent *xev,
368 GdkEvent *event,
369 gpointer data)
370 {
371 XEvent *xevent = (XEvent *)xev;
372 GdkWindowCache *cache = data;
373
374 GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
375
376 if (display->have_shapes &&
377 xevent->type == display->shape_event_base + ShapeNotify)
378 {
379 XShapeEvent *xse = (XShapeEvent*)xevent;
380 GList *node;
381
382 node = g_hash_table_lookup (cache->child_hash,
383 GUINT_TO_POINTER (xse->window));
384 if (node)
385 {
386 GdkCacheChild *child = node->data;
387 child->shape_valid = FALSE;
388 if (child->shape)
389 {
390 gdk_region_destroy (child->shape);
391 child->shape = NULL;
392 }
393 }
394
395 return GDK_FILTER_REMOVE;
396 }
397
398 return GDK_FILTER_CONTINUE;
399 }
400
401 static GdkFilterReturn
402 gdk_window_cache_filter (GdkXEvent *xev,
403 GdkEvent *event,
404 gpointer data)
405 {
406 XEvent *xevent = (XEvent *)xev;
407 GdkWindowCache *cache = data;
408
409 switch (xevent->type)
410 {
411 case CirculateNotify:
412 break;
413 case ConfigureNotify:
414 {
415 XConfigureEvent *xce = &xevent->xconfigure;
416 GList *node;
417
418 node = g_hash_table_lookup (cache->child_hash,
419 GUINT_TO_POINTER (xce->window));
420 if (node)
421 {
422 GdkCacheChild *child = node->data;
423 child->x = xce->x;
424 child->y = xce->y;
425 child->width = xce->width;
426 child->height = xce->height;
427 if (xce->above == None && (node->next))
428 {
429 GList *last = g_list_last (cache->children);
430 cache->children = g_list_remove_link (cache->children, node);
431 last->next = node;
432 node->next = NULL;
433 node->prev = last;
434 }
435 else
436 {
437 GList *above_node = g_hash_table_lookup (cache->child_hash,
438 GUINT_TO_POINTER (xce->above));
439 if (above_node && node->next != above_node)
440 {
441 /* Put the window above (before in the list) above_node
442 */
443 cache->children = g_list_remove_link (cache->children, node);
444 node->prev = above_node->prev;
445 if (node->prev)
446 node->prev->next = node;
447 else
448 cache->children = node;
449 node->next = above_node;
450 above_node->prev = node;
451 }
452 }
453 }
454 break;
455 }
456 case CreateNotify:
457 {
458 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
459
460 if (!g_hash_table_lookup (cache->child_hash,
461 GUINT_TO_POINTER (xcwe->window)))
462 gdk_window_cache_add (cache, xcwe->window,
463 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
464 FALSE);
465 break;
466 }
467 case DestroyNotify:
468 {
469 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
470 GList *node;
471
472 node = g_hash_table_lookup (cache->child_hash,
473 GUINT_TO_POINTER (xdwe->window));
474 if (node)
475 {
476 GdkCacheChild *child = node->data;
477
478 g_hash_table_remove (cache->child_hash,
479 GUINT_TO_POINTER (xdwe->window));
480 cache->children = g_list_remove_link (cache->children, node);
481 /* window is destroyed, no need to disable ShapeNotify */
482 free_cache_child (child, NULL);
483 g_list_free_1 (node);
484 }
485 break;
486 }
487 case MapNotify:
488 {
489 XMapEvent *xme = &xevent->xmap;
490 GList *node;
491
492 node = g_hash_table_lookup (cache->child_hash,
493 GUINT_TO_POINTER (xme->window));
494 if (node)
495 {
496 GdkCacheChild *child = node->data;
497 child->mapped = TRUE;
498 }
499 break;
500 }
501 case ReparentNotify:
502 break;
503 case UnmapNotify:
504 {
505 XMapEvent *xume = &xevent->xmap;
506 GList *node;
507
508 node = g_hash_table_lookup (cache->child_hash,
509 GUINT_TO_POINTER (xume->window));
510 if (node)
511 {
512 GdkCacheChild *child = node->data;
513 child->mapped = FALSE;
514 }
515 break;
516 }
517 default:
518 return GDK_FILTER_CONTINUE;
519 }
520 return GDK_FILTER_REMOVE;
521 }
522
523 static GdkWindowCache *
524 gdk_window_cache_new (GdkScreen *screen)
525 {
526 XWindowAttributes xwa;
527 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
528 GdkWindow *root_window = gdk_screen_get_root_window (screen);
529 GdkChildInfoX11 *children;
530 guint nchildren, i;
531 Window cow;
532
533 GdkWindowCache *result = g_new (GdkWindowCache, 1);
534
535 result->children = NULL;
536 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
537 result->screen = screen;
538 result->ref_count = 1;
539
540 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
541 result->old_event_mask = xwa.your_event_mask;
542
543 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
544 {
545 GList *toplevel_windows, *list;
546 GdkWindow *window;
547 gint x, y, width, height;
548
549 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
550 for (list = toplevel_windows; list; list = list->next) {
551 window = GDK_WINDOW (list->data);
552 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
553 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
554 x, y, width, height,
555 gdk_window_is_visible (window));
556 }
557 g_list_free (toplevel_windows);
558 return result;
559 }
560
561 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
562 result->old_event_mask | SubstructureNotifyMask);
563 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
564 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
565
566 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
567 GDK_WINDOW_XWINDOW (root_window),
568 FALSE, NULL,
569 &children, &nchildren))
570 return result;
571
572 for (i = 0; i < nchildren ; i++)
573 {
574 gdk_window_cache_add (result, children[i].window,
575 children[i].x, children[i].y, children[i].width, children[i].height,
576 children[i].is_mapped);
577 }
578
579 g_free (children);
580
581 #ifdef HAVE_XCOMPOSITE
582 /*
583 * Add the composite overlay window to the cache, as this can be a reasonable
584 * Xdnd proxy as well.
585 * This is only done when the screen is composited in order to avoid mapping
586 * the COW. We assume that the CM is using the COW (which is true for pretty
587 * much any CM currently in use).
588 */
589 if (gdk_screen_is_composited (screen))
590 {
591 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XWINDOW (root_window));
592 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
593 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XWINDOW (root_window));
594 }
595 #endif
596
597 return result;
598 }
599
600 static void
601 gdk_window_cache_destroy (GdkWindowCache *cache)
602 {
603 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
604
605 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
606 GDK_WINDOW_XWINDOW (root_window),
607 cache->old_event_mask);
608 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
609 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
610
611 gdk_error_trap_push ();
612
613 g_list_foreach (cache->children, (GFunc)free_cache_child,
614 gdk_screen_get_display (cache->screen));
615
616 gdk_flush ();
617 gdk_error_trap_pop ();
618
619 g_list_free (cache->children);
620 g_hash_table_destroy (cache->child_hash);
621
622 g_free (cache);
623 }
624
625 static GdkWindowCache *
626 gdk_window_cache_ref (GdkWindowCache *cache)
627 {
628 cache->ref_count += 1;
629
630 return cache;
631 }
632
633 static void
634 gdk_window_cache_unref (GdkWindowCache *cache)
635 {
636 g_assert (cache->ref_count > 0);
637
638 cache->ref_count -= 1;
639
640 if (cache->ref_count == 0)
641 {
642 window_caches = g_slist_remove (window_caches, cache);
643 gdk_window_cache_destroy (cache);
644 }
645 }
646
647 GdkWindowCache *
648 gdk_window_cache_get (GdkScreen *screen)
649 {
650 GSList *list;
651 GdkWindowCache *cache;
652
653 for (list = window_caches; list; list = list->next)
654 {
655 cache = list->data;
656 if (cache->screen == screen)
657 return gdk_window_cache_ref (cache);
658 }
659
660 cache = gdk_window_cache_new (screen);
661
662 window_caches = g_slist_prepend (window_caches, cache);
663
664 return cache;
665 }
666
667
668 static gboolean
669 is_pointer_within_shape (GdkDisplay *display,
670 GdkCacheChild *child,
671 gint x_pos,
672 gint y_pos)
673 {
674 if (!child->shape_selected)
675 {
676 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
677
678 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
679 child->shape_selected = TRUE;
680 }
681 if (!child->shape_valid)
682 {
683 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
684 GdkRegion *input_shape;
685
686 child->shape = NULL;
687 if (gdk_display_supports_shapes (display))
688 child->shape = _xwindow_get_shape (display_x11->xdisplay,
689 child->xid, ShapeBounding);
690 #ifdef ShapeInput
691 input_shape = NULL;
692 if (gdk_display_supports_input_shapes (display))
693 input_shape = _xwindow_get_shape (display_x11->xdisplay,
694 child->xid, ShapeInput);
695
696 if (child->shape && input_shape)
697 {
698 gdk_region_intersect (child->shape, input_shape);
699 gdk_region_destroy (input_shape);
700 }
701 else if (input_shape)
702 {
703 child->shape = input_shape;
704 }
705 #endif
706
707 child->shape_valid = TRUE;
708 }
709
710 return child->shape == NULL ||
711 gdk_region_point_in (child->shape, x_pos, y_pos);
712 }
713
714 static Window
715 get_client_window_at_coords_recurse (GdkDisplay *display,
716 Window win,
717 gboolean is_toplevel,
718 gint x,
719 gint y)
720 {
721 GdkChildInfoX11 *children;
722 unsigned int nchildren;
723 int i;
724 gboolean found_child = FALSE;
725 GdkChildInfoX11 child = { 0, };
726 gboolean has_wm_state = FALSE;
727
728 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
729 is_toplevel? &has_wm_state : NULL,
730 &children, &nchildren))
731 return None;
732
733 if (has_wm_state)
734 {
735 g_free (children);
736
737 return win;
738 }
739
740 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
741 {
742 GdkChildInfoX11 *cur_child = &children[i];
743
744 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
745 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
746 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
747 {
748 x -= cur_child->x;
749 y -= cur_child->y;
750 child = *cur_child;
751 found_child = TRUE;
752 }
753 }
754
755 g_free (children);
756
757 if (found_child)
758 {
759 if (child.has_wm_state)
760 return child.window;
761 else
762 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
763 }
764 else
765 return None;
766 }
767
768 static Window
769 get_client_window_at_coords (GdkWindowCache *cache,
770 Window ignore,
771 gint x_root,
772 gint y_root)
773 {
774 GList *tmp_list;
775 Window retval = None;
776
777 gdk_error_trap_push ();
778
779 tmp_list = cache->children;
780
781 while (tmp_list && !retval)
782 {
783 GdkCacheChild *child = tmp_list->data;
784
785 if ((child->xid != ignore) && (child->mapped))
786 {
787 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
788 (y_root >= child->y) && (y_root < child->y + child->height))
789 {
790 GdkDisplay *display = gdk_screen_get_display (cache->screen);
791
792 if (!is_pointer_within_shape (display, child,
793 x_root - child->x,
794 y_root - child->y))
795 {
796 tmp_list = tmp_list->next;
797 continue;
798 }
799
800 retval = get_client_window_at_coords_recurse (display,
801 child->xid, TRUE,
802 x_root - child->x,
803 y_root - child->y);
804 if (!retval)
805 retval = child->xid;
806 }
807 }
808 tmp_list = tmp_list->next;
809 }
810
811 gdk_error_trap_pop ();
812
813 if (retval)
814 return retval;
815 else
816 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
817 }
818
819 /*************************************************************
820 ***************************** MOTIF *************************
821 *************************************************************/
822
823 /* values used in the message type for Motif DND */
824 enum {
825 XmTOP_LEVEL_ENTER,
826 XmTOP_LEVEL_LEAVE,
827 XmDRAG_MOTION,
828 XmDROP_SITE_ENTER,
829 XmDROP_SITE_LEAVE,
830 XmDROP_START,
831 XmDROP_FINISH,
832 XmDRAG_DROP_FINISH,
833 XmOPERATION_CHANGED
834 };
835
836 /* Values used to specify type of protocol to use */
837 enum {
838 XmDRAG_NONE,
839 XmDRAG_DROP_ONLY,
840 XmDRAG_PREFER_PREREGISTER,
841 XmDRAG_PREREGISTER,
842 XmDRAG_PREFER_DYNAMIC,
843 XmDRAG_DYNAMIC,
844 XmDRAG_PREFER_RECEIVER
845 };
846
847 /* Operation codes */
848 enum {
849 XmDROP_NOOP,
850 XmDROP_MOVE = 0x01,
851 XmDROP_COPY = 0x02,
852 XmDROP_LINK = 0x04
853 };
854
855 /* Drop site status */
856 enum {
857 XmNO_DROP_SITE = 0x01,
858 XmDROP_SITE_INVALID = 0x02,
859 XmDROP_SITE_VALID = 0x03
860 };
861
862 /* completion status */
863 enum {
864 XmDROP,
865 XmDROP_HELP,
866 XmDROP_CANCEL,
867 XmDROP_INTERRUPT
868 };
869
870 /* Byte swapping routines. The motif specification leaves it
871 * up to us to save a few bytes in the client messages
872 */
873 static gchar local_byte_order = '\0';
874
875 #ifdef G_ENABLE_DEBUG
876 static void
877 print_target_list (GList *targets)
878 {
879 while (targets)
880 {
881 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
882 g_message ("\t%s", name);
883 g_free (name);
884 targets = targets->next;
885 }
886 }
887 #endif /* G_ENABLE_DEBUG */
888
889 static void
890 init_byte_order (void)
891 {
892 guint32 myint = 0x01020304;
893 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
894 }
895
896 static guint16
897 card16_to_host (guint16 x, gchar byte_order) {
898 if (byte_order == local_byte_order)
899 return x;
900 else
901 return (x << 8) | (x >> 8);
902 }
903
904 static guint32
905 card32_to_host (guint32 x, gchar byte_order) {
906 if (byte_order == local_byte_order)
907 return x;
908 else
909 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
910 }
911
912 /* Motif packs together fields of varying length into the
913 * client message. We can't rely on accessing these
914 * through data.s[], data.l[], etc, because on some architectures
915 * (i.e., Alpha) these won't be valid for format == 8.
916 */
917
918 #define MOTIF_XCLIENT_BYTE(xevent,i) \
919 (xevent)->xclient.data.b[i]
920 #define MOTIF_XCLIENT_SHORT(xevent,i) \
921 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
922 #define MOTIF_XCLIENT_LONG(xevent,i) \
923 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
924
925 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
926 #define MOTIF_UNPACK_SHORT(xevent,i) \
927 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
928 #define MOTIF_UNPACK_LONG(xevent,i) \
929 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
930
931 /***** Dest side ***********/
932
933 /* Property placed on source windows */
934 typedef struct _MotifDragInitiatorInfo {
935 guint8 byte_order;
936 guint8 protocol_version;
937 guint16 targets_index;
938 guint32 selection_atom;
939 } MotifDragInitiatorInfo;
940
941 /* Header for target table on the drag window */
942 typedef struct _MotifTargetTableHeader {
943 guchar byte_order;
944 guchar protocol_version;
945 guint16 n_lists;
946 guint32 total_size;
947 } MotifTargetTableHeader;
948
949 /* Property placed on target windows */
950 typedef struct _MotifDragReceiverInfo {
951 guint8 byte_order;
952 guint8 protocol_version;
953 guint8 protocol_style;
954 guint8 pad;
955 guint32 proxy_window;
956 guint16 num_drop_sites;
957 guint16 padding;
958 guint32 total_size;
959 } MotifDragReceiverInfo;
960
961 /* Target table handling */
962
963 static GdkFilterReturn
964 motif_drag_window_filter (GdkXEvent *xevent,
965 GdkEvent *event,
966 gpointer data)
967 {
968 XEvent *xev = (XEvent *)xevent;
969 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
970 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
971
972 switch (xev->xany.type)
973 {
974 case DestroyNotify:
975 display_x11->motif_drag_window = None;
976 display_x11->motif_drag_gdk_window = NULL;
977 break;
978 case PropertyNotify:
979 if (display_x11->motif_target_lists &&
980 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
981 motif_read_target_table (display);
982 break;
983 }
984 return GDK_FILTER_REMOVE;
985 }
986
987 static Window
988 motif_lookup_drag_window (GdkDisplay *display,
989 Display *lookup_xdisplay)
990 {
991 Window retval = None;
992 gulong bytes_after, nitems;
993 Atom type;
994 gint format;
995 guchar *data;
996
997 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
998 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
999 0, 1, FALSE,
1000 XA_WINDOW, &type, &format, &nitems, &bytes_after,
1001 &data);
1002
1003 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
1004 {
1005 retval = *(Window *)data;
1006 GDK_NOTE (DND,
1007 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
1008 }
1009
1010 if (type != None)
1011 XFree (data);
1012
1013 return retval;
1014 }
1015
1016 /* Finds the window where global Motif drag information is stored.
1017 * If it doesn't exist and 'create' is TRUE, create one.
1018 */
1019 static Window
1020 motif_find_drag_window (GdkDisplay *display,
1021 gboolean create)
1022 {
1023 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1024
1025 if (!display_x11->motif_drag_window)
1026 {
1027 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
1028 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
1029
1030 if (!display_x11->motif_drag_window && create)
1031 {
1032 /* Create a persistant window. (Copied from LessTif) */
1033
1034 Display *persistant_xdisplay;
1035 XSetWindowAttributes attr;
1036 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
1037 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1038
1039 XGrabServer (persistant_xdisplay);
1040
1041 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1042
1043 if (!display_x11->motif_drag_window)
1044 {
1045 attr.override_redirect = True;
1046 attr.event_mask = PropertyChangeMask;
1047
1048 display_x11->motif_drag_window =
1049 XCreateWindow (persistant_xdisplay,
1050 RootWindow (persistant_xdisplay, 0),
1051 -100, -100, 10, 10, 0, 0,
1052 InputOnly, (Visual *)CopyFromParent,
1053 (CWOverrideRedirect | CWEventMask), &attr);
1054
1055 GDK_NOTE (DND,
1056 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1057
1058 XChangeProperty (persistant_xdisplay,
1059 RootWindow (persistant_xdisplay, 0),
1060 motif_drag_window_atom, XA_WINDOW,
1061 32, PropModeReplace,
1062 (guchar *)&motif_drag_window_atom, 1);
1063
1064 }
1065 XUngrabServer (persistant_xdisplay);
1066 XCloseDisplay (persistant_xdisplay);
1067 }
1068
1069 /* There is a miniscule race condition here if the drag window
1070 * gets destroyed exactly now.
1071 */
1072 if (display_x11->motif_drag_window)
1073 {
1074 display_x11->motif_drag_gdk_window =
1075 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1076 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1077 motif_drag_window_filter,
1078 NULL);
1079 }
1080 }
1081
1082 return display_x11->motif_drag_window;
1083 }
1084
1085 static void
1086 motif_read_target_table (GdkDisplay *display)
1087 {
1088 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1089 gulong bytes_after, nitems;
1090 Atom type;
1091 gint format;
1092 gint i, j;
1093
1094 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1095
1096 if (display_x11->motif_target_lists)
1097 {
1098 for (i=0; i<display_x11->motif_n_target_lists; i++)
1099 g_list_free (display_x11->motif_target_lists[i]);
1100
1101 g_free (display_x11->motif_target_lists);
1102 display_x11->motif_target_lists = NULL;
1103 display_x11->motif_n_target_lists = 0;
1104 }
1105
1106 if (motif_find_drag_window (display, FALSE))
1107 {
1108 guchar *data;
1109 MotifTargetTableHeader *header = NULL;
1110 guchar *target_bytes = NULL;
1111 guchar *p;
1112 gboolean success = FALSE;
1113
1114 gdk_error_trap_push ();
1115 XGetWindowProperty (display_x11->xdisplay,
1116 display_x11->motif_drag_window,
1117 motif_drag_targets_atom,
1118 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1119 motif_drag_targets_atom,
1120 &type, &format, &nitems, &bytes_after,
1121 &data);
1122
1123 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1124 goto error;
1125
1126 header = (MotifTargetTableHeader *)data;
1127
1128 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1129 header->total_size = card32_to_host (header->total_size, header->byte_order);
1130
1131 gdk_error_trap_push ();
1132 XGetWindowProperty (display_x11->xdisplay,
1133 display_x11->motif_drag_window,
1134 motif_drag_targets_atom,
1135 (sizeof(MotifTargetTableHeader)+3)/4,
1136 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1137 FALSE,
1138 motif_drag_targets_atom, &type, &format, &nitems,
1139 &bytes_after, &target_bytes);
1140
1141 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
1142 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1143 goto error;
1144
1145 display_x11->motif_n_target_lists = header->n_lists;
1146 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1147
1148 p = target_bytes;
1149 for (i=0; i<header->n_lists; i++)
1150 {
1151 gint n_targets;
1152 guint32 *targets;
1153
1154 if (p + sizeof(guint16) - target_bytes > nitems)
1155 goto error;
1156
1157 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1158
1159 /* We need to make a copy of the targets, since it may
1160 * be unaligned
1161 */
1162 targets = g_new (guint32, n_targets);
1163 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1164
1165 p += sizeof(guint16) + n_targets * sizeof(guint32);
1166 if (p - target_bytes > nitems)
1167 goto error;
1168
1169 for (j=0; j<n_targets; j++)
1170 display_x11->motif_target_lists[i] =
1171 g_list_prepend (display_x11->motif_target_lists[i],
1172 GUINT_TO_POINTER (card32_to_host (targets[j],
1173 header->byte_order)));
1174 g_free (targets);
1175 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1176 }
1177
1178 success = TRUE;
1179
1180 error:
1181 if (header)
1182 XFree (header);
1183
1184 if (target_bytes)
1185 XFree (target_bytes);
1186
1187 if (!success)
1188 {
1189 if (display_x11->motif_target_lists)
1190 {
1191 g_free (display_x11->motif_target_lists);
1192 display_x11->motif_target_lists = NULL;
1193 display_x11->motif_n_target_lists = 0;
1194 }
1195 g_warning ("Error reading Motif target table\n");
1196 }
1197 }
1198 }
1199
1200 static gint
1201 targets_sort_func (gconstpointer a, gconstpointer b)
1202 {
1203 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1204 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1205 }
1206
1207 /* Check if given (sorted) list is in the targets table */
1208 static gint
1209 motif_target_table_check (GdkDisplay *display,
1210 GList *sorted)
1211 {
1212 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1213 GList *tmp_list1, *tmp_list2;
1214 gint i;
1215
1216 for (i=0; i<display_x11->motif_n_target_lists; i++)
1217 {
1218 tmp_list1 = display_x11->motif_target_lists[i];
1219 tmp_list2 = sorted;
1220
1221 while (tmp_list1 && tmp_list2)
1222 {
1223 if (tmp_list1->data != tmp_list2->data)
1224 break;
1225
1226 tmp_list1 = tmp_list1->next;
1227 tmp_list2 = tmp_list2->next;
1228 }
1229 if (!tmp_list1 && !tmp_list2) /* Found it */
1230 return i;
1231 }
1232
1233 return -1;
1234 }
1235
1236 static gint
1237 motif_add_to_target_table (GdkDisplay *display,
1238 GList *targets) /* targets is list of GdkAtom */
1239 {
1240 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1241 GList *sorted = NULL;
1242 gint index = -1;
1243 gint i;
1244 GList *tmp_list;
1245
1246 /* make a sorted copy of the list */
1247
1248 while (targets)
1249 {
1250 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1251 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1252 targets = targets->next;
1253 }
1254
1255 /* First check if it is there already */
1256
1257 if (display_x11->motif_target_lists)
1258 index = motif_target_table_check (display, sorted);
1259
1260 /* We need to grab the server while doing this, to ensure
1261 * atomiticity. Ugh
1262 */
1263
1264 if (index < 0)
1265 {
1266 /* We need to make sure that it exists _before_ we grab the
1267 * server, since we can't open a new connection after we
1268 * grab the server.
1269 */
1270 motif_find_drag_window (display, TRUE);
1271
1272 gdk_x11_display_grab (display);
1273 motif_read_target_table (display);
1274
1275 /* Check again, in case it was added in the meantime */
1276
1277 if (display_x11->motif_target_lists)
1278 index = motif_target_table_check (display, sorted);
1279
1280 if (index < 0)
1281 {
1282 guint32 total_size = 0;
1283 guchar *data;
1284 guchar *p;
1285 guint16 *p16;
1286 MotifTargetTableHeader *header;
1287
1288 if (!display_x11->motif_target_lists)
1289 {
1290 display_x11->motif_target_lists = g_new (GList *, 1);
1291 display_x11->motif_n_target_lists = 1;
1292 }
1293 else
1294 {
1295 display_x11->motif_n_target_lists++;
1296 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1297 sizeof(GList *) * display_x11->motif_n_target_lists);
1298 }
1299 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1300 sorted = NULL;
1301 index = display_x11->motif_n_target_lists - 1;
1302
1303 total_size = sizeof (MotifTargetTableHeader);
1304 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1305 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1306
1307 data = g_malloc (total_size);
1308
1309 header = (MotifTargetTableHeader *)data;
1310 p = data + sizeof(MotifTargetTableHeader);
1311
1312 header->byte_order = local_byte_order;
1313 header->protocol_version = 0;
1314 header->n_lists = display_x11->motif_n_target_lists;
1315 header->total_size = total_size;
1316
1317 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1318 {
1319 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1320 guint32 *targets = g_new (guint32, n_targets);
1321 guint32 *p32 = targets;
1322
1323 tmp_list = display_x11->motif_target_lists[i];
1324 while (tmp_list)
1325 {
1326 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1327
1328 tmp_list = tmp_list->next;
1329 p32++;
1330 }
1331
1332 p16 = (guint16 *)p;
1333 p += sizeof(guint16);
1334
1335 memcpy (p, targets, n_targets * sizeof(guint32));
1336
1337 *p16 = n_targets;
1338 p += sizeof(guint32) * n_targets;
1339 g_free (targets);
1340 }
1341
1342 XChangeProperty (display_x11->xdisplay,
1343 display_x11->motif_drag_window,
1344 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1345 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1346 8, PropModeReplace,
1347 data, total_size);
1348 }
1349 gdk_x11_display_ungrab (display);
1350 }
1351
1352 g_list_free (sorted);
1353 return index;
1354 }
1355
1356 /* Translate flags */
1357
1358 static void
1359 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1360 {
1361 guint recommended_op = flags & 0x000f;
1362 guint possible_ops = (flags & 0x0f0) >> 4;
1363
1364 switch (recommended_op)
1365 {
1366 case XmDROP_MOVE:
1367 context->suggested_action = GDK_ACTION_MOVE;
1368 break;
1369 case XmDROP_COPY:
1370 context->suggested_action = GDK_ACTION_COPY;
1371 break;
1372 case XmDROP_LINK:
1373 context->suggested_action = GDK_ACTION_LINK;
1374 break;
1375 default:
1376 context->suggested_action = GDK_ACTION_COPY;
1377 break;
1378 }
1379
1380 context->actions = 0;
1381 if (possible_ops & XmDROP_MOVE)
1382 context->actions |= GDK_ACTION_MOVE;
1383 if (possible_ops & XmDROP_COPY)
1384 context->actions |= GDK_ACTION_COPY;
1385 if (possible_ops & XmDROP_LINK)
1386 context->actions |= GDK_ACTION_LINK;
1387 }
1388
1389 static guint16
1390 motif_dnd_get_flags (GdkDragContext *context)
1391 {
1392 guint16 flags = 0;
1393
1394 switch (context->suggested_action)
1395 {
1396 case GDK_ACTION_MOVE:
1397 flags = XmDROP_MOVE;
1398 break;
1399 case GDK_ACTION_COPY:
1400 flags = XmDROP_COPY;
1401 break;
1402 case GDK_ACTION_LINK:
1403 flags = XmDROP_LINK;
1404 break;
1405 default:
1406 flags = XmDROP_NOOP;
1407 break;
1408 }
1409
1410 if (context->actions & GDK_ACTION_MOVE)
1411 flags |= XmDROP_MOVE << 8;
1412 if (context->actions & GDK_ACTION_COPY)
1413 flags |= XmDROP_COPY << 8;
1414 if (context->actions & GDK_ACTION_LINK)
1415 flags |= XmDROP_LINK << 8;
1416
1417 return flags;
1418 }
1419
1420 /* Source Side */
1421
1422 static void
1423 motif_set_targets (GdkDragContext *context)
1424 {
1425 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1426 MotifDragInitiatorInfo info;
1427 gint i;
1428 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1429
1430 info.byte_order = local_byte_order;
1431 info.protocol_version = 0;
1432
1433 info.targets_index = motif_add_to_target_table (display, context->targets);
1434
1435 for (i=0; ; i++)
1436 {
1437 gchar buf[20];
1438 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1439
1440 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1441 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1442 break;
1443 }
1444
1445 info.selection_atom = private->motif_selection;
1446
1447 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1448 GDK_DRAWABLE_XID (context->source_window),
1449 private->motif_selection,
1450 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1451 8, PropModeReplace,
1452 (guchar *)&info, sizeof (info));
1453
1454 private->motif_targets_set = 1;
1455 }
1456
1457 static guint32
1458 motif_check_dest (GdkDisplay *display,
1459 Window win)
1460 {
1461 gboolean retval = FALSE;
1462 guchar *data;
1463 MotifDragReceiverInfo *info;
1464 Atom type = None;
1465 int format;
1466 unsigned long nitems, after;
1467 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1468
1469 gdk_error_trap_push ();
1470 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1471 motif_drag_receiver_info_atom,
1472 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1473 &type, &format, &nitems, &after,
1474 &data);
1475
1476 if (gdk_error_trap_pop() == 0)
1477 {
1478 if (type != None)
1479 {
1480 info = (MotifDragReceiverInfo *)data;
1481
1482 if ((format == 8) && (nitems == sizeof(*info)))
1483 {
1484 if ((info->protocol_version == 0) &&
1485 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1486 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1487 (info->protocol_style == XmDRAG_DYNAMIC)))
1488 retval = TRUE;
1489 }
1490 else
1491 {
1492 GDK_NOTE (DND,
1493 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1494 }
1495
1496 XFree (info);
1497 }
1498 }
1499
1500 return retval ? win : None;
1501 }
1502
1503 static void
1504 motif_send_enter (GdkDragContext *context,
1505 guint32 time)
1506 {
1507 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1508 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1509 XEvent xev;
1510
1511 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1512 return; /* Motif Dnd requires getting properties on the root window */
1513
1514 xev.xclient.type = ClientMessage;
1515 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1516 xev.xclient.format = 8;
1517 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1518
1519 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1520 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1521 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1522 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1523 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1524
1525 if (!private->motif_targets_set)
1526 motif_set_targets (context);
1527
1528 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1529 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1530
1531 if (!_gdk_send_xevent (display,
1532 GDK_DRAWABLE_XID (context->dest_window),
1533 FALSE, 0, &xev))
1534 GDK_NOTE (DND,
1535 g_message ("Send event to %lx failed",
1536 GDK_DRAWABLE_XID (context->dest_window)));
1537 }
1538
1539 static void
1540 motif_send_leave (GdkDragContext *context,
1541 guint32 time)
1542 {
1543 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1544 XEvent xev;
1545
1546 xev.xclient.type = ClientMessage;
1547 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1548 xev.xclient.format = 8;
1549 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1550
1551 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1552 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1553 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1554 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1555 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1556 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1557 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1558
1559 if (!_gdk_send_xevent (display,
1560 GDK_DRAWABLE_XID (context->dest_window),
1561 FALSE, 0, &xev))
1562 GDK_NOTE (DND,
1563 g_message ("Send event to %lx failed",
1564 GDK_DRAWABLE_XID (context->dest_window)));
1565 }
1566
1567 static gboolean
1568 motif_send_motion (GdkDragContext *context,
1569 gint x_root,
1570 gint y_root,
1571 GdkDragAction action,
1572 guint32 time)
1573 {
1574 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1575 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1576 gboolean retval;
1577 XEvent xev;
1578
1579 xev.xclient.type = ClientMessage;
1580 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1581 xev.xclient.format = 8;
1582 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1583
1584 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1585 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1586 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1587 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1588 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1589
1590 if ((context->suggested_action != private->old_action) ||
1591 (context->actions != private->old_actions))
1592 {
1593 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1594
1595 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1596 retval = TRUE;
1597 }
1598 else
1599 {
1600 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1601
1602 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1603 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1604
1605 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1606 retval = FALSE;
1607 }
1608
1609 if (!_gdk_send_xevent (display,
1610 GDK_DRAWABLE_XID (context->dest_window),
1611 FALSE, 0, &xev))
1612 GDK_NOTE (DND,
1613 g_message ("Send event to %lx failed",
1614 GDK_DRAWABLE_XID (context->dest_window)));
1615
1616 return retval;
1617 }
1618
1619 static void
1620 motif_send_drop (GdkDragContext *context, guint32 time)
1621 {
1622 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1623 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1624 XEvent xev;
1625
1626 xev.xclient.type = ClientMessage;
1627 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1628 xev.xclient.format = 8;
1629 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1630
1631 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1632 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1633 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1634 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1635
1636 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1637 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1638
1639 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1640 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1641
1642 if (!_gdk_send_xevent (display,
1643 GDK_DRAWABLE_XID (context->dest_window),
1644 FALSE, 0, &xev))
1645 GDK_NOTE (DND,
1646 g_message ("Send event to %lx failed",
1647 GDK_DRAWABLE_XID (context->dest_window)));
1648 }
1649
1650 /* Target Side */
1651
1652 static gboolean
1653 motif_read_initiator_info (GdkDisplay *display,
1654 Window source_window,
1655 Atom atom,
1656 GList **targets,
1657 Atom *selection)
1658 {
1659 GList *tmp_list;
1660 Atom type;
1661 gint format;
1662 gulong nitems;
1663 gulong bytes_after;
1664 guchar *data;
1665 MotifDragInitiatorInfo *initiator_info;
1666
1667 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1668
1669 gdk_error_trap_push ();
1670 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1671 0, sizeof(*initiator_info), FALSE,
1672 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1673 &type, &format, &nitems, &bytes_after,
1674 &data);
1675
1676 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1677 {
1678 g_warning ("Error reading initiator info\n");
1679 return FALSE;
1680 }
1681
1682 initiator_info = (MotifDragInitiatorInfo *)data;
1683
1684 motif_read_target_table (display);
1685
1686 initiator_info->targets_index =
1687 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1688 initiator_info->selection_atom =
1689 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1690
1691 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1692 {
1693 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1694 XFree (initiator_info);
1695 return FALSE;
1696 }
1697
1698 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1699
1700 *targets = NULL;
1701 while (tmp_list)
1702 {
1703 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1704 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1705 tmp_list = tmp_list->prev;
1706 }
1707
1708 #ifdef G_ENABLE_DEBUG
1709 if (_gdk_debug_flags & GDK_DEBUG_DND)
1710 print_target_list (*targets);
1711 #endif /* G_ENABLE_DEBUG */
1712
1713 *selection = initiator_info->selection_atom;
1714
1715 XFree (initiator_info);
1716
1717 return TRUE;
1718 }
1719
1720 static GdkDragContext *
1721 motif_drag_context_new (GdkWindow *dest_window,
1722 guint32 timestamp,
1723 guint32 source_window,
1724 guint32 atom)
1725 {
1726 GdkDragContext *new_context;
1727 GdkDragContextPrivateX11 *private;
1728 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1729 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1730
1731 /* FIXME, current_dest_drag really shouldn't be NULL'd
1732 * if we error below.
1733 */
1734 if (display_x11->current_dest_drag != NULL)
1735 {
1736 if (timestamp >= display_x11->current_dest_drag->start_time)
1737 {
1738 g_object_unref (display_x11->current_dest_drag);
1739 display_x11->current_dest_drag = NULL;
1740 }
1741 else
1742 return NULL;
1743 }
1744
1745 new_context = gdk_drag_context_new ();
1746 private = PRIVATE_DATA (new_context);
1747
1748 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1749 new_context->is_source = FALSE;
1750
1751 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1752 if (new_context->source_window)
1753 g_object_ref (new_context->source_window);
1754 else
1755 {
1756 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1757 if (!new_context->source_window)
1758 {
1759 g_object_unref (new_context);
1760 return NULL;
1761 }
1762 }
1763
1764 new_context->dest_window = dest_window;
1765 g_object_ref (dest_window);
1766 new_context->start_time = timestamp;
1767
1768 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1769 source_window,
1770 atom,
1771 &new_context->targets,
1772 &private->motif_selection))
1773 {
1774 g_object_unref (new_context);
1775 return NULL;
1776 }
1777
1778 return new_context;
1779 }
1780
1781 /*
1782 * The MOTIF drag protocol has no real provisions for distinguishing
1783 * multiple simultaneous drops. If the sources grab the pointer
1784 * when doing drags, that shouldn't happen, in any case. If it
1785 * does, we can't do much except hope for the best.
1786 */
1787
1788 static GdkFilterReturn
1789 motif_top_level_enter (GdkEvent *event,
1790 guint16 flags,
1791 guint32 timestamp,
1792 guint32 source_window,
1793 guint32 atom)
1794 {
1795 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1796 GdkDragContext *new_context;
1797
1798 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1799 flags, timestamp, source_window, atom));
1800
1801 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1802 if (!new_context)
1803 return GDK_FILTER_REMOVE;
1804
1805 event->dnd.type = GDK_DRAG_ENTER;
1806 event->dnd.context = new_context;
1807 g_object_ref (new_context);
1808
1809 display_x11->current_dest_drag = new_context;
1810
1811 return GDK_FILTER_TRANSLATE;
1812 }
1813
1814 static GdkFilterReturn
1815 motif_top_level_leave (GdkEvent *event,
1816 guint16 flags,
1817 guint32 timestamp)
1818 {
1819 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1820
1821 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1822 flags, timestamp));
1823
1824 if ((display_x11->current_dest_drag != NULL) &&
1825 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1826 (timestamp >= display_x11->current_dest_drag->start_time))
1827 {
1828 event->dnd.type = GDK_DRAG_LEAVE;
1829 /* Pass ownership of context to the event */
1830 event->dnd.context = display_x11->current_dest_drag;
1831
1832 display_x11->current_dest_drag = NULL;
1833
1834 return GDK_FILTER_TRANSLATE;
1835 }
1836 else
1837 return GDK_FILTER_REMOVE;
1838 }
1839
1840 static GdkFilterReturn
1841 motif_motion (GdkEvent *event,
1842 guint16 flags,
1843 guint32 timestamp,
1844 gint16 x_root,
1845 gint16 y_root)
1846 {
1847 GdkDragContextPrivateX11 *private;
1848 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1849
1850 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1851 flags, timestamp, x_root, y_root));
1852
1853 if ((display_x11->current_dest_drag != NULL) &&
1854 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1855 (timestamp >= display_x11->current_dest_drag->start_time))
1856 {
1857 private = PRIVATE_DATA (display_x11->current_dest_drag);
1858
1859 event->dnd.type = GDK_DRAG_MOTION;
1860 event->dnd.context = display_x11->current_dest_drag;
1861 g_object_ref (display_x11->current_dest_drag);
1862
1863 event->dnd.time = timestamp;
1864
1865 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1866
1867 event->dnd.x_root = x_root;
1868 event->dnd.y_root = y_root;
1869
1870 private->last_x = x_root;
1871 private->last_y = y_root;
1872
1873 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1874
1875 return GDK_FILTER_TRANSLATE;
1876 }
1877
1878 return GDK_FILTER_REMOVE;
1879 }
1880
1881 static GdkFilterReturn
1882 motif_operation_changed (GdkEvent *event,
1883 guint16 flags,
1884 guint32 timestamp)
1885 {
1886 GdkDragContextPrivateX11 *private;
1887 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1888 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1889 flags, timestamp));
1890
1891 if ((display_x11->current_dest_drag != NULL) &&
1892 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1893 (timestamp >= display_x11->current_dest_drag->start_time))
1894 {
1895 event->dnd.type = GDK_DRAG_MOTION;
1896 event->dnd.send_event = FALSE;
1897 event->dnd.context = display_x11->current_dest_drag;
1898 g_object_ref (display_x11->current_dest_drag);
1899
1900 event->dnd.time = timestamp;
1901 private = PRIVATE_DATA (display_x11->current_dest_drag);
1902
1903 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1904
1905 event->dnd.x_root = private->last_x;
1906 event->dnd.y_root = private->last_y;
1907
1908 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1909
1910 return GDK_FILTER_TRANSLATE;
1911 }
1912
1913 return GDK_FILTER_REMOVE;
1914 }
1915
1916 static GdkFilterReturn
1917 motif_drop_start (GdkEvent *event,
1918 guint16 flags,
1919 guint32 timestamp,
1920 guint32 source_window,
1921 guint32 atom,
1922 gint16 x_root,
1923 gint16 y_root)
1924 {
1925 GdkDragContext *new_context;
1926 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1927
1928 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1929 flags, timestamp, x_root, y_root, source_window, atom));
1930
1931 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1932 if (!new_context)
1933 return GDK_FILTER_REMOVE;
1934
1935 motif_dnd_translate_flags (new_context, flags);
1936
1937 event->dnd.type = GDK_DROP_START;
1938 event->dnd.context = new_context;
1939 event->dnd.time = timestamp;
1940 event->dnd.x_root = x_root;
1941 event->dnd.y_root = y_root;
1942
1943 gdk_x11_window_set_user_time (event->any.window, timestamp);
1944
1945 g_object_ref (new_context);
1946 display_x11->current_dest_drag = new_context;
1947
1948 return GDK_FILTER_TRANSLATE;
1949 }
1950
1951 static GdkFilterReturn
1952 motif_drag_status (GdkEvent *event,
1953 guint16 flags,
1954 guint32 timestamp)
1955 {
1956 GdkDragContext *context;
1957 GdkDisplay *display;
1958
1959 GDK_NOTE (DND,
1960 g_message ("Motif status message: flags %x", flags));
1961
1962 display = gdk_drawable_get_display (event->any.window);
1963 if (!display)
1964 return GDK_FILTER_REMOVE;
1965
1966 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1967
1968 if (context)
1969 {
1970 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1971 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1972 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1973 private->drag_status = GDK_DRAG_STATUS_DRAG;
1974
1975 event->dnd.type = GDK_DRAG_STATUS;
1976 event->dnd.send_event = FALSE;
1977 event->dnd.context = context;
1978 g_object_ref (context);
1979
1980 event->dnd.time = timestamp;
1981
1982 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1983 {
1984 switch (flags & 0x000f)
1985 {
1986 case XmDROP_NOOP:
1987 context->action = 0;
1988 break;
1989 case XmDROP_MOVE:
1990 context->action = GDK_ACTION_MOVE;
1991 break;
1992 case XmDROP_COPY:
1993 context->action = GDK_ACTION_COPY;
1994 break;
1995 case XmDROP_LINK:
1996 context->action = GDK_ACTION_LINK;
1997 break;
1998 }
1999 }
2000 else
2001 context->action = 0;
2002
2003 return GDK_FILTER_TRANSLATE;
2004 }
2005 return GDK_FILTER_REMOVE;
2006 }
2007
2008 static GdkFilterReturn
2009 motif_dnd_filter (GdkXEvent *xev,
2010 GdkEvent *event,
2011 gpointer data)
2012 {
2013 XEvent *xevent = (XEvent *)xev;
2014
2015 guint8 reason;
2016 guint16 flags;
2017 guint32 timestamp;
2018 guint32 source_window;
2019 Atom atom;
2020 gint16 x_root, y_root;
2021 gboolean is_reply;
2022
2023 if (!event->any.window ||
2024 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2025 return GDK_FILTER_CONTINUE; /* Not for us */
2026
2027 /* First read some fields common to all Motif DND messages */
2028
2029 reason = MOTIF_UNPACK_BYTE (xevent, 0);
2030 flags = MOTIF_UNPACK_SHORT (xevent, 1);
2031 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
2032
2033 is_reply = ((reason & 0x80) != 0);
2034
2035 switch (reason & 0x7f)
2036 {
2037 case XmTOP_LEVEL_ENTER:
2038 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2039 atom = MOTIF_UNPACK_LONG (xevent, 3);
2040 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2041 case XmTOP_LEVEL_LEAVE:
2042 return motif_top_level_leave (event, flags, timestamp);
2043
2044 case XmDRAG_MOTION:
2045 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2046 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2047
2048 if (!is_reply)
2049 return motif_motion (event, flags, timestamp, x_root, y_root);
2050 else
2051 return motif_drag_status (event, flags, timestamp);
2052
2053 case XmDROP_SITE_ENTER:
2054 return motif_drag_status (event, flags, timestamp);
2055
2056 case XmDROP_SITE_LEAVE:
2057 return motif_drag_status (event,
2058 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2059 timestamp);
2060 case XmDROP_START:
2061 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2062 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2063 atom = MOTIF_UNPACK_LONG (xevent, 3);
2064 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2065
2066 if (!is_reply)
2067 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2068
2069 break;
2070 case XmOPERATION_CHANGED:
2071 if (!is_reply)
2072 return motif_operation_changed (event, flags, timestamp);
2073 else
2074 return motif_drag_status (event, flags, timestamp);
2075
2076 break;
2077 /* To the best of my knowledge, these next two messages are
2078 * not part of the protocol, though they are defined in
2079 * the header files.
2080 */
2081 case XmDROP_FINISH:
2082 case XmDRAG_DROP_FINISH:
2083 break;
2084 }
2085
2086 return GDK_FILTER_REMOVE;
2087 }
2088
2089 /*************************************************************
2090 ***************************** XDND **************************
2091 *************************************************************/
2092
2093 /* Utility functions */
2094
2095 static struct {
2096 const gchar *name;
2097 GdkAtom atom;
2098 GdkDragAction action;
2099 } xdnd_actions_table[] = {
2100 { "XdndActionCopy", None, GDK_ACTION_COPY },
2101 { "XdndActionMove", None, GDK_ACTION_MOVE },
2102 { "XdndActionLink", None, GDK_ACTION_LINK },
2103 { "XdndActionAsk", None, GDK_ACTION_ASK },
2104 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2105 };
2106
2107 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2108 static gboolean xdnd_actions_initialized = FALSE;
2109
2110 static void
2111 xdnd_initialize_actions (void)
2112 {
2113 gint i;
2114
2115 xdnd_actions_initialized = TRUE;
2116 for (i=0; i < xdnd_n_actions; i++)
2117 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2118 }
2119
2120 static GdkDragAction
2121 xdnd_action_from_atom (GdkDisplay *display,
2122 Atom xatom)
2123 {
2124 GdkAtom atom;
2125 gint i;
2126
2127 if (xatom == None)
2128 return 0;
2129
2130 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2131
2132 if (!xdnd_actions_initialized)
2133 xdnd_initialize_actions();
2134
2135 for (i=0; i<xdnd_n_actions; i++)
2136 if (atom == xdnd_actions_table[i].atom)
2137 return xdnd_actions_table[i].action;
2138
2139 return 0;
2140 }
2141
2142 static Atom
2143 xdnd_action_to_atom (GdkDisplay *display,
2144 GdkDragAction action)
2145 {
2146 gint i;
2147
2148 if (!xdnd_actions_initialized)
2149 xdnd_initialize_actions();
2150
2151 for (i=0; i<xdnd_n_actions; i++)
2152 if (action == xdnd_actions_table[i].action)
2153 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2154
2155 return None;
2156 }
2157
2158 /* Source side */
2159
2160 static GdkFilterReturn
2161 xdnd_status_filter (GdkXEvent *xev,
2162 GdkEvent *event,
2163 gpointer data)
2164 {
2165 GdkDisplay *display;
2166 XEvent *xevent = (XEvent *)xev;
2167 guint32 dest_window = xevent->xclient.data.l[0];
2168 guint32 flags = xevent->xclient.data.l[1];
2169 Atom action = xevent->xclient.data.l[4];
2170 GdkDragContext *context;
2171
2172 if (!event->any.window ||
2173 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2174 return GDK_FILTER_CONTINUE; /* Not for us */
2175
2176 GDK_NOTE (DND,
2177 g_message ("XdndStatus: dest_window: %#x action: %ld",
2178 dest_window, action));
2179
2180 display = gdk_drawable_get_display (event->any.window);
2181 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2182
2183 if (context)
2184 {
2185 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2186 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2187 private->drag_status = GDK_DRAG_STATUS_DRAG;
2188
2189 event->dnd.send_event = FALSE;
2190 event->dnd.type = GDK_DRAG_STATUS;
2191 event->dnd.context = context;
2192 g_object_ref (context);
2193
2194 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2195 if (!(action != 0) != !(flags & 1))
2196 {
2197 GDK_NOTE (DND,
2198 g_warning ("Received status event with flags not corresponding to action!\n"));
2199 action = 0;
2200 }
2201
2202 context->action = xdnd_action_from_atom (display, action);
2203
2204 return GDK_FILTER_TRANSLATE;
2205 }
2206
2207 return GDK_FILTER_REMOVE;
2208 }
2209
2210 static GdkFilterReturn
2211 xdnd_finished_filter (GdkXEvent *xev,
2212 GdkEvent *event,
2213 gpointer data)
2214 {
2215 GdkDisplay *display;
2216 XEvent *xevent = (XEvent *)xev;
2217 guint32 dest_window = xevent->xclient.data.l[0];
2218 GdkDragContext *context;
2219 GdkDragContextPrivateX11 *private;
2220
2221 if (!event->any.window ||
2222 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2223 return GDK_FILTER_CONTINUE; /* Not for us */
2224
2225 GDK_NOTE (DND,
2226 g_message ("XdndFinished: dest_window: %#x", dest_window));
2227
2228 display = gdk_drawable_get_display (event->any.window);
2229 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2230
2231 if (context)
2232 {
2233 private = PRIVATE_DATA (context);
2234 if (private->version == 5)
2235 private->drop_failed = xevent->xclient.data.l[1] == 0;
2236
2237 event->dnd.type = GDK_DROP_FINISHED;
2238 event->dnd.context = context;
2239 g_object_ref (context);
2240
2241 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2242
2243 return GDK_FILTER_TRANSLATE;
2244 }
2245
2246 return GDK_FILTER_REMOVE;
2247 }
2248
2249 static void
2250 xdnd_set_targets (GdkDragContext *context)
2251 {
2252 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2253 Atom *atomlist;
2254 GList *tmp_list = context->targets;
2255 gint i;
2256 gint n_atoms = g_list_length (context->targets);
2257 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2258
2259 atomlist = g_new (Atom, n_atoms);
2260 i = 0;
2261 while (tmp_list)
2262 {
2263 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2264 tmp_list = tmp_list->next;
2265 i++;
2266 }
2267
2268 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2269 GDK_DRAWABLE_XID (context->source_window),
2270 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2271 XA_ATOM, 32, PropModeReplace,
2272 (guchar *)atomlist, n_atoms);
2273
2274 g_free (atomlist);
2275
2276 private->xdnd_targets_set = 1;
2277 }
2278
2279 static void
2280 xdnd_set_actions (GdkDragContext *context)
2281 {
2282 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2283 Atom *atomlist;
2284 gint i;
2285 gint n_atoms;
2286 guint actions;
2287 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2288
2289 if (!xdnd_actions_initialized)
2290 xdnd_initialize_actions();
2291
2292 actions = context->actions;
2293 n_atoms = 0;
2294 for (i=0; i<xdnd_n_actions; i++)
2295 {
2296 if (actions & xdnd_actions_table[i].action)
2297 {
2298 actions &= ~xdnd_actions_table[i].action;
2299 n_atoms++;
2300 }
2301 }
2302
2303 atomlist = g_new (Atom, n_atoms);
2304
2305 actions = context->actions;
2306 n_atoms = 0;
2307 for (i=0; i<xdnd_n_actions; i++)
2308 {
2309 if (actions & xdnd_actions_table[i].action)
2310 {
2311 actions &= ~xdnd_actions_table[i].action;
2312 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2313 n_atoms++;
2314 }
2315 }
2316
2317 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2318 GDK_DRAWABLE_XID (context->source_window),
2319 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2320 XA_ATOM, 32, PropModeReplace,
2321 (guchar *)atomlist, n_atoms);
2322
2323 g_free (atomlist);
2324
2325 private->xdnd_actions_set = TRUE;
2326 private->xdnd_actions = context->actions;
2327 }
2328
2329 static void
2330 send_client_message_async_cb (Window window,
2331 gboolean success,
2332 gpointer data)
2333 {
2334 GdkDragContext *context = data;
2335 GDK_NOTE (DND,
2336 g_message ("Got async callback for #%lx, success = %d",
2337 window, success));
2338
2339 /* On failure, we immediately continue with the protocol
2340 * so we don't end up blocking for a timeout
2341 */
2342 if (!success &&
2343 context->dest_window &&
2344 window == GDK_WINDOW_XID (context->dest_window))
2345 {
2346 GdkEvent temp_event;
2347 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2348
2349 g_object_unref (context->dest_window);
2350 context->dest_window = NULL;
2351 context->action = 0;
2352
2353 private->drag_status = GDK_DRAG_STATUS_DRAG;
2354
2355 temp_event.dnd.type = GDK_DRAG_STATUS;
2356 temp_event.dnd.window = context->source_window;
2357 temp_event.dnd.send_event = TRUE;
2358 temp_event.dnd.context = context;
2359 temp_event.dnd.time = GDK_CURRENT_TIME;
2360
2361 gdk_event_put (&temp_event);
2362 }
2363
2364 g_object_unref (context);
2365 }
2366
2367
2368 static GdkDisplay *
2369 gdk_drag_context_get_display (GdkDragContext *context)
2370 {
2371 if (context->source_window)
2372 return GDK_DRAWABLE_DISPLAY (context->source_window);
2373 else if (context->dest_window)
2374 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2375
2376 g_assert_not_reached ();
2377 return NULL;
2378 }
2379
2380 static void
2381 send_client_message_async (GdkDragContext *context,
2382 Window window,
2383 gboolean propagate,
2384 glong event_mask,
2385 XClientMessageEvent *event_send)
2386 {
2387 GdkDisplay *display = gdk_drag_context_get_display (context);
2388
2389 g_object_ref (context);
2390
2391 _gdk_x11_send_client_message_async (display, window,
2392 propagate, event_mask, event_send,
2393 send_client_message_async_cb, context);
2394 }
2395
2396 static gboolean
2397 xdnd_send_xevent (GdkDragContext *context,
2398 GdkWindow *window,
2399 gboolean propagate,
2400 XEvent *event_send)
2401 {
2402 GdkDisplay *display = gdk_drag_context_get_display (context);
2403 Window xwindow;
2404 glong event_mask;
2405
2406 g_assert (event_send->xany.type == ClientMessage);
2407
2408 /* We short-circuit messages to ourselves */
2409 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2410 {
2411 gint i;
2412
2413 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2414 {
2415 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2416 event_send->xclient.message_type)
2417 {
2418 GdkEvent temp_event;
2419 temp_event.any.window = window;
2420
2421 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2422 {
2423 gdk_event_put (&temp_event);
2424 g_object_unref (temp_event.dnd.context);
2425 }
2426
2427 return TRUE;
2428 }
2429 }
2430 }
2431
2432 xwindow = GDK_WINDOW_XWINDOW (window);
2433
2434 if (_gdk_x11_display_is_root_window (display, xwindow))
2435 event_mask = ButtonPressMask;
2436 else
2437 event_mask = 0;
2438
2439 send_client_message_async (context, xwindow, propagate, event_mask,
2440 &event_send->xclient);
2441
2442 return TRUE;
2443 }
2444
2445 static void
2446 xdnd_send_enter (GdkDragContext *context)
2447 {
2448 XEvent xev;
2449 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2450 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2451
2452 xev.xclient.type = ClientMessage;
2453 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2454 xev.xclient.format = 32;
2455 xev.xclient.window = private->drop_xid ?
2456 private->drop_xid :
2457 GDK_DRAWABLE_XID (context->dest_window);
2458 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2459 xev.xclient.data.l[1] = (private->version << 24); /* version */
2460 xev.xclient.data.l[2] = 0;
2461 xev.xclient.data.l[3] = 0;
2462 xev.xclient.data.l[4] = 0;
2463
2464 GDK_NOTE(DND,
2465 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2466 GDK_DRAWABLE_XID (context->source_window), private->version));
2467 if (g_list_length (context->targets) > 3)
2468 {
2469 if (!private->xdnd_targets_set)
2470 xdnd_set_targets (context);
2471 xev.xclient.data.l[1] |= 1;
2472 }
2473 else
2474 {
2475 GList *tmp_list = context->targets;
2476 gint i = 2;
2477
2478 while (tmp_list)
2479 {
2480 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2481 GDK_POINTER_TO_ATOM (tmp_list->data));
2482 tmp_list = tmp_list->next;
2483 i++;
2484 }
2485 }
2486
2487 if (!xdnd_send_xevent (context, context->dest_window,
2488 FALSE, &xev))
2489 {
2490 GDK_NOTE (DND,
2491 g_message ("Send event to %lx failed",
2492 GDK_DRAWABLE_XID (context->dest_window)));
2493 g_object_unref (context->dest_window);
2494 context->dest_window = NULL;
2495 }
2496 }
2497
2498 static void
2499 xdnd_send_leave (GdkDragContext *context)
2500 {
2501 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2502 XEvent xev;
2503
2504 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2505
2506 xev.xclient.type = ClientMessage;
2507 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2508 xev.xclient.format = 32;
2509 xev.xclient.window = private->drop_xid ?
2510 private->drop_xid :
2511 GDK_DRAWABLE_XID (context->dest_window);
2512 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2513 xev.xclient.data.l[1] = 0;
2514 xev.xclient.data.l[2] = 0;
2515 xev.xclient.data.l[3] = 0;
2516 xev.xclient.data.l[4] = 0;
2517
2518 if (!xdnd_send_xevent (context, context->dest_window,
2519 FALSE, &xev))
2520 {
2521 GDK_NOTE (DND,
2522 g_message ("Send event to %lx failed",
2523 GDK_DRAWABLE_XID (context->dest_window)));
2524 g_object_unref (context->dest_window);
2525 context->dest_window = NULL;
2526 }
2527 }
2528
2529 static void
2530 xdnd_send_drop (GdkDragContext *context, guint32 time)
2531 {
2532 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2533 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2534 XEvent xev;
2535
2536 xev.xclient.type = ClientMessage;
2537 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2538 xev.xclient.format = 32;
2539 xev.xclient.window = private->drop_xid ?
2540 private->drop_xid :
2541 GDK_DRAWABLE_XID (context->dest_window);
2542 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2543 xev.xclient.data.l[1] = 0;
2544 xev.xclient.data.l[2] = time;
2545 xev.xclient.data.l[3] = 0;
2546 xev.xclient.data.l[4] = 0;
2547
2548 if (!xdnd_send_xevent (context, context->dest_window,
2549 FALSE, &xev))
2550 {
2551 GDK_NOTE (DND,
2552 g_message ("Send event to %lx failed",
2553 GDK_DRAWABLE_XID (context->dest_window)));
2554 g_object_unref (context->dest_window);
2555 context->dest_window = NULL;
2556 }
2557 }
2558
2559 static void
2560 xdnd_send_motion (GdkDragContext *context,
2561 gint x_root,
2562 gint y_root,
2563 GdkDragAction action,
2564 guint32 time)
2565 {
2566 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2567 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2568 XEvent xev;
2569
2570 xev.xclient.type = ClientMessage;
2571 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2572 xev.xclient.format = 32;
2573 xev.xclient.window = private->drop_xid ?
2574 private->drop_xid :
2575 GDK_DRAWABLE_XID (context->dest_window);
2576 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2577 xev.xclient.data.l[1] = 0;
2578 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2579 xev.xclient.data.l[3] = time;
2580 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2581
2582 if (!xdnd_send_xevent (context, context->dest_window,
2583 FALSE, &xev))
2584 {
2585 GDK_NOTE (DND,
2586 g_message ("Send event to %lx failed",
2587 GDK_DRAWABLE_XID (context->dest_window)));
2588 g_object_unref (context->dest_window);
2589 context->dest_window = NULL;
2590 }
2591 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2592 }
2593
2594 static guint32
2595 xdnd_check_dest (GdkDisplay *display,
2596 Window win,
2597 guint *xdnd_version)
2598 {
2599 gboolean retval = FALSE;
2600 Atom type = None;
2601 int format;
2602 unsigned long nitems, after;
2603 guchar *data;
2604 Atom *version;
2605 Window *proxy_data;
2606 Window proxy;
2607 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2608 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2609
2610 proxy = None;
2611
2612 gdk_error_trap_push ();
2613
2614 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2615 xdnd_proxy_atom, 0,
2616 1, False, AnyPropertyType,
2617 &type, &format, &nitems, &after,
2618 &data) == Success)
2619 {
2620 if (type != None)
2621 {
2622 proxy_data = (Window *)data;
2623
2624 if ((format == 32) && (nitems == 1))
2625 {
2626 proxy = *proxy_data;
2627 }
2628 else
2629 GDK_NOTE (DND,
2630 g_warning ("Invalid XdndProxy "
2631 "property on window %ld\n", win));
2632
2633 XFree (proxy_data);
2634 }
2635
2636 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2637 xdnd_aware_atom, 0,
2638 1, False, AnyPropertyType,
2639 &type, &format, &nitems, &after,
2640 &data) == Success) &&
2641 type != None)
2642 {
2643 version = (Atom *)data;
2644
2645 if ((format == 32) && (nitems == 1))
2646 {
2647 if (*version >= 3)
2648 retval = TRUE;
2649 if (xdnd_version)
2650 *xdnd_version = *version;
2651 }
2652 else
2653 GDK_NOTE (DND,
2654 g_warning ("Invalid XdndAware "
2655 "property on window %ld\n", win));
2656
2657 XFree (version);
2658 }
2659 }
2660
2661 gdk_error_trap_pop ();
2662
2663 return retval ? (proxy ? proxy : win) : None;
2664 }
2665
2666 /* Target side */
2667
2668 static void
2669 xdnd_read_actions (GdkDragContext *context)
2670 {
2671 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2672 Atom type;
2673 int format;
2674 gulong nitems, after;
2675 guchar *data;
2676 Atom *atoms;
2677
2678 gint i;
2679
2680 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2681
2682 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2683 {
2684 /* Get the XdndActionList, if set */
2685
2686 gdk_error_trap_push ();
2687
2688 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2689 GDK_DRAWABLE_XID (context->source_window),
2690 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2691 0, 65536,
2692 False, XA_ATOM, &type, &format, &nitems,
2693 &after, &data) == Success &&
2694 type == XA_ATOM)
2695 {
2696 atoms = (Atom *)data;
2697
2698 context->actions = 0;
2699
2700 for (i=0; i<nitems; i++)
2701 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2702
2703 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2704
2705 #ifdef G_ENABLE_DEBUG
2706 if (_gdk_debug_flags & GDK_DEBUG_DND)
2707 {
2708 GString *action_str = g_string_new (NULL);
2709 if (context->actions & GDK_ACTION_MOVE)
2710 g_string_append(action_str, "MOVE ");
2711 if (context->actions & GDK_ACTION_COPY)
2712 g_string_append(action_str, "COPY ");
2713 if (context->actions & GDK_ACTION_LINK)
2714 g_string_append(action_str, "LINK ");
2715 if (context->actions & GDK_ACTION_ASK)
2716 g_string_append(action_str, "ASK ");
2717
2718 g_message("Xdnd actions = %s", action_str->str);
2719 g_string_free (action_str, TRUE);
2720 }
2721 #endif /* G_ENABLE_DEBUG */
2722
2723 }
2724
2725 if (data)
2726 XFree (data);
2727
2728 gdk_error_trap_pop ();
2729 }
2730 else
2731 {
2732 /* Local drag
2733 */
2734 GdkDragContext *source_context;
2735
2736 source_context = gdk_drag_context_find (display, TRUE,
2737 GDK_DRAWABLE_XID (context->source_window),
2738 GDK_DRAWABLE_XID (context->dest_window));
2739
2740 if (source_context)
2741 {
2742 context->actions = source_context->actions;
2743 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2744 }
2745 }
2746 }
2747
2748 /* We have to make sure that the XdndActionList we keep internally
2749 * is up to date with the XdndActionList on the source window
2750 * because we get no notification, because Xdnd wasn't meant
2751 * to continually send actions. So we select on PropertyChangeMask
2752 * and add this filter.
2753 */
2754 static GdkFilterReturn
2755 xdnd_source_window_filter (GdkXEvent *xev,
2756 GdkEvent *event,
2757 gpointer cb_data)
2758 {
2759 XEvent *xevent = (XEvent *)xev;
2760 GdkDragContext *context = cb_data;
2761 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2762
2763 if ((xevent->xany.type == PropertyNotify) &&
2764 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2765 {
2766 xdnd_read_actions (context);
2767
2768 return GDK_FILTER_REMOVE;
2769 }
2770
2771 return GDK_FILTER_CONTINUE;
2772 }
2773
2774 static void
2775 xdnd_manage_source_filter (GdkDragContext *context,
2776 GdkWindow *window,
2777 gboolean add_filter)
2778 {
2779 if (!GDK_WINDOW_DESTROYED (window) &&
2780 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2781 {
2782 gdk_error_trap_push ();
2783
2784 if (add_filter)
2785 {
2786 gdk_window_set_events (window,
2787 gdk_window_get_events (window) |
2788 GDK_PROPERTY_CHANGE_MASK);
2789 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2790 }
2791 else
2792 {
2793 gdk_window_remove_filter (window,
2794 xdnd_source_window_filter,
2795 context);
2796 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2797 * but we might want it for other reasons. (Like
2798 * INCR selection transactions).
2799 */
2800 }
2801
2802 gdk_display_sync (gdk_drawable_get_display (window));
2803 gdk_error_trap_pop ();
2804 }
2805 }
2806
2807 static void
2808 base_precache_atoms (GdkDisplay *display)
2809 {
2810 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2811
2812 if (!display_x11->base_dnd_atoms_precached)
2813 {
2814 static const char *const precache_atoms[] = {
2815 "ENLIGHTENMENT_DESKTOP",
2816 "WM_STATE",
2817 "XdndAware",
2818 "XdndProxy",
2819 "_MOTIF_DRAG_RECEIVER_INFO"
2820 };
2821
2822 _gdk_x11_precache_atoms (display,
2823 precache_atoms, G_N_ELEMENTS (precache_atoms));
2824
2825 display_x11->base_dnd_atoms_precached = TRUE;
2826 }
2827 }
2828
2829 static void
2830 xdnd_precache_atoms (GdkDisplay *display)
2831 {
2832 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2833
2834 if (!display_x11->xdnd_atoms_precached)
2835 {
2836 static const char *const precache_atoms[] = {
2837 "XdndActionAsk",
2838 "XdndActionCopy",
2839 "XdndActionLink",
2840 "XdndActionList",
2841 "XdndActionMove",
2842 "XdndActionPrivate",
2843 "XdndDrop",
2844 "XdndEnter",
2845 "XdndFinished",
2846 "XdndLeave",
2847 "XdndPosition",
2848 "XdndSelection",
2849 "XdndStatus",
2850 "XdndTypeList"
2851 };
2852
2853 _gdk_x11_precache_atoms (display,
2854 precache_atoms, G_N_ELEMENTS (precache_atoms));
2855
2856 display_x11->xdnd_atoms_precached = TRUE;
2857 }
2858 }
2859
2860 static GdkFilterReturn
2861 xdnd_enter_filter (GdkXEvent *xev,
2862 GdkEvent *event,
2863 gpointer cb_data)
2864 {
2865 GdkDisplay *display;
2866 GdkDisplayX11 *display_x11;
2867 XEvent *xevent = (XEvent *)xev;
2868 GdkDragContext *new_context;
2869 gint i;
2870
2871 Atom type;
2872 int format;
2873 gulong nitems, after;
2874 guchar *data;
2875 Atom *atoms;
2876
2877 guint32 source_window;
2878 gboolean get_types;
2879 gint version;
2880
2881 if (!event->any.window ||
2882 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2883 return GDK_FILTER_CONTINUE; /* Not for us */
2884
2885 source_window = xevent->xclient.data.l[0];
2886 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2887 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2888
2889 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2890 display_x11 = GDK_DISPLAY_X11 (display);
2891
2892 xdnd_precache_atoms (display);
2893
2894 GDK_NOTE (DND,
2895 g_message ("XdndEnter: source_window: %#x, version: %#x",
2896 source_window, version));
2897
2898 if (version < 3)
2899 {
2900 /* Old source ignore */
2901 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2902 return GDK_FILTER_REMOVE;
2903 }
2904
2905 if (display_x11->current_dest_drag != NULL)
2906 {
2907 g_object_unref (display_x11->current_dest_drag);
2908 display_x11->current_dest_drag = NULL;
2909 }
2910
2911 new_context = gdk_drag_context_new ();
2912 new_context->protocol = GDK_DRAG_PROTO_XDND;
2913 PRIVATE_DATA(new_context)->version = version;
2914
2915 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2916 if (new_context->source_window)
2917 g_object_ref (new_context->source_window);
2918 else
2919 {
2920 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2921 if (!new_context->source_window)
2922 {
2923 g_object_unref (new_context);
2924 return GDK_FILTER_REMOVE;
2925 }
2926 }
2927 new_context->dest_window = event->any.window;
2928 g_object_ref (new_context->dest_window);
2929
2930 new_context->targets = NULL;
2931 if (get_types)
2932 {
2933 gdk_error_trap_push ();
2934 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2935 source_window,
2936 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2937 0, 65536,
2938 False, XA_ATOM, &type, &format, &nitems,
2939 &after, &data);
2940
2941 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2942 {
2943 g_object_unref (new_context);
2944
2945 if (data)
2946 XFree (data);
2947
2948 return GDK_FILTER_REMOVE;
2949 }
2950
2951 atoms = (Atom *)data;
2952
2953 for (i=0; i<nitems; i++)
2954 new_context->targets =
2955 g_list_append (new_context->targets,
2956 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2957 atoms[i])));
2958
2959 XFree(atoms);
2960 }
2961 else
2962 {
2963 for (i=0; i<3; i++)
2964 if (xevent->xclient.data.l[2+i])
2965 new_context->targets =
2966 g_list_append (new_context->targets,
2967 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2968 xevent->xclient.data.l[2+i])));
2969 }
2970
2971 #ifdef G_ENABLE_DEBUG
2972 if (_gdk_debug_flags & GDK_DEBUG_DND)
2973 print_target_list (new_context->targets);
2974 #endif /* G_ENABLE_DEBUG */
2975
2976 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2977 xdnd_read_actions (new_context);
2978
2979 event->dnd.type = GDK_DRAG_ENTER;
2980 event->dnd.context = new_context;
2981 g_object_ref (new_context);
2982
2983 display_x11->current_dest_drag = new_context;
2984
2985 return GDK_FILTER_TRANSLATE;
2986 }
2987
2988 static GdkFilterReturn
2989 xdnd_leave_filter (GdkXEvent *xev,
2990 GdkEvent *event,
2991 gpointer data)
2992 {
2993 XEvent *xevent = (XEvent *)xev;
2994 guint32 source_window = xevent->xclient.data.l[0];
2995 GdkDisplay *display;
2996 GdkDisplayX11 *display_x11;
2997
2998 if (!event->any.window ||
2999 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3000 return GDK_FILTER_CONTINUE; /* Not for us */
3001
3002 GDK_NOTE (DND,
3003 g_message ("XdndLeave: source_window: %#x",
3004 source_window));
3005
3006 display = GDK_DRAWABLE_DISPLAY (event->any.window);
3007 display_x11 = GDK_DISPLAY_X11 (display);
3008
3009 xdnd_precache_atoms (display);
3010
3011 if ((display_x11->current_dest_drag != NULL) &&
3012 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3013 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3014 {
3015 event->dnd.type = GDK_DRAG_LEAVE;
3016 /* Pass ownership of context to the event */
3017 event->dnd.context = display_x11->current_dest_drag;
3018
3019 display_x11->current_dest_drag = NULL;
3020
3021 return GDK_FILTER_TRANSLATE;
3022 }
3023 else
3024 return GDK_FILTER_REMOVE;
3025 }
3026
3027 static GdkFilterReturn
3028 xdnd_position_filter (GdkXEvent *xev,
3029 GdkEvent *event,
3030 gpointer data)
3031 {
3032 XEvent *xevent = (XEvent *)xev;
3033 guint32 source_window = xevent->xclient.data.l[0];
3034 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3035 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3036 guint32 time = xevent->xclient.data.l[3];
3037 Atom action = xevent->xclient.data.l[4];
3038
3039 GdkDisplay *display;
3040 GdkDisplayX11 *display_x11;
3041
3042 if (!event->any.window ||
3043 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3044 return GDK_FILTER_CONTINUE; /* Not for us */
3045
3046 GDK_NOTE (DND,
3047 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3048 source_window, x_root, y_root, time, action));
3049
3050 display = GDK_DRAWABLE_DISPLAY (event->any.window);
3051 display_x11 = GDK_DISPLAY_X11 (display);
3052
3053 xdnd_precache_atoms (display);
3054
3055 if ((display_x11->current_dest_drag != NULL) &&
3056 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3057 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3058 {
3059 event->dnd.type = GDK_DRAG_MOTION;
3060 event->dnd.context = display_x11->current_dest_drag;
3061 g_object_ref (display_x11->current_dest_drag);
3062
3063 event->dnd.time = time;
3064
3065 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
3066
3067 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
3068 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
3069
3070 event->dnd.x_root = x_root;
3071 event->dnd.y_root = y_root;
3072
3073 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
3074 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
3075
3076 return GDK_FILTER_TRANSLATE;
3077 }
3078
3079 return GDK_FILTER_REMOVE;
3080 }
3081
3082 static GdkFilterReturn
3083 xdnd_drop_filter (GdkXEvent *xev,
3084 GdkEvent *event,
3085 gpointer data)
3086 {
3087 XEvent *xevent = (XEvent *)xev;
3088 guint32 source_window = xevent->xclient.data.l[0];
3089 guint32 time = xevent->xclient.data.l[2];
3090 GdkDisplay *display;
3091 GdkDisplayX11 *display_x11;
3092
3093 if (!event->any.window ||
3094 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3095 return GDK_FILTER_CONTINUE; /* Not for us */
3096
3097 GDK_NOTE (DND,
3098 g_message ("XdndDrop: source_window: %#x time: %d",
3099 source_window, time));
3100
3101 display = GDK_DRAWABLE_DISPLAY (event->any.window);
3102 display_x11 = GDK_DISPLAY_X11 (display);
3103
3104 xdnd_precache_atoms (display);
3105
3106 if ((display_x11->current_dest_drag != NULL) &&
3107 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3108 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3109 {
3110 GdkDragContextPrivateX11 *private;
3111 private = PRIVATE_DATA (display_x11->current_dest_drag);
3112
3113 event->dnd.type = GDK_DROP_START;
3114
3115 event->dnd.context = display_x11->current_dest_drag;
3116 g_object_ref (display_x11->current_dest_drag);
3117
3118 event->dnd.time = time;
3119 event->dnd.x_root = private->last_x;
3120 event->dnd.y_root = private->last_y;
3121
3122 gdk_x11_window_set_user_time (event->any.window, time);
3123
3124 return GDK_FILTER_TRANSLATE;
3125 }
3126
3127 return GDK_FILTER_REMOVE;
3128 }
3129
3130 /*************************************************************
3131 ************************** Public API ***********************
3132 *************************************************************/
3133 void
3134 _gdk_dnd_init (GdkDisplay *display)
3135 {
3136 int i;
3137 init_byte_order ();
3138
3139 gdk_display_add_client_message_filter (
3140 display,
3141 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3142 motif_dnd_filter, NULL);
3143
3144 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3145 {
3146 gdk_display_add_client_message_filter (
3147 display,
3148 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3149 xdnd_filters[i].func, NULL);
3150 }
3151 }
3152
3153 /* Source side */
3154
3155 static void
3156 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
3157 {
3158 if (context->dest_window)
3159 {
3160 switch (context->protocol)
3161 {
3162 case GDK_DRAG_PROTO_MOTIF:
3163 motif_send_leave (context, time);
3164 break;
3165 case GDK_DRAG_PROTO_XDND:
3166 xdnd_send_leave (context);
3167 break;
3168 case GDK_DRAG_PROTO_ROOTWIN:
3169 case GDK_DRAG_PROTO_NONE:
3170 default:
3171 break;
3172 }
3173
3174 g_object_unref (context->dest_window);
3175 context->dest_window = NULL;
3176 }
3177 }
3178
3179 /**
3180 * gdk_drag_begin:
3181 * @window: the source window for this drag.
3182 * @targets: (transfer none) (element-type GdkAtom): the offered targets,
3183 * as list of #GdkAtom<!-- -->s
3184 *
3185 * Starts a drag and creates a new drag context for it.
3186 *
3187 * This function is called by the drag source.
3188 *
3189 * Return value: a newly created #GdkDragContext.
3190 **/
3191 GdkDragContext *
3192 gdk_drag_begin (GdkWindow *window,
3193 GList *targets)
3194 {
3195 GdkDragContext *new_context;
3196
3197 g_return_val_if_fail (window != NULL, NULL);
3198 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
3199
3200 new_context = gdk_drag_context_new ();
3201 new_context->is_source = TRUE;
3202 new_context->source_window = window;
3203 g_object_ref (window);
3204
3205 new_context->targets = g_list_copy (targets);
3206 precache_target_list (new_context);
3207
3208 new_context->actions = 0;
3209
3210 return new_context;
3211 }
3212
3213 static GdkNativeWindow
3214 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3215 GdkNativeWindow xid,
3216 GdkDragProtocol *protocol,
3217 guint *version)
3218
3219 {
3220 GdkWindow *window;
3221 GdkNativeWindow retval;
3222 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3223
3224 base_precache_atoms (display);
3225
3226 /* Check for a local drag
3227 */
3228 window = gdk_window_lookup_for_display (display, xid);
3229 if (window &&
3230 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3231 {
3232 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3233 {
3234 *protocol = GDK_DRAG_PROTO_XDND;
3235 *version = 5;
3236 xdnd_precache_atoms (display);
3237 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3238 return xid;
3239 }
3240 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3241 {
3242 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3243 GDK_NOTE (DND, g_message ("Entering root window\n"));
3244 return xid;
3245 }
3246 }
3247 else if ((retval = xdnd_check_dest (display, xid, version)))
3248 {
3249 *protocol = GDK_DRAG_PROTO_XDND;
3250 xdnd_precache_atoms (display);
3251 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3252 return retval;
3253 }
3254 else if ((retval = motif_check_dest (display, xid)))
3255 {
3256 *protocol = GDK_DRAG_PROTO_MOTIF;
3257 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3258 return retval;
3259 }
3260 else
3261 {
3262 /* Check if this is a root window */
3263
3264 gboolean rootwin = FALSE;
3265 Atom type = None;
3266 int format;
3267 unsigned long nitems, after;
3268 unsigned char *data;
3269
3270 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3271 rootwin = TRUE;
3272
3273 gdk_error_trap_push ();
3274
3275 if (!rootwin)
3276 {
3277 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3278 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3279 0, 0, False, AnyPropertyType,
3280 &type, &format, &nitems, &after, &data) == Success &&
3281 type != None)
3282 {
3283 XFree (data);
3284 rootwin = TRUE;
3285 }
3286 }
3287
3288 /* Until I find out what window manager the next one is for,
3289 * I'm leaving it commented out. It's supported in the
3290 * xscreensaver sources, though.
3291 */
3292 #if 0
3293 if (!rootwin)
3294 {
3295 if (XGetWindowProperty (gdk_display, win,
3296 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3297 0, 0, False, AnyPropertyType,
3298 &type, &format, &nitems, &data) &&
3299 type != None)
3300 {
3301 XFree (data);
3302 rootwin = TRUE;
3303 }
3304 }
3305 #endif
3306
3307 gdk_error_trap_pop ();
3308
3309 if (rootwin)
3310 {
3311 GDK_NOTE (DND, g_message ("Entering root window\n"));
3312 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3313 return xid;
3314 }
3315 }
3316
3317 *protocol = GDK_DRAG_PROTO_NONE;
3318
3319 return 0; /* a.k.a. None */
3320 }
3321
3322 /**
3323 * gdk_drag_get_protocol_for_display:
3324 * @display: the #GdkDisplay where the destination window resides
3325 * @xid: the windowing system id of the destination window.
3326 * @protocol: location where the supported DND protocol is returned.
3327 * @returns: the windowing system id of the window where the drop should happen. This
3328 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3329 * support Drag and Drop.
3330 *
3331 * Finds out the DND protocol supported by a window.
3332 *
3333 * Since: 2.2
3334 */
3335 GdkNativeWindow
3336 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3337 GdkNativeWindow xid,
3338 GdkDragProtocol *protocol)
3339 {
3340 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3341 }
3342
3343 static GdkWindowCache *
3344 drag_context_find_window_cache (GdkDragContext *context,
3345 GdkScreen *screen)
3346 {
3347 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3348 GSList *tmp_list;
3349 GdkWindowCache *cache;
3350
3351 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3352 {
3353 cache = tmp_list->data;
3354 if (cache->screen == screen)
3355 return cache;
3356 }
3357
3358 cache = gdk_window_cache_get (screen);
3359 private->window_caches = g_slist_prepend (private->window_caches, cache);
3360
3361 return cache;
3362 }
3363
3364 /**
3365 * gdk_drag_find_window_for_screen:
3366 * @context: a #GdkDragContext
3367 * @drag_window: a window which may be at the pointer position, but
3368 * should be ignored, since it is put up by the drag source as an icon.
3369 * @screen: the screen where the destination window is sought.
3370 * @x_root: the x position of the pointer in root coordinates.
3371 * @y_root: the y position of the pointer in root coordinates.
3372 * @dest_window: (out): location to store the destination window in.
3373 * @protocol: (out): location to store the DND protocol in.
3374 *
3375 * Finds the destination window and DND protocol to use at the
3376 * given pointer position.
3377 *
3378 * This function is called by the drag source to obtain the
3379 * @dest_window and @protocol parameters for gdk_drag_motion().
3380 *
3381 * Since: 2.2
3382 **/
3383 void
3384 gdk_drag_find_window_for_screen (GdkDragContext *context,
3385 GdkWindow *drag_window,
3386 GdkScreen *screen,
3387 gint x_root,
3388 gint y_root,
3389 GdkWindow **dest_window,
3390 GdkDragProtocol *protocol)
3391 {
3392 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3393 GdkWindowCache *window_cache;
3394 GdkDisplay *display;
3395 Window dest;
3396
3397 g_return_if_fail (context != NULL);
3398
3399 display = GDK_WINDOW_DISPLAY (context->source_window);
3400
3401 window_cache = drag_context_find_window_cache (context, screen);
3402
3403 dest = get_client_window_at_coords (window_cache,
3404 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3405 GDK_DRAWABLE_XID (drag_window) : None,
3406 x_root, y_root);
3407
3408 if (private->dest_xid != dest)
3409 {
3410 Window recipient;
3411 private->dest_xid = dest;
3412
3413 /* Check if new destination accepts drags, and which protocol */
3414
3415 /* There is some ugliness here. We actually need to pass
3416 * _three_ pieces of information to drag_motion - dest_window,
3417 * protocol, and the XID of the unproxied window. The first
3418 * two are passed explicitely, the third implicitly through
3419 * protocol->dest_xid.
3420 */
3421 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3422 protocol, &private->version)))
3423 {
3424 *dest_window = gdk_window_lookup_for_display (display, recipient);
3425 if (*dest_window)
3426 g_object_ref (*dest_window);
3427 else
3428 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3429 }
3430 else
3431 *dest_window = NULL;
3432 }
3433 else
3434 {
3435 *dest_window = context->dest_window;
3436 if (*dest_window)
3437 g_object_ref (*dest_window);
3438 *protocol = context->protocol;
3439 }
3440 }
3441
3442 /**
3443 * gdk_drag_motion:
3444 * @context: a #GdkDragContext.
3445 * @dest_window: the new destination window, obtained by
3446 * gdk_drag_find_window().
3447 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3448 * @x_root: the x position of the pointer in root coordinates.
3449 * @y_root: the y position of the pointer in root coordinates.
3450 * @suggested_action: the suggested action.
3451 * @possible_actions: the possible actions.
3452 * @time_: the timestamp for this operation.
3453 *
3454 * Updates the drag context when the pointer moves or the
3455 * set of actions changes.
3456 *
3457 * This function is called by the drag source.
3458 *
3459 * Return value: FIXME
3460 **/
3461 gboolean
3462 gdk_drag_motion (GdkDragContext *context,
3463 GdkWindow *dest_window,
3464 GdkDragProtocol protocol,
3465 gint x_root,
3466 gint y_root,
3467 GdkDragAction suggested_action,
3468 GdkDragAction possible_actions,
3469 guint32 time)
3470 {
3471 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3472
3473 g_return_val_if_fail (context != NULL, FALSE);
3474 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3475
3476 private->old_actions = context->actions;
3477 context->actions = possible_actions;
3478
3479 if (private->old_actions != possible_actions)
3480 private->xdnd_actions_set = FALSE;
3481
3482 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3483 {
3484 /* This ugly hack is necessary since GTK+ doesn't know about
3485 * the XDND protocol version, and in particular doesn't know
3486 * that gdk_drag_find_window_for_screen() has the side-effect
3487 * of setting private->version, and therefore sometimes call
3488 * gdk_drag_motion() without a prior call to
3489 * gdk_drag_find_window_for_screen(). This happens, e.g.
3490 * when GTK+ is proxying DND events to embedded windows.
3491 */
3492 if (dest_window)
3493 {
3494 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3495
3496 xdnd_check_dest (display,
3497 GDK_DRAWABLE_XID (dest_window),
3498 &private->version);
3499 }
3500 }
3501
3502 /* When we have a Xdnd target, make sure our XdndActionList
3503 * matches the current actions;
3504 */
3505 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3506 {
3507 if (dest_window)
3508 {
3509 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3510 xdnd_set_actions (context);
3511 else if (context->dest_window == dest_window)
3512 {
3513 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3514 GdkDragContext *dest_context;
3515
3516 dest_context = gdk_drag_context_find (display, FALSE,
3517 GDK_DRAWABLE_XID (context->source_window),
3518 GDK_DRAWABLE_XID (dest_window));
3519
3520 if (dest_context)
3521 {
3522 dest_context->actions = context->actions;
3523 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3524 }
3525 }
3526 }
3527 }
3528
3529 if (context->dest_window != dest_window)
3530 {
3531 GdkEvent temp_event;
3532
3533 /* Send a leave to the last destination */
3534 gdk_drag_do_leave (context, time);
3535 private->drag_status = GDK_DRAG_STATUS_DRAG;
3536
3537 /* Check if new destination accepts drags, and which protocol */
3538
3539 if (dest_window)
3540 {
3541 context->dest_window = dest_window;
3542 private->drop_xid = private->dest_xid;
3543 g_object_ref (context->dest_window);
3544 context->protocol = protocol;
3545
3546 switch (protocol)
3547 {
3548 case GDK_DRAG_PROTO_MOTIF:
3549 motif_send_enter (context, time);
3550 break;
3551
3552 case GDK_DRAG_PROTO_XDND:
3553 xdnd_send_enter (context);
3554 break;
3555
3556 case GDK_DRAG_PROTO_ROOTWIN:
3557 case GDK_DRAG_PROTO_NONE:
3558 default:
3559 break;
3560 }
3561 private->old_action = suggested_action;
3562 context->suggested_action = suggested_action;
3563 private->old_actions = possible_actions;
3564 }
3565 else
3566 {
3567 context->dest_window = NULL;
3568 private->drop_xid = None;
3569 context->action = 0;
3570 }
3571
3572 /* Push a status event, to let the client know that
3573 * the drag changed
3574 */
3575
3576 temp_event.dnd.type = GDK_DRAG_STATUS;
3577 temp_event.dnd.window = context->source_window;
3578 /* We use this to signal a synthetic status. Perhaps
3579 * we should use an extra field...
3580 */
3581 temp_event.dnd.send_event = TRUE;
3582
3583 temp_event.dnd.context = context;
3584 temp_event.dnd.time = time;
3585
3586 gdk_event_put (&temp_event);
3587 }
3588 else
3589 {
3590 private->old_action = context->suggested_action;
3591 context->suggested_action = suggested_action;
3592 }
3593
3594 /* Send a drag-motion event */
3595
3596 private->last_x = x_root;
3597 private->last_y = y_root;
3598
3599 if (context->dest_window)
3600 {
3601 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3602 {
3603 switch (context->protocol)
3604 {
3605 case GDK_DRAG_PROTO_MOTIF:
3606 motif_send_motion (context, x_root, y_root, suggested_action, time);
3607 break;
3608
3609 case GDK_DRAG_PROTO_XDND:
3610 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3611 break;
3612
3613 case GDK_DRAG_PROTO_ROOTWIN:
3614 {
3615 GdkEvent temp_event;
3616 /* GTK+ traditionally has used application/x-rootwin-drop,
3617 * but the XDND spec specifies x-rootwindow-drop.
3618 */
3619 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3620 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3621
3622 if (g_list_find (context->targets,
3623 GDK_ATOM_TO_POINTER (target1)) ||
3624 g_list_find (context->targets,
3625 GDK_ATOM_TO_POINTER (target2)))
3626 context->action = context->suggested_action;
3627 else
3628 context->action = 0;
3629
3630 temp_event.dnd.type = GDK_DRAG_STATUS;
3631 temp_event.dnd.window = context->source_window;
3632 temp_event.dnd.send_event = FALSE;
3633 temp_event.dnd.context = context;
3634 temp_event.dnd.time = time;
3635
3636 gdk_event_put (&temp_event);
3637 }
3638 break;
3639 case GDK_DRAG_PROTO_NONE:
3640 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3641 break;
3642 default:
3643 break;
3644 }
3645 }
3646 else
3647 return TRUE;
3648 }
3649
3650 return FALSE;
3651 }
3652
3653 /**
3654 * gdk_drag_drop:
3655 * @context: a #GdkDragContext.
3656 * @time_: the timestamp for this operation.
3657 *
3658 * Drops on the current destination.
3659 *
3660 * This function is called by the drag source.
3661 **/
3662 void
3663 gdk_drag_drop (GdkDragContext *context,
3664 guint32 time)
3665 {
3666 g_return_if_fail (context != NULL);
3667
3668 if (context->dest_window)
3669 {
3670 switch (context->protocol)
3671 {
3672 case GDK_DRAG_PROTO_MOTIF:
3673 motif_send_leave (context, time);
3674 motif_send_drop (context, time);
3675 break;
3676
3677 case GDK_DRAG_PROTO_XDND:
3678 xdnd_send_drop (context, time);
3679 break;
3680
3681 case GDK_DRAG_PROTO_ROOTWIN:
3682 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3683 break;
3684 case GDK_DRAG_PROTO_NONE:
3685 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3686 break;
3687 default:
3688 break;
3689 }
3690 }
3691 }
3692
3693 /**
3694 * gdk_drag_abort:
3695 * @context: a #GdkDragContext.
3696 * @time_: the timestamp for this operation.
3697 *
3698 * Aborts a drag without dropping.
3699 *
3700 * This function is called by the drag source.
3701 **/
3702 void
3703 gdk_drag_abort (GdkDragContext *context,
3704 guint32 time)
3705 {
3706 g_return_if_fail (context != NULL);
3707
3708 gdk_drag_do_leave (context, time);
3709 }
3710
3711 /* Destination side */
3712
3713 /**
3714 * gdk_drag_status:
3715 * @context: a #GdkDragContext.
3716 * @action: the selected action which will be taken when a drop happens,
3717 * or 0 to indicate that a drop will not be accepted.
3718 * @time_: the timestamp for this operation.
3719 *
3720 * Selects one of the actions offered by the drag source.
3721 *
3722 * This function is called by the drag destination in response to
3723 * gdk_drag_motion() called by the drag source.
3724 **/
3725 void
3726 gdk_drag_status (GdkDragContext *context,
3727 GdkDragAction action,
3728 guint32 time)
3729 {
3730 GdkDragContextPrivateX11 *private;
3731 XEvent xev;
3732 GdkDisplay *display;
3733
3734 g_return_if_fail (context != NULL);
3735
3736 private = PRIVATE_DATA (context);
3737 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3738
3739 context->action = action;
3740
3741 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3742 {
3743 gboolean need_coords = FALSE;
3744
3745 xev.xclient.type = ClientMessage;
3746 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3747 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3748 xev.xclient.format = 8;
3749 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3750
3751 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3752 {
3753 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3754 }
3755 else
3756 {
3757 if ((action != 0) != (private->old_action != 0))
3758 {
3759 if (action != 0)
3760 {
3761 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3762 need_coords = TRUE;
3763 }
3764 else
3765 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3766 }
3767 else
3768 {
3769 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3770 need_coords = TRUE;
3771 }
3772 }
3773
3774 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3775
3776 switch (action)
3777 {
3778 case GDK_ACTION_MOVE:
3779 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3780 break;
3781 case GDK_ACTION_COPY:
3782 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3783 break;
3784 case GDK_ACTION_LINK:
3785 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3786 break;
3787 default:
3788 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3789 break;
3790 }
3791
3792 if (action)
3793 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3794 else
3795 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3796
3797 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3798
3799 if (need_coords)
3800 {
3801 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3802 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3803 }
3804 else
3805 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3806
3807 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3808 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3809
3810 if (!_gdk_send_xevent (display,
3811 GDK_DRAWABLE_XID (context->source_window),
3812 FALSE, 0, &xev))
3813 GDK_NOTE (DND,
3814 g_message ("Send event to %lx failed",
3815 GDK_DRAWABLE_XID (context->source_window)));
3816 }
3817 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3818 {
3819 xev.xclient.type = ClientMessage;
3820 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3821 xev.xclient.format = 32;
3822 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3823
3824 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3825 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3826 xev.xclient.data.l[2] = 0;
3827 xev.xclient.data.l[3] = 0;
3828 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3829
3830 if (!xdnd_send_xevent (context, context->source_window,
3831 FALSE, &xev))
3832 GDK_NOTE (DND,
3833 g_message ("Send event to %lx failed",
3834 GDK_DRAWABLE_XID (context->source_window)));
3835 }
3836
3837 private->old_action = action;
3838 }
3839
3840 /**
3841 * gdk_drop_reply:
3842 * @context: a #GdkDragContext.
3843 * @ok: %TRUE if the drop is accepted.
3844 * @time_: the timestamp for this operation.
3845 *
3846 * Accepts or rejects a drop.
3847 *
3848 * This function is called by the drag destination in response
3849 * to a drop initiated by the drag source.
3850 **/
3851 void
3852 gdk_drop_reply (GdkDragContext *context,
3853 gboolean ok,
3854 guint32 time)
3855 {
3856 GdkDragContextPrivateX11 *private;
3857
3858 g_return_if_fail (context != NULL);
3859
3860 private = PRIVATE_DATA (context);
3861
3862 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3863 {
3864 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3865 XEvent xev;
3866
3867 xev.xclient.type = ClientMessage;
3868 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3869 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3870 xev.xclient.format = 8;
3871
3872 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3873 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3874 if (ok)
3875 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3876 (XmDROP_SITE_VALID << 4) |
3877 (XmDROP_NOOP << 8) |
3878 (XmDROP << 12);
3879 else
3880 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3881 (XmNO_DROP_SITE << 4) |
3882 (XmDROP_NOOP << 8) |
3883 (XmDROP_CANCEL << 12);
3884 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3885 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3886 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3887 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3888 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3889
3890 _gdk_send_xevent (display,
3891 GDK_DRAWABLE_XID (context->source_window),
3892 FALSE, 0, &xev);
3893 }
3894 }
3895
3896 /**
3897 * gdk_drop_finish:
3898 * @context: a #GtkDragContext.
3899 * @success: %TRUE if the data was successfully received.
3900 * @time_: the timestamp for this operation.
3901 *
3902 * Ends the drag operation after a drop.
3903 *
3904 * This function is called by the drag destination.
3905 **/
3906 void
3907 gdk_drop_finish (GdkDragContext *context,
3908 gboolean success,
3909 guint32 time)
3910 {
3911 g_return_if_fail (context != NULL);
3912
3913 if (context->protocol == GDK_DRAG_PROTO_XDND)
3914 {
3915 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3916 XEvent xev;
3917
3918 xev.xclient.type = ClientMessage;
3919 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3920 xev.xclient.format = 32;
3921 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3922
3923 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3924 if (success)
3925 {
3926 xev.xclient.data.l[1] = 1;
3927 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3928 context->action);
3929 }
3930 else
3931 {
3932 xev.xclient.data.l[1] = 0;
3933 xev.xclient.data.l[2] = None;
3934 }
3935 xev.xclient.data.l[3] = 0;
3936 xev.xclient.data.l[4] = 0;
3937
3938 if (!xdnd_send_xevent (context, context->source_window,
3939 FALSE, &xev))
3940 GDK_NOTE (DND,
3941 g_message ("Send event to %lx failed",
3942 GDK_DRAWABLE_XID (context->source_window)));
3943 }
3944 }
3945
3946
3947 void
3948 gdk_window_register_dnd (GdkWindow *window)
3949 {
3950 static const gulong xdnd_version = 5;
3951 MotifDragReceiverInfo info;
3952 Atom motif_drag_receiver_info_atom;
3953 GdkDisplay *display = gdk_drawable_get_display (window);
3954
3955 g_return_if_fail (window != NULL);
3956
3957 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3958 return;
3959
3960 base_precache_atoms (display);
3961
3962 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3963 return;
3964 else
3965 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3966
3967 /* Set Motif drag receiver information property */
3968
3969 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3970 "_MOTIF_DRAG_RECEIVER_INFO");
3971 /* initialize to zero to avoid writing uninitialized data to socket */
3972 memset(&info, 0, sizeof(info));
3973 info.byte_order = local_byte_order;
3974 info.protocol_version = 0;
3975 info.protocol_style = XmDRAG_DYNAMIC;
3976 info.proxy_window = None;
3977 info.num_drop_sites = 0;
3978 info.total_size = sizeof(info);
3979
3980 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3981 motif_drag_receiver_info_atom,
3982 motif_drag_receiver_info_atom,
3983 8, PropModeReplace,
3984 (guchar *)&info,
3985 sizeof (info));
3986
3987 /* Set XdndAware */
3988
3989 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3990 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3991 GDK_DRAWABLE_XID (window),
3992 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3993 XA_ATOM, 32, PropModeReplace,
3994 (guchar *)&xdnd_version, 1);
3995 }
3996
3997 /**
3998 * gdk_drag_get_selection:
3999 * @context: a #GdkDragContext.
4000 *
4001 * Returns the selection atom for the current source window.
4002 *
4003 * Return value: the selection atom.
4004 **/
4005 GdkAtom
4006 gdk_drag_get_selection (GdkDragContext *context)
4007 {
4008 g_return_val_if_fail (context != NULL, GDK_NONE);
4009
4010 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
4011 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
4012 (PRIVATE_DATA (context))->motif_selection);
4013 else if (context->protocol == GDK_DRAG_PROTO_XDND)
4014 return gdk_atom_intern_static_string ("XdndSelection");
4015 else
4016 return GDK_NONE;
4017 }
4018
4019 /**
4020 * gdk_drag_drop_succeeded:
4021 * @context: a #GdkDragContext
4022 *
4023 * Returns whether the dropped data has been successfully
4024 * transferred. This function is intended to be used while
4025 * handling a %GDK_DROP_FINISHED event, its return value is
4026 * meaningless at other times.
4027 *
4028 * Return value: %TRUE if the drop was successful.
4029 *
4030 * Since: 2.6
4031 **/
4032 gboolean
4033 gdk_drag_drop_succeeded (GdkDragContext *context)
4034 {
4035 GdkDragContextPrivateX11 *private;
4036
4037 g_return_val_if_fail (context != NULL, FALSE);
4038
4039 private = PRIVATE_DATA (context);
4040
4041 return !private->drop_failed;
4042 }
4043
4044 #define __GDK_DND_X11_C__
4045 #include "gdkaliasdef.c"