]> Pileus Git - ~andy/gtk/blobdiff - gdk/x11/gdkdnd-x11.c
x11: Use window macros instead of drawable macros
[~andy/gtk] / gdk / x11 / gdkdnd-x11.c
index 101d5590ad68a2858a988579cee30b7cbc7074e4..8927d5ef819f7d6eca13b48c38fa6467d7cb33a7 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include <config.h>
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-#include <string.h>
+#include "config.h"
+
+#include "gdkdnd.h"
 
-#include "gdk.h"          /* For gdk_flush() */
+#include "gdkmain.h"
 #include "gdkx.h"
 #include "gdkasync.h"
-#include "gdkdnd.h"
 #include "gdkproperty.h"
 #include "gdkprivate-x11.h"
 #include "gdkinternals.h"
 #include "gdkscreen-x11.h"
 #include "gdkdisplay-x11.h"
-#include "gdkalias.h"
+
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <X11/Xatom.h>
+#include <X11/extensions/shape.h>
+#include <X11/extensions/Xcomposite.h>
+
+#include <string.h>
 
 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
 
@@ -53,6 +58,9 @@ typedef struct {
   guint32 xid;
   gint x, y, width, height;
   gboolean mapped;
+  gboolean shape_selected;
+  gboolean shape_valid;
+  cairo_region_t *shape;
 } GdkCacheChild;
 
 typedef struct {
@@ -89,6 +97,7 @@ struct _GdkDragContextPrivateX11 {
   guint version;                /* Xdnd protocol version */
 
   GSList *window_caches;
+  GdkDevice *device;
 };
 
 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
@@ -130,7 +139,7 @@ static void gdk_drag_context_finalize   (GObject              *object);
 
 static GList *contexts;
 
-const static struct {
+static const struct {
   const char *atom_name;
   GdkFilterFunc func;
 } xdnd_filters[] = {
@@ -142,7 +151,7 @@ const static struct {
   { "XdndDrop",     xdnd_drop_filter },
 };
              
-G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT);
+G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
 
 static void
 gdk_drag_context_init (GdkDragContext *dragcontext)
@@ -214,31 +223,52 @@ gdk_drag_context_new (void)
 }
 
 /**
- * gdk_drag_context_ref:
- * @context: a #GdkDragContext.
- * 
- * Deprecated function; use g_object_ref() instead.
+ * gdk_drag_context_set_device:
+ * @context: a #GdkDragContext
+ * @device: a #GdkDevice
+ *
+ * Associates a #GdkDevice to @context, so all Drag and Drop events
+ * for @context are emitted as if they came from this device.
  **/
-void            
-gdk_drag_context_ref (GdkDragContext *context)
+void
+gdk_drag_context_set_device (GdkDragContext *context,
+                             GdkDevice      *device)
 {
+  GdkDragContextPrivateX11 *private;
+
   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
+  g_return_if_fail (GDK_IS_DEVICE (device));
 
-  g_object_ref (context);
+  private = PRIVATE_DATA (context);
+
+  if (private->device)
+    {
+      g_object_unref (private->device);
+      private->device = NULL;
+    }
+
+  if (device)
+    private->device = g_object_ref (device);
 }
 
 /**
- * gdk_drag_context_unref:
- * @context: a #GdkDragContext.
- * 
- * Deprecated function; use g_object_unref() instead.
+ * gdk_drag_context_get_device:
+ * @context: a #GdkDragContext
+ *
+ * Returns the #GdkDevice associated to the drag context.
+ *
+ * Returns: (transfer none): The #GdkDevice associated to @context.
  **/
-void            
-gdk_drag_context_unref (GdkDragContext *context)
+GdkDevice *
+gdk_drag_context_get_device (GdkDragContext *context)
 {
-  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
+  GdkDragContextPrivateX11 *private;
 
-  g_object_unref (context);
+  g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
+
+  private = PRIVATE_DATA (context);
+
+  return private->device;
 }
 
 static GdkDragContext *
@@ -257,19 +287,19 @@ gdk_drag_context_find (GdkDisplay *display,
       context = (GdkDragContext *)tmp_list->data;
       private = PRIVATE_DATA (context);
 
-      if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
-         (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
+      if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
+         (context->dest_window && gdk_window_get_display (context->dest_window) != display))
        continue;
 
       context_dest_xid = context->dest_window ? 
                             (private->drop_xid ?
                               private->drop_xid :
-                              GDK_DRAWABLE_XID (context->dest_window)) :
+                              GDK_WINDOW_XID (context->dest_window)) :
                             None;
 
       if ((!context->is_source == !is_source) &&
          ((source_xid == None) || (context->source_window &&
-           (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
+           (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
          ((dest_xid == None) || (context_dest_xid == dest_xid)))
        return context;
       
@@ -304,6 +334,23 @@ precache_target_list (GdkDragContext *context)
 
 /* Utility functions */
 
+static void
+free_cache_child (GdkCacheChild *child,
+                  GdkDisplay    *display)
+{
+  if (child->shape)
+    cairo_region_destroy (child->shape);
+
+  if (child->shape_selected && display)
+    {
+      GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
+
+      XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
+    }
+
+  g_free (child);
+}
+
 static void
 gdk_window_cache_add (GdkWindowCache *cache,
                      guint32 xid,
@@ -318,12 +365,50 @@ gdk_window_cache_add (GdkWindowCache *cache,
   child->width = width;
   child->height = height;
   child->mapped = mapped;
+  child->shape_selected = FALSE;
+  child->shape_valid = FALSE;
+  child->shape = NULL;
 
   cache->children = g_list_prepend (cache->children, child);
   g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid), 
                       cache->children);
 }
 
+static GdkFilterReturn
+gdk_window_cache_shape_filter (GdkXEvent *xev,
+                               GdkEvent  *event,
+                               gpointer   data)
+{
+  XEvent *xevent = (XEvent *)xev;
+  GdkWindowCache *cache = data;
+
+  GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
+
+  if (display->have_shapes &&
+      xevent->type == display->shape_event_base + ShapeNotify)
+    {
+      XShapeEvent *xse = (XShapeEvent*)xevent;
+      GList *node;
+
+      node = g_hash_table_lookup (cache->child_hash,
+                                  GUINT_TO_POINTER (xse->window));
+      if (node)
+        {
+          GdkCacheChild *child = node->data;
+          child->shape_valid = FALSE;
+          if (child->shape)
+            {
+              cairo_region_destroy (child->shape);
+              child->shape = NULL;
+            }
+        }
+
+      return GDK_FILTER_REMOVE;
+    }
+
+  return GDK_FILTER_CONTINUE;
+}
+
 static GdkFilterReturn
 gdk_window_cache_filter (GdkXEvent *xev,
                         GdkEvent  *event,
@@ -399,10 +484,13 @@ gdk_window_cache_filter (GdkXEvent *xev,
                                    GUINT_TO_POINTER (xdwe->window));
        if (node) 
          {
+           GdkCacheChild *child = node->data;
+
            g_hash_table_remove (cache->child_hash,
                                 GUINT_TO_POINTER (xdwe->window));
            cache->children = g_list_remove_link (cache->children, node);
-           g_free (node->data);
+           /* window is destroyed, no need to disable ShapeNotify */
+           free_cache_child (child, NULL);
            g_list_free_1 (node);
          }
        break;
@@ -430,7 +518,7 @@ gdk_window_cache_filter (GdkXEvent *xev,
 
        node = g_hash_table_lookup (cache->child_hash, 
                                    GUINT_TO_POINTER (xume->window));
-       if (node) 
+       if (node)
          {
            GdkCacheChild *child = node->data;
            child->mapped = FALSE;
@@ -451,6 +539,7 @@ gdk_window_cache_new (GdkScreen *screen)
   GdkWindow *root_window = gdk_screen_get_root_window (screen);
   GdkChildInfoX11 *children;
   guint nchildren, i;
+  Window cow;
   
   GdkWindowCache *result = g_new (GdkWindowCache, 1);
 
@@ -460,9 +549,29 @@ gdk_window_cache_new (GdkScreen *screen)
 
   XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
   result->old_event_mask = xwa.your_event_mask;
+
+  if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client)) 
+    {
+      GList *toplevel_windows, *list;
+      GdkWindow *window;
+      gint x, y, width, height;
+      
+      toplevel_windows = gdk_screen_get_toplevel_windows (screen);
+      for (list = toplevel_windows; list; list = list->next) {
+       window = GDK_WINDOW (list->data);
+       gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
+       gdk_window_cache_add (result, GDK_WINDOW_XID (window), 
+                             x, y, width, height, 
+                             gdk_window_is_visible (window));
+      }
+      g_list_free (toplevel_windows);
+      return result;
+    }
+
   XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
                result->old_event_mask | SubstructureNotifyMask);
   gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
+  gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
 
   if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
                                       GDK_WINDOW_XWINDOW (root_window),
@@ -479,6 +588,22 @@ gdk_window_cache_new (GdkScreen *screen)
 
   g_free (children);
 
+#ifdef HAVE_XCOMPOSITE
+  /*
+   * Add the composite overlay window to the cache, as this can be a reasonable
+   * Xdnd proxy as well.
+   * This is only done when the screen is composited in order to avoid mapping
+   * the COW. We assume that the CM is using the COW (which is true for pretty
+   * much any CM currently in use).
+   */
+  if (gdk_screen_is_composited (screen))
+    {
+      cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XWINDOW (root_window));
+      gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
+      XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XWINDOW (root_window));
+    }
+#endif
+
   return result;
 }
 
@@ -486,19 +611,67 @@ static void
 gdk_window_cache_destroy (GdkWindowCache *cache)
 {
   GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
-  
-  XSelectInput (GDK_WINDOW_XDISPLAY (root_window), 
+
+  XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
                GDK_WINDOW_XWINDOW (root_window),
                cache->old_event_mask);
   gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
+  gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
+
+  gdk_error_trap_push ();
+
+  g_list_foreach (cache->children, (GFunc)free_cache_child,
+      gdk_screen_get_display (cache->screen));
+
+  gdk_error_trap_pop_ignored ();
 
-  g_list_foreach (cache->children, (GFunc)g_free, NULL);
   g_list_free (cache->children);
   g_hash_table_destroy (cache->child_hash);
 
   g_free (cache);
 }
 
+static gboolean
+is_pointer_within_shape (GdkDisplay    *display,
+                         GdkCacheChild *child,
+                         gint           x_pos,
+                         gint           y_pos)
+{
+  if (!child->shape_selected)
+    {
+      GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
+
+      XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
+      child->shape_selected = TRUE;
+    }
+  if (!child->shape_valid)
+    {
+      GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
+      cairo_region_t *input_shape;
+
+      child->shape = _xwindow_get_shape (display_x11->xdisplay,
+                                         child->xid, ShapeBounding);
+#ifdef ShapeInput
+      input_shape = _xwindow_get_shape (display_x11->xdisplay,
+                                        child->xid, ShapeInput);
+      if (child->shape && input_shape)
+        {
+          cairo_region_intersect (child->shape, input_shape);
+          cairo_region_destroy (input_shape);
+        }
+      else if (input_shape)
+        {
+          child->shape = input_shape;
+        }
+#endif
+
+      child->shape_valid = TRUE;
+    }
+
+  return child->shape == NULL ||
+         cairo_region_contains_point (child->shape, x_pos, y_pos);
+}
+
 static Window
 get_client_window_at_coords_recurse (GdkDisplay *display,
                                     Window      win,
@@ -571,23 +744,32 @@ get_client_window_at_coords (GdkWindowCache *cache,
       GdkCacheChild *child = tmp_list->data;
 
       if ((child->xid != ignore) && (child->mapped))
-       {
-         if ((x_root >= child->x) && (x_root < child->x + child->width) &&
-             (y_root >= child->y) && (y_root < child->y + child->height))
-           {
-             retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
-                                                           child->xid, TRUE,
-                                                           x_root - child->x,
-                                                           y_root - child->y);
-             if (!retval)
-               retval = child->xid;
-           }
-         
-       }
+        {
+          if ((x_root >= child->x) && (x_root < child->x + child->width) &&
+              (y_root >= child->y) && (y_root < child->y + child->height))
+            {
+              GdkDisplay *display = gdk_screen_get_display (cache->screen);
+
+              if (!is_pointer_within_shape (display, child,
+                                            x_root - child->x,
+                                            y_root - child->y))
+                {
+                  tmp_list = tmp_list->next;
+                  continue;
+                }
+
+              retval = get_client_window_at_coords_recurse (display,
+                  child->xid, TRUE,
+                  x_root - child->x,
+                  y_root - child->y);
+              if (!retval)
+                retval = child->xid;
+            }
+        }
       tmp_list = tmp_list->next;
     }
 
-  gdk_error_trap_pop ();
+  gdk_error_trap_pop_ignored ();
   
   if (retval)
     return retval;
@@ -1204,7 +1386,7 @@ motif_set_targets (GdkDragContext *context)
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
   MotifDragInitiatorInfo info;
   gint i;
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   
   info.byte_order = local_byte_order;
   info.protocol_version = 0;
@@ -1223,8 +1405,8 @@ motif_set_targets (GdkDragContext *context)
 
   info.selection_atom = private->motif_selection;
 
-  XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
-                  GDK_DRAWABLE_XID (context->source_window),
+  XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
+                  GDK_WINDOW_XID (context->source_window),
                   private->motif_selection,
                   gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
                   8, PropModeReplace,
@@ -1284,19 +1466,22 @@ motif_send_enter (GdkDragContext  *context,
                  guint32          time)
 {
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   XEvent xev;
 
+  if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
+    return; /* Motif Dnd requires getting properties on the root window */
+
   xev.xclient.type = ClientMessage;
   xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
   xev.xclient.format = 8;
-  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
+  xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
 
   MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
   MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
   MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
   MOTIF_XCLIENT_LONG (&xev, 1) = time;
-  MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
+  MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
 
   if (!private->motif_targets_set)
     motif_set_targets (context);
@@ -1305,24 +1490,24 @@ motif_send_enter (GdkDragContext  *context,
   MOTIF_XCLIENT_LONG (&xev, 4) = 0;
 
   if (!_gdk_send_xevent (display,
-                        GDK_DRAWABLE_XID (context->dest_window),
+                        GDK_WINDOW_XID (context->dest_window),
                         FALSE, 0, &xev))
     GDK_NOTE (DND, 
              g_message ("Send event to %lx failed",
-                        GDK_DRAWABLE_XID (context->dest_window)));
+                        GDK_WINDOW_XID (context->dest_window)));
 }
 
 static void
 motif_send_leave (GdkDragContext  *context,
                  guint32          time)
 {
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   XEvent xev;
 
   xev.xclient.type = ClientMessage;
   xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
   xev.xclient.format = 8;
-  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
+  xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
 
   MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
   MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
@@ -1333,11 +1518,11 @@ motif_send_leave (GdkDragContext  *context,
   MOTIF_XCLIENT_LONG (&xev, 4) = 0;
 
   if (!_gdk_send_xevent (display,
-                        GDK_DRAWABLE_XID (context->dest_window),
+                        GDK_WINDOW_XID (context->dest_window),
                         FALSE, 0, &xev))
     GDK_NOTE (DND, 
              g_message ("Send event to %lx failed",
-                        GDK_DRAWABLE_XID (context->dest_window)));
+                        GDK_WINDOW_XID (context->dest_window)));
 }
 
 static gboolean
@@ -1348,14 +1533,14 @@ motif_send_motion (GdkDragContext  *context,
                    guint32         time)
 {
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   gboolean retval;
   XEvent xev;
 
   xev.xclient.type = ClientMessage;
   xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
   xev.xclient.format = 8;
-  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
+  xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
 
   MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
   MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
@@ -1383,11 +1568,11 @@ motif_send_motion (GdkDragContext  *context,
     }
 
   if (!_gdk_send_xevent (display,
-                        GDK_DRAWABLE_XID (context->dest_window),
+                        GDK_WINDOW_XID (context->dest_window),
                         FALSE, 0, &xev))
     GDK_NOTE (DND, 
              g_message ("Send event to %lx failed",
-                        GDK_DRAWABLE_XID (context->dest_window)));
+                        GDK_WINDOW_XID (context->dest_window)));
 
   return retval;
 }
@@ -1396,13 +1581,13 @@ static void
 motif_send_drop (GdkDragContext *context, guint32 time)
 {
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   XEvent xev;
 
   xev.xclient.type = ClientMessage;
   xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
   xev.xclient.format = 8;
-  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
+  xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
 
   MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
   MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
@@ -1413,14 +1598,14 @@ motif_send_drop (GdkDragContext *context, guint32 time)
   MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
 
   MOTIF_XCLIENT_LONG (&xev, 3)  = private->motif_selection;
-  MOTIF_XCLIENT_LONG (&xev, 4)  = GDK_DRAWABLE_XID (context->source_window);
+  MOTIF_XCLIENT_LONG (&xev, 4)  = GDK_WINDOW_XID (context->source_window);
 
   if (!_gdk_send_xevent (display,
-                        GDK_DRAWABLE_XID (context->dest_window),
+                        GDK_WINDOW_XID (context->dest_window),
                         FALSE, 0, &xev))
     GDK_NOTE (DND, 
              g_message ("Send event to %lx failed",
-                        GDK_DRAWABLE_XID (context->dest_window)));
+                        GDK_WINDOW_XID (context->dest_window)));
 }
 
 /* Target Side */
@@ -1501,7 +1686,7 @@ motif_drag_context_new (GdkWindow *dest_window,
 {
   GdkDragContext *new_context;
   GdkDragContextPrivateX11 *private;
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
 
   /* FIXME, current_dest_drag really shouldn't be NULL'd
@@ -1568,7 +1753,7 @@ motif_top_level_enter (GdkEvent *event,
                       guint32   source_window, 
                       guint32   atom)
 {
-  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
+  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
   GdkDragContext *new_context;
 
   GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
@@ -1592,7 +1777,7 @@ motif_top_level_leave (GdkEvent *event,
                       guint16   flags, 
                       guint32   timestamp)
 {
-  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
+  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
 
   GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
                           flags, timestamp));
@@ -1621,7 +1806,7 @@ motif_motion (GdkEvent *event,
              gint16    y_root)
 {
   GdkDragContextPrivateX11 *private;
-  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
+  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
   
   GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
                           flags, timestamp, x_root, y_root));
@@ -1660,7 +1845,7 @@ motif_operation_changed (GdkEvent *event,
                         guint32   timestamp)
 {
   GdkDragContextPrivateX11 *private;
-  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
+  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
   GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
                           flags, timestamp));
 
@@ -1699,7 +1884,7 @@ motif_drop_start (GdkEvent *event,
                  gint16    y_root)
 {
   GdkDragContext *new_context;
-  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
+  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
 
   GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
                           flags, timestamp, x_root, y_root, source_window, atom));
@@ -1735,11 +1920,11 @@ motif_drag_status (GdkEvent *event,
   GDK_NOTE (DND, 
            g_message ("Motif status message: flags %x", flags));
 
-  display = gdk_drawable_get_display (event->any.window);
+  display = gdk_window_get_display (event->any.window);
   if (!display)
     return GDK_FILTER_REMOVE;
   
-  context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
+  context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
 
   if (context)
     {
@@ -1897,9 +2082,14 @@ static GdkDragAction
 xdnd_action_from_atom (GdkDisplay *display,
                       Atom        xatom)
 {
-  GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
+  GdkAtom atom;
   gint i;
 
+  if (xatom == None)
+    return 0;
+
+  atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
+
   if (!xdnd_actions_initialized)
     xdnd_initialize_actions();
 
@@ -1948,7 +2138,7 @@ xdnd_status_filter (GdkXEvent *xev,
            g_message ("XdndStatus: dest_window: %#x  action: %ld",
                       dest_window, action));
 
-  display = gdk_drawable_get_display (event->any.window);
+  display = gdk_window_get_display (event->any.window);
   context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
   
   if (context)
@@ -1960,6 +2150,7 @@ xdnd_status_filter (GdkXEvent *xev,
       event->dnd.send_event = FALSE;
       event->dnd.type = GDK_DRAG_STATUS;
       event->dnd.context = context;
+      gdk_event_set_device (event, gdk_drag_context_get_device (context));
       g_object_ref (context);
 
       event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
@@ -1996,7 +2187,7 @@ xdnd_finished_filter (GdkXEvent *xev,
   GDK_NOTE (DND, 
            g_message ("XdndFinished: dest_window: %#x", dest_window));
 
-  display = gdk_drawable_get_display (event->any.window);
+  display = gdk_window_get_display (event->any.window);
   context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
   
   if (context)
@@ -2007,6 +2198,7 @@ xdnd_finished_filter (GdkXEvent *xev,
       
       event->dnd.type = GDK_DROP_FINISHED;
       event->dnd.context = context;
+      gdk_event_set_device (event, gdk_drag_context_get_device (context));
       g_object_ref (context);
 
       event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
@@ -2025,7 +2217,7 @@ xdnd_set_targets (GdkDragContext *context)
   GList *tmp_list = context->targets;
   gint i;
   gint n_atoms = g_list_length (context->targets);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
 
   atomlist = g_new (Atom, n_atoms);
   i = 0;
@@ -2036,8 +2228,8 @@ xdnd_set_targets (GdkDragContext *context)
       i++;
     }
 
-  XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
-                  GDK_DRAWABLE_XID (context->source_window),
+  XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
+                  GDK_WINDOW_XID (context->source_window),
                   gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
                   XA_ATOM, 32, PropModeReplace,
                   (guchar *)atomlist, n_atoms);
@@ -2055,7 +2247,7 @@ xdnd_set_actions (GdkDragContext *context)
   gint i;
   gint n_atoms;
   guint actions;
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
 
   if (!xdnd_actions_initialized)
     xdnd_initialize_actions();
@@ -2085,8 +2277,8 @@ xdnd_set_actions (GdkDragContext *context)
        }
     }
 
-  XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
-                  GDK_DRAWABLE_XID (context->source_window),
+  XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
+                  GDK_WINDOW_XID (context->source_window),
                   gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
                   XA_ATOM, 32, PropModeReplace,
                   (guchar *)atomlist, n_atoms);
@@ -2114,7 +2306,7 @@ send_client_message_async_cb (Window   window,
       context->dest_window &&
       window == GDK_WINDOW_XID (context->dest_window))
     {
-      GdkEvent temp_event;
+      GdkEvent *temp_event;
       GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
 
       g_object_unref (context->dest_window);
@@ -2123,13 +2315,16 @@ send_client_message_async_cb (Window   window,
 
       private->drag_status = GDK_DRAG_STATUS_DRAG;
 
-      temp_event.dnd.type = GDK_DRAG_STATUS;
-      temp_event.dnd.window = context->source_window;
-      temp_event.dnd.send_event = TRUE;
-      temp_event.dnd.context = context;
-      temp_event.dnd.time = GDK_CURRENT_TIME;
+      temp_event = gdk_event_new (GDK_DRAG_STATUS);
+      temp_event->dnd.window = g_object_ref (context->source_window);
+      temp_event->dnd.send_event = TRUE;
+      temp_event->dnd.context = g_object_ref (context);
+      temp_event->dnd.time = GDK_CURRENT_TIME;
+      gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
 
-      gdk_event_put (&temp_event);
+      gdk_event_put (temp_event);
+
+      gdk_event_free (temp_event);
     }
 
   g_object_unref (context);
@@ -2140,9 +2335,9 @@ static GdkDisplay *
 gdk_drag_context_get_display (GdkDragContext *context)
 {
   if (context->source_window)
-    return GDK_DRAWABLE_DISPLAY (context->source_window);
+    return GDK_WINDOW_DISPLAY (context->source_window);
   else if (context->dest_window)
-    return GDK_DRAWABLE_DISPLAY (context->dest_window);
+    return GDK_WINDOW_DISPLAY (context->dest_window);
 
   g_assert_not_reached ();
   return NULL;
@@ -2186,13 +2381,15 @@ xdnd_send_xevent (GdkDragContext *context,
          if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
              event_send->xclient.message_type)
            {
-             GdkEvent temp_event;
-             temp_event.any.window = window;
+             GdkEvent *temp_event;
+
+              temp_event = gdk_event_new (GDK_NOTHING);
+              temp_event->any.window = g_object_ref (window);
 
-             if  ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
+             if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
                {
-                 gdk_event_put (&temp_event);
-                 g_object_unref (temp_event.dnd.context);
+                 gdk_event_put (temp_event);
+                  gdk_event_free (temp_event);
                }
              
              return TRUE;
@@ -2218,15 +2415,15 @@ xdnd_send_enter (GdkDragContext *context)
 {
   XEvent xev;
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
 
   xev.xclient.type = ClientMessage;
   xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
   xev.xclient.format = 32;
   xev.xclient.window = private->drop_xid ? 
                            private->drop_xid : 
-                           GDK_DRAWABLE_XID (context->dest_window);
-  xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
+                           GDK_WINDOW_XID (context->dest_window);
+  xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
   xev.xclient.data.l[1] = (private->version << 24); /* version */
   xev.xclient.data.l[2] = 0;
   xev.xclient.data.l[3] = 0;
@@ -2234,7 +2431,7 @@ xdnd_send_enter (GdkDragContext *context)
 
   GDK_NOTE(DND,
           g_message ("Sending enter source window %#lx XDND protocol version %d\n",
-                     GDK_DRAWABLE_XID (context->source_window), private->version));
+                     GDK_WINDOW_XID (context->source_window), private->version));
   if (g_list_length (context->targets) > 3)
     {
       if (!private->xdnd_targets_set)
@@ -2260,7 +2457,7 @@ xdnd_send_enter (GdkDragContext *context)
     {
       GDK_NOTE (DND, 
                g_message ("Send event to %lx failed",
-                          GDK_DRAWABLE_XID (context->dest_window)));
+                          GDK_WINDOW_XID (context->dest_window)));
       g_object_unref (context->dest_window);
       context->dest_window = NULL;
     }
@@ -2269,7 +2466,7 @@ xdnd_send_enter (GdkDragContext *context)
 static void
 xdnd_send_leave (GdkDragContext *context)
 {
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   XEvent xev;
 
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
@@ -2279,8 +2476,8 @@ xdnd_send_leave (GdkDragContext *context)
   xev.xclient.format = 32;
   xev.xclient.window = private->drop_xid ? 
                            private->drop_xid : 
-                           GDK_DRAWABLE_XID (context->dest_window);
-  xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
+                           GDK_WINDOW_XID (context->dest_window);
+  xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
   xev.xclient.data.l[1] = 0;
   xev.xclient.data.l[2] = 0;
   xev.xclient.data.l[3] = 0;
@@ -2291,7 +2488,7 @@ xdnd_send_leave (GdkDragContext *context)
     {
       GDK_NOTE (DND, 
                g_message ("Send event to %lx failed",
-                          GDK_DRAWABLE_XID (context->dest_window)));
+                          GDK_WINDOW_XID (context->dest_window)));
       g_object_unref (context->dest_window);
       context->dest_window = NULL;
     }
@@ -2301,7 +2498,7 @@ static void
 xdnd_send_drop (GdkDragContext *context, guint32 time)
 {
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   XEvent xev;
 
   xev.xclient.type = ClientMessage;
@@ -2309,8 +2506,8 @@ xdnd_send_drop (GdkDragContext *context, guint32 time)
   xev.xclient.format = 32;
   xev.xclient.window = private->drop_xid ? 
                            private->drop_xid : 
-                           GDK_DRAWABLE_XID (context->dest_window);
-  xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
+                           GDK_WINDOW_XID (context->dest_window);
+  xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
   xev.xclient.data.l[1] = 0;
   xev.xclient.data.l[2] = time;
   xev.xclient.data.l[3] = 0;
@@ -2321,7 +2518,7 @@ xdnd_send_drop (GdkDragContext *context, guint32 time)
     {
       GDK_NOTE (DND, 
                g_message ("Send event to %lx failed",
-                          GDK_DRAWABLE_XID (context->dest_window)));
+                          GDK_WINDOW_XID (context->dest_window)));
       g_object_unref (context->dest_window);
       context->dest_window = NULL;
     }
@@ -2335,7 +2532,7 @@ xdnd_send_motion (GdkDragContext *context,
                  guint32         time)
 {
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
-  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
   XEvent xev;
 
   xev.xclient.type = ClientMessage;
@@ -2343,8 +2540,8 @@ xdnd_send_motion (GdkDragContext *context,
   xev.xclient.format = 32;
   xev.xclient.window = private->drop_xid ? 
                            private->drop_xid : 
-                           GDK_DRAWABLE_XID (context->dest_window);
-  xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
+                           GDK_WINDOW_XID (context->dest_window);
+  xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
   xev.xclient.data.l[1] = 0;
   xev.xclient.data.l[2] = (x_root << 16) | y_root;
   xev.xclient.data.l[3] = time;
@@ -2355,7 +2552,7 @@ xdnd_send_motion (GdkDragContext *context,
     {
       GDK_NOTE (DND, 
                g_message ("Send event to %lx failed",
-                          GDK_DRAWABLE_XID (context->dest_window)));
+                          GDK_WINDOW_XID (context->dest_window)));
       g_object_unref (context->dest_window);
       context->dest_window = NULL;
     }
@@ -2429,7 +2626,7 @@ xdnd_check_dest (GdkDisplay *display,
        }
     }
 
-  gdk_error_trap_pop ();
+  gdk_error_trap_pop_ignored ();
   
   return retval ? (proxy ? proxy : win) : None;
 }
@@ -2457,7 +2654,7 @@ xdnd_read_actions (GdkDragContext *context)
       gdk_error_trap_push ();
       
       if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
-                             GDK_DRAWABLE_XID (context->source_window),
+                             GDK_WINDOW_XID (context->source_window),
                              gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
                              0, 65536,
                              False, XA_ATOM, &type, &format, &nitems,
@@ -2496,7 +2693,7 @@ xdnd_read_actions (GdkDragContext *context)
       if (data)
        XFree (data);
       
-      gdk_error_trap_pop ();
+      gdk_error_trap_pop_ignored ();
     }
   else
     {
@@ -2505,8 +2702,8 @@ xdnd_read_actions (GdkDragContext *context)
       GdkDragContext *source_context;
 
       source_context = gdk_drag_context_find (display, TRUE,
-                                             GDK_DRAWABLE_XID (context->source_window),
-                                             GDK_DRAWABLE_XID (context->dest_window));
+                                             GDK_WINDOW_XID (context->source_window),
+                                             GDK_WINDOW_XID (context->dest_window));
 
       if (source_context)
        {
@@ -2570,8 +2767,7 @@ xdnd_manage_source_filter (GdkDragContext *context,
           */
        }
       
-      gdk_display_sync (gdk_drawable_get_display (window));
-      gdk_error_trap_pop ();  
+      gdk_error_trap_pop_ignored ();  
     }
 }
 
@@ -2633,6 +2829,7 @@ xdnd_enter_filter (GdkXEvent *xev,
                   GdkEvent  *event,
                   gpointer   cb_data)
 {
+  GdkDeviceManager *device_manager;
   GdkDisplay *display;
   GdkDisplayX11 *display_x11;
   XEvent *xevent = (XEvent *)xev;
@@ -2657,7 +2854,7 @@ xdnd_enter_filter (GdkXEvent *xev,
   get_types = ((xevent->xclient.data.l[1] & 1) != 0);
   version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
   
-  display = GDK_DRAWABLE_DISPLAY (event->any.window);
+  display = GDK_WINDOW_DISPLAY (event->any.window);
   display_x11 = GDK_DISPLAY_X11 (display);
 
   xdnd_precache_atoms (display);
@@ -2683,6 +2880,10 @@ xdnd_enter_filter (GdkXEvent *xev,
   new_context->protocol = GDK_DRAG_PROTO_XDND;
   PRIVATE_DATA(new_context)->version = version;
 
+  /* FIXME: Should extend DnD protocol to have device info */
+  device_manager = gdk_display_get_device_manager (display);
+  gdk_drag_context_set_device (new_context, gdk_device_manager_get_client_pointer (device_manager));
+
   new_context->source_window = gdk_window_lookup_for_display (display, source_window);
   if (new_context->source_window)
     g_object_ref (new_context->source_window);
@@ -2702,7 +2903,7 @@ xdnd_enter_filter (GdkXEvent *xev,
   if (get_types)
     {
       gdk_error_trap_push ();
-      XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window), 
+      XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window), 
                          source_window, 
                          gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
                          0, 65536,
@@ -2749,6 +2950,7 @@ xdnd_enter_filter (GdkXEvent *xev,
 
   event->dnd.type = GDK_DRAG_ENTER;
   event->dnd.context = new_context;
+  gdk_event_set_device (event, gdk_drag_context_get_device (new_context));
   g_object_ref (new_context);
 
   display_x11->current_dest_drag = new_context;
@@ -2774,18 +2976,19 @@ xdnd_leave_filter (GdkXEvent *xev,
            g_message ("XdndLeave: source_window: %#x",
                       source_window));
 
-  display = GDK_DRAWABLE_DISPLAY (event->any.window);
+  display = GDK_WINDOW_DISPLAY (event->any.window);
   display_x11 = GDK_DISPLAY_X11 (display);
 
   xdnd_precache_atoms (display);
 
   if ((display_x11->current_dest_drag != NULL) &&
       (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
-      (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
+      (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
     {
       event->dnd.type = GDK_DRAG_LEAVE;
       /* Pass ownership of context to the event */
       event->dnd.context = display_x11->current_dest_drag;
+      gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
 
       display_x11->current_dest_drag = NULL;
 
@@ -2818,17 +3021,18 @@ xdnd_position_filter (GdkXEvent *xev,
            g_message ("XdndPosition: source_window: %#x position: (%d, %d)  time: %d  action: %ld",
                       source_window, x_root, y_root, time, action));
 
-  display = GDK_DRAWABLE_DISPLAY (event->any.window);
+  display = GDK_WINDOW_DISPLAY (event->any.window);
   display_x11 = GDK_DISPLAY_X11 (display);
   
   xdnd_precache_atoms (display);
 
   if ((display_x11->current_dest_drag != NULL) &&
       (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
-      (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
+      (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
     {
       event->dnd.type = GDK_DRAG_MOTION;
       event->dnd.context = display_x11->current_dest_drag;
+      gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
       g_object_ref (display_x11->current_dest_drag);
 
       event->dnd.time = time;
@@ -2869,14 +3073,14 @@ xdnd_drop_filter (GdkXEvent *xev,
            g_message ("XdndDrop: source_window: %#x  time: %d",
                       source_window, time));
 
-  display = GDK_DRAWABLE_DISPLAY (event->any.window);
+  display = GDK_WINDOW_DISPLAY (event->any.window);
   display_x11 = GDK_DISPLAY_X11 (display);
 
   xdnd_precache_atoms (display);
 
   if ((display_x11->current_dest_drag != NULL) &&
       (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
-      (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
+      (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
     {
       GdkDragContextPrivateX11 *private;
       private = PRIVATE_DATA (display_x11->current_dest_drag);
@@ -2884,6 +3088,7 @@ xdnd_drop_filter (GdkXEvent *xev,
       event->dnd.type = GDK_DROP_START;
 
       event->dnd.context = display_x11->current_dest_drag;
+      gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
       g_object_ref (display_x11->current_dest_drag);
 
       event->dnd.time = time;
@@ -2950,21 +3155,26 @@ gdk_drag_do_leave (GdkDragContext *context, guint32 time)
 /**
  * gdk_drag_begin:
  * @window: the source window for this drag.
- * @targets: the list of offered targets.
+ * @targets: (transfer none) (element-type GdkAtom): the offered targets,
+ *     as list of #GdkAtom<!-- -->s
  * 
  * Starts a drag and creates a new drag context for it.
  *
  * This function is called by the drag source.
  * 
- * Return value: a newly created #GdkDragContext.
+ * Return value: (transfer full): a newly created #GdkDragContext.
  **/
 GdkDragContext * 
 gdk_drag_begin (GdkWindow     *window,
                GList         *targets)
 {
   GdkDragContext *new_context;
-  
+  GdkDisplay *display;
+  GdkDevice *device;
+  GdkDeviceManager *device_manager;
+
   g_return_val_if_fail (window != NULL, NULL);
+  g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
 
   new_context = gdk_drag_context_new ();
   new_context->is_source = TRUE;
@@ -2976,18 +3186,23 @@ gdk_drag_begin (GdkWindow     *window,
   
   new_context->actions = 0;
 
+  display = gdk_window_get_display (window);
+  device_manager = gdk_display_get_device_manager (display);
+  device = gdk_device_manager_get_client_pointer (device_manager);
+  gdk_drag_context_set_device (new_context, device);
+
   return new_context;
 }
 
-static guint32
+static GdkNativeWindow
 _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
-                                   guint32          xid,
+                                   GdkNativeWindow  xid,
                                    GdkDragProtocol *protocol,
                                    guint           *version)
 
 {
   GdkWindow *window;
-  guint32 retval;
+  GdkNativeWindow retval;
   g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
 
   base_precache_atoms (display);
@@ -3012,11 +3227,8 @@ _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
          GDK_NOTE (DND, g_message ("Entering root window\n"));
          return xid;
        }
-              
-       return None;
     }
-  
-  if ((retval = xdnd_check_dest (display, xid, version)))
+  else if ((retval = xdnd_check_dest (display, xid, version)))
     {
       *protocol = GDK_DRAG_PROTO_XDND;
       xdnd_precache_atoms (display);
@@ -3076,7 +3288,7 @@ _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
        }
 #endif      
 
-      gdk_error_trap_pop ();
+      gdk_error_trap_pop_ignored ();
 
       if (rootwin)
        {
@@ -3087,25 +3299,26 @@ _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
     }
 
   *protocol = GDK_DRAG_PROTO_NONE;
-  return None;
+
+  return 0; /* a.k.a. None */
 }
 
 /**
  * gdk_drag_get_protocol_for_display:
  * @display: the #GdkDisplay where the destination window resides
- * @xid: the X id of the destination window.
+ * @xid: the windowing system id of the destination window.
  * @protocol: location where the supported DND protocol is returned.
- * @returns: the X id of the window where the drop should happen. This 
- *     may be @xid or the X id of a proxy window, or None if @xid doesn't
+ * @returns: the windowing system id of the window where the drop should happen. This 
+ *     may be @xid or the id of a proxy window, or zero if @xid doesn't
  *     support Drag and Drop.
  *
  * Finds out the DND protocol supported by a window.
  *
  * Since: 2.2
  */ 
-guint32
+GdkNativeWindow
 gdk_drag_get_protocol_for_display (GdkDisplay      *display,
-                                  guint32          xid,
+                                  GdkNativeWindow  xid,
                                   GdkDragProtocol *protocol)
 {
   return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
@@ -3140,11 +3353,11 @@ drag_context_find_window_cache (GdkDragContext  *context,
  * @screen: the screen where the destination window is sought. 
  * @x_root: the x position of the pointer in root coordinates.
  * @y_root: the y position of the pointer in root coordinates.
- * @dest_window: location to store the destination window in.
- * @protocol: location to store the DND protocol in.
- * 
+ * @dest_window: (out): location to store the destination window in.
+ * @protocol: (out): location to store the DND protocol in.
+ *
  * Finds the destination window and DND protocol to use at the
- * given pointer position. 
+ * given pointer position.
  *
  * This function is called by the drag source to obtain the 
  * @dest_window and @protocol parameters for gdk_drag_motion().
@@ -3172,8 +3385,8 @@ gdk_drag_find_window_for_screen (GdkDragContext  *context,
   window_cache = drag_context_find_window_cache (context, screen);
 
   dest = get_client_window_at_coords (window_cache,
-                                     drag_window ? 
-                                     GDK_DRAWABLE_XID (drag_window) : None,
+                                     drag_window && GDK_WINDOW_IS_X11 (drag_window) 
+                                     GDK_WINDOW_XID (drag_window) : None,
                                      x_root, y_root);
 
   if (private->dest_xid != dest)
@@ -3242,6 +3455,7 @@ gdk_drag_motion (GdkDragContext *context,
   GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
 
   g_return_val_if_fail (context != NULL, FALSE);
+  g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
 
   private->old_actions = context->actions;
   context->actions = possible_actions;
@@ -3264,7 +3478,7 @@ gdk_drag_motion (GdkDragContext *context,
          GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
          
          xdnd_check_dest (display, 
-                          GDK_DRAWABLE_XID (dest_window), 
+                          GDK_WINDOW_XID (dest_window), 
                           &private->version);
        }
     }
@@ -3284,8 +3498,8 @@ gdk_drag_motion (GdkDragContext *context,
              GdkDragContext *dest_context;
                    
              dest_context = gdk_drag_context_find (display, FALSE,
-                                                   GDK_DRAWABLE_XID (context->source_window),
-                                                   GDK_DRAWABLE_XID (dest_window));
+                                                   GDK_WINDOW_XID (context->source_window),
+                                                   GDK_WINDOW_XID (dest_window));
 
              if (dest_context)
                {
@@ -3298,7 +3512,7 @@ gdk_drag_motion (GdkDragContext *context,
 
   if (context->dest_window != dest_window)
     {
-      GdkEvent temp_event;
+      GdkEvent *temp_event;
 
       /* Send a leave to the last destination */
       gdk_drag_do_leave (context, time);
@@ -3342,18 +3556,19 @@ gdk_drag_motion (GdkDragContext *context,
       /* Push a status event, to let the client know that
        * the drag changed 
        */
-
-      temp_event.dnd.type = GDK_DRAG_STATUS;
-      temp_event.dnd.window = context->source_window;
+      temp_event = gdk_event_new (GDK_DRAG_STATUS);
+      temp_event->dnd.window = g_object_ref (context->source_window);
       /* We use this to signal a synthetic status. Perhaps
        * we should use an extra field...
        */
-      temp_event.dnd.send_event = TRUE;
+      temp_event->dnd.send_event = TRUE;
 
-      temp_event.dnd.context = context;
-      temp_event.dnd.time = time;
+      temp_event->dnd.context = g_object_ref (context);
+      temp_event->dnd.time = time;
+      gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
 
-      gdk_event_put (&temp_event);
+      gdk_event_put (temp_event);
+      gdk_event_free (temp_event);
     }
   else
     {
@@ -3382,7 +3597,7 @@ gdk_drag_motion (GdkDragContext *context,
 
            case GDK_DRAG_PROTO_ROOTWIN:
              {
-               GdkEvent temp_event;
+               GdkEvent *temp_event;
                /* GTK+ traditionally has used application/x-rootwin-drop,
                 * but the XDND spec specifies x-rootwindow-drop.
                 */
@@ -3397,13 +3612,15 @@ gdk_drag_motion (GdkDragContext *context,
                else
                  context->action = 0;
 
-               temp_event.dnd.type = GDK_DRAG_STATUS;
-               temp_event.dnd.window = context->source_window;
-               temp_event.dnd.send_event = FALSE;
-               temp_event.dnd.context = context;
-               temp_event.dnd.time = time;
+                temp_event = gdk_event_new (GDK_DRAG_STATUS);
+               temp_event->dnd.window = g_object_ref (context->source_window);
+               temp_event->dnd.send_event = FALSE;
+               temp_event->dnd.context = g_object_ref (context);
+               temp_event->dnd.time = time;
+                gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
 
-               gdk_event_put (&temp_event);
+               gdk_event_put (temp_event);
+                gdk_event_free (temp_event);
              }
              break;
            case GDK_DRAG_PROTO_NONE:
@@ -3504,7 +3721,7 @@ gdk_drag_status (GdkDragContext   *context,
   g_return_if_fail (context != NULL);
 
   private = PRIVATE_DATA (context);
-  display = GDK_DRAWABLE_DISPLAY (context->source_window);
+  display = GDK_WINDOW_DISPLAY (context->source_window);
   
   context->action = action;
 
@@ -3516,7 +3733,7 @@ gdk_drag_status (GdkDragContext   *context,
       xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
                                                                        "_MOTIF_DRAG_AND_DROP_MESSAGE");
       xev.xclient.format = 8;
-      xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
+      xev.xclient.window = GDK_WINDOW_XID (context->source_window);
 
       if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
        {
@@ -3578,20 +3795,20 @@ gdk_drag_status (GdkDragContext   *context,
       MOTIF_XCLIENT_LONG (&xev, 4) = 0;
 
       if (!_gdk_send_xevent (display,
-                            GDK_DRAWABLE_XID (context->source_window),
+                            GDK_WINDOW_XID (context->source_window),
                             FALSE, 0, &xev))
        GDK_NOTE (DND, 
                  g_message ("Send event to %lx failed",
-                            GDK_DRAWABLE_XID (context->source_window)));
+                            GDK_WINDOW_XID (context->source_window)));
     }
   else if (context->protocol == GDK_DRAG_PROTO_XDND)
     {
       xev.xclient.type = ClientMessage;
       xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
       xev.xclient.format = 32;
-      xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
+      xev.xclient.window = GDK_WINDOW_XID (context->source_window);
 
-      xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
+      xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
       xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
       xev.xclient.data.l[2] = 0;
       xev.xclient.data.l[3] = 0;
@@ -3601,7 +3818,7 @@ gdk_drag_status (GdkDragContext   *context,
                             FALSE, &xev))
        GDK_NOTE (DND, 
                  g_message ("Send event to %lx failed",
-                            GDK_DRAWABLE_XID (context->source_window)));
+                            GDK_WINDOW_XID (context->source_window)));
     }
 
   private->old_action = action;
@@ -3631,7 +3848,7 @@ gdk_drop_reply (GdkDragContext   *context,
   
   if (context->protocol == GDK_DRAG_PROTO_MOTIF)
     {
-      GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+      GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
       XEvent xev;
 
       xev.xclient.type = ClientMessage;
@@ -3658,7 +3875,7 @@ gdk_drop_reply (GdkDragContext   *context,
       MOTIF_XCLIENT_LONG (&xev, 4) = 0;
       
       _gdk_send_xevent (display,
-                       GDK_DRAWABLE_XID (context->source_window),
+                       GDK_WINDOW_XID (context->source_window),
                        FALSE, 0, &xev);
     }
 }
@@ -3682,15 +3899,15 @@ gdk_drop_finish (GdkDragContext   *context,
 
   if (context->protocol == GDK_DRAG_PROTO_XDND)
     {
-      GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
+      GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
       XEvent xev;
 
       xev.xclient.type = ClientMessage;
       xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
       xev.xclient.format = 32;
-      xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
+      xev.xclient.window = GDK_WINDOW_XID (context->source_window);
       
-      xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
+      xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
       if (success)
        {
          xev.xclient.data.l[1] = 1;
@@ -3709,21 +3926,30 @@ gdk_drop_finish (GdkDragContext   *context,
                             FALSE, &xev))
        GDK_NOTE (DND, 
                  g_message ("Send event to %lx failed",
-                            GDK_DRAWABLE_XID (context->source_window)));
+                            GDK_WINDOW_XID (context->source_window)));
     }
 }
 
 
+/**
+ * gdk_window_register_dnd:
+ * @window: a #GdkWindow.
+ *
+ * Registers a window as a potential drop destination.
+ */
 void            
 gdk_window_register_dnd (GdkWindow      *window)
 {
   static const gulong xdnd_version = 5;
   MotifDragReceiverInfo info;
   Atom motif_drag_receiver_info_atom;
-  GdkDisplay *display = gdk_drawable_get_display (window);
+  GdkDisplay *display = gdk_window_get_display (window);
 
   g_return_if_fail (window != NULL);
 
+  if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
+    return;
+
   base_precache_atoms (display);
 
   if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
@@ -3735,6 +3961,8 @@ gdk_window_register_dnd (GdkWindow      *window)
 
   motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
                                                                         "_MOTIF_DRAG_RECEIVER_INFO");
+  /* initialize to zero to avoid writing uninitialized data to socket */
+  memset(&info, 0, sizeof(info));
   info.byte_order = local_byte_order;
   info.protocol_version = 0;
   info.protocol_style = XmDRAG_DYNAMIC;
@@ -3742,7 +3970,7 @@ gdk_window_register_dnd (GdkWindow      *window)
   info.num_drop_sites = 0;
   info.total_size = sizeof(info);
 
-  XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
+  XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
                   motif_drag_receiver_info_atom,
                   motif_drag_receiver_info_atom,
                   8, PropModeReplace,
@@ -3753,7 +3981,7 @@ gdk_window_register_dnd (GdkWindow      *window)
 
   /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
-                  GDK_DRAWABLE_XID (window),
+                  GDK_WINDOW_XID (window),
                   gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
                   XA_ATOM, 32, PropModeReplace,
                   (guchar *)&xdnd_version, 1);
@@ -3773,7 +4001,7 @@ gdk_drag_get_selection (GdkDragContext *context)
   g_return_val_if_fail (context != NULL, GDK_NONE);
 
   if (context->protocol == GDK_DRAG_PROTO_MOTIF)
-    return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
+    return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
                                              (PRIVATE_DATA (context))->motif_selection);
   else if (context->protocol == GDK_DRAG_PROTO_XDND)
     return gdk_atom_intern_static_string ("XdndSelection");
@@ -3785,7 +4013,7 @@ gdk_drag_get_selection (GdkDragContext *context)
  * gdk_drag_drop_succeeded:
  * @context: a #GdkDragContext
  * 
- * Returns wether the dropped data has been successfully 
+ * Returns whether the dropped data has been successfully 
  * transferred. This function is intended to be used while 
  * handling a %GDK_DROP_FINISHED event, its return value is
  * meaningless at other times.
@@ -3805,6 +4033,3 @@ gdk_drag_drop_succeeded (GdkDragContext *context)
 
   return !private->drop_failed;
 }
-
-#define __GDK_DND_X11_C__
-#include "gdkaliasdef.c"