diff --git a/programs/dtwm/WmFunction.h b/programs/dtwm/WmFunction.h index b73c57c2..5cc3e0eb 100644 --- a/programs/dtwm/WmFunction.h +++ b/programs/dtwm/WmFunction.h @@ -90,6 +90,7 @@ extern Boolean F_McdeSpaces (String args, ClientData *pCD, XEvent *event); extern Boolean McdeSpacesEvent (XEvent *event); extern Boolean McdeDesktopEvent (XEvent *event); extern Boolean McdeDesktopMenuEvent (XEvent *event); +extern Boolean McdeDesktopRootEvent (XEvent *event); extern void McdeDesktopInit (void); extern void McdeDesktopRebuild (void); extern void McdeDesktopWorkspaceChanged (WmWorkspaceData *pWS); diff --git a/programs/dtwm/WmMain.c b/programs/dtwm/WmMain.c index 98170132..45a8fc4a 100644 --- a/programs/dtwm/WmMain.c +++ b/programs/dtwm/WmMain.c @@ -184,6 +184,9 @@ main (int argc, char *argv [], char *environ []) /* MCDE: the keyboard and pointer of the workspaces overview (F8) */ if (McdeSpacesEvent (&event)) continue; + /* MCDE: a right click on the empty desktop (its own menu) */ + if (McdeDesktopRootEvent (&event)) + continue; /* MCDE: a click on the desktop icon menu */ if (McdeDesktopMenuEvent (&event)) continue; diff --git a/programs/dtwm/WmMcdeDesktop.c b/programs/dtwm/WmMcdeDesktop.c index eba771b9..3865ffa9 100644 --- a/programs/dtwm/WmMcdeDesktop.c +++ b/programs/dtwm/WmMcdeDesktop.c @@ -96,6 +96,7 @@ typedef struct { int workspace; /* 0 = all, else 1 + the workspace's index */ int x, y; /* the icon window's place */ int hasPos; + int moved; /* the person dragged it: Clean Up leaves it */ Window win; Pixmap glyph, mask; unsigned int gw, gh; @@ -118,9 +119,12 @@ static int deskTimer; static int deskDirty; static int deskEventFd = -1; static int deskScreenW, deskScreenH; +static int deskShow = 1; /* Show Desktop Icons */ static void DeskTimerCB(XtPointer client, XtIntervalId *id); static void DeskScheduleRebuild(void); +static void DeskLayout(void); +static void DeskConfSave(void); /* ------------------------------------------------------------------ */ /* the configuration: ~/.dt/desktop.conf */ @@ -179,6 +183,7 @@ DeskConfLoad(void) char path[512], line[1024]; FILE *f; DeskItem *cur = NULL; + int sectionDesktop = 0; DeskConfPath(path, sizeof path); if ((f = fopen(path, "r")) == NULL) @@ -192,7 +197,13 @@ DeskConfLoad(void) if (line[0] == '\0' || line[0] == '#') continue; if (DeskKey(line, key, sizeof key)) { - cur = DeskFind(key); + sectionDesktop = strcmp(key, "desktop") == 0; + cur = sectionDesktop ? NULL : DeskFind(key); + continue; + } + if (sectionDesktop) { + if (strncmp(line, "show=", 5) == 0) + deskShow = atoi(line + 5) != 0; continue; } if (cur == NULL) @@ -205,6 +216,8 @@ DeskConfLoad(void) cur->hasPos = 1; } else if (strncmp(line, "workspace=", 10) == 0) cur->workspace = atoi(line + 10); + else if (strncmp(line, "moved=", 6) == 0) + cur->moved = atoi(line + 6); } fclose(f); } @@ -227,6 +240,7 @@ DeskConfSave(void) } fprintf(f, "# MCDE desktop icons: key, place and workspace\n"); fprintf(f, "# (WmMcdeDesktop.c; the positions are kept for good)\n"); + fprintf(f, "\n[desktop]\nshow=%d\n", deskShow); for (i = 0; i < deskCount; i++) { /* a ']' in a key is escaped, a '\' too */ for (o = 0; deskItem[i].key[o] != '\0' && @@ -241,6 +255,8 @@ DeskConfSave(void) fprintf(f, "\n[%s]\n", key); fprintf(f, "x=%d\ny=%d\nworkspace=%d\n", deskItem[i].x, deskItem[i].y, deskItem[i].workspace); + if (deskItem[i].moved) + fprintf(f, "moved=1\n"); } if (fclose(f) != 0 || rename(tmp, path) != 0) { if (getenv("MCDE_DESK_DEBUG") != NULL) @@ -374,6 +390,10 @@ DeskMap(int i) if (it->win == None) return; + if (!deskShow) { + XUnmapWindow(deskDpy, it->win); + return; + } if (it->workspace == 0 || it->workspace == active + 1) { XMoveWindow(deskDpy, it->win, it->x, it->y); XMapRaised(deskDpy, it->win); @@ -520,11 +540,17 @@ DeskTrash(int i) } /* ------------------------------------------------------------------ */ -/* the context menu: an override-redirect window of its own, so that the */ -/* click reaches it and not the icon or the root (a Motif popup on an */ -/* override-redirect icon did not take the click, measured 2026-11-15) */ +/* the context menus: an override-redirect window of its own, so that */ +/* the click reaches it and not the icon or the root (a Motif popup on */ +/* an override-redirect icon did not take the click, measured */ +/* 2026-11-15). One menu for an icon, one for the empty desktop. */ /* ------------------------------------------------------------------ */ +enum { + DESK_MENU_ICON, + DESK_MENU_ROOT +}; + enum { DESK_MENU_OPEN, DESK_MENU_HOME, @@ -534,10 +560,19 @@ enum { DESK_MENU_SEP2, DESK_MENU_UNMOUNT, DESK_MENU_TRASH, - DESK_MENU_COUNT + DESK_MENU_ICON_COUNT }; -static const char *const deskMenuText[DESK_MENU_COUNT] = { +enum { + DESK_MENU_NEWFOLDER, + DESK_MENU_CLEANUP, + DESK_MENU_BACKDROP, + DESK_MENU_SEP3, + DESK_MENU_SHOW, + DESK_MENU_ROOT_COUNT +}; + +static const char *const deskIconMenuText[DESK_MENU_ICON_COUNT] = { "Open", "Open Home", NULL, @@ -548,22 +583,49 @@ static const char *const deskMenuText[DESK_MENU_COUNT] = { "Move to Trash" }; +static const char *const deskRootMenuText[DESK_MENU_ROOT_COUNT] = { + "New Folder", + "Clean Up Icons", + "Change Backdrop...", + NULL, + "Show Desktop Icons" +}; + #define DESK_MENU_W 230 #define DESK_MENU_ROW 20 #define DESK_MENU_SEP 7 #define DESK_MENU_PAD 4 +static Widget deskPopup; static Window deskMenuWin; static int deskMenuFor = -1; static int deskMenuHover = -1; +static int deskMenuKind = DESK_MENU_ICON; + +static int +DeskMenuCount(void) +{ + return deskMenuKind == DESK_MENU_ROOT ? DESK_MENU_ROOT_COUNT : + DESK_MENU_ICON_COUNT; +} + +static const char * +DeskMenuTextAt(int i) +{ + if (deskMenuKind == DESK_MENU_ROOT) + return i >= 0 && i < DESK_MENU_ROOT_COUNT ? + deskRootMenuText[i] : NULL; + return i >= 0 && i < DESK_MENU_ICON_COUNT ? deskIconMenuText[i] : + NULL; +} static int DeskMenuHeight(void) { int h = DESK_MENU_PAD * 2, i; - for (i = 0; i < DESK_MENU_COUNT; i++) - h += deskMenuText[i] == NULL ? DESK_MENU_SEP : DESK_MENU_ROW; + for (i = 0; i < DeskMenuCount(); i++) + h += DeskMenuTextAt(i) == NULL ? DESK_MENU_SEP : DESK_MENU_ROW; return h; } @@ -573,16 +635,19 @@ DeskMenuTop(int i) int y = DESK_MENU_PAD, k; for (k = 0; k < i; k++) - y += deskMenuText[k] == NULL ? DESK_MENU_SEP : DESK_MENU_ROW; + y += DeskMenuTextAt(k) == NULL ? DESK_MENU_SEP : DESK_MENU_ROW; return y; } -/* is this entry usable for item I? */ +/* is this entry usable? For the icon menu for item I; for the root menu + * always. */ static int DeskMenuEnabled(int i, int entry) { DeskItem *it = i >= 0 && i < deskCount ? &deskItem[i] : NULL; + if (deskMenuKind == DESK_MENU_ROOT) + return 1; if (it == NULL) return 0; switch (entry) { @@ -613,10 +678,11 @@ DeskMenuDraw(void) XSetForeground(deskDpy, deskGC, deskBg); XFillRectangle(deskDpy, deskMenuWin, deskGC, 0, 0, DESK_MENU_W, (unsigned) DeskMenuHeight()); - for (i = 0; i < DESK_MENU_COUNT; i++) { + for (i = 0; i < DeskMenuCount(); i++) { + const char *text = DeskMenuTextAt(i); int y = DeskMenuTop(i); - if (deskMenuText[i] == NULL) { + if (text == NULL) { XSetForeground(deskDpy, deskGC, deskMid); XDrawLine(deskDpy, deskMenuWin, deskGC, 6, y + 3, DESK_MENU_W - 7, y + 3); @@ -630,8 +696,12 @@ DeskMenuDraw(void) DESK_MENU_W - 4, DESK_MENU_ROW); XSetForeground(deskDpy, deskGC, DeskMenuEnabled(deskMenuFor, i) ? deskFg : deskMid); - XDrawString(deskDpy, deskMenuWin, deskGC, 12, y + 14, - deskMenuText[i], (int) strlen(deskMenuText[i])); + if (deskMenuKind == DESK_MENU_ROOT && i == DESK_MENU_SHOW) + XDrawString(deskDpy, deskMenuWin, deskGC, 6, y + 14, + deskShow ? "[x]" : "[ ]", 3); + XDrawString(deskDpy, deskMenuWin, deskGC, + deskMenuKind == DESK_MENU_ROOT && i == DESK_MENU_SHOW ? + 30 : 12, y + 14, text, (int) strlen(text)); } XSetForeground(deskDpy, deskGC, deskFg); XDrawRectangle(deskDpy, deskMenuWin, deskGC, 0, 0, DESK_MENU_W - 1, @@ -651,14 +721,17 @@ DeskMenuClose(void) } static void -DeskMenuPost(int i, XEvent *event) +DeskMenuPost(int i, int kind, XEvent *event) { XSetWindowAttributes a; int x = event->xbutton.x_root; int y = event->xbutton.y_root; - int h = DeskMenuHeight(); + int h; DeskMenuClose(); + deskMenuKind = kind; + deskMenuFor = i; + h = DeskMenuHeight(); if (x + DESK_MENU_W > deskScreenW) x = deskScreenW - DESK_MENU_W; if (y + h > deskScreenH) @@ -673,20 +746,98 @@ DeskMenuPost(int i, XEvent *event) (unsigned) h, 0, CopyFromParent, InputOutput, CopyFromParent, CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask, &a); - deskMenuFor = i; deskMenuHover = -1; DeskMenuDraw(); XMapRaised(deskDpy, deskMenuWin); XFlush(deskDpy); } +/* the next free "New Folder" in DIR; into BUF */ +static void +DeskNewFolderName(const char *dir, char *buf, size_t len) +{ + struct stat st; + int n; + + for (n = 0; n < 1000; n++) { + if (n == 0) + snprintf(buf, len, "%s/New Folder", dir); + else + snprintf(buf, len, "%s/New Folder %d", dir, n); + if (stat(buf, &st) != 0) + return; + } +} + +static void +DeskNewFolder(void) +{ + const char *home = getenv("HOME"); + char dir[512], path[600]; + + if (home == NULL) + return; + snprintf(dir, sizeof dir, "%s/Desktop", home); + (void) mkdir(dir, 0755); + DeskNewFolderName(dir, path, sizeof path); + if (mkdir(path, 0755) == 0) + DeskScheduleRebuild(); +} + +static void +DeskCleanUp(void) +{ + int i; + + for (i = 0; i < deskCount; i++) + if (!deskItem[i].moved) + deskItem[i].hasPos = 0; + DeskLayout(); + DeskConfSave(); + DeskScheduleRebuild(); +} + +static void +DeskBackdrop(void) +{ + pid_t pid; + + if ((pid = fork()) == 0) { + setsid(); + execlp("dtsystemsettings", "dtsystemsettings", "-pane", + "screensaver", (char *) NULL); + _exit(127); + } +} + static void DeskMenuAction(int i, int entry) { - DeskItem *it = &deskItem[i]; + DeskItem *it = i >= 0 && i < deskCount ? &deskItem[i] : NULL; if (!DeskMenuEnabled(i, entry)) return; + if (deskMenuKind == DESK_MENU_ROOT) { + switch (entry) { + case DESK_MENU_NEWFOLDER: + DeskNewFolder(); + break; + case DESK_MENU_CLEANUP: + DeskCleanUp(); + break; + case DESK_MENU_BACKDROP: + DeskBackdrop(); + break; + case DESK_MENU_SHOW: + deskShow = !deskShow; + DeskConfSave(); + McdeDesktopWorkspaceChanged(NULL); + break; + } + return; + } + if (it == NULL) + return; switch (entry) { case DESK_MENU_OPEN: DeskOpen(i); @@ -732,13 +883,13 @@ McdeDesktopMenuEvent(XEvent *event) return True; case MotionNotify: y = event->xmotion.y; - for (i = 0; i < DESK_MENU_COUNT; i++) { + for (i = 0; i < DeskMenuCount(); i++) { + const char *text = DeskMenuTextAt(i); int top = DeskMenuTop(i); - int h = deskMenuText[i] == NULL ? DESK_MENU_SEP : - DESK_MENU_ROW; + int h = text == NULL ? DESK_MENU_SEP : DESK_MENU_ROW; if (y >= top && y < top + h) { - entry = deskMenuText[i] == NULL ? -1 : i; + entry = text == NULL ? -1 : i; break; } } @@ -755,13 +906,13 @@ McdeDesktopMenuEvent(XEvent *event) return True; case ButtonPress: y = event->xbutton.y; - for (i = 0; i < DESK_MENU_COUNT; i++) { + for (i = 0; i < DeskMenuCount(); i++) { + const char *text = DeskMenuTextAt(i); int top = DeskMenuTop(i); - int h = deskMenuText[i] == NULL ? DESK_MENU_SEP : - DESK_MENU_ROW; + int h = text == NULL ? DESK_MENU_SEP : DESK_MENU_ROW; if (y >= top && y < top + h) { - entry = deskMenuText[i] == NULL ? -1 : i; + entry = text == NULL ? -1 : i; break; } } @@ -775,6 +926,18 @@ McdeDesktopMenuEvent(XEvent *event) } } +/* a right click on the empty desktop: the desktop's own menu */ +Boolean +McdeDesktopRootEvent(XEvent *event) +{ + if (event->type != ButtonPress || event->xbutton.button != Button3) + return False; + if (pSD == NULL || event->xany.window != pSD->rootWindow) + return False; + DeskMenuPost(-1, DESK_MENU_ROOT, event); + return True; +} + /* ------------------------------------------------------------------ */ /* events */ /* ------------------------------------------------------------------ */ @@ -809,7 +972,7 @@ McdeDesktopEvent(XEvent *event) if (event->xbutton.button == Button3) { deskSel = i; DeskDrawAll(); - DeskMenuPost(i, event); + DeskMenuPost(i, DESK_MENU_ICON, event); return True; } if (event->xbutton.button != Button1) @@ -857,6 +1020,7 @@ McdeDesktopEvent(XEvent *event) return True; if (deskDragged) { deskItem[deskDrag].hasPos = 1; + deskItem[deskDrag].moved = 1; DeskConfSave(); } deskDrag = -1;