gdkdnd-x11.c 82.7 KB
Newer Older
1
/* GDK - The GIMP Drawing Kit
2
 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * 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 22 23 24 25 26

/*
 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
 * 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
Started  
Owen Taylor committed
30 31 32 33 34

#include "gdk.h"          /* For gdk_flush() */
#include "gdkdnd.h"
#include "gdkproperty.h"
#include "gdkprivate.h"
35 36
#include "gdkx.h"

37
typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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;
} GdkWindowCache;

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

  GdkAtom motif_selection;
  GdkAtom xdnd_selection;
  guint   ref_count;

  guint16 last_x;		/* Coordinates from last event */
  guint16 last_y;
70 71 72
  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 */
73

74 75
  Window dest_xid;              /* The last window we looked up */
  Window drop_xid;            /* The (non-proxied) window that is receiving drops */
76
  guint xdnd_targets_set : 1;   /* Whether we've already set XdndTypeList */
77
  guint xdnd_actions_set : 1;   /* Whether we've already set XdndActionList */
78
  guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  guint motif_targets_set : 1;  /* Whether we've already set motif initiator info */
  guint drag_status : 4;	/* current status of drag */

  GdkWindowCache *window_cache;
};

GdkDragContext *current_dest_drag = NULL;

/* Forward declarations */

static void gdk_window_cache_destroy (GdkWindowCache *cache);

static void     motif_read_target_table (void);
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);

120 121 122 123
static void   xdnd_manage_source_filter (GdkDragContext *context,
					 GdkWindow      *window,
					 gboolean        add_filter);

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/* Drag Contexts */

static GList *contexts;

GdkDragContext *
gdk_drag_context_new        (void)
{
  GdkDragContextPrivate *result;

  result = g_new0 (GdkDragContextPrivate, 1);

  result->ref_count = 1;

  contexts = g_list_prepend (contexts, result);

  return (GdkDragContext *)result;
}

void            
gdk_drag_context_ref (GdkDragContext *context)
{
Jeff Garzik's avatar
Jeff Garzik committed
145 146
  g_return_if_fail (context != NULL);

147 148 149 150 151 152 153
  ((GdkDragContextPrivate *)context)->ref_count++;
}

void            
gdk_drag_context_unref (GdkDragContext *context)
{
  GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
154

Jeff Garzik's avatar
Jeff Garzik committed
155
  g_return_if_fail (context != NULL);
156
  g_return_if_fail (private->ref_count > 0);
Jeff Garzik's avatar
Jeff Garzik committed
157

158 159
  private->ref_count--;
  
160 161 162 163 164 165 166
  if (private->ref_count == 0)
    {
      g_dataset_destroy (private);
      
      g_list_free (context->targets);

      if (context->source_window)
167 168 169 170 171 172 173
	{
	  if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
	      !context->is_source)
	    xdnd_manage_source_filter (context, context->source_window, FALSE);

	  gdk_window_unref (context->source_window);
	}
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

      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, private);
      g_free (private);
    }
}

static GdkDragContext *
gdk_drag_context_find (gboolean is_source,
		       Window   source_xid,
		       Window   dest_xid)
{
  GList *tmp_list = contexts;
Jeff Garzik's avatar
Jeff Garzik committed
192
  GdkDragContext *context;
193 194
  GdkDragContextPrivate *private;
  Window context_dest_xid;
195 196 197

  while (tmp_list)
    {
Jeff Garzik's avatar
Jeff Garzik committed
198
      context = (GdkDragContext *)tmp_list->data;
199 200 201
      private = (GdkDragContextPrivate *)context;

      context_dest_xid = context->dest_window ? 
202 203
	                   (private->drop_xid ?
			      private->drop_xid :
204 205
			      GDK_WINDOW_XWINDOW (context->dest_window)) :
	                   None;
206 207 208 209

      if ((!context->is_source == !is_source) &&
	  ((source_xid == None) || (context->source_window &&
	    (GDK_WINDOW_XWINDOW (context->source_window) == source_xid))) &&
210
	  ((dest_xid == None) || (context_dest_xid == dest_xid)))
211 212 213 214 215 216 217 218 219 220 221 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 273 274 275 276 277 278 279 280 281 282
	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)
		  {
		    cache->children = g_list_remove_link (cache->children, node);
		    node->next = above_node->next;
283 284
		    if (node->next)
		      node->next->prev = node;
285 286 287 288 289 290 291 292 293 294
		    node->prev = above_node;
		    above_node->next = node;
		  }
	      }
	  }
	break;
      }
    case CreateNotify:
      {
	XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 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
	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 *
gdk_window_cache_new (void)
{
  XWindowAttributes xwa;
  Window root, parent, *children;
  unsigned int nchildren;
  int i;
  
  gint old_warnings = gdk_error_warnings;
  
  GdkWindowCache *result = g_new (GdkWindowCache, 1);

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

  XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
  result->old_event_mask = xwa.your_event_mask;
  XSelectInput (gdk_display, gdk_root_window,
		result->old_event_mask | SubstructureNotifyMask);
  gdk_window_add_filter ((GdkWindow *)&gdk_root_parent, 
			 gdk_window_cache_filter, result);
  
  gdk_error_code = 0;
  gdk_error_warnings = 0;

  if (XQueryTree(gdk_display, gdk_root_window, 
		 &root, &parent, &children, &nchildren) == 0)
    return result;
  
  for (i = 0; i < nchildren ; i++)
    {
      XGetWindowAttributes (gdk_display, children[i], &xwa);

      gdk_window_cache_add (result, children[i],
			    xwa.x, xwa.y, xwa.width, xwa.height,
			    xwa.map_state != IsUnmapped);

393
      if (gdk_error_code)
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
	gdk_error_code = 0;
      else
	{
	  gdk_window_cache_add (result, children[i],
				xwa.x, xwa.y, xwa.width, xwa.height,
				(xwa.map_state != IsUnmapped));
	}
    }

  XFree (children);

  gdk_error_warnings = old_warnings;

  return result;
}

static void
gdk_window_cache_destroy (GdkWindowCache *cache)
{
  XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
  gdk_window_remove_filter ((GdkWindow *)&gdk_root_parent, 
			    gdk_window_cache_filter, cache);

  g_list_foreach (cache->children, (GFunc)g_free, NULL);
  g_list_free (cache->children);
  g_hash_table_destroy (cache->child_hash);
420 421

  g_free (cache);
422 423 424 425
}

static Window
get_client_window_at_coords_recurse (Window  win,
426 427
				     gint    x,
				     gint    y)
428
{
429 430 431 432
  Window root, tmp_parent, *children;
  unsigned int nchildren;
  int i;
  Window child = None;
433 434 435 436 437 438 439 440 441 442 443 444 445 446
  Atom type = None;
  int format;
  unsigned long nitems, after;
  unsigned char *data;
  
  static Atom wm_state_atom = None;

  if (!wm_state_atom)
    wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
    
  XGetWindowProperty (gdk_display, win, 
		      wm_state_atom, 0, 0, False, AnyPropertyType,
		      &type, &format, &nitems, &after, &data);
  
447
  if (gdk_error_code)
448 449
    {
      gdk_error_code = 0;
450

451 452 453 454 455 456 457 458 459
      return None;
    }

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

460 461
#if 0
  /* This is beautiful! Damn Enlightenment and click-to-focus */
462 463 464
  XTranslateCoordinates (gdk_display, gdk_root_window, win,
			 x_root, y_root, &dest_x, &dest_y, &child);

465
  if (gdk_error_code)
466 467
    {
      gdk_error_code = 0;
468

469 470
      return None;
    }
471 472 473 474 475 476
  
#else
  if (XQueryTree(gdk_display, win,
		 &root, &tmp_parent, &children, &nchildren) == 0)
    return 0;

477
  if (!gdk_error_code)
478 479 480 481 482 483 484
    {
      for (i = nchildren - 1; (i >= 0) && (child == None); i--)
	{
	  XWindowAttributes xwa;
	  
	  XGetWindowAttributes (gdk_display, children[i], &xwa);
	  
485
	  if (gdk_error_code)
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	    gdk_error_code = 0;
	  else 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))
	    {
	      x -= xwa.x;
	      y -= xwa.y;
	      child = children[i];
	    }
	}
      
      XFree (children);
    }
  else
    gdk_error_code = 0;
#endif  
502 503

  if (child)
504
    return get_client_window_at_coords_recurse (child, x, y);
505 506 507 508
  else
    return None;
}

Jeff Garzik's avatar
Jeff Garzik committed
509
static Window 
510
get_client_window_at_coords (GdkWindowCache *cache,
511 512 513
			     Window          ignore,
			     gint            x_root,
			     gint            y_root)
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
{
  GList *tmp_list;
  Window retval = None;

  gint old_warnings = gdk_error_warnings;
  
  gdk_error_code = 0;
  gdk_error_warnings = 0;

  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))
	    {
	      retval = get_client_window_at_coords_recurse (child->xid,
535 536
							    x_root - child->x, 
							    y_root - child->y);
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	      if (!retval)
		retval = child->xid;
	    }
	  
	}
      tmp_list = tmp_list->next;
    }

  gdk_error_warnings = old_warnings;
  if (retval)
    return retval;
  else
    return gdk_root_window;
}

#if 0
static Window
get_client_window_at_coords_recurse (Window  win,
				     gint    x_root,
				     gint    y_root)
{
  Window child;
  Atom type = None;
  int format;
  unsigned long nitems, after;
  unsigned char *data;
  int dest_x, dest_y;
  
  static Atom wm_state_atom = None;

  if (!wm_state_atom)
    wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
    
  XGetWindowProperty (gdk_display, win, 
		      wm_state_atom, 0, 0, False, AnyPropertyType,
		      &type, &format, &nitems, &after, &data);
  
574
  if (gdk_error_code)
575 576
    {
      gdk_error_code = 0;
577

578 579 580 581 582 583 584 585 586 587 588 589
      return None;
    }

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

  XTranslateCoordinates (gdk_display, gdk_root_window, win,
			 x_root, y_root, &dest_x, &dest_y, &child);

590
  if (gdk_error_code)
591 592
    {
      gdk_error_code = 0;
593

594 595 596 597 598 599 600 601 602
      return None;
    }

  if (child)
    return get_client_window_at_coords_recurse (child, x_root, y_root);
  else
    return None;
}

Jeff Garzik's avatar
Jeff Garzik committed
603
static Window 
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
get_client_window_at_coords (Window  ignore,
			     gint    x_root,
			     gint    y_root)
{
  Window root, parent, *children;
  unsigned int nchildren;
  int i;
  Window retval = None;
  
  gint old_warnings = gdk_error_warnings;
  
  gdk_error_code = 0;
  gdk_error_warnings = 0;

  if (XQueryTree(gdk_display, gdk_root_window, 
		 &root, &parent, &children, &nchildren) == 0)
    return 0;
621

622 623 624 625 626 627 628 629
  for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
    {
      if (children[i] != ignore)
	{
	  XWindowAttributes xwa;

	  XGetWindowAttributes (gdk_display, children[i], &xwa);

630
	  if (gdk_error_code)
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 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 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 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 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	    gdk_error_code = 0;
	  else if ((xwa.map_state == IsViewable) &&
		   (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
		   (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
	    {
	      retval = get_client_window_at_coords_recurse (children[i],
							    x_root, y_root);
	      if (!retval)
		retval = children[i];
	    }
	}
    }

  XFree (children);

  gdk_error_warnings = old_warnings;
  if (retval)
    return retval;
  else
    return gdk_root_window;
}
#endif

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

/* Static data for MOTIF DND */
static GList **motif_target_lists = NULL;
static gint motif_n_target_lists = -1;

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

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
/* 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))
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 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

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

static Window motif_drag_window = None;
static GdkWindow *motif_drag_gdk_window = NULL;

static GdkAtom motif_drag_targets_atom = GDK_NONE;
static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;

/* Target table handling */

GdkFilterReturn
motif_drag_window_filter (GdkXEvent *xevent,
			  GdkEvent  *event,
			  gpointer data)
{
  XEvent *xev = (XEvent *)xevent;

  switch (xev->xany.type)
    {
    case DestroyNotify:
      motif_drag_window = None;
      motif_drag_gdk_window = NULL;
      break;
    case PropertyNotify:
      if (motif_target_lists &&
	  motif_drag_targets_atom &&
	  (xev->xproperty.atom == motif_drag_targets_atom))
	motif_read_target_table();
      break;
    }
  return GDK_FILTER_REMOVE;
}

831
static Atom motif_drag_window_atom = GDK_NONE;
832 833 834 835 836 837 838 839 840 841 842

static Window
motif_lookup_drag_window (Display *display)
{
  Window retval = None;
  gulong bytes_after, nitems;
  GdkAtom type;
  gint format;
  guchar *data;

  XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
843
		      0, 1, FALSE,
844 845 846
		      XA_WINDOW, &type, &format, &nitems, &bytes_after,
		      &data);
  
847
  if ((format == 32) && (nitems == 1) && (bytes_after == 0))
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
    {
      retval = *(Window *)data;
      GDK_NOTE(DND, 
	       g_message ("Found drag window %#lx\n", motif_drag_window));
    }

  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.
 */
Window 
motif_find_drag_window (gboolean create)
{
  if (!motif_drag_window)
    {
      if (!motif_drag_window_atom)
	motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);

      motif_drag_window = motif_lookup_drag_window (gdk_display);
      
      if (!motif_drag_window && create)
	{
	  /* Create a persistant window. (Copied from LessTif) */
	  
	  Display *display;
	  XSetWindowAttributes attr;
879
	  display = XOpenDisplay (gdk_display_name);
880 881 882 883 884 885 886 887 888 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 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
	  XSetCloseDownMode (display, RetainPermanent);

	  XGrabServer (display);
	  
	  motif_drag_window = motif_lookup_drag_window (display);

	  if (!motif_drag_window)
	    {
	      attr.override_redirect = True;
	      attr.event_mask = PropertyChangeMask;
	      
	      motif_drag_window = 
		XCreateWindow(display, DefaultRootWindow(display),
			      -100, -100, 10, 10, 0, 0,
			      InputOnly, CopyFromParent,
			      (CWOverrideRedirect | CWEventMask), &attr);
	      
	      GDK_NOTE (DND,
			g_message ("Created drag window %#lx\n", motif_drag_window));
	      
	      XChangeProperty (display, gdk_root_window,
			       motif_drag_window_atom, XA_WINDOW,
			       32, PropModeReplace,
			       (guchar *)&motif_drag_window_atom, 1);

	    }
	  XUngrabServer (display);
	  XCloseDisplay (display);
	}

      /* There is a miniscule race condition here if the drag window
       * gets destroyed exactly now.
       */
      if (motif_drag_window)
	{
	  motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
	  gdk_window_add_filter (motif_drag_gdk_window,
				 motif_drag_window_filter,
				 NULL);
	}
    }

  return motif_drag_window;
}

static void 
motif_read_target_table (void)
{
  gulong bytes_after, nitems;
  GdkAtom type;
  gint format;
  gint i, j;

  if (!motif_drag_targets_atom)
    motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);

  if (motif_target_lists)
    {
      for (i=0; i<motif_n_target_lists; i++)
	g_list_free (motif_target_lists[i]);
      
      g_free (motif_target_lists);
      motif_target_lists = NULL;
      motif_n_target_lists = 0;
    }

  if (motif_find_drag_window (FALSE))
    {
      MotifTargetTableHeader *header = NULL;
      guchar *target_bytes = NULL;
      guchar *p;
      gboolean success = FALSE;

      XGetWindowProperty (gdk_display, motif_drag_window, 
			  motif_drag_targets_atom,
			  0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
			  motif_drag_targets_atom, 
			  &type, &format, &nitems, &bytes_after,
			  (guchar **)&header);

      if ((format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
	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);

      XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
			  (sizeof(MotifTargetTableHeader)+3)/4, 
968
			  (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
			  FALSE,
			  motif_drag_targets_atom, &type, &format, &nitems, 
			  &bytes_after, &target_bytes);
      
      if ((format != 8) || (bytes_after != 0) || 
	  (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
	  goto error;

      motif_n_target_lists = header->n_lists;
      motif_target_lists = g_new0 (GList *, motif_n_target_lists);

      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);
990 991 992 993 994 995

	  /* 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);
996 997 998 999 1000 1001 1002 1003 1004 1005

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

	  for (j=0; j<n_targets; j++)
	    motif_target_lists[i] = 
	      g_list_prepend (motif_target_lists[i],
			      GUINT_TO_POINTER (card32_to_host (targets[j], 
								header->byte_order)));
1006
	  g_free (targets);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 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
	  motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
	}

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

      if (!success)
	{
	  if (motif_target_lists)
	    {
	      g_free (motif_target_lists);
	      motif_target_lists = NULL;
	      motif_n_target_lists = 0;
	    }
	  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
motif_target_table_check (GList *sorted)
{
  GList *tmp_list1, *tmp_list2;
  gint i;

  for (i=0; i<motif_n_target_lists; i++)
    {
      tmp_list1 = motif_target_lists[i];
      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
motif_add_to_target_table (GList *targets)
{
  GList *sorted = NULL;
  gint index = -1;
  gint i;
  GList *tmp_list;
  
  /* make a sorted copy of the list */
  
  while (targets)
    {
      sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
      targets = targets->next;
    }

  /* First check if it is their already */

  if (motif_target_lists)
    index = motif_target_table_check (sorted);

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

      XGrabServer(gdk_display);
      motif_read_target_table();
    
      /* Check again, in case it was added in the meantime */
      
      if (motif_target_lists)
	index =  motif_target_table_check (sorted);

      if (index < 0)
	{
	  guint32 total_size = 0;
	  guchar *data;
	  guchar *p;
	  guint16 *p16;
	  MotifTargetTableHeader *header;
	  
	  if (!motif_target_lists)
	    {
	      motif_target_lists = g_new (GList *, 1);
	      motif_n_target_lists = 1;
	    }
	  else
	    {
	      motif_n_target_lists++;
	      motif_target_lists = g_realloc (motif_target_lists,
					      sizeof(GList *) * motif_n_target_lists);
	    }
	  motif_target_lists[motif_n_target_lists - 1] = sorted;
	  sorted = NULL;
	  index = motif_n_target_lists - 1;

	  total_size = sizeof (MotifTargetTableHeader);
	  for (i = 0; i < motif_n_target_lists ; i++)
	    total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);

	  data = g_malloc (total_size);

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

	  header->byte_order = local_byte_order;
	  header->protocol_version = 0;
	  header->n_lists = motif_n_target_lists;
	  header->total_size = total_size;

	  for (i = 0; i < motif_n_target_lists ; i++)
	    {
1146 1147 1148
	      guint16 n_targets = g_list_length (motif_target_lists[i]);
	      guint32 *targets = g_new (guint32, n_targets);
	      guint32 *p32 = targets;
1149 1150 1151 1152
	      
	      tmp_list = motif_target_lists[i];
	      while (tmp_list)
		{
1153 1154
		  *p32 = GPOINTER_TO_UINT (tmp_list->data);
		  
1155
		  tmp_list = tmp_list->next;
1156
		  p32++;
1157
		}
1158 1159 1160 1161 1162 1163 1164 1165 1166

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

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

	      *p16 = n_targets;
	      p += sizeof(guint32) * n_targets;
	      g_free (targets);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
	    }

	  XChangeProperty (gdk_display, motif_drag_window,
			   motif_drag_targets_atom,
			   motif_drag_targets_atom,
			   8, PropModeReplace,
			   data, total_size);
	}
      XUngrabServer(gdk_display);
    }

  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)
{
  GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
  MotifDragInitiatorInfo info;
  gint i;
  static GdkAtom motif_drag_initiator_info = GDK_NONE;

  if (!motif_drag_initiator_info)
    motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);

  info.byte_order = local_byte_order;
  info.protocol_version = 0;
  
  info.targets_index = motif_add_to_target_table (context->targets);

  for (i=0; ; i++)
    {
      gchar buf[20];
      g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
      
      private->motif_selection = gdk_atom_intern (buf, FALSE);
      if (!XGetSelectionOwner (gdk_display, private->motif_selection))
	break;
    }

  info.selection_atom = private->motif_selection;

  XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
		   GDK_WINDOW_XWINDOW (context->source_window),
		   private->motif_selection,
		   motif_drag_initiator_info, 8, PropModeReplace,
		   (guchar *)&info, sizeof (info));

  private->motif_targets_set = 1;
}

1285
guint32
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
motif_check_dest (Window win)
{
  gboolean retval = FALSE;
  MotifDragReceiverInfo *info;
  Atom type = None;
  int format;
  unsigned long nitems, after;

  if (!motif_drag_receiver_info_atom)
    motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
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
  XGetWindowProperty (gdk_display, win, 
		      motif_drag_receiver_info_atom, 
		      0, (sizeof(*info)+3)/4, False, AnyPropertyType,
		      &type, &format, &nitems, &after, 
		      (guchar **)&info);
  
  if (type != None)
    {
      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);
    }

1322
  return retval ? win : GDK_NONE;
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
  
}

static void
motif_send_enter (GdkDragContext  *context,
		  guint32          time)
{
  XEvent xev;
  GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;

  xev.xclient.type = ClientMessage;
  xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
  xev.xclient.format = 8;
  xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);

1338 1339 1340 1341 1342
  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;
  MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1343 1344 1345 1346

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

1347
  MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

  if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
			FALSE, 0, &xev))
    GDK_NOTE (DND, 
	      g_message ("Send event to %lx failed",
			 GDK_WINDOW_XWINDOW (context->dest_window)));
}

static void
motif_send_leave (GdkDragContext  *context,
		  guint32          time)
{
  XEvent xev;

  xev.xclient.type = ClientMessage;
  xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
  xev.xclient.format = 8;
  xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);

1367 1368 1369 1370 1371 1372
  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;
  MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
  MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396

  if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
			FALSE, 0, &xev))
    GDK_NOTE (DND, 
	      g_message ("Send event to %lx failed",
			 GDK_WINDOW_XWINDOW (context->dest_window)));
}

static gboolean
motif_send_motion (GdkDragContext  *context,
		    gint            x_root, 
		    gint            y_root,
		    GdkDragAction   action,
		    guint32         time)
{
  gboolean retval;
  XEvent xev;
  GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;

  xev.xclient.type = ClientMessage;
  xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
  xev.xclient.format = 8;
  xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);

1397 1398 1399
  MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
  MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
  MOTIF_XCLIENT_LONG (&xev, 1) = time;
1400

1401 1402
  if ((context->suggested_action != private->old_action) ||
      (context->actions != private->old_actions))
1403
    {
1404
      MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1405

1406
      /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1407 1408 1409 1410
      retval = TRUE;
    }
  else
    {
1411
      MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1412

1413 1414
      MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
      MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
      
      private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
      retval = FALSE;
    }

  if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
			FALSE, 0, &xev))
    GDK_NOTE (DND, 
	      g_message ("Send event to %lx failed",
			 GDK_WINDOW_XWINDOW (context->dest_window)));

  return retval;
}

static void
motif_send_drop (GdkDragContext *context, guint32 time)
{
  XEvent xev;
  GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;

  xev.xclient.type = ClientMessage;
  xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
  xev.xclient.format = 8;
  xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);

1440 1441 1442 1443
  MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
  MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
  MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
  MOTIF_XCLIENT_LONG (&xev, 1)  = time;
1444

1445 1446
  MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
  MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1447

1448 1449
  MOTIF_XCLIENT_LONG (&xev, 3)  = private->motif_selection;
  MOTIF_XCLIENT_LONG (&xev, 4)  = GDK_WINDOW_XWINDOW (context->source_window);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

  if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
			FALSE, 0, &xev))
    GDK_NOTE (DND, 
	      g_message ("Send event to %lx failed",
			 GDK_WINDOW_XWINDOW (context->dest_window)));
}

/* Target Side */

static gboolean
motif_read_initiator_info (Window source_window, 
			   Atom atom,
			   GList  **targets,
			   GdkAtom *selection)
{
  GList *tmp_list;
  static GdkAtom motif_drag_initiator_info = GDK_NONE;
  GdkAtom type;
  gint format;
  gulong nitems;
  gulong bytes_after;

  MotifDragInitiatorInfo *initiator_info;

  if (!motif_drag_initiator_info)
    motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);

  XGetWindowProperty (gdk_display, source_window, atom,
		      0, sizeof(*initiator_info), FALSE,
		      motif_drag_initiator_info, 
		      &type, &format, &nitems, &bytes_after,
		      (guchar **)&initiator_info);

  if ((format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
    {
      g_warning ("Error reading initiator info\n");
      return FALSE;
    }

  motif_read_target_table ();

Owen Taylor's avatar
Owen Taylor committed
1492 1493 1494 1495 1496
  initiator_info->targets_index = 
    card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
  initiator_info->selection_atom = 
    card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
  
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
  if (initiator_info->targets_index >= motif_n_target_lists)
    {
      g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
      XFree (initiator_info);
      return GDK_FILTER_REMOVE;
    }

  tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);

  *targets = NULL;
  while (tmp_list)
    {
      *targets = g_list_prepend (*targets,
				 tmp_list->data);
      tmp_list = tmp_list->prev;
    }

#ifdef G_ENABLE_DEBUG
  if (gdk_debug_flags & GDK_DEBUG_DND)
    print_target_list (*targets);
#endif /* G_ENABLE_DEBUG */

  *selection = initiator_info->selection_atom;

  XFree (initiator_info);

  return TRUE;
}

static GdkDragContext *
motif_drag_context_new (GdkWindow *dest_window,
			guint32    timestamp,
			guint32    source_window,
			guint32    atom)
{
  GdkDragContext *new_context;
  GdkDragContextPrivate *private;

  /* FIXME, current_dest_drag really shouldn't be NULL'd
   * if we error below.
   */
  if (current_dest_drag != NULL)
    {
      if (timestamp >= current_dest_drag->start_time)
	{
	  gdk_drag_context_unref (current_dest_drag);
	  current_dest_drag = NULL;
	}
      else
	return NULL;
    }

  new_context = gdk_drag_context_new ();
  private = (GdkDragContextPrivate *)new_context;

  new_context->protocol = GDK_DRAG_PROTO_MOTIF;
  new_context->is_source = FALSE;

  new_context->source_window = gdk_window_lookup (source_window);
  if (new_context->source_window)
    gdk_window_ref (new_context->source_window);
  else
    {
      new_context->source_window = gdk_window_foreign_new (source_window);
      if (!new_context->source_window)
	{
	  gdk_drag_context_unref (new_context);
	  return NULL;
	}
    }

  new_context->dest_window = dest_window;
  gdk_window_ref (dest_window);
  new_context->start_time = timestamp;

  if (!motif_read_initiator_info(source_window,
				 atom,
				 &new_context->targets,
				 &private->motif_selection))
    {
      gdk_drag_context_unref (new_context);
      return NULL;
    }

  return new_context;
}

/*
 * The MOTIF drag protocol has no real provisions for distinguishing
 * multiple simultaneous drops. If the sources grab the pointer
 * when doing drags, that shouldn't happen, in any case. If it
 * does, we can't do much except hope for the best.
 */

static GdkFilterReturn
motif_top_level_enter (GdkEvent *event,
		       guint16   flags, 
		       guint32   timestamp, 
		       guint32   source_window, 
		       guint32   atom)
{
  GdkDragContext *new_context;

  GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
			   flags, timestamp, source_window, atom));

  new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
  if (!new_context)
    return GDK_FILTER_REMOVE;

  event->dnd.type = GDK_DRAG_ENTER;
  event->dnd.context = new_context;
  gdk_drag_context_ref (new_context);

  current_dest_drag = new_context;

  return GDK_FILTER_TRANSLATE;
}

GdkFilterReturn
motif_top_level_leave (GdkEvent *event,
		       guint16   flags, 
		       guint32   timestamp)
{
  GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
			   flags, timestamp));

  if ((current_dest_drag != NULL) &&
      (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
      (timestamp >= current_dest_drag->start_time))
    {
      event->dnd.type = GDK_DRAG_LEAVE;
      /* Pass ownership of context to the event */
      event->dnd.context = current_dest_drag;

      current_dest_drag = NULL