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