gdkdnd-x11.c 73.8 KB
Newer Older
1
/* GDK - The GIMP Drawing Kit
2
 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25
#include "config.h"
26

27
#include "gdkx11dnd.h"
28
#include "gdkdndprivate.h"
Owen Taylor's avatar
Owen Taylor committed
29

30
#include "gdkmain.h"
Matthias Clasen's avatar
Matthias Clasen committed
31
#include "gdkinternals.h"
32
#include "gdkasync.h"
Owen Taylor's avatar
Owen Taylor committed
33
#include "gdkproperty.h"
34
#include "gdkprivate-x11.h"
35 36
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
37

38 39 40 41
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
42
#ifdef HAVE_XCOMPOSITE
43
#include <X11/extensions/Xcomposite.h>
44
#endif
45 46 47

#include <string.h>

48 49 50 51 52 53 54 55 56 57 58
typedef enum {
  GDK_DRAG_STATUS_DRAG,
  GDK_DRAG_STATUS_MOTION_WAIT,
  GDK_DRAG_STATUS_ACTION_WAIT,
  GDK_DRAG_STATUS_DROP
} GtkDragStatus;

typedef struct {
  guint32 xid;
  gint x, y, width, height;
  gboolean mapped;
59 60
  gboolean shape_selected;
  gboolean shape_valid;
61
  cairo_region_t *shape;
62 63 64 65 66 67
} GdkCacheChild;

typedef struct {
  GList *children;
  GHashTable *child_hash;
  guint old_event_mask;
68
  GdkScreen *screen;
69
  gint ref_count;
70 71
} GdkWindowCache;

Matthias Clasen's avatar
Matthias Clasen committed
72

73 74
struct _GdkX11DragContext
{
75 76 77 78
  GdkDragContext context;

  guint   ref_count;

79
  guint16 last_x;              /* Coordinates from last event */
80
  guint16 last_y;
81 82 83
  GdkDragAction old_action;    /* The last action we sent to the source */
  GdkDragAction old_actions;   /* The last actions we sent to the source */
  GdkDragAction xdnd_actions;  /* What is currently set in XdndActionList */
84 85 86
  guint version;               /* Xdnd protocol version */

  GSList *window_caches;
87 88 89 90 91 92 93 94

  Window dest_xid;             /* The last window we looked up */
  Window drop_xid;             /* The (non-proxied) window that is receiving drops */
  guint xdnd_targets_set  : 1; /* Whether we've already set XdndTypeList */
  guint xdnd_actions_set  : 1; /* Whether we've already set XdndActionList */
  guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
  guint drag_status       : 4; /* current status of drag */
  guint drop_failed       : 1; /* Whether the drop was unsuccessful */
95 96
};

97 98 99 100 101
struct _GdkX11DragContextClass
{
  GdkDragContextClass parent_class;
};

102 103
/* Forward declarations */

104 105 106
static GdkWindowCache *gdk_window_cache_get   (GdkScreen      *screen);
static GdkWindowCache *gdk_window_cache_ref   (GdkWindowCache *cache);
static void            gdk_window_cache_unref (GdkWindowCache *cache);
107

108
static GdkFilterReturn xdnd_enter_filter    (GdkXEvent *xev,
109 110
                                             GdkEvent  *event,
                                             gpointer   data);
111
static GdkFilterReturn xdnd_leave_filter    (GdkXEvent *xev,
112 113
                                             GdkEvent  *event,
                                             gpointer   data);
114
static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
115 116
                                             GdkEvent  *event,
                                             gpointer   data);
117
static GdkFilterReturn xdnd_status_filter   (GdkXEvent *xev,
118 119
                                             GdkEvent  *event,
                                             gpointer   data);
120
static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
121 122
                                             GdkEvent  *event,
                                             gpointer   data);
123
static GdkFilterReturn xdnd_drop_filter     (GdkXEvent *xev,
124 125
                                             GdkEvent  *event,
                                             gpointer   data);
126

127
static void   xdnd_manage_source_filter (GdkDragContext *context,
128 129
                                         GdkWindow      *window,
                                         gboolean        add_filter);
130

131
static GList *contexts;
132
static GSList *window_caches;
133

Matthias Clasen's avatar
Matthias Clasen committed
134
static const struct {
135 136
  const char *atom_name;
  GdkFilterFunc func;
Matthias Clasen's avatar
Matthias Clasen committed
137
} xdnd_filters[] = {
138 139 140 141 142 143
  { "XdndEnter",                    xdnd_enter_filter },
  { "XdndLeave",                    xdnd_leave_filter },
  { "XdndPosition",                 xdnd_position_filter },
  { "XdndStatus",                   xdnd_status_filter },
  { "XdndFinished",                 xdnd_finished_filter },
  { "XdndDrop",                     xdnd_drop_filter },
144
};
145 146


147
G_DEFINE_TYPE (GdkX11DragContext, gdk_x11_drag_context, GDK_TYPE_DRAG_CONTEXT)
148

Matthias Clasen's avatar
Matthias Clasen committed
149
static void
150
gdk_x11_drag_context_init (GdkX11DragContext *context)
Matthias Clasen's avatar
Matthias Clasen committed
151 152 153 154
{
  contexts = g_list_prepend (contexts, context);
}

155 156
static void        gdk_x11_drag_context_finalize (GObject *object);
static GdkWindow * gdk_x11_drag_context_find_window (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
157 158 159 160 161
                                                     GdkWindow       *drag_window,
                                                     GdkScreen       *screen,
                                                     gint             x_root,
                                                     gint             y_root,
                                                     GdkDragProtocol *protocol);
162
static gboolean    gdk_x11_drag_context_drag_motion (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
163 164 165 166 167 168 169
                                                     GdkWindow       *dest_window,
                                                     GdkDragProtocol  protocol,
                                                     gint             x_root,
                                                     gint             y_root,
                                                     GdkDragAction    suggested_action,
                                                     GdkDragAction    possible_actions,
                                                     guint32          time);
170
static void        gdk_x11_drag_context_drag_status (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
171 172
                                                     GdkDragAction    action,
                                                     guint32          time_);
173
static void        gdk_x11_drag_context_drag_abort  (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
174
                                                     guint32          time_);
175
static void        gdk_x11_drag_context_drag_drop   (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
176
                                                     guint32          time_);
177
static void        gdk_x11_drag_context_drop_reply  (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
178 179
                                                     gboolean         accept,
                                                     guint32          time_);
180
static void        gdk_x11_drag_context_drop_finish (GdkDragContext  *context,
Matthias Clasen's avatar
Matthias Clasen committed
181 182
                                                     gboolean         success,
                                                     guint32          time_);
183 184
static gboolean    gdk_x11_drag_context_drop_status (GdkDragContext  *context);
static GdkAtom     gdk_x11_drag_context_get_selection (GdkDragContext  *context);
185

186
static void
187
gdk_x11_drag_context_class_init (GdkX11DragContextClass *klass)
188
{
189
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Matthias Clasen's avatar
Matthias Clasen committed
190
  GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
191

192
  object_class->finalize = gdk_x11_drag_context_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
193

194 195 196 197 198 199 200 201 202
  context_class->find_window = gdk_x11_drag_context_find_window;
  context_class->drag_status = gdk_x11_drag_context_drag_status;
  context_class->drag_motion = gdk_x11_drag_context_drag_motion;
  context_class->drag_abort = gdk_x11_drag_context_drag_abort;
  context_class->drag_drop = gdk_x11_drag_context_drag_drop;
  context_class->drop_reply = gdk_x11_drag_context_drop_reply;
  context_class->drop_finish = gdk_x11_drag_context_drop_finish;
  context_class->drop_status = gdk_x11_drag_context_drop_status;
  context_class->get_selection = gdk_x11_drag_context_get_selection;
203 204
}

205
static void
206
gdk_x11_drag_context_finalize (GObject *object)
207
{
208
  GdkDragContext *context = GDK_DRAG_CONTEXT (object);
209
  GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (object);
210 211

  if (context->source_window)
212
    {
Matthias Clasen's avatar
Matthias Clasen committed
213
      if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
214 215
        xdnd_manage_source_filter (context, context->source_window, FALSE);
    }
216

217 218
  g_slist_free_full (x11_context->window_caches, (GDestroyNotify)gdk_window_cache_unref);
  x11_context->window_caches = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
219

220
  contexts = g_list_remove (contexts, context);
221

222
  G_OBJECT_CLASS (gdk_x11_drag_context_parent_class)->finalize (object);
223
}
224

225 226
/* Drag Contexts */

227
static GdkDragContext *
228
gdk_drag_context_find (GdkDisplay *display,
229 230 231
                       gboolean    is_source,
                       Window      source_xid,
                       Window      dest_xid)
232
{
233
  GList *tmp_list;
Jeff Garzik's avatar
Jeff Garzik committed
234
  GdkDragContext *context;
235
  GdkX11DragContext *context_x11;
236
  Window context_dest_xid;
237

238
  for (tmp_list = contexts; tmp_list; tmp_list = tmp_list->next)
239
    {
Jeff Garzik's avatar
Jeff Garzik committed
240
      context = (GdkDragContext *)tmp_list->data;
241
      context_x11 = (GdkX11DragContext *)context;
242

243
      if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
244
          (context->dest_window && gdk_window_get_display (context->dest_window) != display))
Matthias Clasen's avatar
Matthias Clasen committed
245
        continue;
246

Matthias Clasen's avatar
Matthias Clasen committed
247 248 249 250 251
      context_dest_xid = context->dest_window
                            ? (context_x11->drop_xid
                                  ? context_x11->drop_xid
                                  : GDK_WINDOW_XID (context->dest_window))
                            : None;
252 253

      if ((!context->is_source == !is_source) &&
Matthias Clasen's avatar
Matthias Clasen committed
254 255 256 257
          ((source_xid == None) || (context->source_window &&
            (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
          ((dest_xid == None) || (context_dest_xid == dest_xid)))
        return context;
258
    }
Matthias Clasen's avatar
Matthias Clasen committed
259

260 261 262
  return NULL;
}

263 264 265 266 267 268 269 270 271 272
static void
precache_target_list (GdkDragContext *context)
{
  if (context->targets)
    {
      GPtrArray *targets = g_ptr_array_new ();
      GList *tmp_list;
      int i;

      for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
273
        g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
274 275

      _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
276 277
                               (const gchar **)targets->pdata,
                               targets->len);
278 279

      for (i =0; i < targets->len; i++)
280
        g_free (targets->pdata[i]);
281 282 283 284 285

      g_ptr_array_free (targets, TRUE);
    }
}

286 287
/* Utility functions */

288 289 290 291 292
static void
free_cache_child (GdkCacheChild *child,
                  GdkDisplay    *display)
{
  if (child->shape)
Benjamin Otte's avatar
Benjamin Otte committed
293
    cairo_region_destroy (child->shape);
294 295 296

  if (child->shape_selected && display)
    {
297
      GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
298 299 300 301 302 303 304

      XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
    }

  g_free (child);
}

305 306
static void
gdk_window_cache_add (GdkWindowCache *cache,
307 308 309 310 311 312
                      guint32         xid,
                      gint            x,
                      gint            y,
                      gint            width,
                      gint            height,
                      gboolean        mapped)
313 314 315 316 317 318 319 320 321
{
  GdkCacheChild *child = g_new (GdkCacheChild, 1);

  child->xid = xid;
  child->x = x;
  child->y = y;
  child->width = width;
  child->height = height;
  child->mapped = mapped;
322 323 324
  child->shape_selected = FALSE;
  child->shape_valid = FALSE;
  child->shape = NULL;
325 326

  cache->children = g_list_prepend (cache->children, child);
327 328
  g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
                       cache->children);
329 330
}

331 332 333 334 335 336 337 338
static GdkFilterReturn
gdk_window_cache_shape_filter (GdkXEvent *xev,
                               GdkEvent  *event,
                               gpointer   data)
{
  XEvent *xevent = (XEvent *)xev;
  GdkWindowCache *cache = data;

339
  GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

  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)
            {
Benjamin Otte's avatar
Benjamin Otte committed
355
              cairo_region_destroy (child->shape);
356 357 358 359 360 361 362 363 364 365
              child->shape = NULL;
            }
        }

      return GDK_FILTER_REMOVE;
    }

  return GDK_FILTER_CONTINUE;
}

366 367
static GdkFilterReturn
gdk_window_cache_filter (GdkXEvent *xev,
368 369
                         GdkEvent  *event,
                         gpointer   data)
370 371 372 373 374 375 376 377 378 379
{
  XEvent *xevent = (XEvent *)xev;
  GdkWindowCache *cache = data;

  switch (xevent->type)
    {
    case CirculateNotify:
      break;
    case ConfigureNotify:
      {
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        XConfigureEvent *xce = &xevent->xconfigure;
        GList *node;

        node = g_hash_table_lookup (cache->child_hash,
                                    GUINT_TO_POINTER (xce->window));
        if (node)
          {
            GdkCacheChild *child = node->data;
            child->x = xce->x;
            child->y = xce->y;
            child->width = xce->width;
            child->height = xce->height;
            if (xce->above == None && (node->next))
              {
                GList *last = g_list_last (cache->children);
                cache->children = g_list_remove_link (cache->children, node);
                last->next = node;
                node->next = NULL;
                node->prev = last;
              }
            else
              {
                GList *above_node = g_hash_table_lookup (cache->child_hash,
                                                         GUINT_TO_POINTER (xce->above));
                if (above_node && node->next != above_node)
                  {
                    /* Put the window above (before in the list) above_node */
                    cache->children = g_list_remove_link (cache->children, node);
                    node->prev = above_node->prev;
                    if (node->prev)
                      node->prev->next = node;
                    else
                      cache->children = node;
                    node->next = above_node;
                    above_node->prev = node;
                  }
              }
          }
        break;
419 420 421
      }
    case CreateNotify:
      {
422 423 424 425 426 427 428 429
        XCreateWindowEvent *xcwe = &xevent->xcreatewindow;

        if (!g_hash_table_lookup (cache->child_hash,
                                  GUINT_TO_POINTER (xcwe->window)))
          gdk_window_cache_add (cache, xcwe->window,
                                xcwe->x, xcwe->y, xcwe->width, xcwe->height,
                                FALSE);
        break;
430 431 432
      }
    case DestroyNotify:
      {
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
        GList *node;

        node = g_hash_table_lookup (cache->child_hash,
                                    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);
            /* window is destroyed, no need to disable ShapeNotify */
            free_cache_child (child, NULL);
            g_list_free_1 (node);
          }
        break;
450 451 452
      }
    case MapNotify:
      {
453 454 455 456 457 458 459 460 461 462 463
        XMapEvent *xme = &xevent->xmap;
        GList *node;

        node = g_hash_table_lookup (cache->child_hash,
                                    GUINT_TO_POINTER (xme->window));
        if (node)
          {
            GdkCacheChild *child = node->data;
            child->mapped = TRUE;
          }
        break;
464 465 466 467 468
      }
    case ReparentNotify:
      break;
    case UnmapNotify:
      {
469 470 471 472 473 474 475 476 477 478 479
        XMapEvent *xume = &xevent->xmap;
        GList *node;

        node = g_hash_table_lookup (cache->child_hash,
                                    GUINT_TO_POINTER (xume->window));
        if (node)
          {
            GdkCacheChild *child = node->data;
            child->mapped = FALSE;
          }
        break;
480 481 482 483 484 485 486 487
      }
    default:
      return GDK_FILTER_CONTINUE;
    }
  return GDK_FILTER_REMOVE;
}

static GdkWindowCache *
488
gdk_window_cache_new (GdkScreen *screen)
489 490
{
  XWindowAttributes xwa;
491 492
  Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
  GdkWindow *root_window = gdk_screen_get_root_window (screen);
493 494
  GdkChildInfoX11 *children;
  guint nchildren, i;
495
#ifdef HAVE_XCOMPOSITE
496
  Window cow;
497
#endif
498

499 500 501 502
  GdkWindowCache *result = g_new (GdkWindowCache, 1);

  result->children = NULL;
  result->child_hash = g_hash_table_new (g_direct_hash, NULL);
503
  result->screen = screen;
504
  result->ref_count = 1;
505

506
  XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
507
  result->old_event_mask = xwa.your_event_mask;
508

509
  if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_X11_SCREEN (screen)->display)->trusted_client))
510 511 512 513
    {
      GList *toplevel_windows, *list;
      GdkWindow *window;
      gint x, y, width, height;
514

515
      toplevel_windows = gdk_screen_get_toplevel_windows (screen);
516 517 518 519 520 521 522 523
      for (list = toplevel_windows; list; list = list->next)
        {
          window = GDK_WINDOW (list->data);
          gdk_window_get_geometry (window, &x, &y, &width, &height);
          gdk_window_cache_add (result, GDK_WINDOW_XID (window),
                                x, y, width, height,
                                gdk_window_is_visible (window));
        }
524 525 526 527
      g_list_free (toplevel_windows);
      return result;
    }

528
  XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
529
                result->old_event_mask | SubstructureNotifyMask);
530
  gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
531
  gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
532

533
  if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
534 535 536
                                       GDK_WINDOW_XID (root_window),
                                       FALSE, NULL,
                                       &children, &nchildren))
537
    return result;
538

539 540
  for (i = 0; i < nchildren ; i++)
    {
541
      gdk_window_cache_add (result, children[i].window,
542 543
                            children[i].x, children[i].y, children[i].width, children[i].height,
                            children[i].is_mapped);
544 545
    }

546
  g_free (children);
547

548
#ifdef HAVE_XCOMPOSITE
549 550 551 552 553 554 555 556 557
  /*
   * 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))
    {
558
      cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
559
      gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
560
      XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
561
    }
562
#endif
563

564 565 566 567 568 569
  return result;
}

static void
gdk_window_cache_destroy (GdkWindowCache *cache)
{
570
  GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
571
  GdkDisplay *display;
572 573

  XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
574 575
                GDK_WINDOW_XID (root_window),
                cache->old_event_mask);
576
  gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
577
  gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
578

579
  display = gdk_screen_get_display (cache->screen);
580

581 582 583
  gdk_x11_display_error_trap_push (display);
  g_list_foreach (cache->children, (GFunc)free_cache_child, display);
  gdk_x11_display_error_trap_pop_ignored (display);
584

585 586
  g_list_free (cache->children);
  g_hash_table_destroy (cache->child_hash);
587 588

  g_free (cache);
589 590
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
static GdkWindowCache *
gdk_window_cache_ref (GdkWindowCache *cache)
{
  cache->ref_count += 1;

  return cache;
}

static void
gdk_window_cache_unref (GdkWindowCache *cache)
{
  g_assert (cache->ref_count > 0);

  cache->ref_count -= 1;

  if (cache->ref_count == 0)
    {
      window_caches = g_slist_remove (window_caches, cache);
      gdk_window_cache_destroy (cache);
    }
}

GdkWindowCache *
gdk_window_cache_get (GdkScreen *screen)
{
  GSList *list;
  GdkWindowCache *cache;

  for (list = window_caches; list; list = list->next)
    {
      cache = list->data;
      if (cache->screen == screen)
        return gdk_window_cache_ref (cache);
    }

626
  cache = gdk_window_cache_new (screen);
627

628
  window_caches = g_slist_prepend (window_caches, cache);
629

630
  return cache;
631 632
}

633 634 635 636 637
static gboolean
is_pointer_within_shape (GdkDisplay    *display,
                         GdkCacheChild *child,
                         gint           x_pos,
                         gint           y_pos)
638
{
639
  if (!child->shape_selected)
640
    {
641
      GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
642

643 644 645 646 647 648 649
      XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
      child->shape_selected = TRUE;
    }
  if (!child->shape_valid)
    {
      GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
      cairo_region_t *input_shape;
650

651 652 653 654 655 656 657 658 659
      child->shape = NULL;
      if (gdk_display_supports_shapes (display))
        child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
                                                   child->xid, ShapeBounding);
#ifdef ShapeInput
      input_shape = NULL;
      if (gdk_display_supports_input_shapes (display))
        input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
                                                  child->xid, ShapeInput);
660

661 662 663 664 665 666 667 668 669 670
      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
671

672
      child->shape_valid = TRUE;
673 674
    }

675 676
  return child->shape == NULL ||
         cairo_region_contains_point (child->shape, x_pos, y_pos);
677 678
}

679 680 681 682 683 684
static Window
get_client_window_at_coords_recurse (GdkDisplay *display,
                                     Window      win,
                                     gboolean    is_toplevel,
                                     gint        x,
                                     gint        y)
685
{
686 687 688 689 690 691
  GdkChildInfoX11 *children;
  unsigned int nchildren;
  int i;
  gboolean found_child = FALSE;
  GdkChildInfoX11 child = { 0, };
  gboolean has_wm_state = FALSE;
692

693 694 695 696
  if (!_gdk_x11_get_window_child_info (display, win, TRUE,
                                       is_toplevel? &has_wm_state : NULL,
                                       &children, &nchildren))
    return None;
697

698 699 700
  if (has_wm_state)
    {
      g_free (children);
701

702 703
      return win;
    }
704

705 706 707
  for (i = nchildren - 1; (i >= 0) && !found_child; i--)
    {
      GdkChildInfoX11 *cur_child = &children[i];
708

709 710 711 712 713 714 715 716 717 718
      if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
          (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
          (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
        {
          x -= cur_child->x;
          y -= cur_child->y;
          child = *cur_child;
          found_child = TRUE;
        }
    }
719

720
  g_free (children);
721

722 723 724 725 726 727 728 729 730
  if (found_child)
    {
      if (child.has_wm_state)
        return child.window;
      else
        return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
    }
  else
    return None;
731
}
732

733 734 735 736 737
static Window
get_client_window_at_coords (GdkWindowCache *cache,
                             Window          ignore,
                             gint            x_root,
                             gint            y_root)
738
{
739 740
  GList *tmp_list;
  Window retval = None;
741
  GdkDisplay *display;
742

743
  display = gdk_screen_get_display (cache->screen);
744

745
  gdk_x11_display_error_trap_push (display);
746

747
  tmp_list = cache->children;
748

749
  while (tmp_list && !retval)
750
    {
751
      GdkCacheChild *child = tmp_list->data;
752

753
      if ((child->xid != ignore) && (child->mapped))
754
        {
755 756
          if ((x_root >= child->x) && (x_root < child->x + child->width) &&
              (y_root >= child->y) && (y_root < child->y + child->height))
757
            {
758 759 760 761 762 763 764 765 766 767 768 769 770 771
              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;
772 773
            }
        }
774
      tmp_list = tmp_list->next;
775
    }
776

777
  gdk_x11_display_error_trap_pop_ignored (display);
778

779 780 781 782 783
  if (retval)
    return retval;
  else
    return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
}
784

785 786 787 788 789
#ifdef G_ENABLE_DEBUG
static void
print_target_list (GList *targets)
{
  while (targets)
790
    {
791 792 793 794
      gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
      g_message ("\t%s", name);
      g_free (name);
      targets = targets->next;
795 796
    }
}
797
#endif /* G_ENABLE_DEBUG */
798 799 800 801 802 803 804 805

/*************************************************************
 ***************************** XDND **************************
 *************************************************************/

/* Utility functions */

static struct {
806
  const gchar *name;
807
  GdkAtom atom;
808 809
  GdkDragAction action;
} xdnd_actions_table[] = {
810 811 812 813 814
    { "XdndActionCopy",    None, GDK_ACTION_COPY },
    { "XdndActionMove",    None, GDK_ACTION_MOVE },
    { "XdndActionLink",    None, GDK_ACTION_LINK },
    { "XdndActionAsk",     None, GDK_ACTION_ASK  },
    { "XdndActionPrivate", None, GDK_ACTION_COPY },
815 816
  };

817
static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
818 819 820 821 822 823
static gboolean xdnd_actions_initialized = FALSE;

static void
xdnd_initialize_actions (void)
{
  gint i;
824

825
  xdnd_actions_initialized = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
826
  for (i = 0; i < xdnd_n_actions; i++)
Matthias Clasen's avatar
Matthias Clasen committed
827
    xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
828 829 830
}

static GdkDragAction
831
xdnd_action_from_atom (GdkDisplay *display,
832
                       Atom        xatom)
833
{
834
  GdkAtom atom;
835 836
  gint i;

837 838 839 840 841
  if (xatom == None)
    return 0;

  atom = gdk_x11_xatom_to_atom_for_display (display, xatom);

842 843 844
  if (!xdnd_actions_initialized)
    xdnd_initialize_actions();

Matthias Clasen's avatar
Matthias Clasen committed
845
  for (i = 0; i < xdnd_n_actions; i++)
846 847 848 849 850 851
    if (atom == xdnd_actions_table[i].atom)
      return xdnd_actions_table[i].action;

  return 0;
}

852
static Atom
853
xdnd_action_to_atom (GdkDisplay    *display,
854
                     GdkDragAction  action)
855 856 857 858 859 860
{
  gint i;

  if (!xdnd_actions_initialized)
    xdnd_initialize_actions();

Matthias Clasen's avatar
Matthias Clasen committed
861
  for (i = 0; i < xdnd_n_actions; i++)
862
    if (action == xdnd_actions_table[i].action)
863
      return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
864

865
  return None;
866 867 868 869
}

/* Source side */

870
static GdkFilterReturn
871
xdnd_status_filter (GdkXEvent *xev,
872 873
                    GdkEvent  *event,
                    gpointer   data)
874
{
875
  GdkDisplay *display;
876 877 878
  XEvent *xevent = (XEvent *)xev;
  guint32 dest_window = xevent->xclient.data.l[0];
  guint32 flags = xevent->xclient.data.l[1];
879
  Atom action = xevent->xclient.data.l[4];
880
  GdkDragContext *context;
881 882 883

  if (!event->any.window ||
      gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
884 885 886 887 888
    return GDK_FILTER_CONTINUE;                 /* Not for us */

  GDK_NOTE (DND,
            g_message ("XdndStatus: dest_window: %#x  action: %ld",
                       dest_window, action));
889

890
  display = gdk_window_get_display (event->any.window);
891
  context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
892

893 894
  if (context)
    {
895
      GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
Matthias Clasen's avatar
Matthias Clasen committed
896 897
      if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
        context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
898

899 900 901
      event->dnd.send_event = FALSE;
      event->dnd.type = GDK_DRAG_STATUS;
      event->dnd.context = context;
902
      gdk_event_set_device (event, gdk_drag_context_get_device (context));
903
      g_object_ref (context);
904 905 906

      event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
      if (!(action != 0) != !(flags & 1))
907 908 909 910 911
        {
          GDK_NOTE (DND,
                    g_warning ("Received status event with flags not corresponding to action!\n"));
          action = 0;
        }
912

913
      context->action = xdnd_action_from_atom (display, action);
914 915 916 917 918 919 920

      return GDK_FILTER_TRANSLATE;
    }

  return GDK_FILTER_REMOVE;
}

921
static GdkFilterReturn
922
xdnd_finished_filter (GdkXEvent *xev,
923 924
                      GdkEvent  *event,
                      gpointer   data)
925
{
926
  GdkDisplay *display;
927 928 929
  XEvent *xevent = (XEvent *)xev;
  guint32 dest_window = xevent->xclient.data.l[0];
  GdkDragContext *context;
930
  GdkX11DragContext *context_x11;
931 932 933

  if (!event->any.window ||
      gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
934 935 936 937
    return GDK_FILTER_CONTINUE;                 /* Not for us */

  GDK_NOTE (DND,
            g_message ("XdndFinished: dest_window: %#x", dest_window));
938

939
  display = gdk_window_get_display (event->any.window);
940
  context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
941

942 943
  if (context)
    {
944
      context_x11 = GDK_X11_DRAG_CONTEXT (context);
Matthias Clasen's avatar
Matthias Clasen committed
945
      if (context_x11->version == 5)
946
        context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
Matthias Clasen's avatar
Matthias Clasen committed
947

948 949
      event->dnd.type = GDK_DROP_FINISHED;
      event->dnd.context = context;
950
      gdk_event_set_device (event, gdk_drag_context_get_device (context));
951
      g_object_ref (context);
952

953 954
      event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */

955 956 957 958 959 960 961
      return GDK_FILTER_TRANSLATE;
    }

  return GDK_FILTER_REMOVE;
}

static void
962
xdnd_set_targets (GdkX11DragContext *context_x11)
963
{
Matthias Clasen's avatar
Matthias Clasen committed
964
  GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
965
  Atom *atomlist;
966 967
  GList *tmp_list = context->targets;
  gint i;
968
  gint n_atoms = g_list_length (context->targets);
969
  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
970

971
  atomlist = g_new (Atom, n_atoms);
972 973 974
  i = 0;
  while (tmp_list)
    {
975
      atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
976 977 978 979
      tmp_list = tmp_list->next;
      i++;
    }

980
  XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
981 982 983 984
                   GDK_WINDOW_XID (context->source_window),
                   gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
                   XA_ATOM, 32, PropModeReplace,
                   (guchar *)atomlist, n_atoms);
985

986 987
  g_free (atomlist);

Matthias Clasen's avatar
Matthias Clasen committed
988
  context_x11->xdnd_targets_set = 1;
989 990
}

991
static void
992
xdnd_set_actions (GdkX11DragContext *context_x11)
993
{
Matthias Clasen's avatar
Matthias Clasen committed
994
  GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
995
  Atom *atomlist;
996 997 998
  gint i;
  gint n_atoms;
  guint actions;
999
  GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1000 1001 1002

  if (!xdnd_actions_initialized)
    xdnd_initialize_actions();
1003

1004 1005
  actions = context->actions;
  n_atoms = 0;
Matthias Clasen's avatar
Matthias Clasen committed
1006
  for (i = 0; i < xdnd_n_actions; i++)
1007 1008
    {
      if (actions & xdnd_actions_table[i].action)
1009 1010 1011 1012
        {
          actions &= ~xdnd_actions_table[i].action;
          n_atoms++;
        }
1013 1014
    }

1015
  atomlist = g_new (Atom, n_atoms);
1016 1017 1018

  actions = context->actions;
  n_atoms = 0;
Matthias Clasen's avatar
Matthias Clasen committed
1019
  for (i = 0; i < xdnd_n_actions; i++)
1020 1021
    {
      if (actions & xdnd_actions_table[i].action)
1022 1023 1024 1025 1026
        {
          actions &= ~xdnd_actions_table[i].action;
          atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
          n_atoms++;
        }
1027 1028
    }

1029
  XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1030 1031 1032 1033
                   GDK_WINDOW_XID (context->source_window),
                   gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
                   XA_ATOM, 32, PropModeReplace,
                   (guchar *)atomlist, n_atoms);
1034

1035 1036
  g_free (atomlist);

Matthias Clasen's avatar
Matthias Clasen committed
1037 1038
  context_x11->xdnd_actions_set = TRUE;
  context_x11->xdnd_actions = context->actions;
1039 1040
}

1041
static void
1042
send_client_message_async_cb (Window   window,
1043 1044
                              gboolean success,
                              gpointer data)
1045 1046 1047
{
  GdkDragContext *context = data;
  GDK_NOTE (DND,
1048 1049
            g_message ("Got async callback for #%lx, success = %d",
                       window, success));
1050 1051 1052 1053 1054 1055 1056 1057

  /* On failure, we immediately continue with the protocol
   * so we don't end up blocking for a timeout
   */
  if (!success &&
      context->dest_window &&
      window == GDK_WINDOW_XID (context->dest_window))
    {
1058
      GdkEvent *temp_event;
1059
      GdkX11DragContext *context_x11 = data;