gdkdnd-x11.c 107 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 "gdkdndprivate.h"
Owen Taylor's avatar
Owen Taylor committed
30

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

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

#include <string.h>

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

Matthias Clasen's avatar
Matthias Clasen committed
71 72 73 74 75 76 77
#define GDK_TYPE_DRAG_CONTEXT_X11 (gdk_drag_context_x11_get_type ())
#define GDK_DRAG_CONTEXT_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_DRAG_CONTEXT_X11, GdkDragContextX11))

typedef struct _GdkDragContextX11 GdkDragContextX11;
typedef struct _GdkDragContextClass GdkDragContextX11Class;

struct _GdkDragContextX11 {
78 79
  GdkDragContext context;

80
  Atom motif_selection;
81 82 83 84
  guint   ref_count;

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

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

100
  GSList *window_caches;
101 102 103 104 105 106
};

/* Forward declarations */

static void gdk_window_cache_destroy (GdkWindowCache *cache);

107 108
static void motif_read_target_table (GdkDisplay *display);

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

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

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

136 137
static GList *contexts;

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


Matthias Clasen's avatar
Matthias Clasen committed
151
G_DEFINE_TYPE (GdkDragContextX11, gdk_drag_context_x11, GDK_TYPE_DRAG_CONTEXT)
152

Matthias Clasen's avatar
Matthias Clasen committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static void
gdk_drag_context_x11_init (GdkDragContextX11 *context)
{
  contexts = g_list_prepend (contexts, context);
}

static void        gdk_drag_context_x11_finalize (GObject *object);
static GdkWindow * gdk_drag_context_x11_find_window (GdkDragContext  *context,
                                                     GdkWindow       *drag_window,
                                                     GdkScreen       *screen,
                                                     gint             x_root,
                                                     gint             y_root,
                                                     GdkDragProtocol *protocol);
static gboolean    gdk_drag_context_x11_drag_motion (GdkDragContext  *context,
                                                     GdkWindow       *dest_window,
                                                     GdkDragProtocol  protocol,
                                                     gint             x_root,
                                                     gint             y_root,
                                                     GdkDragAction    suggested_action,
                                                     GdkDragAction    possible_actions,
                                                     guint32          time);
static void        gdk_drag_context_x11_drag_status (GdkDragContext  *context,
                                                     GdkDragAction    action,
                                                     guint32          time_);
static void        gdk_drag_context_x11_drag_abort  (GdkDragContext  *context,
                                                     guint32          time_);
static void        gdk_drag_context_x11_drag_drop   (GdkDragContext  *context,
                                                     guint32          time_);
static void        gdk_drag_context_x11_drop_reply  (GdkDragContext  *context,
                                                     gboolean         accept,
                                                     guint32          time_);
static void        gdk_drag_context_x11_drop_finish (GdkDragContext  *context,
                                                     gboolean         success,
                                                     guint32          time_);
static gboolean    gdk_drag_context_x11_drop_status (GdkDragContext  *context);
static GdkAtom     gdk_drag_context_x11_get_selection (GdkDragContext  *context);
189

190
static void
Matthias Clasen's avatar
Matthias Clasen committed
191
gdk_drag_context_x11_class_init (GdkDragContextX11Class *klass)
192
{
193
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Matthias Clasen's avatar
Matthias Clasen committed
194
  GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
195

Matthias Clasen's avatar
Matthias Clasen committed
196
  object_class->finalize = gdk_drag_context_x11_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
197

Matthias Clasen's avatar
Matthias Clasen committed
198 199 200 201 202 203 204 205 206
  context_class->find_window = gdk_drag_context_x11_find_window;
  context_class->drag_status = gdk_drag_context_x11_drag_status;
  context_class->drag_motion = gdk_drag_context_x11_drag_motion;
  context_class->drag_abort = gdk_drag_context_x11_drag_abort;
  context_class->drag_drop = gdk_drag_context_x11_drag_drop;
  context_class->drop_reply = gdk_drag_context_x11_drop_reply;
  context_class->drop_finish = gdk_drag_context_x11_drop_finish;
  context_class->drop_status = gdk_drag_context_x11_drop_status;
  context_class->get_selection = gdk_drag_context_x11_get_selection;
207 208
}

209
static void
Matthias Clasen's avatar
Matthias Clasen committed
210
gdk_drag_context_x11_finalize (GObject *object)
211
{
Matthias Clasen's avatar
Matthias Clasen committed
212
  GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (object);
213 214 215
  GdkDragContext *context = GDK_DRAG_CONTEXT (object);

  if (context->source_window)
216
    {
Matthias Clasen's avatar
Matthias Clasen committed
217
      if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
218 219
        xdnd_manage_source_filter (context, context->source_window, FALSE);
    }
220

Matthias Clasen's avatar
Matthias Clasen committed
221 222
  g_slist_free_full (context_x11->window_caches, (GDestroyNotify)gdk_window_cache_destroy);

223
  contexts = g_list_remove (contexts, context);
224

Matthias Clasen's avatar
Matthias Clasen committed
225
  G_OBJECT_CLASS (gdk_drag_context_x11_parent_class)->finalize (object);
226
}
227

228 229
/* Drag Contexts */

230
static GdkDragContext *
231 232 233 234
gdk_drag_context_find (GdkDisplay *display,
		       gboolean    is_source,
		       Window      source_xid,
		       Window      dest_xid)
235 236
{
  GList *tmp_list = contexts;
Jeff Garzik's avatar
Jeff Garzik committed
237
  GdkDragContext *context;
Matthias Clasen's avatar
Matthias Clasen committed
238
  GdkDragContextX11 *context_x11;
239
  Window context_dest_xid;
240 241 242

  while (tmp_list)
    {
Jeff Garzik's avatar
Jeff Garzik committed
243
      context = (GdkDragContext *)tmp_list->data;
Matthias Clasen's avatar
Matthias Clasen committed
244
      context_x11 = (GdkDragContextX11 *)context;
245

246 247
      if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
	  (context->dest_window && gdk_window_get_display (context->dest_window) != display))
Matthias Clasen's avatar
Matthias Clasen committed
248
        continue;
249

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

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

262 263
      tmp_list = tmp_list->next;
    }
Matthias Clasen's avatar
Matthias Clasen committed
264

265 266 267
  return NULL;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
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);
    }
}

291 292
/* Utility functions */

293 294 295 296 297
static void
free_cache_child (GdkCacheChild *child,
                  GdkDisplay    *display)
{
  if (child->shape)
Benjamin Otte's avatar
Benjamin Otte committed
298
    cairo_region_destroy (child->shape);
299 300 301 302 303 304 305 306 307 308 309

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

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

  g_free (child);
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323
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;
324 325 326
  child->shape_selected = FALSE;
  child->shape_valid = FALSE;
  child->shape = NULL;
327 328 329 330 331 332

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

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
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
357
              cairo_region_destroy (child->shape);
358 359 360 361 362 363 364 365 366 367
              child->shape = NULL;
            }
        }

      return GDK_FILTER_REMOVE;
    }

  return GDK_FILTER_CONTINUE;
}

368 369 370 371 372 373 374 375 376 377 378 379 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
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));
406
		if (above_node && node->next != above_node)
407
		  {
408 409
		    /* Put the window above (before in the list) above_node
		     */
410
		    cache->children = g_list_remove_link (cache->children, node);
411 412 413
		    node->prev = above_node->prev;
		    if (node->prev)
		      node->prev->next = node;
414 415
		    else
		      cache->children = node;
416 417
		    node->next = above_node;
		    above_node->prev = node;
418 419 420 421 422 423 424 425
		  }
	      }
	  }
	break;
      }
    case CreateNotify:
      {
	XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	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) 
	  {
443 444
	    GdkCacheChild *child = node->data;

445 446 447
	    g_hash_table_remove (cache->child_hash,
				 GUINT_TO_POINTER (xdwe->window));
	    cache->children = g_list_remove_link (cache->children, node);
448 449
	    /* window is destroyed, no need to disable ShapeNotify */
	    free_cache_child (child, NULL);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	    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));
477
	if (node)
478 479 480 481 482 483 484 485 486 487 488 489 490
	  {
	    GdkCacheChild *child = node->data;
	    child->mapped = FALSE;
	  }
	break;
      }
    default:
      return GDK_FILTER_CONTINUE;
    }
  return GDK_FILTER_REMOVE;
}

static GdkWindowCache *
491
gdk_window_cache_new (GdkScreen *screen)
492 493
{
  XWindowAttributes xwa;
494 495
  Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
  GdkWindow *root_window = gdk_screen_get_root_window (screen);
496 497
  GdkChildInfoX11 *children;
  guint nchildren, i;
498
  Window cow;
499 500 501 502 503
  
  GdkWindowCache *result = g_new (GdkWindowCache, 1);

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

506
  XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
507
  result->old_event_mask = xwa.your_event_mask;
508 509 510 511 512 513 514 515 516 517

  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);
518
	gdk_window_get_geometry (window, &x, &y, &width, &height);
519 520 521 522 523 524 525 526
	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;
    }

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

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

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

545
  g_free (children);
546

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

563 564 565 566 567 568
  return result;
}

static void
gdk_window_cache_destroy (GdkWindowCache *cache)
{
569
  GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
570 571

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

577 578 579
  gdk_error_trap_push ();

  g_list_foreach (cache->children, (GFunc)free_cache_child,
580
      gdk_screen_get_display (cache->screen));
581

Matthias Clasen's avatar
Matthias Clasen committed
582
  gdk_error_trap_pop_ignored ();
583

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

  g_free (cache);
588 589
}

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
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);
606
      cairo_region_t *input_shape;
607 608 609 610 611 612 613 614

      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
615 616
          cairo_region_intersect (child->shape, input_shape);
          cairo_region_destroy (input_shape);
617 618 619 620 621 622 623 624 625 626 627
        }
      else if (input_shape)
        {
          child->shape = input_shape;
        }
#endif

      child->shape_valid = TRUE;
    }

  return child->shape == NULL ||
Benjamin Otte's avatar
Benjamin Otte committed
628
         cairo_region_contains_point (child->shape, x_pos, y_pos);
629 630
}

631 632 633
static Window
get_client_window_at_coords_recurse (GdkDisplay *display,
				     Window      win,
634
				     gboolean    is_toplevel,
635 636 637 638 639 640 641
				     gint        x,
				     gint        y)
{
  GdkChildInfoX11 *children;
  unsigned int nchildren;
  int i;
  gboolean found_child = FALSE;
642
  GdkChildInfoX11 child = { 0, };
643
  gboolean has_wm_state = FALSE;
644 645

  if (!_gdk_x11_get_window_child_info (display, win, TRUE,
646
				       is_toplevel? &has_wm_state : NULL,
647
				       &children, &nchildren))
648
    return None;
649

650
  if (has_wm_state)
651 652 653 654 655
    {
      g_free (children);

      return win;
    }
656

657
  for (i = nchildren - 1; (i >= 0) && !found_child; i--)
658
    {
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
      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
679
	return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
680
    }
681 682 683 684
  else
    return None;
}

Jeff Garzik's avatar
Jeff Garzik committed
685
static Window 
686
get_client_window_at_coords (GdkWindowCache *cache,
687 688 689
			     Window          ignore,
			     gint            x_root,
			     gint            y_root)
690 691 692 693
{
  GList *tmp_list;
  Window retval = None;

694
  gdk_error_trap_push ();
695 696 697 698 699 700 701 702
  
  tmp_list = cache->children;

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

      if ((child->xid != ignore) && (child->mapped))
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
        {
          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;
            }
        }
725 726 727
      tmp_list = tmp_list->next;
    }

Matthias Clasen's avatar
Matthias Clasen committed
728
  gdk_error_trap_pop_ignored ();
729 730 731 732
  
  if (retval)
    return retval;
  else
733
    return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
}

/*************************************************************
 ***************************** 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)
    {
798
      gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
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
      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);
}

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
/* 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))
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

/***** 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 */

880
static GdkFilterReturn
881 882 883 884 885
motif_drag_window_filter (GdkXEvent *xevent,
			  GdkEvent  *event,
			  gpointer data)
{
  XEvent *xev = (XEvent *)xevent;
886 887
  GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window); 
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
888 889 890 891

  switch (xev->xany.type)
    {
    case DestroyNotify:
892 893
      display_x11->motif_drag_window = None;
      display_x11->motif_drag_gdk_window = NULL;
894 895
      break;
    case PropertyNotify:
896 897 898
      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);
899 900 901 902 903 904
      break;
    }
  return GDK_FILTER_REMOVE;
}

static Window
905 906
motif_lookup_drag_window (GdkDisplay *display,
			  Display    *lookup_xdisplay)
907 908 909
{
  Window retval = None;
  gulong bytes_after, nitems;
910
  Atom type;
911 912 913
  gint format;
  guchar *data;

914
  XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
915
		      gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
916
		      0, 1, FALSE,
917 918 919
		      XA_WINDOW, &type, &format, &nitems, &bytes_after,
		      &data);
  
920
  if ((format == 32) && (nitems == 1) && (bytes_after == 0))
921 922
    {
      retval = *(Window *)data;
923 924
      GDK_NOTE (DND, 
		g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
925 926 927 928 929 930 931 932 933 934 935
    }

  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.
 */
936
static Window 
937 938
motif_find_drag_window (GdkDisplay *display,
			gboolean    create)
939
{
940 941 942
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  
  if (!display_x11->motif_drag_window)
943
    {
944 945
      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);
946
      
947
      if (!display_x11->motif_drag_window && create)
948 949 950
	{
	  /* Create a persistant window. (Copied from LessTif) */
	  
951
	  Display *persistant_xdisplay;
952
	  XSetWindowAttributes attr;
953 954
	  persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
	  XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
955

956
	  XGrabServer (persistant_xdisplay);
957
	  
958
	  display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
959

960
	  if (!display_x11->motif_drag_window)
961 962 963 964
	    {
	      attr.override_redirect = True;
	      attr.event_mask = PropertyChangeMask;
	      
965 966
	       display_x11->motif_drag_window = 
		XCreateWindow (persistant_xdisplay, 
967
			       RootWindow (persistant_xdisplay, 0),
968
			      -100, -100, 10, 10, 0, 0,
Matthias Clasen's avatar
Matthias Clasen committed
969
			      InputOnly, (Visual *)CopyFromParent,
970 971 972
			      (CWOverrideRedirect | CWEventMask), &attr);
	      
	      GDK_NOTE (DND,
973
			g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
974
	      
975
	      XChangeProperty (persistant_xdisplay, 
976
			       RootWindow (persistant_xdisplay, 0),
977 978 979 980 981
			       motif_drag_window_atom, XA_WINDOW,
			       32, PropModeReplace,
			       (guchar *)&motif_drag_window_atom, 1);

	    }
982 983
	  XUngrabServer (persistant_xdisplay);
	  XCloseDisplay (persistant_xdisplay);
984 985 986 987 988
	}

      /* There is a miniscule race condition here if the drag window
       * gets destroyed exactly now.
       */
989
      if (display_x11->motif_drag_window)
990
	{
991 992
	  display_x11->motif_drag_gdk_window =
	    gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
993
	  gdk_window_add_filter (display_x11->motif_drag_gdk_window,
994 995 996 997 998
				 motif_drag_window_filter,
				 NULL);
	}
    }

999
  return display_x11->motif_drag_window;
1000 1001 1002
}

static void 
1003
motif_read_target_table (GdkDisplay *display)
1004
{
1005
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1006
  gulong bytes_after, nitems;
1007
  Atom type;
1008 1009
  gint format;
  gint i, j;
1010 1011
  
  Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1012

1013
  if (display_x11->motif_target_lists)
1014
    {
1015 1016
      for (i=0; i<display_x11->motif_n_target_lists; i++)
	g_list_free (display_x11->motif_target_lists[i]);
1017
      
1018 1019 1020
      g_free (display_x11->motif_target_lists);
      display_x11->motif_target_lists = NULL;
      display_x11->motif_n_target_lists = 0;
1021 1022
    }

1023
  if (motif_find_drag_window (display, FALSE))
1024
    {
1025
      guchar *data;
1026 1027 1028 1029 1030
      MotifTargetTableHeader *header = NULL;
      guchar *target_bytes = NULL;
      guchar *p;
      gboolean success = FALSE;

Owen Taylor's avatar
Owen Taylor committed
1031
      gdk_error_trap_push ();
1032 1033
      XGetWindowProperty (display_x11->xdisplay, 
			  display_x11->motif_drag_window, 
1034 1035 1036 1037
			  motif_drag_targets_atom,
			  0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
			  motif_drag_targets_atom, 
			  &type, &format, &nitems, &bytes_after,
1038
			  &data);
1039

Owen Taylor's avatar
Owen Taylor committed
1040
      if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1041 1042
	goto error;

1043 1044
      header = (MotifTargetTableHeader *)data;

1045 1046 1047
      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
1048
      gdk_error_trap_push ();
1049 1050 1051
      XGetWindowProperty (display_x11->xdisplay, 
			  display_x11->motif_drag_window, 
		          motif_drag_targets_atom,
1052
			  (sizeof(MotifTargetTableHeader)+3)/4, 
1053
			  (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1054 1055 1056 1057
			  FALSE,
			  motif_drag_targets_atom, &type, &format, &nitems, 
			  &bytes_after, &target_bytes);
      
Owen Taylor's avatar
Owen Taylor committed
1058
      if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) || 
1059 1060 1061
	  (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
	  goto error;

1062 1063
      display_x11->motif_n_target_lists = header->n_lists;
      display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

      p = target_bytes;
      for (i=0; i<header->n_lists; i++)
	{
	  gint n_targets;
	  guint32 *targets;
	  
	  if (p + sizeof(guint16) - target_bytes > nitems)
	    goto error;

	  n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1075 1076 1077 1078 1079 1080

	  /* We need to make a copy of the targets, since it may
	   * be unaligned
	   */
	  targets = g_new (guint32, n_targets);
	  memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1081 1082 1083 1084 1085 1086

	  p +=  sizeof(guint16) + n_targets * sizeof(guint32);
	  if (p - target_bytes > nitems)
	    goto error;

	  for (j=0; j<n_targets; j++)
1087 1088 1089
	    display_x11->motif_target_lists[i] = 
	      g_list_prepend (display_x11->motif_target_lists[i],
			      GUINT_TO_POINTER (card32_to_host (targets[j],
1090
								header->byte_order)));
1091
	  g_free (targets);
1092
	  display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	}

      success = TRUE;
      
    error:
      if (header)
	XFree (header);
      
      if (target_bytes)
	XFree (target_bytes);

      if (!success)
	{
1106
	  if (display_x11->motif_target_lists)
1107
	    {
1108 1109 1110
	      g_free (display_x11->motif_target_lists);
	      display_x11->motif_target_lists = NULL;
	      display_x11->motif_n_target_lists = 0;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	    }
	  g_warning ("Error reading Motif target table\n");
	}
    }
}

static gint
targets_sort_func (gconstpointer a, gconstpointer b)
{
  return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
    -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
}

/* Check if given (sorted) list is in the targets table */
1125
static gint
1126 1127
motif_target_table_check (GdkDisplay *display,
			  GList      *sorted)
1128
{
1129
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1130 1131 1132
  GList *tmp_list1, *tmp_list2;
  gint i;

1133
  for (i=0; i<display_x11->motif_n_target_lists; i++)
1134
    {
1135
      tmp_list1 = display_x11->motif_target_lists[i];
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
      tmp_list2 = sorted;
      
      while (tmp_list1 && tmp_list2)
	{
	  if (tmp_list1->data != tmp_list2->data)
	    break;

	  tmp_list1 = tmp_list1->next;
	  tmp_list2 = tmp_list2->next;
	}
      if (!tmp_list1 && !tmp_list2)	/* Found it */
	return i;
    }

  return -1;
}

static gint
1154 1155
motif_add_to_target_table (GdkDisplay *display,
			   GList      *targets) /* targets is list of GdkAtom */
1156
{
1157
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1158 1159 1160 1161 1162 1163 1164 1165 1166
  GList *sorted = NULL;
  gint index = -1;
  gint i;
  GList *tmp_list;
  
  /* make a sorted copy of the list */
  
  while (targets)
    {
1167
      Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1168
      sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1169 1170 1171
      targets = targets->next;
    }

1172
  /* First check if it is there already */
1173

1174 1175
  if (display_x11->motif_target_lists)
    index = motif_target_table_check (display, sorted);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

  /* We need to grab the server while doing this, to ensure
   * atomiticity. Ugh
   */

  if (index < 0)
    {
      /* We need to make sure that it exists _before_ we grab the
       * server, since we can't open a new connection after we
       * grab the server. 
       */
1187
      motif_find_drag_window (display, TRUE);
1188

1189 1190
      gdk_x11_display_grab (display);
      motif_read_target_table (display);
1191 1192 1193
    
      /* Check again, in case it was added in the meantime */
      
1194
      if (display_x11->motif_target_lists)
1195
	index = motif_target_table_check (display, sorted);
1196 1197 1198 1199 1200 1201 1202 1203 1204

      if (index < 0)
	{
	  guint32 total_size = 0;
	  guchar *data;
	  guchar *p;
	  guint16 *p16;
	  MotifTargetTableHeader *header;
	  
1205
	  if (!display_x11->motif_target_lists)
1206
	    {
1207 1208
	      display_x11->motif_target_lists = g_new (GList *, 1);
	      display_x11->motif_n_target_lists = 1;
1209 1210 1211
	    }
	  else
	    {
1212 1213 1214
	      display_x11->motif_n_target_lists++;
	      display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
							   sizeof(GList *) * display_x11->motif_n_target_lists);
1215
	    }
1216
	  display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1217
	  sorted = NULL;
1218
	  index = display_x11->motif_n_target_lists - 1;
1219 1220

	  total_size = sizeof (MotifTargetTableHeader);
1221 1222
	  for (i = 0; i < display_x11->motif_n_target_lists ; i++)
	    total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1223 1224 1225 1226 1227 1228 1229 1230

	  data = g_malloc (total_size);

	  header = (MotifTargetTableHeader *)data;
	  p = data + sizeof(MotifTargetTableHeader);

	  header->byte_order = local_byte_order;
	  header->protocol_version = 0;
1231
	  header->n_lists = display_x11->motif_n_target_lists;
1232 1233
	  header->total_size = total_size;

1234
	  for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1235
	    {
1236
	      guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1237 1238
	      guint32 *targets = g_new (guint32, n_targets);
	      guint32 *p32 = targets;
1239
	      
1240
	      tmp_list = display_x11->motif_target_lists[i];
1241 1242
	      while (tmp_list)
		{
1243 1244
		  *p32 = GPOINTER_TO_UINT (tmp_list->data);
		  
1245
		  tmp_list = tmp_list->next;
1246
		  p32++;
1247
		}
1248 1249 1250 1251 1252 1253 1254 1255 1256

	      p16 = (guint16 *)p;
	      p += sizeof(guint16);

	      memcpy (p, targets, n_targets * sizeof(guint32));

	      *p16 = n_targets;
	      p += sizeof(guint32) * n_targets;
	      g_free (targets);
1257 1258
	    }

1259 1260 1261 1262
	  XChangeProperty (display_x11->xdisplay,
			   display_x11->motif_drag_window,
			   gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
			   gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1263 1264 1265
			   8, PropModeReplace,
			   data, total_size);
	}
1266
      gdk_x11_display_ungrab (display);
1267 1268 1269 1270 1271 1272 1273 1274 1275
    }

  g_list_free (sorted);
  return index;
}

/* Translate flags */

static void
Matthias Clasen's avatar
Matthias Clasen committed
1276 1277
motif_dnd_translate_flags (GdkDragContextX11 *context_x11,
                           guint16            flags)
1278
{
Matthias Clasen's avatar
Matthias Clasen committed
1279
  GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
  guint recommended_op = flags & 0x000f;
  guint possible_ops = (flags & 0x0f0) >> 4;
  
  switch (recommended_op)
    {
    case XmDROP_MOVE:
      context->suggested_action = GDK_ACTION_MOVE;
      break;
    case XmDROP_COPY:
      context->suggested_action = GDK_ACTION_COPY;
      break;
    case XmDROP_LINK:
      context->suggested_action = GDK_ACTION_LINK;
      break;
    default:
      context->suggested_action = GDK_ACTION_COPY;
      break;
    }

  context->actions = 0;
  if (possible_ops & XmDROP_MOVE)
    context->actions |= GDK_ACTION_MOVE;
  if (possible_ops & XmDROP_COPY)
    context->actions |= GDK_ACTION_COPY;
  if (possible_ops & XmDROP_LINK)
    context->actions |= GDK_ACTION_LINK;
}

static guint16
motif_dnd_get_flags (GdkDragContext *context)
{
  guint16 flags = 0;
  
  switch (context->suggested_action)
    {
    case GDK_ACTION_MOVE:
      flags = XmDROP_MOVE;
      break;
    case GDK_ACTION_COPY:
      flags = XmDROP_COPY;
      break;
    case GDK_ACTION_LINK:
      flags = XmDROP_LINK;
      break;
    default:
      flags = XmDROP_NOOP;
      break;
    }
  
  if (context->actions & GDK_ACTION_MOVE)
    flags |= XmDROP_MOVE << 8;
  if (context->actions & GDK_ACTION_COPY)
    flags |= XmDROP_COPY << 8;
  if (context->actions & GDK_ACTION_LINK)
    flags |= XmDROP_LINK << 8;

  return flags;
}

/* Source Side */

static void