From 23787aa4b5fd89b14286f2d3f34adcc6b807d258 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 16 Nov 2026 08:58:03 +0000 Subject: [PATCH] MCDE desktop: the trash icon (t0284) 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. --- programs/dtwm/WmMcdeDesktop.c | 353 +++++++++++++++++++++++++++++++++- 1 file changed, 350 insertions(+), 3 deletions(-) diff --git a/programs/dtwm/WmMcdeDesktop.c b/programs/dtwm/WmMcdeDesktop.c index 3865ffa9..adfa9963 100644 --- a/programs/dtwm/WmMcdeDesktop.c +++ b/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();