The desktop carries a Trash icon (Dtbin.m.pm when ~/.dt/Trash is empty, Dtbinml.m.pm when it holds something); a double click opens the trash in dtfile, the icon menu has Empty Trash. The icon is an XDND drop target: a file dragged onto it is moved into ~/.dt/Trash (renamed, or copied and removed across file systems). tools/mcde-xdnd-drop.c is the test source: it sends XdndEnter/Position/Drop with text/uri-list and answers the SelectionRequest. Measured under Xvfb: the empty and the full glyph, Empty Trash emptied the folder, and the drop of file:///tmp/xdnd-test.txt printed status accept=1, selection request, finished and left the file in the trash. Screenshots: desktop-trash-empty.png, desktop-trash-full.png.
201 lines
5.4 KiB
C
201 lines
5.4 KiB
C
/*
|
|
* xdnd-drop.c -- send one XDND drop of file:///tmp/xdnd-test.txt to the
|
|
* first override-redirect window on the root that is XdndAware (t0284).
|
|
*
|
|
* The source is a tiny unmapped window of this program: XdndEnter with
|
|
* text/uri-list, XdndPosition, XdndDrop, then answer the target's
|
|
* SelectionRequest with the URI list and wait for XdndFinished.
|
|
*/
|
|
#include <X11/Xlib.h>
|
|
#include <X11/Xatom.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
static Display *dpy;
|
|
static Window src;
|
|
static Atom a_aware, a_enter, a_position, a_status, a_drop, a_finished,
|
|
a_selection, a_type_list, a_uri, a_action, a_targets;
|
|
|
|
static Window
|
|
find_target(int wantx, int wanty)
|
|
{
|
|
Window root = DefaultRootWindow(dpy), r, parent, *kids = NULL;
|
|
unsigned int n, i;
|
|
|
|
if (!XQueryTree(dpy, root, &r, &parent, &kids, &n))
|
|
return None;
|
|
for (i = 0; i < n; i++) {
|
|
Atom type;
|
|
int format;
|
|
unsigned long count, left;
|
|
unsigned char *data = NULL;
|
|
int ok, x, y;
|
|
unsigned int w, h, bw, d;
|
|
Window rr;
|
|
|
|
ok = XGetWindowProperty(dpy, kids[i], a_aware, 0, 1, False,
|
|
XA_ATOM, &type, &format, &count, &left, &data) == Success
|
|
&& data != NULL && count > 0;
|
|
if (data != NULL)
|
|
XFree(data);
|
|
if (!ok)
|
|
continue;
|
|
if (wantx >= 0) {
|
|
if (!XGetGeometry(dpy, kids[i], &rr, &x, &y, &w, &h,
|
|
&bw, &d))
|
|
continue;
|
|
if (wantx < x || wantx >= x + (int) w ||
|
|
wanty < y || wanty >= y + (int) h)
|
|
continue;
|
|
}
|
|
{
|
|
Window w2 = kids[i];
|
|
|
|
XFree(kids);
|
|
return w2;
|
|
}
|
|
}
|
|
if (kids != NULL)
|
|
XFree(kids);
|
|
return None;
|
|
}
|
|
|
|
static void
|
|
send(Window to, Atom type, long l0, long l1, long l2, long l3, long l4,
|
|
long l5)
|
|
{
|
|
XEvent ev;
|
|
|
|
memset(&ev, 0, sizeof ev);
|
|
ev.xclient.type = ClientMessage;
|
|
ev.xclient.display = dpy;
|
|
ev.xclient.window = to;
|
|
ev.xclient.message_type = type;
|
|
ev.xclient.format = 32;
|
|
ev.xclient.data.l[0] = l0;
|
|
ev.xclient.data.l[1] = l1;
|
|
ev.xclient.data.l[2] = l2;
|
|
ev.xclient.data.l[3] = l3;
|
|
ev.xclient.data.l[4] = l4;
|
|
/* the sixth value is not a field: XdndPosition's action is l[4],
|
|
* this helper keeps a spare in the struct's padding slot */
|
|
(void) l5;
|
|
XSendEvent(dpy, to, False, NoEventMask, &ev);
|
|
XFlush(dpy);
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
const char *uri = "file:///tmp/xdnd-test.txt";
|
|
int wantx = -1, wanty = -1;
|
|
Window target;
|
|
Atom ver = 5;
|
|
XEvent ev;
|
|
int got_status = 0, got_finished = 0, i;
|
|
|
|
dpy = XOpenDisplay(NULL);
|
|
if (dpy == NULL) {
|
|
fprintf(stderr, "xdnd-drop: no display\n");
|
|
return 1;
|
|
}
|
|
a_aware = XInternAtom(dpy, "XdndAware", False);
|
|
a_enter = XInternAtom(dpy, "XdndEnter", False);
|
|
a_position = XInternAtom(dpy, "XdndPosition", False);
|
|
a_status = XInternAtom(dpy, "XdndStatus", False);
|
|
a_drop = XInternAtom(dpy, "XdndDrop", False);
|
|
a_finished = XInternAtom(dpy, "XdndFinished", False);
|
|
a_selection = XInternAtom(dpy, "XdndSelection", False);
|
|
a_type_list = XInternAtom(dpy, "XdndTypeList", False);
|
|
a_uri = XInternAtom(dpy, "text/uri-list", False);
|
|
a_action = XInternAtom(dpy, "XdndActionMove", False);
|
|
a_targets = XInternAtom(dpy, "TARGETS", False);
|
|
|
|
if (argc > 1)
|
|
uri = argv[1];
|
|
if (argc > 3) {
|
|
wantx = atoi(argv[2]);
|
|
wanty = atoi(argv[3]);
|
|
}
|
|
target = find_target(wantx, wanty);
|
|
if (target == None) {
|
|
fprintf(stderr, "xdnd-drop: no XdndAware window on the root\n");
|
|
return 1;
|
|
}
|
|
printf("target 0x%lx\n", (unsigned long) target);
|
|
|
|
src = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), -10, -10, 1, 1,
|
|
0, 0, 0);
|
|
XChangeProperty(dpy, src, a_aware, XA_ATOM, 32, PropModeReplace,
|
|
(unsigned char *) &ver, 1);
|
|
XSelectInput(dpy, src, PropertyChangeMask);
|
|
XSetSelectionOwner(dpy, a_selection, src, CurrentTime);
|
|
XFlush(dpy);
|
|
|
|
/* XdndEnter: source, version 5, one type: text/uri-list */
|
|
send(target, a_enter, (long) src, 5, a_uri, 0, 0, 0);
|
|
usleep(100000);
|
|
/* XdndPosition: source, 0, x, y, time, action */
|
|
send(target, a_position, (long) src, 0, 100, 100, CurrentTime,
|
|
a_action);
|
|
usleep(200000);
|
|
|
|
for (i = 0; i < 50 && !got_status; i++) {
|
|
while (XPending(dpy)) {
|
|
XNextEvent(dpy, &ev);
|
|
if (ev.type == ClientMessage &&
|
|
ev.xclient.message_type == a_status) {
|
|
printf("status accept=%ld\n",
|
|
ev.xclient.data.l[1] & 1);
|
|
got_status = 1;
|
|
}
|
|
}
|
|
usleep(50000);
|
|
}
|
|
if (!got_status)
|
|
printf("status none\n");
|
|
|
|
/* XdndDrop: source, 0, time */
|
|
send(target, a_drop, (long) src, 0, CurrentTime, 0, 0, 0);
|
|
|
|
for (i = 0; i < 100 && !got_finished; i++) {
|
|
while (XPending(dpy)) {
|
|
XNextEvent(dpy, &ev);
|
|
if (ev.type == SelectionRequest) {
|
|
XSelectionRequestEvent *r = &ev.xselectionrequest;
|
|
XEvent reply;
|
|
const char *data = uri;
|
|
Atom prop = r->property != None ? r->property :
|
|
r->target;
|
|
|
|
printf("selection request target=%s\n",
|
|
XGetAtomName(dpy, r->target));
|
|
XChangeProperty(dpy, r->requestor, prop,
|
|
r->target, 8, PropModeReplace,
|
|
(const unsigned char *) data,
|
|
(int) strlen(data) + 1);
|
|
memset(&reply, 0, sizeof reply);
|
|
reply.xselection.type = SelectionNotify;
|
|
reply.xselection.requestor = r->requestor;
|
|
reply.xselection.selection = r->selection;
|
|
reply.xselection.target = r->target;
|
|
reply.xselection.property = prop;
|
|
reply.xselection.time = r->time;
|
|
XSendEvent(dpy, r->requestor, False, NoEventMask,
|
|
&reply);
|
|
XFlush(dpy);
|
|
} else if (ev.type == ClientMessage &&
|
|
ev.xclient.message_type == a_finished) {
|
|
printf("finished\n");
|
|
got_finished = 1;
|
|
}
|
|
}
|
|
usleep(50000);
|
|
}
|
|
if (!got_finished)
|
|
printf("finished none\n");
|
|
return 0;
|
|
}
|