File: xdnd.c

package info (click to toggle)
fbi 2.07-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,240 kB
  • ctags: 2,357
  • sloc: ansic: 17,896; sh: 59; makefile: 33; perl: 12
file content (321 lines) | stat: -rw-r--r-- 9,053 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
70
71
72
73
74
75
76
77
78
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
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
/*
 * basic Xdnd support for Motif 2.x
 * 
 * Receive drops only.  Works fine in parallel with Motif DnD.
 *
 * Initiate drags is probably hard do do without breaking Motif DnD or
 * heavily mucking with the Motif internals.  Highlighting seems to be
 * non-trivial too.
 *
 * Usage:
 *    (1) register XdndAction as "Xdnd"
 *    (2) register Widgets using XdndDropSink (acts like XmeDropSink
 *        for Motif DnD)
 *    (3) the transfer callback functions have to call
 *        XdndDropFinished() when they are done (i.e. after calling
 *        XmTransferDone)
 *
 * Data transfer is done using the usual Motif 2.x way, using UTM.
 * Read: XmNdestinationCallback will be called, with
 * XmDestinationCallbackStruct->selection set to XdndSelection.
 *
 * It is up to the application to handle the Xdnd MIME targets
 * correctly, i.e. accept both "TEXT" and "text/plain" targets for
 * example.  Otherwise Xdnd support shouldn't need much work if Motif
 * DnD support is present already.
 *
 * Known problems:
 *   - Not working with KDE 2.x as they don't provide a TARGETS
 *     target (which IMO is illegal, see ICCCM specs).
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xm/Xm.h>
#include <Xm/Transfer.h>
#include <Xm/TransferP.h>

#include "xdnd.h"

/* ---------------------------------------------------------------------- */

int xdnd_debug = 0;

static Atom XdndAware;
static Atom XdndTypeList;
static Atom XdndSelection;

static Atom XdndEnter;
static Atom XdndPosition;
static Atom XdndStatus;
static Atom XdndLeave;
static Atom XdndDrop;
static Atom XdndFinished;

static Atom XdndActionCopy;
static Atom XdndActionMove;
static Atom XdndActionLink;
static Atom XdndActionAsk;
static Atom XdndActionPrivate;

/* ---------------------------------------------------------------------- */

static void XdndInit(Display *dpy)
{
    if (XdndAware)
	return;

    XdndAware         = XInternAtom(dpy, "XdndAware",         False);
    XdndTypeList      = XInternAtom(dpy, "XdndTypeList",      False);
    XdndSelection     = XInternAtom(dpy, "XdndSelection",     False);

    /* client messages */
    XdndEnter         = XInternAtom(dpy, "XdndEnter",         False);
    XdndPosition      = XInternAtom(dpy, "XdndPosition",      False);
    XdndStatus        = XInternAtom(dpy, "XdndStatus",        False);
    XdndLeave         = XInternAtom(dpy, "XdndLeave",         False);
    XdndDrop          = XInternAtom(dpy, "XdndDrop",          False);
    XdndFinished      = XInternAtom(dpy, "XdndFinished",      False);

    /* actions */
    XdndActionCopy    = XInternAtom(dpy, "XdndActionCopy",    False);
    XdndActionMove    = XInternAtom(dpy, "XdndActionMove",    False);
    XdndActionLink    = XInternAtom(dpy, "XdndActionLink",    False);
    XdndActionAsk     = XInternAtom(dpy, "XdndActionAsk",     False);
    XdndActionPrivate = XInternAtom(dpy, "XdndActionPrivate", False);
}

static void
init_window(Widget widget)
{
    int version = 4;

    /* window */
    XChangeProperty(XtDisplay(widget),XtWindow(widget),
		    XdndAware, XA_ATOM, 32, PropModeReplace,
		    (XtPointer)&version, 1);

    /* shell */
    while (!XtIsShell(widget))
	widget = XtParent(widget);

    XChangeProperty(XtDisplay(widget),XtWindow(widget),
		    XdndAware, XA_ATOM, 32, PropModeReplace,
		    (XtPointer)&version, 1);
    XtOverrideTranslations(widget, XtParseTranslationTable
                           ("<Message>XdndEnter:    Xdnd()\n"
			    "<Message>XdndPosition: Xdnd()\n"
			    "<Message>XdndLeave:    Xdnd()\n"
			    "<Message>XdndDrop:     Xdnd()"));
}

static Widget
find_window(Widget widget, int wx, int wy, int rx, int ry)
{
    WidgetList children;
    Cardinal nchildren;
    Dimension x,y,w,h;
    Widget found = NULL;
    int i;

    nchildren = 0;
    XtVaGetValues(widget,XtNchildren,&children,
                  XtNnumChildren,&nchildren,NULL);
    fprintf(stderr,"findwindow %s\n",XtName(widget));
    for (i = nchildren-1; i >= 0; i--) {
	XtVaGetValues(children[i],XtNx,&x,XtNy,&y,
		      XtNwidth,&w,XtNheight,&h,NULL);
	if (!XtIsManaged(children[i]))
	    continue;
	if (XtIsSubclass(children[i],xmGadgetClass))
	    continue;
	if (rx < wx+x || rx > wx+x+w)
	    continue;
	if (ry < wy+y || ry > wy+y+h)
	    continue;
	found = children[i];
	break;
    }
    if (found) {
	fprintf(stderr,"  more: %s\n",XtName(found));
	return find_window(found,wx+x,wy+y,rx,ry);
    }
    fprintf(stderr,"  done: %s\n",XtName(widget));
    return widget;
}

static int
check_window(Widget widget)
{
    Atom type;
    int format,rc;
    unsigned long nitems,rest;
    unsigned long *ldata;

    rc = XGetWindowProperty(XtDisplay(widget),XtWindow(widget),
			    XdndAware,0,64,False,AnyPropertyType,
			    &type,&format,&nitems,&rest,
			    (XtPointer)&ldata);
    XFree(ldata);
    return rc == Success && nitems > 0;
}

static XtEnum get_operation(Atom action)
{
    if (XdndActionCopy == action)
	return XmCOPY;
    if (XdndActionLink == action)
	return XmLINK;
    if (XdndActionMove == action)
	return XmMOVE;
    return 0;
}

static Atom get_action(XtEnum operation)
{
    if (XmCOPY == operation)
	return XdndActionCopy;
    if (XmLINK == operation)
	return XdndActionLink;
    if (XmMOVE == operation)
	return XdndActionMove;
    return None;
}

static void
XdndEvent(Widget widget, XtPointer clientdata, XEvent *event, Boolean *cont)
{
    switch(event->type) {
    case MapNotify:
	init_window(widget);
	break;
    }
}

/* ---------------------------------------------------------------------- */

/*
 * not very nice this way, but as you can hardly have two drags at the
 * same time with one pointer only it should be fine ...
 */
static Widget target;
static int target_ok,drop_ok;
static Window source;
static XtEnum operation;

void
XdndAction(Widget widget, XEvent *event,
	   String *params, Cardinal *num_params)
{
    char *name;
    XEvent reply;
        
    if (NULL == event)
	return;
    if (ClientMessage != event->type)
	return;

    if (XdndEnter == event->xclient.message_type) {
	if (xdnd_debug)
	    fprintf(stderr,"Xdnd: Enter: win=0x%lx ver=%ld more=%s\n",
		    event->xclient.data.l[0],
		    event->xclient.data.l[1] >> 24,
		    (event->xclient.data.l[1] & 1) ? "yes" : "no");
    }

    if (XdndPosition == event->xclient.message_type) {
	source = event->xclient.data.l[0];
	target = find_window(widget,0,0,
			     event->xclient.data.l[2] >> 16,
			     event->xclient.data.l[2] & 0xffff);
	target_ok = check_window(target);
	if (target_ok) {
	    operation = get_operation(event->xclient.data.l[4]);
	    operation = XmCOPY;  /* FIXME */
	    drop_ok   = 1;
	} else {
	    operation = 0;
	    drop_ok   = 0;
	}
	if (xdnd_debug) {
	    name = NULL;
	    if (event->xclient.data.l[4])
		name=XGetAtomName(XtDisplay(widget),event->xclient.data.l[4]);
	    fprintf(stderr,"Xdnd: Position: win=0x%lx pos=+%ld+%ld ts=%ld "
		    "ac=%s op=%d widget=%s drop=%s\n",
		    event->xclient.data.l[0],
		    event->xclient.data.l[2] >> 16,
		    event->xclient.data.l[2] & 0xffff,
		    event->xclient.data.l[3],
		    name,operation,
		    XtName(target),target_ok ? "yes" : "no");
	    if (name)
		XFree(name);
	}
	memset(&reply,0,sizeof(reply));
	reply.xany.type = ClientMessage;
	reply.xany.display = XtDisplay(widget);
	reply.xclient.window = event->xclient.data.l[0];
	reply.xclient.message_type = XdndStatus;
	reply.xclient.format = 32;
	reply.xclient.data.l[0] = XtWindow(widget);
	reply.xclient.data.l[1] = drop_ok ? 1 : 0;
	reply.xclient.data.l[4] = get_action(operation);
	XSendEvent(XtDisplay(widget),reply.xclient.window,0,0,&reply);
    }

    if (XdndDrop == event->xclient.message_type) {
	source = event->xclient.data.l[0];
	if (xdnd_debug)
	    fprintf(stderr,"Xdnd: Drop: win=0x%lx ts=%ld\n",
		    event->xclient.data.l[0],
		    event->xclient.data.l[2]);
	XmeNamedSink(target,XdndSelection,XmCOPY,NULL,
		     XtLastTimestampProcessed(XtDisplay(widget)));
    }

    if (XdndLeave == event->xclient.message_type) {
	source = 0;
	if (xdnd_debug)
	    fprintf(stderr,"Xdnd: Leave: win=0x%lx\n",
		    event->xclient.data.l[0]);
    }
}

void XdndDropFinished(Widget widget, XmSelectionCallbackStruct *scs)
{
    XEvent reply;

    if (XdndSelection != scs->selection)
	return;
    if (0 == source)
	return;

    if (xdnd_debug)
	fprintf(stderr,"Xdnd: sending Finished (0x%lx)\n",source);
    memset(&reply,0,sizeof(reply));
    reply.xany.type = ClientMessage;
    reply.xany.display = XtDisplay(widget);
    reply.xclient.window = source;
    reply.xclient.message_type = XdndFinished;
    reply.xclient.format = 32;
    while (!XtIsShell(widget))
	widget = XtParent(widget);
    reply.xclient.data.l[0] = XtWindow(widget);
    XSendEvent(XtDisplay(widget),reply.xclient.window,0,0,&reply);
    source = 0;
}

void XdndDropSink(Widget widget)
{
    XdndInit(XtDisplay(widget));

    if (XtWindow(widget))
	init_window(widget);
    XtAddEventHandler(widget,StructureNotifyMask,True,XdndEvent,NULL);
}