gdkdnd-x11.c 90.6 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 28 29
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <string.h>
Owen Taylor's avatar
Owen Taylor committed
30 31

#include "gdk.h"          /* For gdk_flush() */
32
#include "gdkx.h"
Owen Taylor's avatar
Owen Taylor committed
33 34
#include "gdkdnd.h"
#include "gdkproperty.h"
35
#include "gdkprivate-x11.h"
36 37 38
#include "gdkinternals.h"
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
39

40
typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
41

42 43 44 45 46 47 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;
} GdkCacheChild;

typedef struct {
  GList *children;
  GHashTable *child_hash;
  guint old_event_mask;
59
  GdkScreen *screen;
60 61 62 63 64
} GdkWindowCache;

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

68 69
  Atom motif_selection;
  Atom xdnd_selection;
70 71 72 73
  guint   ref_count;

  guint16 last_x;		/* Coordinates from last event */
  guint16 last_y;
74 75 76
  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 */
77

78 79
  Window dest_xid;              /* The last window we looked up */
  Window drop_xid;            /* The (non-proxied) window that is receiving drops */
80
  guint xdnd_targets_set : 1;   /* Whether we've already set XdndTypeList */
81
  guint xdnd_actions_set : 1;   /* Whether we've already set XdndActionList */
82
  guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
83 84 85 86 87 88
  guint motif_targets_set : 1;  /* Whether we've already set motif initiator info */
  guint drag_status : 4;	/* current status of drag */

  GdkWindowCache *window_cache;
};

89 90
#define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)

91 92 93 94
/* Forward declarations */

static void gdk_window_cache_destroy (GdkWindowCache *cache);

95 96
static void motif_read_target_table (GdkDisplay *display);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
					 GdkEvent  *event,
					 gpointer   data);

static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
					  GdkEvent  *event,
					  gpointer   data);

static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
					  GdkEvent  *event,
					  gpointer   data);

static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
					     GdkEvent  *event,
					     gpointer   data);

static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
					   GdkEvent  *event,
					   gpointer   data);

static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
					    GdkEvent  *event,
					    gpointer   data);

static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
					 GdkEvent  *event,
					 gpointer   data);

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

129 130 131
static void gdk_drag_context_init       (GdkDragContext      *dragcontext);
static void gdk_drag_context_class_init (GdkDragContextClass *klass);
static void gdk_drag_context_finalize   (GObject              *object);
132

133
static gpointer parent_class = NULL;
134 135
static GList *contexts;

136 137
GType
gdk_drag_context_get_type (void)
138
{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (GdkDragContextClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gdk_drag_context_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GdkDragContext),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gdk_drag_context_init,
      };
      
      object_type = g_type_register_static (G_TYPE_OBJECT,
                                            "GdkDragContext",
158
                                            &object_info, 0);
159 160 161 162
    }
  
  return object_type;
}
163

164 165 166 167
static void
gdk_drag_context_init (GdkDragContext *dragcontext)
{
  GdkDragContextPrivateX11 *private;
168

169
  private = g_new0 (GdkDragContextPrivateX11, 1);
170

171
  dragcontext->windowing_data = private;
172

173
  contexts = g_list_prepend (contexts, dragcontext);
174 175
}

176 177
static void
gdk_drag_context_class_init (GdkDragContextClass *klass)
178
{
179 180 181
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);
Jeff Garzik's avatar
Jeff Garzik committed
182

183
  object_class->finalize = gdk_drag_context_finalize;
184 185
}

186 187
static void
gdk_drag_context_finalize (GObject *object)
188
{
189 190
  GdkDragContext *context = GDK_DRAG_CONTEXT (object);
  GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
191
  
192 193 194
  g_list_free (context->targets);

  if (context->source_window)
195
    {
196 197 198
      if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
          !context->is_source)
        xdnd_manage_source_filter (context, context->source_window, FALSE);
199
      
200 201 202 203 204 205 206 207 208 209
      gdk_window_unref (context->source_window);
    }
  
  if (context->dest_window)
    gdk_window_unref (context->dest_window);
  
  if (private->window_cache)
    gdk_window_cache_destroy (private->window_cache);
  
  contexts = g_list_remove (contexts, context);
210

211 212 213 214
  g_free (private);
  
  G_OBJECT_CLASS (parent_class)->finalize (object);
}
215

216 217 218 219 220
/* Drag Contexts */

GdkDragContext *
gdk_drag_context_new        (void)
{
221
  return g_object_new (gdk_drag_context_get_type (), NULL);
222 223 224 225 226 227
}

void            
gdk_drag_context_ref (GdkDragContext *context)
{
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
228

229 230
  g_object_ref (G_OBJECT (context));
}
231

232 233 234 235
void            
gdk_drag_context_unref (GdkDragContext *context)
{
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
236

237
  g_object_unref (G_OBJECT (context));
238 239 240
}

static GdkDragContext *
241 242 243 244
gdk_drag_context_find (GdkDisplay *display,
		       gboolean    is_source,
		       Window      source_xid,
		       Window      dest_xid)
245 246
{
  GList *tmp_list = contexts;
Jeff Garzik's avatar
Jeff Garzik committed
247
  GdkDragContext *context;
248
  GdkDragContextPrivateX11 *private;
249
  Window context_dest_xid;
250 251 252

  while (tmp_list)
    {
Jeff Garzik's avatar
Jeff Garzik committed
253
      context = (GdkDragContext *)tmp_list->data;
254
      private = PRIVATE_DATA (context);
255

256 257 258 259
      if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
	  (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
	continue;

260
      context_dest_xid = context->dest_window ? 
261 262 263 264
                            (private->drop_xid ?
                              private->drop_xid :
                              GDK_DRAWABLE_XID (context->dest_window)) :
	                     None;
265 266 267

      if ((!context->is_source == !is_source) &&
	  ((source_xid == None) || (context->source_window &&
268
	    (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
269
	  ((dest_xid == None) || (context_dest_xid == dest_xid)))
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	return context;
      
      tmp_list = tmp_list->next;
    }
  
  return NULL;
}

/* Utility functions */

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;

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

static GdkFilterReturn
gdk_window_cache_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));
		if (above_node && node->prev != above_node)
		  {
340 341
		    /* Put the window above (before in the list) above_node
		     */
342
		    cache->children = g_list_remove_link (cache->children, node);
343 344 345
		    node->prev = above_node->prev;
		    if (node->prev)
		      node->prev->next = node;
346 347
		    else
		      cache->children = node;
348 349
		    node->next = above_node;
		    above_node->prev = node;
350 351 352 353 354 355 356 357
		  }
	      }
	  }
	break;
      }
    case CreateNotify:
      {
	XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
358

359 360 361 362 363 364 365 366 367 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	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) 
	  {
	    g_hash_table_remove (cache->child_hash,
				 GUINT_TO_POINTER (xdwe->window));
	    cache->children = g_list_remove_link (cache->children, node);
	    g_free (node->data);
	    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));
	if (node) 
	  {
	    GdkCacheChild *child = node->data;
	    child->mapped = FALSE;
	  }
	break;
      }
    default:
      return GDK_FILTER_CONTINUE;
    }
  return GDK_FILTER_REMOVE;
}

static GdkWindowCache *
420
gdk_window_cache_new (GdkScreen *screen)
421 422 423 424 425
{
  XWindowAttributes xwa;
  Window root, parent, *children;
  unsigned int nchildren;
  int i;
426 427
  Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
  GdkWindow *root_window = gdk_screen_get_root_window (screen);
428 429 430 431 432
  
  GdkWindowCache *result = g_new (GdkWindowCache, 1);

  result->children = NULL;
  result->child_hash = g_hash_table_new (g_direct_hash, NULL);
433
  result->screen = screen;
434

435
  XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
436
  result->old_event_mask = xwa.your_event_mask;
437
  XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
438
		result->old_event_mask | SubstructureNotifyMask);
439
  gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
440

441 442
  gdk_error_trap_push ();

443
  if (!XQueryTree(xdisplay, GDK_WINDOW_XWINDOW (root_window),
444 445 446 447 448
		  &root, &parent, &children, &nchildren))
    {
      gdk_error_trap_pop ();
      return result;
    }
449 450 451
  
  for (i = 0; i < nchildren ; i++)
    {
452
      if (XGetWindowAttributes (xdisplay, children[i], &xwa))
453 454 455
	gdk_window_cache_add (result, children[i],
			      xwa.x, xwa.y, xwa.width, xwa.height,
			      xwa.map_state != IsUnmapped);
456 457 458
    }

  XFree (children);
459
  gdk_error_trap_pop ();
460 461 462 463 464 465 466

  return result;
}

static void
gdk_window_cache_destroy (GdkWindowCache *cache)
{
467 468 469 470 471 472
  GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
  
  XSelectInput (GDK_WINDOW_XDISPLAY (root_window), 
		GDK_WINDOW_XWINDOW (root_window),
		cache->old_event_mask);
  gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
473 474 475 476

  g_list_foreach (cache->children, (GFunc)g_free, NULL);
  g_list_free (cache->children);
  g_hash_table_destroy (cache->child_hash);
477 478

  g_free (cache);
479 480 481
}

static Window
482 483 484 485
get_client_window_at_coords_recurse (GdkDisplay *display,
				     Window      win,
				     gint        x,
				     gint        y)
486
{
487 488 489 490
  Window root, tmp_parent, *children;
  unsigned int nchildren;
  int i;
  Window child = None;
491 492 493 494 495
  Atom type = None;
  int format;
  unsigned long nitems, after;
  unsigned char *data;
  
496 497 498
  if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(display), win,
			  gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
			  0, 0, False, AnyPropertyType,
499 500
			  &type, &format, &nitems, &after, &data) != Success)
    return None;
501 502 503 504 505 506 507

  if (type != None)
    {
      XFree (data);
      return win;
    }

508 509
#if 0
  /* This is beautiful! Damn Enlightenment and click-to-focus */
510 511 512
  if (!XTranslateCoordinates (gdk_display, _gdk_root_window, win,
			      x_root, y_root, &dest_x, &dest_y, &child))
    return None;
513 514
  
#else
515 516
  if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), win,
		   &root, &tmp_parent, &children, &nchildren))
517
    return 0;
518 519
  
  for (i = nchildren - 1; (i >= 0) && (child == None); i--)
520
    {
521 522
      XWindowAttributes xwa;
      
523 524
      if (XGetWindowAttributes (GDK_DISPLAY_XDISPLAY (display),
				children[i], &xwa))
525
	{
526 527 528
	  if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
	      (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
	      (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
529 530 531 532 533 534 535
	    {
	      x -= xwa.x;
	      y -= xwa.y;
	      child = children[i];
	    }
	}
    }
536 537
  
  XFree (children);
538
#endif  
539 540

  if (child)
541
    return get_client_window_at_coords_recurse (display, child, x, y);
542 543 544 545
  else
    return None;
}

Jeff Garzik's avatar
Jeff Garzik committed
546
static Window 
547
get_client_window_at_coords (GdkWindowCache *cache,
548 549 550
			     Window          ignore,
			     gint            x_root,
			     gint            y_root)
551 552 553 554
{
  GList *tmp_list;
  Window retval = None;

555
  gdk_error_trap_push ();
556 557 558 559 560 561 562 563 564 565 566 567
  
  tmp_list = cache->children;

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

      if ((child->xid != ignore) && (child->mapped))
	{
	  if ((x_root >= child->x) && (x_root < child->x + child->width) &&
	      (y_root >= child->y) && (y_root < child->y + child->height))
	    {
568 569 570
	      retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
							    child->xid,
							    x_root - child->x,
571
							    y_root - child->y);
572 573 574 575 576 577 578 579
	      if (!retval)
		retval = child->xid;
	    }
	  
	}
      tmp_list = tmp_list->next;
    }

580
  gdk_error_trap_pop ();
581 582 583 584
  
  if (retval)
    return retval;
  else
585
    return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
586 587 588 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 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
}

/*************************************************************
 ***************************** 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)
    {
650
      gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
      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);
}

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
/* 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))
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

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

732
static GdkFilterReturn
733 734 735 736 737
motif_drag_window_filter (GdkXEvent *xevent,
			  GdkEvent  *event,
			  gpointer data)
{
  XEvent *xev = (XEvent *)xevent;
738 739
  GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window); 
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
740 741 742 743

  switch (xev->xany.type)
    {
    case DestroyNotify:
744 745
      display_x11->motif_drag_window = None;
      display_x11->motif_drag_gdk_window = NULL;
746 747
      break;
    case PropertyNotify:
748 749 750
      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);
751 752 753 754 755 756
      break;
    }
  return GDK_FILTER_REMOVE;
}

static Window
757 758
motif_lookup_drag_window (GdkDisplay *display,
			  Display    *lookup_xdisplay)
759 760 761
{
  Window retval = None;
  gulong bytes_after, nitems;
762
  Atom type;
763 764 765
  gint format;
  guchar *data;

766 767
  XGetWindowProperty (lookup_xdisplay, DefaultRootWindow (lookup_xdisplay),
		      gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
768
		      0, 1, FALSE,
769 770 771
		      XA_WINDOW, &type, &format, &nitems, &bytes_after,
		      &data);
  
772
  if ((format == 32) && (nitems == 1) && (bytes_after == 0))
773 774
    {
      retval = *(Window *)data;
775 776
      GDK_NOTE (DND, 
		g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
777 778 779 780 781 782 783 784 785 786 787
    }

  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.
 */
788
static Window 
789 790
motif_find_drag_window (GdkDisplay *display,
			gboolean    create)
791
{
792 793 794
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  
  if (!display_x11->motif_drag_window)
795
    {
796 797
      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);
798
      
799
      if (!display_x11->motif_drag_window && create)
800 801 802
	{
	  /* Create a persistant window. (Copied from LessTif) */
	  
803
	  Display *persistant_xdisplay;
804
	  XSetWindowAttributes attr;
805 806
	  persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
	  XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
807

808
	  XGrabServer (persistant_xdisplay);
809
	  
810
	  display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
811

812
	  if (!display_x11->motif_drag_window)
813 814 815 816
	    {
	      attr.override_redirect = True;
	      attr.event_mask = PropertyChangeMask;
	      
817 818 819
	       display_x11->motif_drag_window = 
		XCreateWindow (persistant_xdisplay, 
			       DefaultRootWindow (persistant_xdisplay),
820 821 822 823 824
			      -100, -100, 10, 10, 0, 0,
			      InputOnly, CopyFromParent,
			      (CWOverrideRedirect | CWEventMask), &attr);
	      
	      GDK_NOTE (DND,
825
			g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
826
	      
827 828
	      XChangeProperty (persistant_xdisplay, 
			       DefaultRootWindow (persistant_xdisplay),
829 830 831 832 833
			       motif_drag_window_atom, XA_WINDOW,
			       32, PropModeReplace,
			       (guchar *)&motif_drag_window_atom, 1);

	    }
834 835
	  XUngrabServer (persistant_xdisplay);
	  XCloseDisplay (persistant_xdisplay);
836 837 838 839 840
	}

      /* There is a miniscule race condition here if the drag window
       * gets destroyed exactly now.
       */
841
      if (display_x11->motif_drag_window)
842
	{
843 844 845
	  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,
846 847 848 849 850
				 motif_drag_window_filter,
				 NULL);
	}
    }

851
  return display_x11->motif_drag_window;
852 853 854
}

static void 
855
motif_read_target_table (GdkDisplay *display)
856
{
857
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
858
  gulong bytes_after, nitems;
859
  Atom type;
860 861
  gint format;
  gint i, j;
862 863
  
  Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
864

865
  if (display_x11->motif_target_lists)
866
    {
867 868
      for (i=0; i<display_x11->motif_n_target_lists; i++)
	g_list_free (display_x11->motif_target_lists[i]);
869
      
870 871 872
      g_free (display_x11->motif_target_lists);
      display_x11->motif_target_lists = NULL;
      display_x11->motif_n_target_lists = 0;
873 874
    }

875
  if (motif_find_drag_window (display, FALSE))
876 877 878 879 880 881
    {
      MotifTargetTableHeader *header = NULL;
      guchar *target_bytes = NULL;
      guchar *p;
      gboolean success = FALSE;

Owen Taylor's avatar
Owen Taylor committed
882
      gdk_error_trap_push ();
883 884
      XGetWindowProperty (display_x11->xdisplay, 
			  display_x11->motif_drag_window, 
885 886 887 888 889 890
			  motif_drag_targets_atom,
			  0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
			  motif_drag_targets_atom, 
			  &type, &format, &nitems, &bytes_after,
			  (guchar **)&header);

Owen Taylor's avatar
Owen Taylor committed
891
      if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
892 893 894 895 896
	goto error;

      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
897
      gdk_error_trap_push ();
898 899 900
      XGetWindowProperty (display_x11->xdisplay, 
			  display_x11->motif_drag_window, 
		          motif_drag_targets_atom,
901
			  (sizeof(MotifTargetTableHeader)+3)/4, 
902
			  (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
903 904 905 906
			  FALSE,
			  motif_drag_targets_atom, &type, &format, &nitems, 
			  &bytes_after, &target_bytes);
      
Owen Taylor's avatar
Owen Taylor committed
907
      if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) || 
908 909 910
	  (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
	  goto error;

911 912
      display_x11->motif_n_target_lists = header->n_lists;
      display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
913 914 915 916 917 918 919 920 921 922 923

      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);
924 925 926 927 928 929

	  /* 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);
930 931 932 933 934 935

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

	  for (j=0; j<n_targets; j++)
936 937 938
	    display_x11->motif_target_lists[i] = 
	      g_list_prepend (display_x11->motif_target_lists[i],
			      GUINT_TO_POINTER (card32_to_host (targets[j],
939
								header->byte_order)));
940
	  g_free (targets);
941
	  display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
942 943 944 945 946 947 948 949 950 951 952 953 954
	}

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

      if (!success)
	{
955
	  if (display_x11->motif_target_lists)
956
	    {
957 958 959
	      g_free (display_x11->motif_target_lists);
	      display_x11->motif_target_lists = NULL;
	      display_x11->motif_n_target_lists = 0;
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
	    }
	  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 */
static gboolean
975 976
motif_target_table_check (GdkDisplay *display,
			  GList      *sorted)
977
{
978
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
979 980 981
  GList *tmp_list1, *tmp_list2;
  gint i;

982
  for (i=0; i<display_x11->motif_n_target_lists; i++)
983
    {
984
      tmp_list1 = display_x11->motif_target_lists[i];
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
      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
1003 1004
motif_add_to_target_table (GdkDisplay *display,
			   GList      *targets) /* targets is list of GdkAtom */
1005
{
1006
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1007 1008 1009 1010 1011 1012 1013 1014 1015
  GList *sorted = NULL;
  gint index = -1;
  gint i;
  GList *tmp_list;
  
  /* make a sorted copy of the list */
  
  while (targets)
    {
1016
      Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1017
      sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1018 1019 1020 1021 1022
      targets = targets->next;
    }

  /* First check if it is their already */

1023 1024
  if (display_x11->motif_target_lists)
    index = motif_target_table_check (display, sorted);
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

  /* 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. 
       */
1036
      motif_find_drag_window (display, TRUE);
1037

1038 1039
      gdk_x11_display_grab (display);
      motif_read_target_table (display);
1040 1041 1042
    
      /* Check again, in case it was added in the meantime */
      
1043 1044
      if (display_x11->motif_target_lists)
	index =  motif_target_table_check (display, sorted);
1045 1046 1047 1048 1049 1050 1051 1052 1053

      if (index < 0)
	{
	  guint32 total_size = 0;
	  guchar *data;
	  guchar *p;
	  guint16 *p16;
	  MotifTargetTableHeader *header;
	  
1054
	  if (!display_x11->motif_target_lists)
1055
	    {
1056 1057
	      display_x11->motif_target_lists = g_new (GList *, 1);
	      display_x11->motif_n_target_lists = 1;
1058 1059 1060
	    }
	  else
	    {
1061 1062 1063
	      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);
1064
	    }
1065
	  display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1066
	  sorted = NULL;
1067
	  index = display_x11->motif_n_target_lists - 1;
1068 1069

	  total_size = sizeof (MotifTargetTableHeader);
1070 1071
	  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]);
1072 1073 1074 1075 1076 1077 1078 1079

	  data = g_malloc (total_size);

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

	  header->byte_order = local_byte_order;
	  header->protocol_version = 0;
1080
	  header->n_lists = display_x11->motif_n_target_lists;
1081 1082
	  header->total_size = total_size;

1083
	  for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1084
	    {
1085
	      guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1086 1087
	      guint32 *targets = g_new (guint32, n_targets);
	      guint32 *p32 = targets;
1088
	      
1089
	      tmp_list = display_x11->motif_target_lists[i];
1090 1091
	      while (tmp_list)
		{
1092 1093
		  *p32 = GPOINTER_TO_UINT (tmp_list->data);
		  
1094
		  tmp_list = tmp_list->next;
1095
		  p32++;
1096
		}
1097 1098 1099 1100 1101 1102 1103 1104 1105

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

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

	      *p16 = n_targets;
	      p += sizeof(guint32) * n_targets;
	      g_free (targets);
1106 1107
	    }

1108 1109 1110 1111
	  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"),
1112 1113 1114
			   8, PropModeReplace,
			   data, total_size);
	}
1115
      gdk_x11_display_ungrab (display);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    }

  g_list_free (sorted);
  return index;
}

/* Translate flags */

static void
motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
{
  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
motif_set_targets (GdkDragContext *context)
{
1191
  GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1192 1193
  MotifDragInitiatorInfo info;
  gint i;
1194 1195
  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
  
1196 1197 1198
  info.byte_order = local_byte_order;
  info.protocol_version = 0;
  
1199
  info.targets_index = motif_add_to_target_table (display, context->targets);
1200 1201 1202 1203 1204 1205

  for (i=0; ; i++)
    {
      gchar buf[20];
      g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
      
1206 1207
      private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
      if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1208 1209 1210 1211 1212
	break;
    }

  info.selection_atom = private->motif_selection;

1213 1214
  XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
		   GDK_DRAWABLE_XID (context->source_window),
1215
		   private->motif_selection,
1216 1217
		   gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
		   8, PropModeReplace,
1218 1219 1220 1221 1222
		   (guchar *)&info, sizeof (info));

  private->motif_targets_set = 1;
}

1223
static guint32
1224 1225
motif_check_dest (GdkDisplay *display,
		  Window      win)
1226 1227 1228 1229 1230 1231
{
  gboolean retval = FALSE;
  MotifDragReceiverInfo *info;
  Atom type = None;
  int format;
  unsigned long nitems, after;
1232
  Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1233

Owen Taylor's avatar
Owen Taylor committed
1234
  gdk_error_trap_push ();
1235
  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win, 
1236 1237 1238 1239
		      motif_drag_receiver_info_atom, 
		      0, (sizeof(*info)+3)/4, False, AnyPropertyType,
		      &type, &format, &nitems, &after, 
		      (guchar **)&info);
Owen Taylor's avatar
Owen Taylor committed
1240 1241

  if (gdk_error_trap_pop() == 0)
1242
    {
Owen Taylor's avatar
Owen Taylor committed
1243
      if (type != None)
1244
	{
Owen Taylor's avatar
Owen Taylor committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	  if ((format == 8) && (nitems == sizeof(*info)))
	    {
	      if ((info->protocol_version == 0) &&
		  ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
		   (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
		   (info->protocol_style == XmDRAG_DYNAMIC)))
		retval = TRUE;
	    }
	  else
	    {
	      GDK_NOTE (DND, 
			g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
	    }
	  
	  XFree (info);
1260 1261 1262
	}
    }

1263
  return retval ? win : None;
1264 1265 1266 1267 1268 1269
}

static void
motif_send_enter (GdkDragContext  *context,
		  guint32          time)
{
1270
  GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1271 1272
  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
  XEvent xev;
1273 1274

  xev.xclient.type = ClientMessage;
1275
  xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1276
  xev.xclient.format = 8;
1277
  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1278

1279 1280 1281 1282
  MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
  MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
  MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
  MOTIF_XCLIENT_LONG (&xev, 1) = time;
1283
  MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1284 1285 1286 1287

  if (!private->motif_targets_set)
    motif_set_targets (context);

1288
  MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1289

1290 1291 1292
  if (!_gdk_send_xevent (display,
			 GDK_DRAWABLE_XID (context->dest_window),
			 FALSE, 0, &xev))
1293 1294
    GDK_NOTE (DND, 
	      g_message ("Send event to %lx failed",
1295
			 GDK_DRAWABLE_XID (context->dest_window)));
1296 1297 1298 1299 1300 1301
}

static void
motif_send_leave (GdkDragContext  *context,
		  guint32          time)
{
1302
  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1303 1304 1305
  XEvent xev;

  xev.xclient.type = ClientMessage;
1306
  xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1307
  xev.xclient.format = 8;
1308
  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1309

1310 1311 1312 1313
  MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
  MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
  MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
  MOTIF_XCLIENT_LONG (&xev, 1) = time;
1314
  MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1315
  MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1316

1317 1318 1319
  if (!_gdk_send_xevent (display,
			 GDK_DRAWABLE_XID (context->dest_window),
			 FALSE, 0, &xev))
1320 1321
    GDK_NOTE (DND, 
	      g_message ("Send event to %lx failed",
1322
			 GDK_DRAWABLE_XID (context->dest_window)));
1323 1324 1325 1326 1327 1328 1329 1330 1331
}

static gboolean
motif_send_motion (GdkDragContext  *context,
		    gint            x_root, 
		    gint            y_root,
		    GdkDragAction   action,
		    guint32         time)
{
1332 1333
  GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
  GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1334 1335 1336 1337
  gboolean retval;
  XEvent xev;

  xev.xclient.type = ClientMessage;
1338
  xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1339
  xev.xclient.format = 8;
1340
  xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1341

1342 1343 1344
  MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
  MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
  MOTIF_XCLIENT_LONG (&xev, 1) = time;
1345

1346 1347
  if ((context->suggested_action != private->old_action) ||
      (context->actions != private->old_actions))
1348
    {
1349
      MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1350