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