2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: proxy.c /main/6 1998/08/03 09:33:32 mgreess $ */
24 /* Copyright (c) 1990, 1991, 1992, 1993 UNIX System Laboratories, Inc. */
25 /* Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 AT&T */
26 /* All Rights Reserved */
28 /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
29 /* UNIX System Laboratories, Inc. */
30 /* The copyright notice above does not evidence any */
31 /* actual or intended publication of such source code. */
34 * The Proxy Agent is used to translate Drag And Drop (dnd)
35 * messages between Motif and OLIT clients.
38 * Any Motif or OLIT client running before the proxy was started will have to
39 * be restarted to participate in drag and drop.
45 #include <X11/Xproto.h>
47 #include <X11/Xutil.h>
48 #include <X11/Xatom.h>
51 #define DPRINTF(args) (void) printf args
52 #define a2s(atom) (atom ? XGetAtomName(dpy, atom) : "0")
57 #define OLIT_DROP_SELECTION 0
58 #define OLIT_DROP_TIME 1
59 #define OLIT_DROP_COORDINATE 2
60 #define OLIT_DROP_SITE_ID 3
61 #define OLIT_DROP_OPERATION 4
63 #define GET_PROPERTY_MAX 1000000
64 #define MOTIF_DRAG_PROTOCOL_VERSION 0
65 #define DROP_TABLE_MAX 10
66 #define MOTIF_MESSAGE_TYPE_MASK 0x7f
67 #define MOTIF_RECEIVER_BIT 0x80
68 #define MOTIF_DROP_SITE_STATUS_MASK ((CARD16) 0xFF0F)
69 #define MOTIF_DROP_SITE_STATUS_VALID ((CARD16) 0x0030)
72 MOTIF_TOP_LEVEL_ENTER,
73 MOTIF_TOP_LEVEL_LEAVE,
75 MOTIF_DROP_SITE_ENTER,
76 MOTIF_DROP_SITE_LEAVE,
79 MOTIF_DRAG_DROP_FINISH,
80 MOTIF_OPERATION_CHANGED
86 XmDRAG_PREFER_PREREGISTER,
88 XmDRAG_PREFER_DYNAMIC,
90 XmDRAG_PREFER_RECEIVER
96 Time time_stamp; /* time of drop_start or trigger */
98 Window proxy_sel_req_win; /* needed for concurrent drops */
99 Window receiver_win; /* was motif_receiver_win!!!
100 * This field was only for the olit
101 * source and was initialized in
102 * HandleOlitTrigger().
104 * Now this field is also served as
105 * the olit destination for the
106 * SUN_DRAGDROP_DONE situation and
107 * this field is initialized in
108 * ProxyMain:SelectionRequest... */
109 CARD32 motif_action; /* for olit source only */
110 INT16 trigger_x; /* for olit source only */
111 INT16 trigger_y; /* for olit source only */
112 Atom status; /* for olit source only!
113 * ATOM_MOTIF_SUCESS or
114 * ATOM_MOTIF_FAILURE.
115 * This field will set in
116 * ProxyMain:SelectionRequest... */
126 BYTE protocol_version;
127 CARD16 num_target_lists B16;
140 } motif_drop_start_t;
144 BYTE protocol_version;
145 CARD16 targets_index B16;
146 CARD32 icc_handle B32;
151 BYTE protocol_version;
152 BYTE drag_protocol_style;
154 CARD32 proxy_window B32;
155 CARD16 num_drop_sites B16;
157 CARD32 heap_offset B32;
160 typedef struct _box {
161 short x1, x2, y1, y2;
171 typedef struct _site {
173 unsigned long site_id;
180 extern char *ProgramName;
181 extern drop_site_t *MasterSiteList;
182 extern Window proxy_win;
183 extern Atom ATOM_DRAGDROP_INTEREST;
184 extern Atom ATOM_WM_STATE;
186 unsigned char *GetInterestProperty();
188 static ForwardConversion();
190 static drop_info_t drop_table[DROP_TABLE_MAX];
191 Atom ATOM_SUN_DND_TRIGGER;
192 Atom ATOM_SUN_DND_DONE;
193 Atom ATOM_SUN_SELECTION_END;
194 Atom ATOM_SUN_SELECTION_ERROR;
195 Atom ATOM_SUN_DND_ACK;
196 Atom ATOM_MOTIF_DRAG_WIN;
197 Atom ATOM_MOTIF_PROXY_WIN;
198 Atom ATOM_MOTIF_DND_MESS;
199 Atom ATOM_MOTIF_INITIATOR_INFO;
200 Atom ATOM_MOTIF_RECEIVER_INFO;
201 Atom ATOM_MOTIF_SUCCESS;
202 Atom ATOM_MOTIF_FAILURE;
203 Atom ATOM_MOTIF_TARGETS;
206 Window motif_drag_win;
207 char motif_byte_order;
210 InitializeByteOrder(void)
212 unsigned int endian = 1;
214 /* if LSByte is set, then hardware uses little endian addressing */
215 if (*((char *)&endian))
216 motif_byte_order = 'l';
218 motif_byte_order = 'B';
221 /* swap 2 bytes of data if the byte order is different */
223 Swap2Bytes(char byte_order, CARD16 data)
225 static CARD16 ret_data;
226 char *old_p = (char *) &data;
227 char *new_p = (char *) &ret_data;
229 if (byte_order == motif_byte_order)
237 /* swap 4 bytes of data if the byte order is different */
239 Swap4Bytes(char byte_order, CARD32 data)
241 static CARD32 ret_data;
242 char *old_p = (char *) &data;
243 char *new_p = (char *) &ret_data;
245 if (byte_order == motif_byte_order)
256 /* Get a new drop record */
257 /* use source_handle to indicate whether record is free or in use */
261 int i, oldest_index=0;
263 for(i=0; i<DROP_TABLE_MAX; i++)
264 if (drop_table[i].source_handle == 0) {
268 /* if there are no more free records then use the oldest record */
269 for(i=1; i<DROP_TABLE_MAX; i++)
270 if (drop_table[i].time_stamp < drop_table[oldest_index].time_stamp)
273 return drop_table+oldest_index;
276 /* Search for a drop record */
277 /* use source_handle as the key to the drop_info record */
279 GetDropInfo(Window proxy_sel_req_win)
283 for(i=0; i<DROP_TABLE_MAX; i++)
284 if (drop_table[i].proxy_sel_req_win == proxy_sel_req_win) {
290 /* Search for a drop record using the proxy_handle as the key */
292 GetDropInfoUsingProxyHandle(Atom proxy_handle)
296 for(i=0; i<DROP_TABLE_MAX; i++)
297 if (drop_table[i].proxy_handle == proxy_handle
298 && drop_table[i].source_handle)
303 /* Delete a drop record */
305 ClearDropInfo(Atom proxy_handle)
308 for(i=0; i<DROP_TABLE_MAX; i++)
309 if (drop_table[i].proxy_handle == proxy_handle) {
310 drop_table[i].source_handle = 0; /* reset flag */
315 /* get the window that has the atom associated with it */
317 GetAtomWindow(Display *dpy, Window win, Atom atom)
321 unsigned int nchildren;
325 unsigned long nitems, after;
329 XGetWindowProperty(dpy, win, atom, 0, 0, False, AnyPropertyType,
330 &type, &format, &nitems, &after, &data);
334 if (!XQueryTree(dpy, win, &root, &parent, &children, &nchildren) ||
337 for (i = nchildren - 1; i >= 0; i--) {
338 if (inf = GetAtomWindow(dpy, children[i], atom))
345 /* get the window that is a property of the inputed window */
347 GetPropertyWindow(Display *dpy, Window in_win, Atom atom)
351 unsigned long lengthRtn;
352 unsigned long bytesafter;
353 Window *property = NULL;
356 if ((XGetWindowProperty (dpy,
366 (unsigned char **) &property) == Success) &&
367 (type == XA_WINDOW) &&
373 XFree ((char *)property);
379 /* startup initialization */
381 ProxyInit(Display *dpy, Window dsdm_win)
383 enum { XA_MOTIF_DRAG_WINDOW, XA_MOTIF_DRAG_PROXY_WINDOW,
384 XA_MOTIF_DRAG_AND_DROP_MESSAGE,
385 XA_MOTIF_DRAG_INITIATOR_INFO, XA_MOTIF_DRAG_RECEIVER_INFO,
386 XAXmTRANSFER_SUCCESS, XAXmTRANSFER_FAILURE,
387 XA_MOTIF_DRAG_TARGETS, XA_SUN_DRAGDROP_TRIGGER,
388 XA_SUN_DRAGDROP_DONE, XA_SUN_SELECTION_END, XA_SUN_SELECTION_ERROR,
389 XA_SUN_DRAGDROP_ACK, XATARGETS, XAMULTIPLE, NUM_ATOMS };
390 static char* atom_names[] = {
391 "_MOTIF_DRAG_WINDOW", "_MOTIF_DRAG_PROXY_WINDOW",
392 "_MOTIF_DRAG_AND_DROP_MESSAGE",
393 "_MOTIF_DRAG_INITIATOR_INFO", "_MOTIF_DRAG_RECEIVER_INFO",
394 "XmTRANSFER_SUCCESS", "XmTRANSFER_FAILURE",
395 "_MOTIF_DRAG_TARGETS", "_SUN_DRAGDROP_TRIGGER",
396 "_SUN_DRAGDROP_DONE", "_SUN_SELECTION_END", "_SUN_SELECTION_ERROR",
397 "_SUN_DRAGDROP_ACK", "TARGETS", "MULTIPLE" };
400 enum { ATOM_BUF_LEN = 25 };
401 char buf[ATOM_BUF_LEN * DROP_TABLE_MAX];
402 char *buf_names[DROP_TABLE_MAX];
403 Atom buf_atoms[DROP_TABLE_MAX];
404 XSetWindowAttributes attr;
405 Atom atoms[NUM_ATOMS];
407 InitializeByteOrder();
409 /* make motif_drag_win and proxy_win persistant */
410 XSetCloseDownMode(dpy, RetainPermanent);
411 XInternAtoms(dpy, atom_names, NUM_ATOMS, False, atoms);
413 ATOM_MOTIF_DRAG_WIN = atoms[XA_MOTIF_DRAG_WINDOW];
414 ATOM_MOTIF_PROXY_WIN = atoms[XA_MOTIF_DRAG_PROXY_WINDOW];
415 ATOM_MOTIF_DND_MESS = atoms[XA_MOTIF_DRAG_AND_DROP_MESSAGE];
416 ATOM_MOTIF_INITIATOR_INFO = atoms[XA_MOTIF_DRAG_INITIATOR_INFO];
417 ATOM_MOTIF_RECEIVER_INFO = atoms[XA_MOTIF_DRAG_RECEIVER_INFO];
418 ATOM_MOTIF_SUCCESS = atoms[XAXmTRANSFER_SUCCESS];
419 ATOM_MOTIF_FAILURE = atoms[XAXmTRANSFER_FAILURE];
420 ATOM_MOTIF_TARGETS = atoms[XA_MOTIF_DRAG_TARGETS];
422 ATOM_SUN_DND_TRIGGER = atoms[XA_SUN_DRAGDROP_TRIGGER];
423 ATOM_SUN_DND_DONE = atoms[XA_SUN_DRAGDROP_DONE];
424 ATOM_SUN_SELECTION_ERROR = atoms[XA_SUN_SELECTION_ERROR];
425 ATOM_SUN_SELECTION_END = atoms[XA_SUN_SELECTION_END];
426 ATOM_SUN_DND_ACK = atoms[XA_SUN_DRAGDROP_ACK];
428 ATOM_TARGETS = atoms[XATARGETS];
429 ATOM_MULTIPLE = atoms[XAMULTIPLE];
431 for (i=0; i<DROP_TABLE_MAX; i++) {
432 buf_names[i] = buf + (ATOM_BUF_LEN * i);
433 sprintf(buf_names[i], "DND_PROXY_HANDLE_%d", i);
435 XInternAtoms(dpy, buf_names, DROP_TABLE_MAX, False, buf_atoms);
437 for (i=0; i<DROP_TABLE_MAX; i++) {
438 drop_table[i].proxy_handle = buf_atoms[i];
439 drop_table[i].proxy_sel_req_win =
441 DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0,
442 InputOnly, CopyFromParent, 0, &attr);
444 motif_drag_win = GetPropertyWindow(dpy, DefaultRootWindow(dpy),
445 ATOM_MOTIF_DRAG_WIN);
447 /* if motif_drag_win doesn't exist then define and declare it */
448 if (motif_drag_win == None) {
449 motif_drag_win = dsdm_win;
450 XChangeProperty (dpy,
451 DefaultRootWindow(dpy),
456 (unsigned char *) &motif_drag_win,
461 /* define and declare the proxy_window */
462 proxy_win = dsdm_win;
463 XChangeProperty(dpy, motif_drag_win,
464 ATOM_MOTIF_PROXY_WIN, /* property */
465 XA_WINDOW, /* type */
467 PropModeReplace, /* mode */
468 (unsigned char *) &proxy_win, /* data */
473 printf("drag_win=0x%lx, proxy_win=0x%lx\n", motif_drag_win, proxy_win);
475 /* Watch for mapping of top level windows */
476 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureNotifyMask);
478 /* Watch for someone taking away proxy_win ownership */
479 XSelectInput(dpy, proxy_win, PropertyChangeMask);
481 for(i=0; i<DROP_TABLE_MAX; i++)
482 XSetSelectionOwner(dpy, drop_table[i].proxy_handle,
483 proxy_win, CurrentTime);
486 /* Place the motif receiver property on the Openlook receiver's top
489 AdvertiseMotifDropSite(Display *dpy, Window win)
491 motif_receiver_t receiver_info;
492 unsigned long nitems;
494 int motif_receiver = False;
496 #define OL_PREVIEW_INDEX 4
497 #define MOTIF_RECEIVER_FLAG 0x80000000
498 /* make sure receiver is not a Motif application */
499 data = (CARD32 *) GetInterestProperty(dpy, win, &nitems);
501 if (nitems > OL_PREVIEW_INDEX &&
502 (data[OL_PREVIEW_INDEX] & MOTIF_RECEIVER_FLAG))
503 motif_receiver = True;
510 printf("AdvertiseMotifDropSite\n");
513 receiver_info.byte_order = motif_byte_order;
514 receiver_info.protocol_version = MOTIF_DRAG_PROTOCOL_VERSION;
515 receiver_info.drag_protocol_style = XmDRAG_DROP_ONLY;
516 receiver_info.proxy_window = proxy_win;
517 receiver_info.num_drop_sites = 0; /* not used in drag_drop_only mode */
518 receiver_info.heap_offset = 0; /* not used in drag_drop_only mode */
522 ATOM_MOTIF_RECEIVER_INFO, /* property */
523 ATOM_MOTIF_RECEIVER_INFO, /* type */
525 PropModeReplace, /* mode */
526 (unsigned char *) &receiver_info, /* data */
527 sizeof(motif_receiver_t) /* size of data in bytes */
532 /* Determine if the drop coordinate is within a rectangle */
534 InRect(int drop_x, int drop_y, int x, int y, unsigned int width, unsigned int height)
536 if ((drop_x >= x) && (drop_x <= (x+width)) &&
537 (drop_y >= y) && (drop_y <= (y+height)))
543 #define DRAGDROP_VERSION 0 /* OLIT dnd version */
544 #define INTEREST_RECT 0
545 #define INTEREST_WINDOW 1
546 #define NEXTWORD(result) if (index >= nitems) { \
550 (result) = data[index++]
552 /* get the drop site given the window and coordinate */
554 GetOlitDropSite(Display *dpy, Window top_level_win, INT16 drop_x, INT16 drop_y,
555 unsigned long *ret_site_id, Window *ret_event_win)
557 unsigned long *data, nitems, version, nsites, event_win, site_id, flags;
558 unsigned long areatype, nrects, areawin;
564 unsigned int width, height, border, ujunk;
567 *ret_event_win = *ret_site_id = 0;
568 data = (unsigned long *) GetInterestProperty(dpy, top_level_win, &nitems);
569 if (data == NULL) return;
571 /* get root window */
572 if (XGetGeometry(dpy, top_level_win, &root_win, &junk, &junk,
573 &ujunk, &ujunk, &ujunk, &ujunk) == 0)
577 if (version != DRAGDROP_VERSION) return;
579 for (i=0; i<nsites && !done; ++i) {
580 NEXTWORD(event_win); NEXTWORD(site_id); NEXTWORD(flags);
585 for (j=0; j<nrects && !done; ++j) {
590 if (XTranslateCoordinates(dpy, top_level_win, root_win, x, y,
592 done = InRect((int)drop_x,(int)drop_y, x, y, width,height);
595 case INTEREST_WINDOW:
597 for (j=0; j<nrects && !done; ++j) {
599 if (0 == XGetGeometry(dpy, areawin, &wjunk, &junk, &junk,
600 &width, &height, &border, &ujunk)) {
602 "%s: XGetGeometry failed on window 0x%lx\n",
603 ProgramName, areawin);
606 if (XTranslateCoordinates(dpy, areawin, root_win, 0, 0,
610 width = width + border;
611 height = height + border;
612 done = InRect((int)drop_x, (int)drop_y, x, y,width,height);
618 "%s: unknown site area type on window 0x%lx\n",
619 ProgramName, top_level_win);
624 *ret_site_id = site_id;
625 *ret_event_win = event_win;
629 #define MOTIF_DROP_MOVE_ACTION (1 << 0)
630 #define MOTIF_DROP_COPY_ACTION (1 << 1)
631 #define MOTIF_DROP_LINK_ACTION (1 << 2)
632 #define MOTIF_DROP_ALLOWED_ACTIONS 0x0F00
633 #define OLIT_DROP_ACKNOWLEDGE_ACTION (1 << 1)
634 #define OLIT_DROP_TRANSIENT_ACTION (1 << 2)
635 #define OLIT_DROP_MOVE_ACTION (1 << 0)
636 #define OLIT_DROP_COPY_ACTION 0
637 #define OLIT_DROP_LINK_ACTION (1 << 10)
639 /* translate an OLIT operation to a Motif operation */
641 ConvertOlitAction(CARD32 olit_action)
645 motif_action = MOTIF_DROP_COPY_ACTION | (MOTIF_DROP_COPY_ACTION << 8);
647 if (olit_action & OLIT_DROP_MOVE_ACTION)
648 motif_action |= MOTIF_DROP_MOVE_ACTION | (MOTIF_DROP_MOVE_ACTION << 8);
649 else if (olit_action & OLIT_DROP_LINK_ACTION)
650 motif_action |= MOTIF_DROP_LINK_ACTION | (MOTIF_DROP_LINK_ACTION << 8);
655 /* translate a Motif operation to an OLIT operation */
657 ConvertMotifAction(CARD16 motif_action)
661 olit_action = OLIT_DROP_TRANSIENT_ACTION;
662 if (motif_action & MOTIF_DROP_MOVE_ACTION)
663 olit_action |= OLIT_DROP_MOVE_ACTION;
664 else if (motif_action & MOTIF_DROP_COPY_ACTION)
665 olit_action |= OLIT_DROP_COPY_ACTION;
666 else if (motif_action & MOTIF_DROP_LINK_ACTION)
667 olit_action |= OLIT_DROP_LINK_ACTION;
673 /* Process Motif ClientMessage */
675 HandleMotifMessage(Display *dpy, XClientMessageEvent *event)
677 motif_drop_start_t info;
678 drop_info_t *drop_info;
681 dest_win = event->window;
682 info.message_type = event->data.b[0];
683 info.byte_order = event->data.b[1];
685 if (info.message_type & MOTIF_RECEIVER_BIT) {
687 If initiator is Motif then forward it this message. */
688 if (event->window != proxy_win)
689 XSendEvent(dpy, event->window, False,
690 NoEventMask, (XEvent *) event);
693 /* initiator message */
694 if ((info.message_type & MOTIF_MESSAGE_TYPE_MASK)== MOTIF_DROP_START) {
695 unsigned long site_id;
697 XClientMessageEvent out_event;
700 /* receiver will never be MOTIF because MOTIF receivers
701 don't set the proxy field of RECEIVER_INFO atom
704 info.flags = Swap2Bytes(info.byte_order, event->data.s[1]);
706 /* since dropSiteStatus can't be determined in a short amount
707 of time, always return a valid drop site */
708 info.flags &= MOTIF_DROP_SITE_STATUS_MASK;
709 info.flags |= MOTIF_DROP_SITE_STATUS_VALID;
711 info.time = Swap4Bytes(info.byte_order, event->data.l[1]);
712 info.x = Swap2Bytes(info.byte_order, event->data.s[4]);
713 info.y = Swap2Bytes(info.byte_order, event->data.s[5]);
714 info.icc_handle = Swap4Bytes(info.byte_order, event->data.l[3]);
715 info.src_window = Swap4Bytes(info.byte_order, event->data.l[4]);
717 /* send a drop start ack here
718 to give ourselves more time to process the drop
719 because GetOlitDropSite or
720 selection processing might take a long time
722 info.message_type = event->data.b[0] |= MOTIF_RECEIVER_BIT;
723 event->data.s[1] = Swap2Bytes(info.byte_order, info.flags);
724 event->window = info.src_window;
725 XSendEvent(dpy, event->window, False,
726 NoEventMask, (XEvent *) event);
728 /* get olit drop site */
730 dest_win, info.x, info.y, &site_id, &event_win);
731 drop_info = NewDropInfo();
732 olit_action = ConvertMotifAction(info.flags);
733 if (event_win && olit_action) {
734 drop_info->source_handle = info.icc_handle;
735 drop_info->time_stamp = info.time;
737 out_event.type = ClientMessage;
738 out_event.send_event = True;
739 out_event.display = dpy;
740 out_event.window = event_win;
741 out_event.message_type = ATOM_SUN_DND_TRIGGER;
742 out_event.format = 32;
744 out_event.data.l[OLIT_DROP_SELECTION]= drop_info->proxy_handle;
745 out_event.data.l[OLIT_DROP_TIME] = info.time;
746 out_event.data.l[OLIT_DROP_COORDINATE] =
747 (info.x << 16) | info.y;
748 out_event.data.l[OLIT_DROP_SITE_ID] = site_id;
749 out_event.data.l[OLIT_DROP_OPERATION] = olit_action;
750 XSendEvent(dpy, out_event.window, False,
751 NoEventMask, (XEvent *) &out_event);
754 /* tell motif initiator that the drop failed */
755 XConvertSelection(dpy,
767 /* Process OLIT ClientMessage */
769 HandleOlitTrigger(Display *dpy, XClientMessageEvent *event)
772 char success = False;
773 Atom source_handle = event->data.l[OLIT_DROP_SELECTION];
774 Time time_stamp = event->data.l[OLIT_DROP_TIME];
776 /* proxy agent only gets ATOM_SUN_DND_TRIGGER if
779 /* map site_id to site_window */
780 for (site = MasterSiteList; site != NULL; site = site->next) {
781 if (site->site_id == event->data.l[OLIT_DROP_SITE_ID]) {
782 drop_info_t *drop_info = NewDropInfo();
784 drop_info->motif_action =
785 ConvertOlitAction(event->data.l[OLIT_DROP_OPERATION]);
786 drop_info->receiver_win = site->window_id;
787 drop_info->source_handle = source_handle;
788 drop_info->trigger_x = (event->data.l[OLIT_DROP_COORDINATE]) >> 16;
789 drop_info->trigger_y =
790 (event->data.l[OLIT_DROP_COORDINATE]) & 0x0000ffff;
791 drop_info->time_stamp = time_stamp;
793 if ((event->data.l[OLIT_DROP_OPERATION])
794 & OLIT_DROP_ACKNOWLEDGE_ACTION) {
795 XConvertSelection(dpy,
796 drop_info->source_handle, /* selection */
798 ATOM_SUN_DND_ACK, /* property */
799 drop_info->proxy_sel_req_win,
800 drop_info->time_stamp /* time */
804 /* get olit initiator targets */
805 XConvertSelection(dpy,
806 drop_info->source_handle, /* selection */
808 drop_info->proxy_handle, /* property */
809 drop_info->proxy_sel_req_win,
810 drop_info->time_stamp /* time */
817 /* tell olit initiator we are done */
818 XConvertSelection(dpy, source_handle,
820 ATOM_SUN_DND_DONE, /* property */
826 /* Place the Motif initiator property on the initiator's top level window */
828 UpdateInitiatorAtom(Display *dpy, Window win, Atom channel, CARD16 targets_index)
830 motif_initiator_t initiator_info;
832 initiator_info.byte_order = motif_byte_order;
833 initiator_info.protocol_version = MOTIF_DRAG_PROTOCOL_VERSION;
834 initiator_info.targets_index = targets_index;
835 initiator_info.icc_handle = channel;
837 XChangeProperty(dpy, win,
838 channel, /* property */
839 ATOM_MOTIF_INITIATOR_INFO, /* type */
841 PropModeReplace, /* mode */
842 (unsigned char *) &initiator_info, /* data */
843 sizeof(motif_initiator_t) /* size of data in bytes */
847 /* Send Motif DROP_START ClientMessage to receiver */
849 SendStartDrop(Display *dpy, Window src_win, drop_info_t *drop_info)
851 motif_drop_start_t info;
852 XClientMessageEvent event;
854 info.message_type = MOTIF_DROP_START;
855 info.byte_order = motif_byte_order;
856 info.flags = drop_info->motif_action;
857 info.time = drop_info->time_stamp;
858 info.x = drop_info->trigger_x;
859 info.y = drop_info->trigger_y;
860 info.icc_handle = drop_info->proxy_handle;
861 info.src_window = src_win;
863 event.type = ClientMessage;
864 event.send_event = True;
866 event.window = drop_info->receiver_win;
867 event.message_type = ATOM_MOTIF_DND_MESS;
869 memcpy(&event.data, &info, sizeof(info));
870 XSendEvent(dpy, event.window, False, NoEventMask, (XEvent *) &event);
873 /* Compare data of type Atom */
875 AtomCompare(const void *a1, const void *a2)
877 const Atom *atom1 = a1;
878 const Atom *atom2 = a2;
880 return(*atom1 - *atom2);
883 /* Get the Motif TARGETS */
885 GetTargetsList(Display *dpy, motif_targets_t **targets_list_p)
887 motif_targets_t *targets_list;
890 unsigned long length, bytes_after;
892 status = XGetWindowProperty(dpy,
895 0L, GET_PROPERTY_MAX,
902 (unsigned char **) &targets_list);
903 if ((status != Success) || (type != ATOM_MOTIF_TARGETS) || (length == 0)) {
904 /* allocate targets_list of zero targets */
905 targets_list = (motif_targets_t *) malloc(sizeof(motif_targets_t));
906 targets_list->byte_order = motif_byte_order;
907 targets_list->protocol_version = MOTIF_DRAG_PROTOCOL_VERSION;
908 targets_list->num_target_lists = 0;
909 targets_list->size = sizeof(motif_targets_t);
911 *targets_list_p = targets_list;
915 MatchTargets(motif_targets_t *targets_list, Atom *atoms, unsigned long atom_cnt, CARD16 *targets_index_p)
919 int num_sub_list_items, sub_list_index;
921 char byte_order = targets_list->byte_order;
922 int num_target_lists;
923 CARD32 target, tmp_data;
925 num_target_lists = Swap2Bytes(byte_order, targets_list->num_target_lists);
926 data = (CARD32 *) (targets_list+1);
927 for (target_index=0; target_index < num_target_lists; target_index++) {
928 num_sub_p = (CARD16 *) data;
929 num_sub_list_items = (int) Swap2Bytes(byte_order, *num_sub_p);
930 data = (CARD32 *) (num_sub_p+1);
932 if (num_sub_list_items == atom_cnt) {
933 for (sub_list_index=0; sub_list_index < num_sub_list_items;
935 /* address isn't long word aligned so copy one byte at a time */
936 memcpy(&tmp_data, data+sub_list_index, 4);
937 target = Swap4Bytes(byte_order, tmp_data);
938 if (target != atoms[sub_list_index])
941 /* if all the atoms match then we got a match */
942 if (sub_list_index >= num_sub_list_items) {
946 data += num_sub_list_items;
949 *targets_index_p = target_index;
952 /* Put OLIT targets on the Motif TARGETS targets list */
954 GetTargetsIndex(Display *dpy, unsigned char *in_data, unsigned long atom_cnt, CARD16 *targets_index_p)
956 Atom *atoms = (Atom *) in_data;
957 motif_targets_t *new_list, *targets_list;
958 int i, old_size, new_size;
960 CARD32 *target_data, tmp_data;
962 motif_initiator_t initiator_info;
964 CARD16 num_target_lists;
967 for (i=0; i<atom_cnt; i++) {
968 printf("GetTargetsIndex: target[%d]=%s\n", i, a2s(atoms[i]));
972 /* search existing targets list for a match, otherwise add our
973 targets to the list */
974 qsort((void *)atoms, (size_t)atom_cnt, (size_t)sizeof(Atom), AtomCompare);
975 GetTargetsList(dpy, &targets_list);
976 MatchTargets(targets_list, atoms, atom_cnt, targets_index_p);
977 byte_order = targets_list->byte_order;
978 num_target_lists = Swap2Bytes(byte_order,targets_list->num_target_lists);
979 if (*targets_index_p < num_target_lists)
982 /* no match, add our targets to the list */
983 old_size = Swap4Bytes(byte_order, targets_list->size);
984 /* new size = old_list_size + target_cnt(CARD16) + targets(CARD32) */
985 new_size = old_size + sizeof(CARD16) + (sizeof(CARD32) * atom_cnt);
986 new_list = (motif_targets_t *)
987 realloc(targets_list, new_size);
989 /* change the new data's byte order to the existing data's byte order */
990 /* put data in motif target list format */
992 new_list->num_target_lists = Swap2Bytes(byte_order, num_target_lists);
993 new_list->size = Swap4Bytes(byte_order, new_size);
995 target_cnt = (CARD16 *) (((BYTE *) new_list) + old_size);
996 *target_cnt = Swap2Bytes(byte_order, atom_cnt); /* target_cnt */
997 target_data = (CARD32 *) (target_cnt + 1);
998 for (i=0; i<atom_cnt; i++) {
999 /* address isn't long word aligned so copy one byte at a time */
1000 tmp_data = Swap4Bytes(byte_order, atoms[i]);
1001 memcpy(target_data+i, &tmp_data, 4);
1004 /* overwrite existing targets_list */
1005 XChangeProperty(dpy, motif_drag_win, ATOM_MOTIF_TARGETS,
1006 ATOM_MOTIF_TARGETS, 8,
1008 (unsigned char *) new_list, new_size);
1012 * update target list property
1013 * advertise psuedo motif initiator info property
1014 * send DROP_START message to motif receiver
1017 ContinueHandleOlitTrigger(Display *dpy, unsigned char *data, unsigned long length, drop_info_t *drop_info)
1019 CARD16 targets_index;
1020 Window psuedo_motif_initiator_win;
1022 GetTargetsIndex(dpy, data, length, &targets_index);
1023 psuedo_motif_initiator_win = proxy_win;
1024 UpdateInitiatorAtom(dpy, psuedo_motif_initiator_win,
1025 drop_info->proxy_handle, targets_index);
1026 SendStartDrop(dpy, psuedo_motif_initiator_win, drop_info);
1029 /* Get OLIT initiator's TARGETS */
1031 ProcessOlitInitiatorConversion(XSelectionEvent *event, drop_info_t *drop_info)
1035 unsigned long length, bytes_after;
1036 unsigned char *data;
1039 /* XView needs this */
1040 if (event->target == ATOM_SUN_DND_ACK)
1041 XChangeProperty(event->display, event->requestor, event->property,
1042 event->target, 32, PropModeReplace, NULL, 0);
1045 sometimes type is TARGETS and sometimes type is ATOM so don't bother
1047 if ((event->target == ATOM_TARGETS) &&
1048 event->property && (XGetWindowProperty (event->display,
1051 0L, GET_PROPERTY_MAX,
1058 (unsigned char **) &data) == Success)
1061 ContinueHandleOlitTrigger(event->display, data, length, drop_info);
1066 /* copy a property and the associated data from one window to another.
1067 Returns True if successful else returns False */
1069 CopyProperty(Display *dpy, Atom property, Window old_win, Window new_win)
1073 unsigned long length, bytes_after;
1074 unsigned char *data;
1076 if (property && (XGetWindowProperty (dpy, old_win, property,
1077 0L, GET_PROPERTY_MAX,
1084 (unsigned char **) &data) == Success)
1086 XChangeProperty(dpy, new_win, property,
1087 type, format, PropModeReplace, data, (int) length);
1095 /* copy the targets property and the associated data
1096 from one window to another.
1097 Returns True if we get ATOM_SUN_DND_DONE or ATOM_SUN_SELECTION_END */
1099 CopyTargets(Display *dpy, Atom prop, Window old_win, Window new_win)
1103 unsigned long length, bytes_after;
1104 unsigned char *data;
1105 target_prop_t *target_info;
1107 int ol_done = False;
1109 if ((XGetWindowProperty (dpy, old_win, prop,
1110 0L, GET_PROPERTY_MAX,
1119 target_info = (target_prop_t *) data;
1120 for(i=0; i < (length / 2); i++, target_info++) {
1121 DPRINTF(("CopyTargets: targets[%d]=%s\n", i, a2s(target_info->target)));
1122 CopyProperty(dpy, target_info->property, old_win, new_win);
1123 if (target_info->target == ATOM_SUN_DND_DONE ||
1124 (target_info->target == ATOM_SUN_SELECTION_END))
1132 /* Forward the SelectionNotify to the receiver.
1133 Returns True if we get ATOM_SUN_DND_DONE or ATOM_SUN_SELECTION_END */
1135 ForwardConversion(XSelectionEvent *event, drop_info_t *drop_info)
1137 int ol_done = False;
1139 if (CopyProperty(event->display, event->property,
1140 event->requestor, drop_info->source_win)) {
1142 if (event->target == ATOM_MULTIPLE) {
1143 /* get target name and property pair
1144 copy all these properties */
1145 ol_done = CopyTargets(event->display, event->property,
1146 event->requestor, drop_info->source_win);
1150 /* initiator couldn't convert property, tell receiver this */
1151 event->property = 0;
1154 event->requestor = drop_info->source_win;
1155 event->selection = drop_info->proxy_handle;
1156 XSendEvent(event->display, event->requestor, False, 0, (XEvent*)event);
1160 /* This routine handles the selectionrequest for the target MULTIPLE.
1161 XtGetSelectionValues generates a selection request with the target
1162 MULTIPLE. This is how multiple targets selection
1163 (a selection request that asks for multiple targets) work:
1164 Window A calls XtGetSelectionValues
1165 Xt places a property on window A which contains all the targets.
1166 Xt asks selection owner of selection 1 to convert.
1167 Window B which is the selection owner of selection 1
1168 gets a selectionrequest.
1169 Window B reads the property on window A to get the targets
1170 and then converts them.
1171 Window B places the converted targets data in properties on window A.
1172 Window B places the target names and the properties holding
1173 the target data (target/property pair) in a property on window A.
1175 With single target selection (XtGetSelectionValue), window B would
1176 get the target from the selectionrequest event structure and convert it.
1178 The following is how the proxy agent behaves when there is a multiple
1180 Window A calls XtGetSelectionValues
1181 Xt places a property on window A which contains all the targets.
1182 Xt asks selection owner of selection 1 to convert.
1183 Proxy agent which is the selection owner of selection 1
1184 gets a selectionrequest.
1185 Proxy agent reads the property on window A to get the targets.
1186 Proxy agent places a property on itself which contains all the targets.
1187 Proxy agent asks selection owner of selection 2 to convert.
1188 Window B which is the selection owner of selection 2
1189 gets a selectionrequest.
1190 Window B reads the property on proxy agent to get the targets
1191 and then converts them.
1192 Window B places the converted targets data in properties on proxy agent.
1193 Window B places the target names and the properties holding
1194 the target data in a property proxy agent.
1195 Proxy agent reads the targets data and places the targets data
1196 in a property on window A.
1197 Proxy agent reads the target names and the properties holding
1198 the target data and places this info in a property on window A.
1201 ForwardMultpleSelectionRequest(XSelectionRequestEvent *event, drop_info_t *drop_info)
1204 unsigned long length, bytes_after;
1205 unsigned char *data;
1207 drop_info->source_win = event->requestor;
1209 if (CopyProperty(event->display, event->property,
1210 event->requestor, drop_info->proxy_sel_req_win)) {
1212 /* change requestor and selection */
1213 event->requestor = drop_info->proxy_sel_req_win;
1214 event->selection = drop_info->source_handle;
1216 /* forward selection request to initiator */
1217 XConvertSelection(event->display,
1228 /* tell the initiator to clean up and do internal cleanup */
1230 DndDone(Display *dpy, Atom status, drop_info_t *drop_info)
1232 XConvertSelection(dpy,
1233 drop_info->source_handle,
1235 drop_info->proxy_handle,
1236 drop_info->proxy_sel_req_win,
1237 drop_info->time_stamp);
1239 /* XView needs this */
1240 if (status == ATOM_SUN_DND_DONE)
1241 XConvertSelection(dpy,
1242 drop_info->source_handle,
1243 ATOM_SUN_SELECTION_END,
1244 drop_info->proxy_handle,
1245 drop_info->proxy_sel_req_win,
1246 drop_info->time_stamp);
1250 ProxyMain(Display *dpy, XEvent *event)
1252 switch(event->type) {
1255 printf("ClientMessage\n");
1257 if (event->xclient.message_type == ATOM_SUN_DND_TRIGGER)
1258 HandleOlitTrigger(dpy, &event->xclient);
1259 else if (event->xclient.message_type == ATOM_MOTIF_DND_MESS)
1260 HandleMotifMessage(dpy, &event->xclient);
1266 client_win = GetAtomWindow(dpy, event->xcreatewindow.window,
1269 /* if client has olit dnd interest then attach motif dnd
1270 interest to client */
1271 if (GetAtomWindow(dpy, client_win, ATOM_DRAGDROP_INTEREST)) {
1272 AdvertiseMotifDropSite(dpy, client_win);
1274 /* look for PropertyChange because olit receivers
1275 don't always declare ATOM_DRAGDROP_INTEREST at MapNotify time
1277 XSelectInput(dpy, client_win, PropertyChangeMask);
1282 case PropertyNotify:
1283 /* printf("PropertyNotify, win=0x%lx\n", event->xproperty.window);*/
1284 /* if a OLIT drop site is advertised
1285 then advertise MOTIF drop site also */
1286 if (event->xproperty.atom == ATOM_DRAGDROP_INTEREST) {
1287 AdvertiseMotifDropSite(dpy, event->xproperty.window);
1289 else if (event->xproperty.atom == ATOM_MOTIF_PROXY_WIN) {
1290 if ((motif_drag_win == event->xproperty.window) &&
1291 (proxy_win != GetPropertyWindow(dpy, motif_drag_win,
1292 ATOM_MOTIF_PROXY_WIN)))
1293 /* proxy_win has been stolen by another client,
1296 /* maybe the following should be a X message */
1297 fprintf(stderr, "%s: lost proxy control\n", ProgramName);
1300 /* In the future, we might want to handle the case where
1301 * a client removes the ATOM_DRAGDROP_INTEREST or the
1302 * ATOM_MOTIF_RECEIVER_INFO atom
1306 case SelectionRequest: {
1307 drop_info_t *drop_info;
1309 printf("SelectionRequest, selection=%s, target=%s, prop=%s, win=0x%lx, time=%ld\n",
1310 a2s(event->xselectionrequest.selection),
1311 a2s(event->xselectionrequest.target),
1312 a2s(event->xselectionrequest.property),
1313 event->xselectionrequest.requestor,
1314 event->xselectionrequest.time
1317 drop_info = GetDropInfoUsingProxyHandle
1318 (event->xselectionrequest.selection);
1319 if (drop_info == NULL)
1320 /* unknown selection, ignore the SelectionRequest */
1322 if (event->xselectionrequest.target == ATOM_SUN_DND_DONE ||
1323 (event->xselectionrequest.target == ATOM_SUN_SELECTION_END)) {
1324 /* tell motif initiator we are done OLIT does not have a
1325 * Failure; so all dnd exit status map to SUCCESS.
1327 * Also record `requestor' id, so that we know who to
1328 * forward when receiving SelectionNotify. */
1329 drop_info->receiver_win = event->xselectionrequest.requestor;
1330 DndDone(dpy, ATOM_MOTIF_SUCCESS, drop_info);
1332 else if (event->xselectionrequest.target == ATOM_MOTIF_SUCCESS
1333 || (event->xselectionrequest.target == ATOM_MOTIF_FAILURE)) {
1334 /* tell olit initiator we are done.
1336 * We will have to remember this target value so that
1337 * we know what to forward when receiving SelectionNotify.
1339 * Note that olit protocol doesn't have a XmTRANSFER_FAILURE
1340 * equivalence, the closest is _SUN_DRAGDROP_ERROR. We
1341 * may want to consider it otherwise olit app will
1342 * think the drop was successful...
1344 drop_info->status = event->xselectionrequest.target;
1345 DndDone(dpy, ATOM_SUN_DND_DONE, drop_info);
1347 else if (event->xselectionrequest.target == ATOM_SUN_SELECTION_ERROR)
1349 /* tell motif initiator OLIT can not handle this drop.
1350 * we have a failure.
1352 DndDone(dpy, ATOM_MOTIF_FAILURE, drop_info);
1354 else if (event->xselectionrequest.target == ATOM_MULTIPLE)
1355 ForwardMultpleSelectionRequest(
1356 &(event->xselectionrequest), drop_info);
1358 /* forward selection request to initiator */
1359 drop_info->source_win = event->xselectionrequest.requestor;
1360 XConvertSelection(event->xselectionrequest.display,
1361 drop_info->source_handle,
1362 event->xselectionrequest.target,
1363 event->xselectionrequest.property,
1364 drop_info->proxy_sel_req_win,
1365 event->xselectionrequest.time
1369 } /* end of case SelectionRequest */
1371 case SelectionNotify:
1373 printf("SelectionNotify, sel=%s, target=%s, prop=%s, w=0x%lx, time=%ld\n",
1374 a2s(event->xselection.selection),
1375 a2s(event->xselection.target),
1376 a2s(event->xselection.property), event->xselection.requestor,
1377 event->xselection.time);
1380 drop_info_t *drop_info = GetDropInfo(event->xselection.requestor);
1382 if (drop_info == NULL ||
1383 (event->xselection.time < drop_info->time_stamp))
1384 /* got SelectionNotify of unknown selection (source) */
1387 /* Trap these two targets, so that the destination side
1388 * will not see selection-time-out */
1389 if (event->xselection.target == ATOM_SUN_DND_DONE || /* olit->motif */
1390 event->xselection.target == ATOM_MOTIF_SUCCESS) { /* motif->olit */
1392 XSelectionEvent * xe = &event->xselection;
1394 /* Currently, in both olit dnd protocol and
1395 * motif dnd protocol, the destination side
1396 * DONT check the value from the source side!
1398 * We will have to copy xe->property to
1399 * drop_info->receiver_win if this is no longer
1400 * TRUE in the future... */
1401 xe->requestor = drop_info->receiver_win;
1402 xe->selection = drop_info->proxy_handle;
1403 xe->target = xe->target == ATOM_SUN_DND_DONE ?
1404 drop_info->status : ATOM_SUN_DND_DONE;
1405 XSendEvent(xe->display, xe->requestor, False,
1406 NoEventMask, event);
1408 ClearDropInfo(drop_info->proxy_handle);
1411 /* if event->property is proxy_handle then the convert request
1412 was from the proxy agent
1413 else the convert request was from the receiver */
1414 if (event->xselection.property == drop_info->proxy_handle ||
1415 (event->xselection.property == ATOM_SUN_DND_ACK)) {
1416 ProcessOlitInitiatorConversion(&event->xselection, drop_info);
1418 if (ForwardConversion(&event->xselection, drop_info)) {
1420 /* We are here ONLY if the target is ATOM_MULTIPLE and
1421 * the target list contains either _SUN_DRAGDROP_DONE or
1422 * _SUN_SELECTION_END. Probably for XView support?! */
1423 DndDone(dpy, ATOM_MOTIF_SUCCESS, drop_info);
1427 } /* end of case SelectionNotify */
1428 } /* end of switch */