]> Pileus Git - ~andy/gtk/blobdiff - gdk/x11/gdkdnd-x11.c
Cleanups
[~andy/gtk] / gdk / x11 / gdkdnd-x11.c
index db5c37cae3c0ec4e7dd1e6d68c21cd9f888387f4..f9104313301fb46ca04589f8387c93cfef2aa60b 100644 (file)
@@ -38,6 +38,7 @@
 #include "gdkinternals.h"
 #include "gdkscreen-x11.h"
 #include "gdkdisplay-x11.h"
+#include "gdkalias.h"
 
 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
 
@@ -125,14 +126,11 @@ static void   xdnd_manage_source_filter (GdkDragContext *context,
                                         GdkWindow      *window,
                                         gboolean        add_filter);
 
-static void gdk_drag_context_init       (GdkDragContext      *dragcontext);
-static void gdk_drag_context_class_init (GdkDragContextClass *klass);
 static void gdk_drag_context_finalize   (GObject              *object);
 
-static gpointer parent_class = NULL;
 static GList *contexts;
 
-const static struct {
+static const struct {
   const char *atom_name;
   GdkFilterFunc func;
 } xdnd_filters[] = {
@@ -144,41 +142,17 @@ const static struct {
   { "XdndDrop",     xdnd_drop_filter },
 };
              
-GType
-gdk_drag_context_get_type (void)
-{
-  static GType object_type = 0;
-
-  if (!object_type)
-    {
-      static const GTypeInfo object_info =
-      {
-        sizeof (GdkDragContextClass),
-        (GBaseInitFunc) NULL,
-        (GBaseFinalizeFunc) NULL,
-        (GClassInitFunc) gdk_drag_context_class_init,
-        NULL,           /* class_finalize */
-        NULL,           /* class_data */
-        sizeof (GdkDragContext),
-        0,              /* n_preallocs */
-        (GInstanceInitFunc) gdk_drag_context_init,
-      };
-      
-      object_type = g_type_register_static (G_TYPE_OBJECT,
-                                            "GdkDragContext",
-                                            &object_info, 0);
-    }
-  
-  return object_type;
-}
+G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
 
 static void
 gdk_drag_context_init (GdkDragContext *dragcontext)
 {
   GdkDragContextPrivateX11 *private;
 
-  private = g_new0 (GdkDragContextPrivateX11, 1);
-
+  private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext, 
+                                        GDK_TYPE_DRAG_CONTEXT, 
+                                        GdkDragContextPrivateX11);
+  
   dragcontext->windowing_data = private;
 
   contexts = g_list_prepend (contexts, dragcontext);
@@ -189,9 +163,9 @@ gdk_drag_context_class_init (GdkDragContextClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  parent_class = g_type_class_peek_parent (klass);
-
   object_class->finalize = gdk_drag_context_finalize;
+
+  g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
 }
 
 static void
@@ -221,9 +195,7 @@ gdk_drag_context_finalize (GObject *object)
   
   contexts = g_list_remove (contexts, context);
 
-  g_free (private);
-  
-  G_OBJECT_CLASS (parent_class)->finalize (object);
+  G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
 }
 
 /* Drag Contexts */
@@ -236,9 +208,9 @@ gdk_drag_context_finalize (GObject *object)
  * Return value: the newly created #GdkDragContext.
  **/
 GdkDragContext *
-gdk_drag_context_new        (void)
+gdk_drag_context_new (void)
 {
-  return g_object_new (gdk_drag_context_get_type (), NULL);
+  return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
 }
 
 /**
@@ -488,6 +460,25 @@ 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);
@@ -538,7 +529,7 @@ get_client_window_at_coords_recurse (GdkDisplay *display,
   unsigned int nchildren;
   int i;
   gboolean found_child = FALSE;
-  GdkChildInfoX11 child;
+  GdkChildInfoX11 child = { 0, };
   gboolean has_wm_state = FALSE;
 
   if (!_gdk_x11_get_window_child_info (display, win, TRUE,
@@ -547,7 +538,11 @@ get_client_window_at_coords_recurse (GdkDisplay *display,
     return None;
 
   if (has_wm_state)
-    return win;
+    {
+      g_free (children);
+
+      return win;
+    }
 
   for (i = nchildren - 1; (i >= 0) && !found_child; i--)
     {
@@ -852,7 +847,7 @@ motif_find_drag_window (GdkDisplay *display,
                XCreateWindow (persistant_xdisplay, 
                               RootWindow (persistant_xdisplay, 0),
                              -100, -100, 10, 10, 0, 0,
-                             InputOnly, CopyFromParent,
+                             InputOnly, (Visual *)CopyFromParent,
                              (CWOverrideRedirect | CWEventMask), &attr);
              
              GDK_NOTE (DND,
@@ -908,6 +903,7 @@ motif_read_target_table (GdkDisplay *display)
 
   if (motif_find_drag_window (display, FALSE))
     {
+      guchar *data;
       MotifTargetTableHeader *header = NULL;
       guchar *target_bytes = NULL;
       guchar *p;
@@ -920,11 +916,13 @@ motif_read_target_table (GdkDisplay *display)
                          0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
                          motif_drag_targets_atom, 
                          &type, &format, &nitems, &bytes_after,
-                         (guchar **)&header);
+                         &data);
 
       if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
        goto error;
 
+      header = (MotifTargetTableHeader *)data;
+
       header->n_lists = card16_to_host (header->n_lists, header->byte_order);
       header->total_size = card32_to_host (header->total_size, header->byte_order);
 
@@ -1259,6 +1257,7 @@ motif_check_dest (GdkDisplay *display,
                  Window      win)
 {
   gboolean retval = FALSE;
+  guchar *data;
   MotifDragReceiverInfo *info;
   Atom type = None;
   int format;
@@ -1270,12 +1269,14 @@ motif_check_dest (GdkDisplay *display,
                      motif_drag_receiver_info_atom, 
                      0, (sizeof(*info)+3)/4, False, AnyPropertyType,
                      &type, &format, &nitems, &after, 
-                     (guchar **)&info);
+                     &data);
 
   if (gdk_error_trap_pop() == 0)
     {
       if (type != None)
        {
+         info = (MotifDragReceiverInfo *)data;
+         
          if ((format == 8) && (nitems == sizeof(*info)))
            {
              if ((info->protocol_version == 0) &&
@@ -1305,6 +1306,9 @@ motif_send_enter (GdkDragContext  *context,
   GdkDisplay *display = GDK_DRAWABLE_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;
@@ -1455,6 +1459,7 @@ motif_read_initiator_info (GdkDisplay *display,
   gint format;
   gulong nitems;
   gulong bytes_after;
+  guchar *data;
   MotifDragInitiatorInfo *initiator_info;
   
   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
@@ -1464,7 +1469,7 @@ motif_read_initiator_info (GdkDisplay *display,
                      0, sizeof(*initiator_info), FALSE,
                      gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
                      &type, &format, &nitems, &bytes_after,
-                     (guchar **)&initiator_info);
+                     &data);
 
   if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
     {
@@ -1472,6 +1477,8 @@ motif_read_initiator_info (GdkDisplay *display,
       return FALSE;
     }
 
+  initiator_info = (MotifDragInitiatorInfo *)data;
+
   motif_read_target_table (display);
 
   initiator_info->targets_index = 
@@ -1731,6 +1738,8 @@ motif_drop_start (GdkEvent *event,
   event->dnd.x_root = x_root;
   event->dnd.y_root = y_root;
 
+  gdk_x11_window_set_user_time (event->any.window, timestamp);
+
   g_object_ref (new_context);
   display_x11->current_dest_drag = new_context;
 
@@ -1882,7 +1891,7 @@ motif_dnd_filter (GdkXEvent *xev,
 /* Utility functions */
 
 static struct {
-  gchar *name;
+  const gchar *name;
   GdkAtom atom;
   GdkDragAction action;
 } xdnd_actions_table[] = {
@@ -1903,7 +1912,7 @@ xdnd_initialize_actions (void)
   
   xdnd_actions_initialized = TRUE;
   for (i=0; i < xdnd_n_actions; i++)
-    xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
+    xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
 }
 
 static GdkDragAction
@@ -2022,6 +2031,8 @@ xdnd_finished_filter (GdkXEvent *xev,
       event->dnd.context = context;
       g_object_ref (context);
 
+      event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
+
       return GDK_FILTER_TRANSLATE;
     }
 
@@ -2376,12 +2387,13 @@ xdnd_send_motion (GdkDragContext *context,
 static guint32
 xdnd_check_dest (GdkDisplay *display,
                 Window      win,
-                gint       *xdnd_version)
+                guint      *xdnd_version)
 {
   gboolean retval = FALSE;
   Atom type = None;
   int format;
   unsigned long nitems, after;
+  guchar *data;
   Atom *version;
   Window *proxy_data;
   Window proxy;
@@ -2396,10 +2408,12 @@ xdnd_check_dest (GdkDisplay *display,
                          xdnd_proxy_atom, 0, 
                          1, False, AnyPropertyType,
                          &type, &format, &nitems, &after, 
-                         (guchar **)&proxy_data) == Success)
+                         &data) == Success)
     {
       if (type != None)
        {
+         proxy_data = (Window *)data;
+         
          if ((format == 32) && (nitems == 1))
            {
              proxy = *proxy_data;
@@ -2416,9 +2430,11 @@ xdnd_check_dest (GdkDisplay *display,
                               xdnd_aware_atom, 0, 
                               1, False, AnyPropertyType,
                               &type, &format, &nitems, &after, 
-                              (guchar **)&version) == Success) &&
+                              &data) == Success) &&
          type != None)
        {
+         version = (Atom *)data;
+         
          if ((format == 32) && (nitems == 1))
            {
              if (*version >= 3)
@@ -2449,7 +2465,8 @@ xdnd_read_actions (GdkDragContext *context)
   Atom type;
   int format;
   gulong nitems, after;
-  Atom *data;
+  guchar *data;
+  Atom *atoms;
 
   gint i;
   
@@ -2466,13 +2483,15 @@ xdnd_read_actions (GdkDragContext *context)
                              gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
                              0, 65536,
                              False, XA_ATOM, &type, &format, &nitems,
-                             &after, (guchar **)&data) == Success &&
+                             &after, &data) == Success &&
          type == XA_ATOM)
        {
+         atoms = (Atom *)data;
+         
          context->actions = 0;
          
          for (i=0; i<nitems; i++)
-           context->actions |= xdnd_action_from_atom (display, data[i]);
+           context->actions |= xdnd_action_from_atom (display, atoms[i]);
          
          PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
          
@@ -2494,8 +2513,10 @@ xdnd_read_actions (GdkDragContext *context)
            }
 #endif /* G_ENABLE_DEBUG */
          
-         XFree(data);
        }
+
+      if (data)
+       XFree (data);
       
       gdk_error_trap_pop ();
     }
@@ -2643,7 +2664,8 @@ xdnd_enter_filter (GdkXEvent *xev,
   Atom type;
   int format;
   gulong nitems, after;
-  Atom *data;
+  guchar *data;
+  Atom *atoms;
 
   guint32 source_window;
   gboolean get_types;
@@ -2707,21 +2729,27 @@ xdnd_enter_filter (GdkXEvent *xev,
                          gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
                          0, 65536,
                          False, XA_ATOM, &type, &format, &nitems,
-                         &after, (guchar **)&data);
+                         &after, &data);
 
       if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
        {
          g_object_unref (new_context);
+
+         if (data)
+           XFree (data);
+
          return GDK_FILTER_REMOVE;
        }
 
+      atoms = (Atom *)data;
+
       for (i=0; i<nitems; i++)
        new_context->targets = 
          g_list_append (new_context->targets,
                         GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
-                                                                                data[i])));
+                                                                                atoms[i])));
 
-      XFree(data);
+      XFree(atoms);
     }
   else
     {
@@ -2883,6 +2911,8 @@ xdnd_drop_filter (GdkXEvent *xev,
       event->dnd.time = time;
       event->dnd.x_root = private->last_x;
       event->dnd.y_root = private->last_y;
+
+      gdk_x11_window_set_user_time (event->any.window, time);
       
       return GDK_FILTER_TRANSLATE;
     }
@@ -2901,14 +2931,14 @@ _gdk_dnd_init (GdkDisplay *display)
 
   gdk_display_add_client_message_filter (
        display,
-       gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
+       gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
        motif_dnd_filter, NULL);
   
   for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
     {
       gdk_display_add_client_message_filter (
        display,
-       gdk_atom_intern (xdnd_filters[i].atom_name, FALSE),
+       gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
        xdnd_filters[i].func, NULL);
     }
 }                    
@@ -2975,7 +3005,7 @@ static guint32
 _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
                                    guint32          xid,
                                    GdkDragProtocol *protocol,
-                                   gint            *version)
+                                   guint           *version)
 
 {
   GdkWindow *window;
@@ -2998,11 +3028,14 @@ _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
          GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
          return xid;
        }
-      else
-       return None;
+      else if (_gdk_x11_display_is_root_window (display, (Window) xid))
+       {
+         *protocol = GDK_DRAG_PROTO_ROOTWIN;
+         GDK_NOTE (DND, g_message ("Entering root window\n"));
+         return xid;
+       }
     }
-  
-  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);
@@ -3055,7 +3088,10 @@ _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
                                  0, 0, False, AnyPropertyType,
                                  &type, &format, &nitems, &data) &&
              type != None)
-           rootwin = TRUE;
+           {
+             XFree (data);
+             rootwin = TRUE;
+           }
        }
 #endif      
 
@@ -3063,6 +3099,7 @@ _gdk_drag_get_protocol_for_display (GdkDisplay      *display,
 
       if (rootwin)
        {
+         GDK_NOTE (DND, g_message ("Entering root window\n"));
          *protocol = GDK_DRAG_PROTO_ROOTWIN;
          return xid;
        }
@@ -3230,7 +3267,27 @@ gdk_drag_motion (GdkDragContext *context,
   
   if (private->old_actions != possible_actions)
     private->xdnd_actions_set = FALSE;
-      
+  
+  if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
+    {
+      /* This ugly hack is necessary since GTK+ doesn't know about
+       * the XDND protocol version, and in particular doesn't know 
+       * that gdk_drag_find_window_for_screen() has the side-effect 
+       * of setting private->version, and therefore sometimes call
+       * gdk_drag_motion() without a prior call to 
+       * gdk_drag_find_window_for_screen(). This happens, e.g.
+       * when GTK+ is proxying DND events to embedded windows.
+       */ 
+      if (dest_window)
+       {
+         GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
+         
+         xdnd_check_dest (display, 
+                          GDK_DRAWABLE_XID (dest_window), 
+                          &private->version);
+       }
+    }
+
   /* When we have a Xdnd target, make sure our XdndActionList
    * matches the current actions;
    */
@@ -3348,8 +3405,8 @@ gdk_drag_motion (GdkDragContext *context,
                /* GTK+ traditionally has used application/x-rootwin-drop,
                 * but the XDND spec specifies x-rootwindow-drop.
                 */
-               GdkAtom target1 = gdk_atom_intern ("application/x-rootwindow-drop", FALSE);
-               GdkAtom target2 = gdk_atom_intern ("application/x-rootwin-drop", FALSE);
+               GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
+               GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
 
                if (g_list_find (context->targets,
                                 GDK_ATOM_TO_POINTER (target1)) ||
@@ -3655,8 +3712,6 @@ gdk_drop_finish (GdkDragContext   *context,
       xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
       if (success)
        {
-         g_print ("sending back success status; version %d\n", 
-                  PRIVATE_DATA (context)->version);
          xev.xclient.data.l[1] = 1;
          xev.xclient.data.l[2] = xdnd_action_to_atom (display, 
                                                       context->action);
@@ -3681,7 +3736,7 @@ gdk_drop_finish (GdkDragContext   *context,
 void            
 gdk_window_register_dnd (GdkWindow      *window)
 {
-  static gulong xdnd_version = 5;
+  static const gulong xdnd_version = 5;
   MotifDragReceiverInfo info;
   Atom motif_drag_receiver_info_atom;
   GdkDisplay *display = gdk_drawable_get_display (window);
@@ -3699,6 +3754,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;
@@ -3740,7 +3797,7 @@ gdk_drag_get_selection (GdkDragContext *context)
     return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
                                              (PRIVATE_DATA (context))->motif_selection);
   else if (context->protocol == GDK_DRAG_PROTO_XDND)
-    return gdk_atom_intern ("XdndSelection", FALSE);
+    return gdk_atom_intern_static_string ("XdndSelection");
   else
     return GDK_NONE;
 }
@@ -3769,3 +3826,6 @@ gdk_drag_drop_succeeded (GdkDragContext *context)
 
   return !private->drop_failed;
 }
+
+#define __GDK_DND_X11_C__
+#include "gdkaliasdef.c"