gdkdnd-x11.c 110 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
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19 20

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

27
#include "config.h"
28

29
#include <X11/Xlib.h>
30
#include <X11/Xutil.h>
31
#include <X11/Xatom.h>
32
#include <X11/extensions/shape.h>
33
#include <X11/extensions/Xcomposite.h>
34

35
#include <string.h>
Owen Taylor's avatar
Started  
Owen Taylor committed
36 37

#include "gdk.h"          /* For gdk_flush() */
38
#include "gdkx.h"
39
#include "gdkasync.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
40 41
#include "gdkdnd.h"
#include "gdkproperty.h"
42
#include "gdkprivate-x11.h"
43 44 45
#include "gdkinternals.h"
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
46

47
typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
48

49 50 51 52 53 54 55 56 57 58 59
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;
60 61
  gboolean shape_selected;
  gboolean shape_valid;
62
  cairo_region_t *shape;
63 64 65 66 67 68
} GdkCacheChild;

typedef struct {
  GList *children;
  GHashTable *child_hash;
  guint old_event_mask;
69
  GdkScreen *screen;
70 71 72 73 74
} GdkWindowCache;

/* Structure that holds information about a drag in progress.
 * this is used on both source and destination sides.
 */
75
struct _GdkDragContextPrivateX11 {
76 77
  GdkDragContext context;

78
  Atom motif_selection;
79 80 81 82
  guint   ref_count;

  guint16 last_x;		/* Coordinates from last event */
  guint16 last_y;
83 84 85
  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 */
86

87 88
  Window dest_xid;              /* The last window we looked up */
  Window drop_xid;            /* The (non-proxied) window that is receiving drops */
89
  guint xdnd_targets_set : 1;   /* Whether we've already set XdndTypeList */
90
  guint xdnd_actions_set : 1;   /* Whether we've already set XdndActionList */
91
  guint xdnd_have_actions : 1;  /* Whether an XdndActionList was provided */
92 93
  guint motif_targets_set : 1;  /* Whether we've already set motif initiator info */
  guint drag_status : 4;	/* current status of drag */
94 95 96
  
  guint drop_failed : 1;        /* Whether the drop was unsuccessful */
  guint version;                /* Xdnd protocol version */
97

98
  GSList *window_caches;
99
  GdkDevice *device;
100 101
};

102 103
#define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)

104 105 106 107
/* Forward declarations */

static void gdk_window_cache_destroy (GdkWindowCache *cache);

108 109
static void motif_read_target_table (GdkDisplay *display);

110 111 112 113
static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
					 GdkEvent  *event,
					 gpointer   data);

114 115 116 117 118 119
static GdkFilterReturn xdnd_enter_filter    (GdkXEvent *xev,
					     GdkEvent  *event,
					     gpointer   data);
static GdkFilterReturn xdnd_leave_filter    (GdkXEvent *xev,
					     GdkEvent  *event,
					     gpointer   data);
120 121 122
static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
					     GdkEvent  *event,
					     gpointer   data);
123 124 125
static GdkFilterReturn xdnd_status_filter   (GdkXEvent *xev,
					     GdkEvent  *event,
					     gpointer   data);
126
static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
127 128 129 130 131
					     GdkEvent  *event,
					     gpointer   data);
static GdkFilterReturn xdnd_drop_filter     (GdkXEvent *xev,
					     GdkEvent  *event,
					     gpointer   data);
132

133 134 135 136
static void   xdnd_manage_source_filter (GdkDragContext *context,
					 GdkWindow      *window,
					 gboolean        add_filter);

137
static void gdk_drag_context_finalize   (GObject              *object);
138 139 140

static GList *contexts;

Matthias Clasen's avatar
Matthias Clasen committed
141
static const struct {
142 143
  const char *atom_name;
  GdkFilterFunc func;
Matthias Clasen's avatar
Matthias Clasen committed
144
} xdnd_filters[] = {
145 146 147 148 149 150 151 152
  { "XdndEnter",    xdnd_enter_filter },
  { "XdndLeave",    xdnd_leave_filter },
  { "XdndPosition", xdnd_position_filter },
  { "XdndStatus",   xdnd_status_filter },
  { "XdndFinished", xdnd_finished_filter },
  { "XdndDrop",     xdnd_drop_filter },
};
	      
Matthias Clasen's avatar
Matthias Clasen committed
153
G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
154

155 156 157 158
static void
gdk_drag_context_init (GdkDragContext *dragcontext)
{
  GdkDragContextPrivateX11 *private;
159

Matthias Clasen's avatar
Matthias Clasen committed
160 161 162 163
  private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext, 
					 GDK_TYPE_DRAG_CONTEXT, 
					 GdkDragContextPrivateX11);
  
164
  dragcontext->windowing_data = private;
165

166
  contexts = g_list_prepend (contexts, dragcontext);
167 168
}

169 170
static void
gdk_drag_context_class_init (GdkDragContextClass *klass)
171
{
172 173 174
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gdk_drag_context_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
175 176

  g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
177 178
}

179 180
static void
gdk_drag_context_finalize (GObject *object)
181
{
182 183
  GdkDragContext *context = GDK_DRAG_CONTEXT (object);
  GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
184
  GSList *tmp_list;
185
  
186 187 188
  g_list_free (context->targets);

  if (context->source_window)
189
    {
190 191 192
      if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
          !context->is_source)
        xdnd_manage_source_filter (context, context->source_window, FALSE);
193
      
194
      g_object_unref (context->source_window);
195 196 197
    }
  
  if (context->dest_window)
198
    g_object_unref (context->dest_window);
199 200 201 202

  for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
    gdk_window_cache_destroy (tmp_list->data);
  g_slist_free (private->window_caches);
203 204
  
  contexts = g_list_remove (contexts, context);
205

Matthias Clasen's avatar
Matthias Clasen committed
206
  G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
207
}
208

209 210
/* Drag Contexts */

Matthias Clasen's avatar
Matthias Clasen committed
211 212 213 214 215 216 217
/**
 * gdk_drag_context_new:
 * 
 * Creates a new #GdkDragContext.
 * 
 * Return value: the newly created #GdkDragContext.
 **/
218
GdkDragContext *
Matthias Clasen's avatar
Matthias Clasen committed
219
gdk_drag_context_new (void)
220
{
Matthias Clasen's avatar
Matthias Clasen committed
221
  return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
222 223
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/**
 * 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_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));

  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_get_device:
 * @context: a #GdkDragContext
 *
 * Returns the #GdkDevice associated to the drag context.
 *
 * Returns: The #GdkDevice associated to @context.
 **/
GdkDevice *
gdk_drag_context_get_device (GdkDragContext *context)
{
  GdkDragContextPrivateX11 *private;

  g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);

  private = PRIVATE_DATA (context);

  return private->device;
}

273
static GdkDragContext *
274 275 276 277
gdk_drag_context_find (GdkDisplay *display,
		       gboolean    is_source,
		       Window      source_xid,
		       Window      dest_xid)
278 279
{
  GList *tmp_list = contexts;
Jeff Garzik's avatar
Jeff Garzik committed
280
  GdkDragContext *context;
281
  GdkDragContextPrivateX11 *private;
282
  Window context_dest_xid;
283 284 285

  while (tmp_list)
    {
Jeff Garzik's avatar
Jeff Garzik committed
286
      context = (GdkDragContext *)tmp_list->data;
287
      private = PRIVATE_DATA (context);
288

289 290 291 292
      if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
	  (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
	continue;

293
      context_dest_xid = context->dest_window ? 
294 295 296 297
                            (private->drop_xid ?
                              private->drop_xid :
                              GDK_DRAWABLE_XID (context->dest_window)) :
	                     None;
298 299 300

      if ((!context->is_source == !is_source) &&
	  ((source_xid == None) || (context->source_window &&
301
	    (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
302
	  ((dest_xid == None) || (context_dest_xid == dest_xid)))
303 304 305 306 307 308 309 310
	return context;
      
      tmp_list = tmp_list->next;
    }
  
  return NULL;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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)
	g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));

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

      for (i =0; i < targets->len; i++)
	g_free (targets->pdata[i]);

      g_ptr_array_free (targets, TRUE);
    }
}

334 335
/* Utility functions */

336 337 338 339 340
static void
free_cache_child (GdkCacheChild *child,
                  GdkDisplay    *display)
{
  if (child->shape)
Benjamin Otte's avatar
Benjamin Otte committed
341
    cairo_region_destroy (child->shape);
342 343 344 345 346 347 348 349 350 351 352

  if (child->shape_selected && display)
    {
      GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);

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

  g_free (child);
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366
static void
gdk_window_cache_add (GdkWindowCache *cache,
		      guint32 xid,
		      gint x, gint y, gint width, gint height, 
		      gboolean mapped)
{
  GdkCacheChild *child = g_new (GdkCacheChild, 1);

  child->xid = xid;
  child->x = x;
  child->y = y;
  child->width = width;
  child->height = height;
  child->mapped = mapped;
367 368 369
  child->shape_selected = FALSE;
  child->shape_valid = FALSE;
  child->shape = NULL;
370 371 372 373 374 375

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

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
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)
            {
Benjamin Otte's avatar
Benjamin Otte committed
400
              cairo_region_destroy (child->shape);
401 402 403 404 405 406 407 408 409 410
              child->shape = NULL;
            }
        }

      return GDK_FILTER_REMOVE;
    }

  return GDK_FILTER_CONTINUE;
}

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static GdkFilterReturn
gdk_window_cache_filter (GdkXEvent *xev,
			 GdkEvent  *event,
			 gpointer   data)
{
  XEvent *xevent = (XEvent *)xev;
  GdkWindowCache *cache = data;

  switch (xevent->type)
    {
    case CirculateNotify:
      break;
    case ConfigureNotify:
      {
	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));
449
		if (above_node && node->next != above_node)
450
		  {
451 452
		    /* Put the window above (before in the list) above_node
		     */
453
		    cache->children = g_list_remove_link (cache->children, node);
454 455 456
		    node->prev = above_node->prev;
		    if (node->prev)
		      node->prev->next = node;
457 458
		    else
		      cache->children = node;
459 460
		    node->next = above_node;
		    above_node->prev = node;
461 462 463 464 465 466 467 468
		  }
	      }
	  }
	break;
      }
    case CreateNotify:
      {
	XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	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;
      }
    case DestroyNotify:
      {
	XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
	GList *node;

	node = g_hash_table_lookup (cache->child_hash, 
				    GUINT_TO_POINTER (xdwe->window));
	if (node) 
	  {
486 487
	    GdkCacheChild *child = node->data;

488 489 490
	    g_hash_table_remove (cache->child_hash,
				 GUINT_TO_POINTER (xdwe->window));
	    cache->children = g_list_remove_link (cache->children, node);
491 492
	    /* window is destroyed, no need to disable ShapeNotify */
	    free_cache_child (child, NULL);
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	    g_list_free_1 (node);
	  }
	break;
      }
    case MapNotify:
      {
	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;
      }
    case ReparentNotify:
      break;
    case UnmapNotify:
      {
	XMapEvent *xume = &xevent->xmap;
	GList *node;

	node = g_hash_table_lookup (cache->child_hash, 
				    GUINT_TO_POINTER (xume->window));
520
	if (node)
521 522 523 524 525 526 527 528 529 530 531 532 533
	  {
	    GdkCacheChild *child = node->data;
	    child->mapped = FALSE;
	  }
	break;
      }
    default:
      return GDK_FILTER_CONTINUE;
    }
  return GDK_FILTER_REMOVE;
}

static GdkWindowCache *
534
gdk_window_cache_new (GdkScreen *screen)
535 536
{
  XWindowAttributes xwa;
537 538
  Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
  GdkWindow *root_window = gdk_screen_get_root_window (screen);
539 540
  GdkChildInfoX11 *children;
  guint nchildren, i;
541
  Window cow;
542 543 544 545 546
  
  GdkWindowCache *result = g_new (GdkWindowCache, 1);

  result->children = NULL;
  result->child_hash = g_hash_table_new (g_direct_hash, NULL);
547
  result->screen = screen;
548

549
  XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
550
  result->old_event_mask = xwa.your_event_mask;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

  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;
    }

570
  XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
571
		result->old_event_mask | SubstructureNotifyMask);
572
  gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
573
  gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
574

575 576
  if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
				       GDK_WINDOW_XWINDOW (root_window),
577 578
				       FALSE, NULL,
				       &children, &nchildren))
579
    return result;
580

581 582
  for (i = 0; i < nchildren ; i++)
    {
583 584 585
      gdk_window_cache_add (result, children[i].window,
			    children[i].x, children[i].y, children[i].width, children[i].height,
			    children[i].is_mapped);
586 587
    }

588
  g_free (children);
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603
  /*
   * 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));
    }

604 605 606 607 608 609
  return result;
}

static void
gdk_window_cache_destroy (GdkWindowCache *cache)
{
610
  GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
611 612

  XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
613 614 615
		GDK_WINDOW_XWINDOW (root_window),
		cache->old_event_mask);
  gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
616
  gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
617

618 619 620
  gdk_error_trap_push ();

  g_list_foreach (cache->children, (GFunc)free_cache_child,
621
      gdk_screen_get_display (cache->screen));
622 623 624 625

  gdk_flush ();
  gdk_error_trap_pop ();

626 627
  g_list_free (cache->children);
  g_hash_table_destroy (cache->child_hash);
628 629

  g_free (cache);
630 631
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
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);
648
      cairo_region_t *input_shape;
649 650 651 652 653 654 655 656

      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)
        {
Benjamin Otte's avatar
Benjamin Otte committed
657 658
          cairo_region_intersect (child->shape, input_shape);
          cairo_region_destroy (input_shape);
659 660 661 662 663 664 665 666 667 668 669
        }
      else if (input_shape)
        {
          child->shape = input_shape;
        }
#endif

      child->shape_valid = TRUE;
    }

  return child->shape == NULL ||
Benjamin Otte's avatar
Benjamin Otte committed
670
         cairo_region_contains_point (child->shape, x_pos, y_pos);
671 672
}

673 674 675
static Window
get_client_window_at_coords_recurse (GdkDisplay *display,
				     Window      win,
676
				     gboolean    is_toplevel,
677 678 679 680 681 682 683
				     gint        x,
				     gint        y)
{
  GdkChildInfoX11 *children;
  unsigned int nchildren;
  int i;
  gboolean found_child = FALSE;
684
  GdkChildInfoX11 child = { 0, };
685
  gboolean has_wm_state = FALSE;
686 687

  if (!_gdk_x11_get_window_child_info (display, win, TRUE,
688
				       is_toplevel? &has_wm_state : NULL,
689
				       &children, &nchildren))
690
    return None;
691

692
  if (has_wm_state)
693 694 695 696 697
    {
      g_free (children);

      return win;
    }
698

699
  for (i = nchildren - 1; (i >= 0) && !found_child; i--)
700
    {
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
      GdkChildInfoX11 *cur_child = &children[i];
       
      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;
 	}
    }
   
  g_free (children);
 
  if (found_child)
    {
      if (child.has_wm_state)
	return child.window;
      else
721
	return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
722
    }
723 724 725 726
  else
    return None;
}

Jeff Garzik's avatar
Jeff Garzik committed
727
static Window 
728
get_client_window_at_coords (GdkWindowCache *cache,
729 730 731
			     Window          ignore,
			     gint            x_root,
			     gint            y_root)
732 733 734 735
{
  GList *tmp_list;
  Window retval = None;

736
  gdk_error_trap_push ();
737 738 739 740 741 742 743 744
  
  tmp_list = cache->children;

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

      if ((child->xid != ignore) && (child->mapped))
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
        {
          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;
            }
        }
767 768 769
      tmp_list = tmp_list->next;
    }

770
  gdk_error_trap_pop ();
771 772 773 774
  
  if (retval)
    return retval;
  else
775
    return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
}

/*************************************************************
 ***************************** MOTIF *************************
 *************************************************************/

/* values used in the message type for Motif DND */
enum {
    XmTOP_LEVEL_ENTER,
    XmTOP_LEVEL_LEAVE,
    XmDRAG_MOTION,
    XmDROP_SITE_ENTER,
    XmDROP_SITE_LEAVE,
    XmDROP_START,
    XmDROP_FINISH,
    XmDRAG_DROP_FINISH,
    XmOPERATION_CHANGED
};

/* Values used to specify type of protocol to use */
enum {
    XmDRAG_NONE,
    XmDRAG_DROP_ONLY,
    XmDRAG_PREFER_PREREGISTER,
    XmDRAG_PREREGISTER,
    XmDRAG_PREFER_DYNAMIC,
    XmDRAG_DYNAMIC,
    XmDRAG_PREFER_RECEIVER
};

/* Operation codes */
enum {
  XmDROP_NOOP,
  XmDROP_MOVE = 0x01,
  XmDROP_COPY = 0x02,
  XmDROP_LINK = 0x04
};

/* Drop site status */
enum {
  XmNO_DROP_SITE = 0x01,
  XmDROP_SITE_INVALID = 0x02,
  XmDROP_SITE_VALID = 0x03
};

/* completion status */
enum {
  XmDROP,
  XmDROP_HELP,
  XmDROP_CANCEL,
  XmDROP_INTERRUPT
};

/* Byte swapping routines. The motif specification leaves it
 * up to us to save a few bytes in the client messages
 */
static gchar local_byte_order = '\0';

#ifdef G_ENABLE_DEBUG
static void
print_target_list (GList *targets)
{
  while (targets)
    {
840
      gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
      g_message ("\t%s", name);
      g_free (name);
      targets = targets->next;
    }
}
#endif /* G_ENABLE_DEBUG */

static void
init_byte_order (void)
{
  guint32 myint = 0x01020304;
  local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
}

static guint16
card16_to_host (guint16 x, gchar byte_order) {
  if (byte_order == local_byte_order)
    return x;
  else
    return (x << 8) | (x >> 8);
}

static guint32
card32_to_host (guint32 x, gchar byte_order) {
  if (byte_order == local_byte_order)
    return x;
  else
    return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
}

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
/* Motif packs together fields of varying length into the
 * client message. We can't rely on accessing these
 * through data.s[], data.l[], etc, because on some architectures
 * (i.e., Alpha) these won't be valid for format == 8. 
 */

#define MOTIF_XCLIENT_BYTE(xevent,i) \
  (xevent)->xclient.data.b[i]
#define MOTIF_XCLIENT_SHORT(xevent,i) \
  ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
#define MOTIF_XCLIENT_LONG(xevent,i) \
  ((gint32 *)&((xevent)->xclient.data.b[0]))[i]

#define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
#define MOTIF_UNPACK_SHORT(xevent,i) \
  card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
#define MOTIF_UNPACK_LONG(xevent,i) \
  card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921

/***** Dest side ***********/

/* Property placed on source windows */
typedef struct _MotifDragInitiatorInfo {
  guint8 byte_order;
  guint8 protocol_version;
  guint16 targets_index;
  guint32 selection_atom;
} MotifDragInitiatorInfo;

/* Header for target table on the drag window */
typedef struct _MotifTargetTableHeader {
  guchar byte_order;
  guchar protocol_version;
  guint16 n_lists;
  guint32 total_size;
} MotifTargetTableHeader;

/* Property placed on target windows */
typedef struct _MotifDragReceiverInfo {
  guint8 byte_order;
  guint8 protocol_version;
  guint8 protocol_style;
  guint8 pad;
  guint32 proxy_window;
  guint16 num_drop_sites;
  guint16 padding;
  guint32 total_size;
} MotifDragReceiverInfo;

/* Target table handling */

922
static GdkFilterReturn
923 924 925 926 927
motif_drag_window_filter (GdkXEvent *xevent,
			  GdkEvent  *event,
			  gpointer data)
{
  XEvent *xev = (XEvent *)xevent;
928 929
  GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window); 
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
930 931 932 933

  switch (xev->xany.type)
    {
    case DestroyNotify:
934 935
      display_x11->motif_drag_window = None;
      display_x11->motif_drag_gdk_window = NULL;
936 937
      break;
    case PropertyNotify:
938 939 940
      if (display_x11->motif_target_lists &&
	  (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
	motif_read_target_table (display);
941 942 943 944 945 946
      break;
    }
  return GDK_FILTER_REMOVE;
}

static Window
947 948
motif_lookup_drag_window (GdkDisplay *display,
			  Display    *lookup_xdisplay)
949 950 951
{
  Window retval = None;
  gulong bytes_after, nitems;
952
  Atom type;
953 954 955
  gint format;
  guchar *data;

956
  XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
957
		      gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
958
		      0, 1, FALSE,
959 960 961
		      XA_WINDOW, &type, &format, &nitems, &bytes_after,
		      &data);
  
962
  if ((format == 32) && (nitems == 1) && (bytes_after == 0))
963 964
    {
      retval = *(Window *)data;
965 966
      GDK_NOTE (DND, 
		g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
967 968 969 970 971 972 973 974 975 976 977
    }

  if (type != None)
    XFree (data);

  return retval;
}

/* Finds the window where global Motif drag information is stored.
 * If it doesn't exist and 'create' is TRUE, create one.
 */
978
static Window 
979 980
motif_find_drag_window (GdkDisplay *display,
			gboolean    create)
981
{
982 983 984
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  
  if (!display_x11->motif_drag_window)
985
    {
986 987
      Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
      display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
988
      
989
      if (!display_x11->motif_drag_window && create)
990 991 992
	{
	  /* Create a persistant window. (Copied from LessTif) */
	  
993
	  Display *persistant_xdisplay;
994
	  XSetWindowAttributes attr;
995 996
	  persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
	  XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
997

998
	  XGrabServer (persistant_xdisplay);
999
	  
1000
	  display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1001

1002
	  if (!display_x11->motif_drag_window)
1003 1004 1005 1006
	    {
	      attr.override_redirect = True;
	      attr.event_mask = PropertyChangeMask;
	      
1007 1008
	       display_x11->motif_drag_window = 
		XCreateWindow (persistant_xdisplay, 
1009
			       RootWindow (persistant_xdisplay, 0),
1010
			      -100, -100, 10, 10, 0, 0,
Matthias Clasen's avatar
Matthias Clasen committed
1011
			      InputOnly, (Visual *)CopyFromParent,
1012 1013 1014
			      (CWOverrideRedirect | CWEventMask), &attr);
	      
	      GDK_NOTE (DND,
1015
			g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1016
	      
1017
	      XChangeProperty (persistant_xdisplay, 
1018
			       RootWindow (persistant_xdisplay, 0),
1019 1020 1021 1022 1023
			       motif_drag_window_atom, XA_WINDOW,
			       32, PropModeReplace,
			       (guchar *)&motif_drag_window_atom, 1);

	    }
1024 1025
	  XUngrabServer (persistant_xdisplay);
	  XCloseDisplay (persistant_xdisplay);
1026 1027 1028 1029 1030
	}

      /* There is a miniscule race condition here if the drag window
       * gets destroyed exactly now.
       */
1031
      if (display_x11->motif_drag_window)
1032
	{
1033 1034 1035
	  display_x11->motif_drag_gdk_window = 
	    gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
	  gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1036 1037 1038 1039 1040
				 motif_drag_window_filter,
				 NULL);
	}
    }

1041
  return display_x11->motif_drag_window;
1042 1043 1044
}

static void 
1045
motif_read_target_table (GdkDisplay *display)
1046
{
1047
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1048
  gulong bytes_after, nitems;
1049
  Atom type;
1050 1051
  gint format;
  gint i, j;
1052 1053
  
  Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1054

1055
  if (display_x11->motif_target_lists)
1056
    {
1057 1058
      for (i=0; i<display_x11->motif_n_target_lists; i++)
	g_list_free (display_x11->motif_target_lists[i]);
1059
      
1060 1061 1062
      g_free (display_x11->motif_target_lists);
      display_x11->motif_target_lists = NULL;
      display_x11->motif_n_target_lists = 0;
1063 1064
    }

1065
  if (motif_find_drag_window (display, FALSE))
1066
    {
Owen Taylor's avatar
Owen Taylor committed
1067
      guchar *data;
1068 1069 1070 1071 1072
      MotifTargetTableHeader *header = NULL;
      guchar *target_bytes = NULL;
      guchar *p;
      gboolean success = FALSE;

Owen Taylor's avatar
Owen Taylor committed
1073
      gdk_error_trap_push ();
1074 1075
      XGetWindowProperty (display_x11->xdisplay, 
			  display_x11->motif_drag_window, 
1076 1077 1078 1079
			  motif_drag_targets_atom,
			  0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
			  motif_drag_targets_atom, 
			  &type, &format, &nitems, &bytes_after,
Owen Taylor's avatar
Owen Taylor committed
1080
			  &data);
1081

Owen Taylor's avatar
Owen Taylor committed
1082
      if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1083 1084
	goto error;

Owen Taylor's avatar
Owen Taylor committed
1085 1086
      header = (MotifTargetTableHeader *)data;

1087 1088 1089
      header->n_lists = card16_to_host (header->n_lists, header->byte_order);
      header->total_size = card32_to_host (header->total_size, header->byte_order);

Owen Taylor's avatar
Owen Taylor committed
1090
      gdk_error_trap_push ();
1091 1092 1093
      XGetWindowProperty (display_x11->xdisplay, 
			  display_x11->motif_drag_window, 
		          motif_drag_targets_atom,
1094
			  (sizeof(MotifTargetTableHeader)+3)/4,