diff --git a/docs/screenshots/desktop/desktop-trash-empty.png b/docs/screenshots/desktop/desktop-trash-empty.png new file mode 100644 index 0000000000..1fbe2d07f8 Binary files /dev/null and b/docs/screenshots/desktop/desktop-trash-empty.png differ diff --git a/docs/screenshots/desktop/desktop-trash-full.png b/docs/screenshots/desktop/desktop-trash-full.png new file mode 100644 index 0000000000..573a7ef3ba Binary files /dev/null and b/docs/screenshots/desktop/desktop-trash-full.png differ diff --git a/ext/src/mcde/programs/dtwm/WmMcdeDesktop.c b/ext/src/mcde/programs/dtwm/WmMcdeDesktop.c index 3865ffa914..adfa996347 100644 --- a/ext/src/mcde/programs/dtwm/WmMcdeDesktop.c +++ b/ext/src/mcde/programs/dtwm/WmMcdeDesktop.c @@ -78,7 +78,8 @@ typedef enum { DESK_DISK, DESK_MEDIA, DESK_NET, - DESK_USER + DESK_USER, + DESK_TRASH } DeskKind; typedef struct { @@ -121,10 +122,20 @@ static int deskEventFd = -1; static int deskScreenW, deskScreenH; static int deskShow = 1; /* Show Desktop Icons */ +/* XDND: a file dragged onto the trash icon */ +static Atom deskXdndAware, deskXdndEnter, deskXdndPosition, deskXdndStatus, + deskXdndDrop, deskXdndFinished, deskXdndSelection, deskXdndActionCopy, + deskXdndActionMove, deskXdndTypeList; +static Window deskXdndSource; +static int deskXdndItem = -1; +static Time deskXdndTime; +static Atom deskXdndProp; + static void DeskTimerCB(XtPointer client, XtIntervalId *id); static void DeskScheduleRebuild(void); static void DeskLayout(void); static void DeskConfSave(void); +static int DeskTrashEmpty(void); /* ------------------------------------------------------------------ */ /* the configuration: ~/.dt/desktop.conf */ @@ -283,6 +294,8 @@ DeskIconName(const DeskItem *it) case DESK_USER: /* fstype carries "d" for a folder and "f" for a file */ return it->fstype[0] == 'd' ? "DtdirB.m.pm" : "McdeFile.m.pm"; + case DESK_TRASH: + return it->fstype[0] == 'f' ? "Dtbinml.m.pm" : "Dtbin.m.pm"; default: return "McdeDisk.m.pm"; } @@ -430,6 +443,18 @@ DeskExec(const char *prog, const char *arg) } } +static void +DeskExec2(const char *prog, const char *arg1, const char *arg2) +{ + pid_t pid; + + if ((pid = fork()) == 0) { + setsid(); + execlp(prog, prog, arg1, arg2, (char *) NULL); + _exit(127); + } +} + static void DeskOpen(int i) { @@ -455,6 +480,8 @@ DeskOpen(int i) } if (it->kind == DESK_COMPUTER) DeskExec("dtfile", "-dir"); + else if (it->kind == DESK_TRASH) + DeskExec2("dtfile", "-dir", it->path); else DeskExec("dtfile", it->path); } @@ -560,6 +587,7 @@ enum { DESK_MENU_SEP2, DESK_MENU_UNMOUNT, DESK_MENU_TRASH, + DESK_MENU_EMPTYTRASH, DESK_MENU_ICON_COUNT }; @@ -580,7 +608,8 @@ static const char *const deskIconMenuText[DESK_MENU_ICON_COUNT] = { "Show on This Workspace Only", NULL, "Unmount", - "Move to Trash" + "Move to Trash", + "Empty Trash" }; static const char *const deskRootMenuText[DESK_MENU_ROOT_COUNT] = { @@ -665,11 +694,147 @@ DeskMenuEnabled(int i, int entry) it->kind == DESK_NET); case DESK_MENU_TRASH: return it->kind == DESK_USER; + case DESK_MENU_EMPTYTRASH: + return it->kind == DESK_TRASH && !DeskTrashEmpty(); default: return 0; } } +static void +DeskXdndInit(void) +{ + deskXdndAware = XInternAtom(deskDpy, "XdndAware", False); + deskXdndEnter = XInternAtom(deskDpy, "XdndEnter", False); + deskXdndPosition = XInternAtom(deskDpy, "XdndPosition", False); + deskXdndStatus = XInternAtom(deskDpy, "XdndStatus", False); + deskXdndDrop = XInternAtom(deskDpy, "XdndDrop", False); + deskXdndFinished = XInternAtom(deskDpy, "XdndFinished", False); + deskXdndSelection = XInternAtom(deskDpy, "XdndSelection", False); + deskXdndActionCopy = XInternAtom(deskDpy, "XdndActionCopy", False); + deskXdndActionMove = XInternAtom(deskDpy, "XdndActionMove", False); + deskXdndTypeList = XInternAtom(deskDpy, "XdndTypeList", False); + deskXdndProp = XInternAtom(deskDpy, "MCDE_DESK_DROP", False); +} + +/* the path of a "file://..." URI, into BUF; 1 when it is a local file */ +static int +DeskUriPath(const char *uri, char *buf, size_t len) +{ + const char *p = uri; + size_t o = 0; + + if (strncmp(p, "file://", 7) == 0) { + p += 7; + if (strncmp(p, "localhost/", 10) == 0) + p += 9; + } else if (strncmp(p, "file:", 5) == 0) + p += 5; + else + return 0; + while (*p != '\0' && o + 1 < len) { + if (*p == '%' && isxdigit((unsigned char) p[1]) && + isxdigit((unsigned char) p[2])) { + char hex[3] = { p[1], p[2], 0 }; + buf[o++] = (char) strtol(hex, NULL, 16); + p += 3; + } else + buf[o++] = *p++; + } + buf[o] = '\0'; + return o > 0; +} + +/* move every local file of the dropped URI list into the trash */ +static void +DeskDropToTrash(const char *data, size_t len) +{ + const char *p = data, *end = data + len; + const char *home = getenv("HOME"); + char trash[512]; + + if (home == NULL) + return; + snprintf(trash, sizeof trash, "%s/.dt/Trash", home); + (void) mkdir(trash, 0700); + while (p < end) { + const char *nl = memchr(p, '\n', (size_t) (end - p)); + char uri[1024], path[1024], dest[1200], base[300]; + const char *dot; + size_t n; + int k; + + n = nl != NULL ? (size_t) (nl - p) : (size_t) (end - p); + if (n >= sizeof uri) + n = sizeof uri - 1; + memcpy(uri, p, n); + uri[n] = '\0'; + p = nl != NULL ? nl + 1 : end; + if (uri[0] == '\0') + continue; + if (getenv("MCDE_DESK_DEBUG") != NULL) + fprintf(stderr, "desktop: drop uri '%s'\n", uri); + if (!DeskUriPath(uri, path, sizeof path)) + continue; + if (getenv("MCDE_DESK_DEBUG") != NULL) + fprintf(stderr, "desktop: drop path '%s'\n", path); + { + const char *slash = strrchr(path, '/'); + snprintf(base, sizeof base, "%s", + slash != NULL ? slash + 1 : path); + } + for (k = 0; k < 1000; k++) { + if (k == 0) + snprintf(dest, sizeof dest, "%s/%s", trash, + base); + else { + dot = strrchr(base, '.'); + if (dot != NULL && dot != base) + snprintf(dest, sizeof dest, + "%s/%.*s.%d%s", trash, + (int) (dot - base), base, k, dot); + else + snprintf(dest, sizeof dest, "%s/%s.%d", + trash, base, k); + } + { + struct stat st; + + if (stat(dest, &st) != 0) + break; + } + } + if (rename(path, dest) != 0) { + /* a file from another file system: copy and remove; + * a folder over a file system boundary is left */ + if (errno == EXDEV) { + FILE *in = fopen(path, "rb"); + FILE *out = in != NULL ? + fopen(dest, "wb") : NULL; + char buf[65536]; + size_t n; + + if (in != NULL && out != NULL) { + while ((n = fread(buf, 1, sizeof buf, + in)) > 0) + (void) fwrite(buf, 1, n, out); + fclose(in); + fclose(out); + (void) unlink(path); + } else { + if (in != NULL) + fclose(in); + if (out != NULL) + fclose(out); + } + } + if (getenv("MCDE_DESK_DEBUG") != NULL) + fprintf(stderr, "desktop: drop %s -> %s: %s\n", + path, dest, strerror(errno)); + } + } +} + static void DeskMenuDraw(void) { @@ -810,6 +975,61 @@ DeskBackdrop(void) } } +/* remove PATH and, if it is a folder, everything below it */ +static int +DeskRemoveTree(const char *path) +{ + struct stat st; + DIR *d; + struct dirent *de; + int rc = 0; + + if (lstat(path, &st) != 0) + return -1; + if (!S_ISDIR(st.st_mode)) + return unlink(path); + if ((d = opendir(path)) == NULL) + return -1; + while ((de = readdir(d)) != NULL) { + char child[1024]; + + if (strcmp(de->d_name, ".") == 0 || + strcmp(de->d_name, "..") == 0) + continue; + snprintf(child, sizeof child, "%s/%s", path, de->d_name); + if (DeskRemoveTree(child) != 0) + rc = -1; + } + closedir(d); + if (rmdir(path) != 0) + rc = -1; + return rc; +} + +static void +DeskEmptyTrash(void) +{ + const char *home = getenv("HOME"); + char dir[512], path[1024]; + DIR *d; + struct dirent *de; + + if (home == NULL) + return; + snprintf(dir, sizeof dir, "%s/.dt/Trash", home); + if ((d = opendir(dir)) == NULL) + return; + while ((de = readdir(d)) != NULL) { + if (strcmp(de->d_name, ".") == 0 || + strcmp(de->d_name, "..") == 0) + continue; + snprintf(path, sizeof path, "%s/%s", dir, de->d_name); + (void) DeskRemoveTree(path); + } + closedir(d); + DeskScheduleRebuild(); +} + static void DeskMenuAction(int i, int entry) { @@ -865,6 +1085,9 @@ DeskMenuAction(int i, int entry) case DESK_MENU_TRASH: DeskTrash(i); break; + case DESK_MENU_EMPTYTRASH: + DeskEmptyTrash(); + break; } } @@ -968,6 +1191,84 @@ McdeDesktopEvent(XEvent *event) if (event->xexpose.count == 0) DeskDraw(i); return True; + case ClientMessage: + if (event->xclient.message_type == deskXdndEnter) { + deskXdndSource = (Window) event->xclient.data.l[0]; + deskXdndItem = i; + return True; + } + if (event->xclient.message_type == deskXdndPosition) { + XEvent reply; + + memset(&reply, 0, sizeof reply); + reply.xclient.type = ClientMessage; + reply.xclient.display = deskDpy; + reply.xclient.window = deskXdndSource; + reply.xclient.message_type = deskXdndStatus; + reply.xclient.format = 32; + reply.xclient.data.l[0] = + deskItem[i].kind == DESK_TRASH ? (long) i : 0; + reply.xclient.data.l[1] = + deskItem[i].kind == DESK_TRASH ? 1 : 0; + reply.xclient.data.l[4] = + (long) (deskItem[i].kind == DESK_TRASH ? + deskXdndActionMove : None); + XSendEvent(deskDpy, deskXdndSource, False, NoEventMask, + &reply); + XFlush(deskDpy); + return True; + } + if (event->xclient.message_type == deskXdndDrop) { + if (deskItem[i].kind == DESK_TRASH) { + deskXdndTime = (Time) event->xclient.data.l[2]; + XConvertSelection(deskDpy, deskXdndSelection, + XA_STRING, deskXdndProp, event->xany.window, + deskXdndTime); + } + return True; + } + if (event->xclient.message_type == deskXdndFinished) { + deskXdndSource = None; + deskXdndItem = -1; + return True; + } + return True; + case SelectionNotify: + if (getenv("MCDE_DESK_DEBUG") != NULL) + fprintf(stderr, "desktop: selection notify prop=%lu\n", + (unsigned long) event->xselection.property); + if (event->xselection.property != None) { + unsigned char *data = NULL; + Atom type; + int format; + unsigned long n, left; + + if (XGetWindowProperty(deskDpy, event->xany.window, + deskXdndProp, 0, 65536, False, XA_STRING, &type, + &format, &n, &left, &data) == Success && + data != NULL) { + DeskDropToTrash((char *) data, n); + XFree(data); + } + XDeleteProperty(deskDpy, event->xany.window, + deskXdndProp); + if (deskXdndSource != None) { + XEvent reply; + + memset(&reply, 0, sizeof reply); + reply.xclient.type = ClientMessage; + reply.xclient.display = deskDpy; + reply.xclient.window = deskXdndSource; + reply.xclient.message_type = deskXdndFinished; + reply.xclient.format = 32; + reply.xclient.data.l[0] = 1; /* success */ + XSendEvent(deskDpy, deskXdndSource, False, + NoEventMask, &reply); + XFlush(deskDpy); + } + DeskScheduleRebuild(); + } + return True; case ButtonPress: if (event->xbutton.button == Button3) { deskSel = i; @@ -1063,6 +1364,44 @@ DeskAdd(const char *key, const char *name, const char *path, return 1; } +/* 1 when ~/.dt/Trash holds nothing (or is not there) */ +static int +DeskTrashEmpty(void) +{ + const char *home = getenv("HOME"); + char dir[512]; + DIR *d; + struct dirent *de; + int empty = 1; + + if (home == NULL) + return 1; + snprintf(dir, sizeof dir, "%s/.dt/Trash", home); + if ((d = opendir(dir)) == NULL) + return 1; + while ((de = readdir(d)) != NULL) { + if (de->d_name[0] != '.') { + empty = 0; + break; + } + } + closedir(d); + return empty; +} + +static void +DeskAddTrash(void) +{ + const char *home = getenv("HOME"); + char path[512]; + + if (home == NULL) + return; + snprintf(path, sizeof path, "%s/.dt/Trash", home); + DeskAdd("trash:", "Trash", path, "", DeskTrashEmpty() ? "e" : "f", + DESK_TRASH, 0, 0, 1, 0); +} + static const char * DeskBase(const char *path) { @@ -1379,12 +1718,18 @@ DeskWindow(int i) a.background_pixel = deskBg; a.border_pixel = deskFg; a.event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask | - Button1MotionMask | StructureNotifyMask; + Button1MotionMask | StructureNotifyMask | PropertyChangeMask; it->win = XCreateWindow(deskDpy, deskRoot, it->x, it->y, DESK_CELL_W, DESK_CELL_H, 0, CopyFromParent, InputOutput, CopyFromParent, CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask, &a); + if (deskXdndAware != None) { + long version = 5; + + XChangeProperty(deskDpy, it->win, deskXdndAware, XA_ATOM, 32, + PropModeReplace, (unsigned char *) &version, 1); + } DeskDraw(i); } @@ -1447,6 +1792,7 @@ McdeDesktopRebuild(void) DeskAddMounts(); DeskAddDisks(); DeskAddUser(); + DeskAddTrash(); DeskConfLoad(); qsort(deskItem, (size_t) deskCount, sizeof deskItem[0], DeskCompare); DeskLayout(); @@ -1514,6 +1860,7 @@ McdeDesktopInit(void) gc.background = deskBg; deskGC = XCreateGC(deskDpy, deskRoot, GCFont | GCForeground | GCBackground, &gc); + DeskXdndInit(); McdeDesktopRebuild(); /* the kernel's device events: a medium comes or goes */ deskEventFd = mcde_sys_disk_events(); diff --git a/tools/mcde-xdnd-drop.c b/tools/mcde-xdnd-drop.c new file mode 100644 index 0000000000..2ef11914aa --- /dev/null +++ b/tools/mcde-xdnd-drop.c @@ -0,0 +1,200 @@ +/* + * 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 +#include +#include +#include +#include +#include + +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; +}