Files
lotherk 753c0f836e dtwm: the desktop icons stay above the wallpaper
The wallpaper is a window of its own (WmBackdrop.c), and the icons are
override-redirect windows.  XLowerWindow on an icon put it *below* that
window: the icon was mapped and invisible (measured 2026-11-22 on the
MacBook, the icon windows were IsViewable at 1174,10 and 96x96, and the
screen showed only the wallpaper).  DeskMap lowers the backdrops after the
icon now, so the order is root < wallpaper < icons < clients, as 4Dwm has it.

Measured: make -C programs/dtwm rc=0, 0 errors (the MacBook build of this
file, /tmp/dtwm-build7.log); the screenshot shows Computer, CD/DVD drive and
Trash at 1174,10 / 1174,98 / 1174,186.
2026-11-22 13:51:31 +01:00

2044 lines
51 KiB
C

/*
* WmMcdeDesktop.c -- MCDE: the icons on the desktop
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The desktop shows one icon for each thing a person can open from here, in
* the style of IRIX 4Dwm (4DWm app-defaults: iconPlacement, iconDecoration
* "label image", iconClick false -- a double click opens). The icons stand
* in the top right corner and fill from the top down, then the next column
* to the left.
*
* The order is fixed: the computer itself ("/", the only system volume),
* then the disk mounts, the removable media (a stick, a CD, a DVD -- also
* when it is not mounted), the network mounts (NFS, SMB, AFP, sshfs), and
* last the person's own files and folders from ~/.dt/Desktop and ~/Desktop.
*
* Every icon keeps its place and its workspace in ~/.dt/desktop.conf; a
* stick, a CD or a disk that comes back appears where it was left. An icon
* is on all workspaces unless its menu was told otherwise (right click ->
* "Show on This Workspace Only"). A not mounted medium is mounted by the
* double click before it is opened.
*
* C99, POSIX, Xt, Motif and Xlib (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <X11/Intrinsic.h>
#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <Xm/MessageB.h>
#include <Xm/Xm.h>
#include <Mcde/McdeSys.h>
#include <Mcde/Sound.h>
#include "WmGlobal.h"
#include "WmFunction.h"
/* the screen dtwm works on (WmMain.c); InitWmGlobal keeps its own local
* pSD, so the global one is still NULL when McdeDesktopInit runs */
extern WmScreenData *pSD;
void McdeDesktopRebuild(void);
static WmScreenData *
DeskScreen(void)
{
int s;
if (pSD != NULL)
return pSD;
for (s = 0; s < wmGD.numScreens; s++)
if (wmGD.Screens[s].managed)
return &wmGD.Screens[s];
return wmGD.numScreens > 0 ? &wmGD.Screens[0] : NULL;
}
#define DESK_MAX 64
#define DESK_CELL_W 96
#define DESK_CELL_H 96
#define DESK_MARGIN 10
#define DESK_LABEL_H 14
#define DESK_CONF ".dt/desktop.conf"
#define DESK_ICON_DIR CDE_INSTALLATION_TOP "/appconfig/icons/C"
typedef enum {
DESK_COMPUTER,
DESK_DISK,
DESK_MEDIA,
DESK_NET,
DESK_USER,
DESK_TRASH
} DeskKind;
typedef struct {
char key[512]; /* "computer:/", "mount:/media/X",
"device:sd1", "file:/home/.../notes.txt" */
char name[128]; /* the label under the icon */
char path[512]; /* the mount point or the file */
char device[32]; /* a medium that is not mounted: sd0, cd0 */
char fstype[32];
DeskKind kind;
int removable;
int system;
int mounted;
int network;
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;
} DeskItem;
static DeskItem deskItem[DESK_MAX];
static int deskCount;
static Display *deskDpy;
static Window deskRoot;
static GC deskGC;
static Pixel deskBg, deskFg, deskMid, deskShadow;
static XFontStruct *deskFont;
static int deskSel = -1;
static Time deskLast;
static int deskLastTile = -1;
static int deskDrag = -1;
static int deskDragDX, deskDragDY;
static int deskDragged;
static int deskTimer;
static int deskDirty;
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 */
/* */
/* [computer:/] */
/* x=1130 */
/* y=10 */
/* workspace=0 */
/* */
/* A key may contain anything but a ']' (escaped as "\]"). Positions */
/* are never forgotten (Konrad, 2026-11-15). */
/* ------------------------------------------------------------------ */
static void
DeskConfPath(char *buf, size_t len)
{
const char *home = getenv("HOME");
if (home == NULL || home[0] == '\0')
home = "/";
snprintf(buf, len, "%s/%s", home, DESK_CONF);
}
static DeskItem *
DeskFind(const char *key)
{
int i;
for (i = 0; i < deskCount; i++)
if (strcmp(deskItem[i].key, key) == 0)
return &deskItem[i];
return NULL;
}
/* the key of a line "[key]" with the escape undone */
static int
DeskKey(const char *line, char *buf, size_t len)
{
size_t o = 0;
const char *p;
if (line[0] != '[')
return 0;
for (p = line + 1; *p != '\0' && *p != ']' && o + 1 < len; p++) {
if (*p == '\\' && p[1] != '\0')
p++;
buf[o++] = *p;
}
buf[o] = '\0';
return *p == ']';
}
static void
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)
return;
while (fgets(line, sizeof line, f) != NULL) {
char *nl = strchr(line, '\n');
char key[512];
if (nl != NULL)
*nl = '\0';
if (line[0] == '\0' || line[0] == '#')
continue;
if (DeskKey(line, key, sizeof 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)
continue;
if (strncmp(line, "x=", 2) == 0) {
cur->x = atoi(line + 2);
cur->hasPos = 1;
} else if (strncmp(line, "y=", 2) == 0) {
cur->y = atoi(line + 2);
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);
}
/* write the whole file: temp in the same directory, then rename */
static void
DeskConfSave(void)
{
char path[512], tmp[600], key[1100];
FILE *f;
int i, o;
DeskConfPath(path, sizeof path);
snprintf(tmp, sizeof tmp, "%s.tmp", path);
if ((f = fopen(tmp, "w")) == NULL) {
if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: cannot write %s: %s\n",
tmp, strerror(errno));
return;
}
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' &&
(size_t) o + 2 < sizeof key; o++) {
if (deskItem[i].key[o] == ']' ||
deskItem[i].key[o] == '\\')
key[o] = '\\';
else
key[o] = deskItem[i].key[o];
}
key[o] = '\0';
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)
fprintf(stderr, "desktop: cannot finish %s: %s\n",
path, strerror(errno));
(void) unlink(tmp);
}
}
/* ------------------------------------------------------------------ */
/* the icons */
/* ------------------------------------------------------------------ */
static const char *
DeskIconName(const DeskItem *it)
{
switch (it->kind) {
case DESK_COMPUTER:
return "IrixDisk.m.pm";
case DESK_NET:
return "IrixNet.m.pm";
case DESK_MEDIA:
/* the medium the drive holds: a CD, a DVD, a diskette or a
* tape, the IRIX Foundation icons at 64x64 */
switch (it->fstype[0]) {
case 'c': return "IrixCD.m.pm"; /* cd9660 */
case 'u': return "IrixDVD.m.pm"; /* udf */
case 'm': return "IrixFloppy.m.pm"; /* msdos */
case 't': return "IrixTape.m.pm"; /* tar, dump */
default: return "IrixDisk.m.pm";
}
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";
case DESK_DISK:
return "IrixDisk.m.pm";
default:
return "IrixDisk.m.pm";
}
}
static void
DeskGlyph(DeskItem *it)
{
char path[600];
const char *name = DeskIconName(it);
if (it->glyph != None)
return;
snprintf(path, sizeof path, "%s/%s", DESK_ICON_DIR, name);
if (XpmReadFileToPixmap(deskDpy, deskRoot, path, &it->glyph,
&it->mask, NULL) != XpmSuccess) {
it->glyph = None;
it->mask = None;
return;
}
{
Window r;
int x, y;
unsigned int w, h, bw, d;
if (XGetGeometry(deskDpy, it->glyph, &r, &x, &y, &w, &h, &bw,
&d))
it->gw = w, it->gh = h;
}
}
/* the label, cut to the cell with three dots */
static void
DeskLabel(const DeskItem *it, char *buf, size_t len)
{
int maxw = DESK_CELL_W - 8;
size_t n;
snprintf(buf, len, "%s", it->name);
if (deskFont == NULL)
return;
if (XTextWidth(deskFont, buf, (int) strlen(buf)) <= maxw)
return;
for (n = strlen(buf); n > 1; n--) {
buf[n] = '\0';
if (XTextWidth(deskFont, buf, (int) n) + 12 <= maxw) {
snprintf(buf + n, len - n, "...");
return;
}
}
snprintf(buf, len, "...");
}
static void
DeskDraw(int i)
{
DeskItem *it = &deskItem[i];
char label[160];
int gw = (int) it->gw, gh = (int) it->gh;
int gx = (DESK_CELL_W - gw) / 2;
int gy = 6;
int lw;
if (it->glyph != None) {
if (it->mask != None) {
XSetClipMask(deskDpy, deskGC, it->mask);
XSetClipOrigin(deskDpy, deskGC, gx, gy);
}
XCopyArea(deskDpy, it->glyph, it->win, deskGC, 0, 0,
(unsigned) gw, (unsigned) gh, gx, gy);
XSetClipMask(deskDpy, deskGC, None);
}
DeskLabel(it, label, sizeof label);
lw = deskFont != NULL ? XTextWidth(deskFont, label,
(int) strlen(label)) : (int) strlen(label) * 6;
/* 4Dwm sets the label on the backdrop, with a shadow, not into a
* tile: the window is shaped to the picture and the text (below) and
* the text gets one pixel of black under it. */
XSetForeground(deskDpy, deskGC, deskShadow);
XDrawString(deskDpy, it->win, deskGC, (DESK_CELL_W - lw) / 2 + 1,
gy + gh + 6 + DESK_LABEL_H - 3, label, (int) strlen(label));
XSetForeground(deskDpy, deskGC, deskFg);
XDrawString(deskDpy, it->win, deskGC, (DESK_CELL_W - lw) / 2,
gy + gh + 6 + DESK_LABEL_H - 4, label, (int) strlen(label));
{
Pixmap shape = XCreatePixmap(deskDpy, it->win, DESK_CELL_W,
DESK_CELL_H, 1);
GC sg = XCreateGC(deskDpy, shape, 0, NULL);
XSetForeground(deskDpy, sg, 0);
XFillRectangle(deskDpy, shape, sg, 0, 0, DESK_CELL_W,
DESK_CELL_H);
XSetForeground(deskDpy, sg, 1);
if (it->glyph != None)
XFillRectangle(deskDpy, shape, sg, gx, gy,
(unsigned) gw, (unsigned) gh);
XFillRectangle(deskDpy, shape, sg, (DESK_CELL_W - lw) / 2,
gy + gh + 6, (unsigned) lw,
(unsigned) DESK_LABEL_H);
XShapeCombineMask(deskDpy, it->win, ShapeBounding, 0, 0,
shape, ShapeSet);
XFreeGC(deskDpy, sg);
XFreePixmap(deskDpy, shape);
}
if (i == deskSel)
XDrawRectangle(deskDpy, it->win, deskGC, 0, 0,
DESK_CELL_W - 1, DESK_CELL_H - 1);
XFlush(deskDpy);
}
/* the index of the active workspace in pSD->pWS, or -1 */
static int
DeskWorkspaceIndex(WmScreenData *pSD)
{
if (pSD == NULL || pSD->pWS == NULL || pSD->pActiveWS == NULL)
return -1;
return (int) (pSD->pActiveWS - pSD->pWS);
}
/* The wallpaper is a window of its own (WmBackdrop.c) and the icons are
* override-redirect windows, so XLowerWindow on an icon puts it *below* the
* wallpaper: the icon is there and invisible (measured 2026-11-22). Lower
* the backdrop first, then the icon sits above the wallpaper and below the
* clients -- the order 4Dwm has. */
static void
DeskLowerBackdrops(void)
{
int i;
if (pSD == NULL)
return;
for (i = 0; i < pSD->numWorkspaces; i++)
if (pSD->pWS[i].backdrop.window != None)
XLowerWindow(deskDpy, pSD->pWS[i].backdrop.window);
}
static void
DeskMap(int i)
{
DeskItem *it = &deskItem[i];
int active = DeskWorkspaceIndex(pSD);
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);
/* The desktop is the bottom of the stack: a window that is
* raised (a full screen program) covers the icons, as 4Dwm's
* does. XMapRaised put them above every client, because an
* override-redirect window is not restacked by the window
* manager. */
XMapWindow(deskDpy, it->win);
XLowerWindow(deskDpy, it->win);
DeskLowerBackdrops();
} else
XUnmapWindow(deskDpy, it->win);
}
static void
DeskDrawAll(void)
{
int i;
for (i = 0; i < deskCount; i++)
if (deskItem[i].win != None)
DeskDraw(i);
}
/* ------------------------------------------------------------------ */
/* opening */
/* ------------------------------------------------------------------ */
static void
DeskExec(const char *prog, const char *arg)
{
pid_t pid;
if ((pid = fork()) == 0) {
setsid();
if (arg != NULL)
execlp(prog, prog, arg, (char *) NULL);
else
execlp(prog, prog, (char *) NULL);
_exit(127);
}
}
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)
{
DeskItem *it;
char mp[256];
if (i < 0 || i >= deskCount)
return;
it = &deskItem[i];
if (!it->mounted && it->kind != DESK_USER &&
it->kind != DESK_COMPUTER && it->device[0] != '\0') {
/* a medium that is not mounted: mount it, then open it */
if (mcde_sys_disk_mount(it->device, mp, sizeof mp) !=
MCDE_SYS_OK) {
if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: mount %s: %s\n",
it->device, mcde_sys_error());
return;
}
snprintf(it->path, sizeof it->path, "%s", mp);
it->mounted = 1;
DeskScheduleRebuild();
}
if (it->kind == DESK_COMPUTER)
DeskExec("dtfile", "-dir");
else if (it->kind == DESK_TRASH)
DeskExec2("dtfile", "-dir", it->path);
else
DeskExec("dtfile", it->path);
}
static void
DeskOpenHome(void)
{
const char *home = getenv("HOME");
DeskExec("dtfile", home != NULL ? home : "/");
}
/* after a change that rearranges the icons: rebuild once, from the
* event loop, so the window serving the event is not destroyed under it */
static void
DeskScheduleRebuild(void)
{
deskDirty = 1;
if (deskTimer == 0)
deskTimer = XtAppAddTimeOut(wmGD.mwmAppContext, 100,
DeskTimerCB, NULL);
}
/* ------------------------------------------------------------------ */
/* the trash: the form dtfile uses (~/.dt/Trash, a .trashinfo each) */
/* ------------------------------------------------------------------ */
static void
DeskTrash(int i)
{
DeskItem *it = &deskItem[i];
char trash[512], info[600], dest[700], base[300], *dot;
const char *home = getenv("HOME");
const char *slash;
struct stat st;
int n;
if (it->kind != DESK_USER || home == NULL)
return;
if (stat(it->path, &st) != 0)
return;
snprintf(trash, sizeof trash, "%s/.dt/Trash", home);
(void) mkdir(trash, 0700);
slash = strrchr(it->path, '/');
base[0] = '\0';
snprintf(base, sizeof base, "%s", slash != NULL ? slash + 1 :
it->path);
for (n = 0; n < 1000; n++) {
if (n == 0)
snprintf(dest, sizeof dest, "%s/%s", trash, base);
else {
/* name.1, name.2 ... */
dot = strrchr(base, '.');
if (dot != NULL && dot != base)
snprintf(dest, sizeof dest, "%s/%.*s.%d%s",
trash, (int) (dot - base), base, n, dot);
else
snprintf(dest, sizeof dest, "%s/%s.%d",
trash, base, n);
}
if (stat(dest, &st) != 0)
break;
}
if (rename(it->path, dest) != 0)
return;
snprintf(info, sizeof info, "%s/%s.trashinfo", trash, base);
{
FILE *f = fopen(info, "w");
time_t t = time(NULL);
struct tm tm;
if (f != NULL) {
localtime_r(&t, &tm);
fprintf(f, "[Trash Info]\nPath=%s\n"
"DeletionDate=%04d-%02d-%02dT%02d:%02d:%02d\n",
it->path, tm.tm_year + 1900, tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
fclose(f);
}
}
DeskConfSave();
DeskScheduleRebuild();
}
/* ------------------------------------------------------------------ */
/* 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,
DESK_MENU_SEP1,
DESK_MENU_ALL,
DESK_MENU_THIS,
DESK_MENU_SEP2,
DESK_MENU_UNMOUNT,
DESK_MENU_TRASH,
DESK_MENU_EMPTYTRASH,
DESK_MENU_ICON_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,
"Show on All Workspaces",
"Show on This Workspace Only",
NULL,
"Unmount",
"Move to Trash",
"Empty 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 < DeskMenuCount(); i++)
h += DeskMenuTextAt(i) == NULL ? DESK_MENU_SEP : DESK_MENU_ROW;
return h;
}
static int
DeskMenuTop(int i)
{
int y = DESK_MENU_PAD, k;
for (k = 0; k < i; k++)
y += DeskMenuTextAt(k) == NULL ? DESK_MENU_SEP : DESK_MENU_ROW;
return y;
}
/* 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) {
case DESK_MENU_OPEN:
return 1;
case DESK_MENU_HOME:
return it->kind == DESK_COMPUTER;
case DESK_MENU_ALL:
return it->workspace != 0;
case DESK_MENU_THIS:
return 1;
case DESK_MENU_UNMOUNT:
return it->mounted && !it->system &&
(it->kind == DESK_DISK || it->kind == DESK_MEDIA ||
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)
{
int i;
XSetForeground(deskDpy, deskGC, deskBg);
XFillRectangle(deskDpy, deskMenuWin, deskGC, 0, 0, DESK_MENU_W,
(unsigned) DeskMenuHeight());
for (i = 0; i < DeskMenuCount(); i++) {
const char *text = DeskMenuTextAt(i);
int y = DeskMenuTop(i);
if (text == NULL) {
XSetForeground(deskDpy, deskGC, deskMid);
XDrawLine(deskDpy, deskMenuWin, deskGC, 6, y + 3,
DESK_MENU_W - 7, y + 3);
continue;
}
if (i == deskMenuHover)
XSetForeground(deskDpy, deskGC, deskMid);
else
XSetForeground(deskDpy, deskGC, deskBg);
XFillRectangle(deskDpy, deskMenuWin, deskGC, 2, y,
DESK_MENU_W - 4, DESK_MENU_ROW);
XSetForeground(deskDpy, deskGC,
DeskMenuEnabled(deskMenuFor, i) ? deskFg : deskMid);
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,
(unsigned) (DeskMenuHeight() - 1));
XFlush(deskDpy);
}
static void
DeskMenuClose(void)
{
if (deskMenuWin != None) {
XDestroyWindow(deskDpy, deskMenuWin);
deskMenuWin = None;
}
deskMenuFor = -1;
deskMenuHover = -1;
}
static void
DeskMenuPost(int i, int kind, XEvent *event)
{
XSetWindowAttributes a;
int x = event->xbutton.x_root;
int y = event->xbutton.y_root;
int h;
DeskMenuClose();
deskMenuKind = kind;
deskMenuFor = i;
h = DeskMenuHeight();
if (x + DESK_MENU_W > deskScreenW)
x = deskScreenW - DESK_MENU_W;
if (y + h > deskScreenH)
y = deskScreenH - h;
memset(&a, 0, sizeof a);
a.override_redirect = True;
a.background_pixel = deskBg;
a.border_pixel = deskFg;
a.event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask | LeaveWindowMask;
deskMenuWin = XCreateWindow(deskDpy, deskRoot, x, y, DESK_MENU_W,
(unsigned) h, 0, CopyFromParent, InputOutput, CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
&a);
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);
}
}
/* 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)
{
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);
break;
case DESK_MENU_HOME:
DeskOpenHome();
break;
case DESK_MENU_ALL:
it->workspace = 0;
DeskConfSave();
DeskMap(i);
break;
case DESK_MENU_THIS:
it->workspace = DeskWorkspaceIndex(pSD) + 1;
DeskConfSave();
DeskMap(i);
break;
case DESK_MENU_UNMOUNT:
if (it->network)
(void) mcde_sys_net_unmount(it->path);
else
(void) mcde_sys_disk_unmount(it->path);
DeskScheduleRebuild();
break;
case DESK_MENU_TRASH:
DeskTrash(i);
break;
case DESK_MENU_EMPTYTRASH:
DeskEmptyTrash();
break;
}
}
/* 1 when the event was the menu's */
Boolean
McdeDesktopMenuEvent(XEvent *event)
{
int y, i, entry = -1;
if (deskMenuWin == None || event->xany.window != deskMenuWin)
return False;
switch (event->type) {
case Expose:
if (event->xexpose.count == 0)
DeskMenuDraw();
return True;
case MotionNotify:
y = event->xmotion.y;
for (i = 0; i < DeskMenuCount(); i++) {
const char *text = DeskMenuTextAt(i);
int top = DeskMenuTop(i);
int h = text == NULL ? DESK_MENU_SEP : DESK_MENU_ROW;
if (y >= top && y < top + h) {
entry = text == NULL ? -1 : i;
break;
}
}
if (entry != deskMenuHover) {
deskMenuHover = entry;
DeskMenuDraw();
}
return True;
case LeaveNotify:
if (deskMenuHover != -1) {
deskMenuHover = -1;
DeskMenuDraw();
}
return True;
case ButtonPress:
y = event->xbutton.y;
for (i = 0; i < DeskMenuCount(); i++) {
const char *text = DeskMenuTextAt(i);
int top = DeskMenuTop(i);
int h = text == NULL ? DESK_MENU_SEP : DESK_MENU_ROW;
if (y >= top && y < top + h) {
entry = text == NULL ? -1 : i;
break;
}
}
if (entry >= 0)
DeskMenuAction(deskMenuFor, entry);
DeskMenuClose();
return True;
default:
DeskMenuClose();
return True;
}
}
/* 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 */
/* ------------------------------------------------------------------ */
static int
DeskIndexWindow(Window w)
{
int i;
for (i = 0; i < deskCount; i++)
if (deskItem[i].win == w)
return i;
return -1;
}
Boolean
McdeDesktopEvent(XEvent *event)
{
int i;
if (event->xany.window == None)
return False;
i = DeskIndexWindow(event->xany.window);
if (i < 0)
return False;
switch (event->type) {
case Expose:
if (event->xexpose.count == 0)
DeskDraw(i);
return True;
case EnterNotify:
/* From a client straight onto an icon there is no root or
* backdrop crossing, so the workspace's enter handler never
* runs: the focus goes to the desktop here (tasksd t0255). */
Do_Focus_Key((ClientData *) NULL, event->xcrossing.time,
ALWAYS_SET_FOCUS);
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;
DeskDrawAll();
DeskMenuPost(i, DESK_MENU_ICON, event);
return True;
}
if (event->xbutton.button != Button1)
return True;
deskSel = i;
DeskDrawAll();
deskDrag = i;
deskDragged = 0;
deskDragDX = event->xbutton.x_root - deskItem[i].x;
deskDragDY = event->xbutton.y_root - deskItem[i].y;
if (deskLastTile == i &&
event->xbutton.time - deskLast < 500) {
deskLastTile = -1;
DeskOpen(i);
} else {
deskLastTile = i;
deskLast = event->xbutton.time;
}
return True;
case MotionNotify:
if (deskDrag < 0)
return True;
{
int nx = event->xmotion.x_root - deskDragDX;
int ny = event->xmotion.y_root - deskDragDY;
if (nx < 0)
nx = 0;
if (ny < 0)
ny = 0;
if (nx > deskScreenW - DESK_CELL_W)
nx = deskScreenW - DESK_CELL_W;
if (ny > deskScreenH - DESK_CELL_H)
ny = deskScreenH - DESK_CELL_H;
if (abs(nx - deskItem[deskDrag].x) > 2 ||
abs(ny - deskItem[deskDrag].y) > 2)
deskDragged = 1;
deskItem[deskDrag].x = nx;
deskItem[deskDrag].y = ny;
XMoveWindow(deskDpy, deskItem[deskDrag].win, nx, ny);
}
return True;
case ButtonRelease:
if (event->xbutton.button != Button1 || deskDrag < 0)
return True;
if (deskDragged) {
deskItem[deskDrag].hasPos = 1;
deskItem[deskDrag].moved = 1;
DeskConfSave();
}
deskDrag = -1;
deskDragged = 0;
return True;
default:
return True;
}
}
/* ------------------------------------------------------------------ */
/* building the list */
/* ------------------------------------------------------------------ */
static int
DeskAdd(const char *key, const char *name, const char *path,
const char *device, const char *fstype, DeskKind kind, int removable,
int system, int mounted, int network)
{
DeskItem *it;
if (deskCount >= DESK_MAX || DeskFind(key) != NULL)
return 0;
it = &deskItem[deskCount++];
memset(it, 0, sizeof *it);
snprintf(it->key, sizeof it->key, "%s", key);
snprintf(it->name, sizeof it->name, "%s", name);
snprintf(it->path, sizeof it->path, "%s", path);
snprintf(it->device, sizeof it->device, "%s", device);
snprintf(it->fstype, sizeof it->fstype, "%s", fstype);
it->kind = kind;
it->removable = removable;
it->system = system;
it->mounted = mounted;
it->network = network;
it->workspace = 0;
it->win = None;
it->glyph = None;
it->mask = None;
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)
{
const char *slash = strrchr(path, '/');
return slash != NULL && slash[1] != '\0' ? slash + 1 : path;
}
/* the order: computer, disks, media, network, the person's own files */
static int
DeskCompare(const void *a, const void *b)
{
const DeskItem *x = a, *y = b;
if (x->kind != y->kind)
return (int) x->kind - (int) y->kind;
return strcasecmp(x->name, y->name);
}
/* the next '|'-separated field of *P; an empty field is "" */
static char *
Field(char **p)
{
char *start = *p;
char *bar;
if (start == NULL)
return (char *) "";
if ((bar = strchr(start, '|')) != NULL) {
*bar = '\0';
*p = bar + 1;
} else
*p = start + strlen(start);
return start;
}
/* the test hook (MCDE_DESK_TEST_MOUNTS): one mount a line,
* source|mountpoint|fstype|label|network|removable|system */
static int
DeskTestMounts(McdeSysMount *m, size_t max)
{
const char *file = getenv("MCDE_DESK_TEST_MOUNTS");
FILE *f;
char line[1024];
int n = 0;
if (file == NULL || (f = fopen(file, "r")) == NULL)
return 0;
while (n < (int) max && fgets(line, sizeof line, f) != NULL) {
char *p = line, *src, *mp, *type, *label, *net, *rem, *sys;
char *nl = strchr(line, '\n');
if (nl != NULL)
*nl = '\0';
if (line[0] == '#' || line[0] == '\0')
continue;
src = Field(&p);
mp = Field(&p);
type = Field(&p);
label = Field(&p);
net = Field(&p);
rem = Field(&p);
sys = p;
memset(&m[n], 0, sizeof m[n]);
snprintf(m[n].source, sizeof m[n].source, "%s", src);
snprintf(m[n].mountpoint, sizeof m[n].mountpoint, "%s", mp);
snprintf(m[n].fstype, sizeof m[n].fstype, "%s", type);
snprintf(m[n].label, sizeof m[n].label, "%s", label);
m[n].network = net != NULL ? atoi(net) : 0;
m[n].removable = rem != NULL ? atoi(rem) : 0;
m[n].system = sys != NULL ? atoi(sys) : 0;
m[n].volume = 1;
n++;
}
fclose(f);
return n;
}
static void
DeskAddMounts(void)
{
McdeSysMount mounts[64];
int n, i;
DeskAdd("computer:/", "Computer", "/", "", "ext4", DESK_COMPUTER,
0, 1, 1, 0);
n = DeskTestMounts(mounts, 64);
if (n <= 0)
n = mcde_sys_mounts(mounts, 64);
if (n < 0)
return;
for (i = 0; i < n; i++) {
char key[512];
const char *what;
if (mounts[i].mountpoint[0] == '\0')
continue;
if (mounts[i].network) {
snprintf(key, sizeof key, "mount:%s",
mounts[i].mountpoint);
what = mounts[i].label[0] != '\0' ? mounts[i].label :
DeskBase(mounts[i].mountpoint);
DeskAdd(key, what, mounts[i].mountpoint, "",
mounts[i].fstype, DESK_NET, 0, 0, 1, 1);
continue;
}
if (!mounts[i].volume)
continue;
/* the computer itself is already the first icon */
if (strcmp(mounts[i].mountpoint, "/") == 0)
continue;
if (mounts[i].system)
continue;
snprintf(key, sizeof key, "mount:%s", mounts[i].mountpoint);
what = mounts[i].label[0] != '\0' ? mounts[i].label :
DeskBase(mounts[i].mountpoint);
DeskAdd(key, what, mounts[i].mountpoint, "", mounts[i].fstype,
mounts[i].removable ? DESK_MEDIA : DESK_DISK, 0, 0, 1, 0);
}
}
/* the test hook (MCDE_DESK_TEST_DISKS): one disk a line,
* name|label|fstype|removable; all are not mounted */
static int
DeskTestDisks(McdeSysDisk *d, size_t max)
{
const char *file = getenv("MCDE_DESK_TEST_DISKS");
FILE *f;
char line[1024];
int n = 0;
if (file == NULL || (f = fopen(file, "r")) == NULL)
return 0;
while (n < (int) max && fgets(line, sizeof line, f) != NULL) {
char *p = line, *name, *label, *type, *rem;
char *nl = strchr(line, '\n');
if (nl != NULL)
*nl = '\0';
if (line[0] == '#' || line[0] == '\0')
continue;
name = Field(&p);
label = Field(&p);
type = Field(&p);
rem = Field(&p);
memset(&d[n], 0, sizeof d[n]);
snprintf(d[n].name, sizeof d[n].name, "%s", name);
snprintf(d[n].label, sizeof d[n].label, "%s", label);
snprintf(d[n].fstype, sizeof d[n].fstype, "%s", type);
d[n].removable = rem != NULL ? atoi(rem) : 1;
n++;
}
fclose(f);
return n;
}
static void
DeskAddDisks(void)
{
McdeSysDisk disks[64];
int n, i;
n = DeskTestDisks(disks, 64);
if (n <= 0)
n = mcde_sys_disks(disks, 64);
if (n < 0)
return;
for (i = 0; i < n; i++) {
char key[512];
const char *what;
if (disks[i].name[0] == '\0')
continue;
if (disks[i].mountpoint[0] != '\0')
continue; /* already from the mount table */
if (!disks[i].removable)
continue;
snprintf(key, sizeof key, "device:%s", disks[i].name);
what = disks[i].label[0] != '\0' ? disks[i].label :
disks[i].model[0] != '\0' ? disks[i].model : disks[i].name;
DeskAdd(key, what, "", disks[i].name, disks[i].fstype,
DESK_MEDIA, 1, 0, 0, 0);
}
}
/* one desktop directory: files and folders, no dot files */
static void
DeskAddDir(const char *dir, char (*seen)[512], int *nseen)
{
DIR *d;
struct dirent *de;
if ((d = opendir(dir)) == NULL)
return;
while ((de = readdir(d)) != NULL) {
char path[512], key[520], st[512];
struct stat sb;
int k, dup = 0;
if (de->d_name[0] == '.')
continue;
snprintf(path, sizeof path, "%s/%s", dir, de->d_name);
snprintf(key, sizeof key, "file:%s", path);
for (k = 0; k < *nseen; k++)
if (strcmp(seen[k], de->d_name) == 0) {
dup = 1;
break;
}
if (dup)
continue;
if (lstat(path, &sb) != 0)
continue;
snprintf(st, sizeof st, "%s", S_ISDIR(sb.st_mode) ? "d" :
"f");
DeskAdd(key, de->d_name, path, "", st, DESK_USER, 0, 0, 1,
0);
if (*nseen < 256) {
snprintf(seen[*nseen], sizeof seen[0], "%s",
de->d_name);
(*nseen)++;
}
}
closedir(d);
}
static void
DeskAddUser(void)
{
static char seen[256][512];
const char *home = getenv("HOME");
char dir[512];
int nseen = 0;
if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: user icons in %s\n",
home != NULL ? home : "(no HOME)");
if (home == NULL)
return;
if (getenv("MCDE_DESK_TEST_DESKTOP") != NULL &&
getenv("MCDE_DESK_TEST_DESKTOP")[0] != '\0') {
DeskAddDir(getenv("MCDE_DESK_TEST_DESKTOP"), seen, &nseen);
return;
}
snprintf(dir, sizeof dir, "%s/.dt/Desktop", home);
DeskAddDir(dir, seen, &nseen);
snprintf(dir, sizeof dir, "%s/Desktop", home);
DeskAddDir(dir, seen, &nseen);
}
/* the next free cell: top right, from the top down, then the next column */
static void
DeskPlace(int i, int rows)
{
DeskItem *it = &deskItem[i];
int cell;
for (cell = 0; cell < 4096; cell++) {
int col = cell / rows;
int row = cell % rows;
int x = deskScreenW - DESK_MARGIN - DESK_CELL_W - col *
DESK_CELL_W;
int y = DESK_MARGIN + row * DESK_CELL_H;
int k, taken = 0;
if (x < 0)
break;
for (k = 0; k < deskCount; k++) {
if (k == i || !deskItem[k].hasPos)
continue;
if (abs(deskItem[k].x - x) < DESK_CELL_W &&
abs(deskItem[k].y - y) < DESK_CELL_H) {
taken = 1;
break;
}
}
if (!taken) {
it->x = x;
it->y = y;
it->hasPos = 1;
return;
}
}
it->x = DESK_MARGIN;
it->y = DESK_MARGIN;
it->hasPos = 1;
}
static void
DeskLayout(void)
{
int rows = (deskScreenH - 2 * DESK_MARGIN) / DESK_CELL_H;
int i;
if (rows < 1)
rows = 1;
for (i = 0; i < deskCount; i++)
if (!deskItem[i].hasPos)
DeskPlace(i, rows);
}
/* ------------------------------------------------------------------ */
/* the windows */
/* ------------------------------------------------------------------ */
static void
DeskWindow(int i)
{
DeskItem *it = &deskItem[i];
XSetWindowAttributes a;
DeskGlyph(it);
memset(&a, 0, sizeof a);
a.override_redirect = True;
a.background_pixel = deskBg;
a.border_pixel = deskFg;
a.event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask |
Button1MotionMask | StructureNotifyMask | PropertyChangeMask |
EnterWindowMask;
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);
}
static void
DeskFreeWindows(void)
{
int i;
for (i = 0; i < deskCount; i++) {
if (deskItem[i].win != None) {
XDestroyWindow(deskDpy, deskItem[i].win);
deskItem[i].win = None;
}
if (deskItem[i].glyph != None) {
XFreePixmap(deskDpy, deskItem[i].glyph);
deskItem[i].glyph = None;
}
if (deskItem[i].mask != None) {
XFreePixmap(deskDpy, deskItem[i].mask);
deskItem[i].mask = None;
}
}
}
static void
DeskTimerCB(XtPointer client, XtIntervalId *id)
{
(void) client;
(void) id;
deskTimer = 0;
if (deskDirty) {
deskDirty = 0;
McdeDesktopRebuild();
}
}
/* the one warning dialog of the desktop (t0093): a device that went away
* while it was still mounted. Motif's error dialog, one OK button, the
* Warning sound of the theme. */
static Widget diskRemovedDialog;
static void
DiskRemovedClosed(Widget w, XtPointer client, XtPointer call)
{
(void) client;
(void) call;
if (diskRemovedDialog == w)
diskRemovedDialog = NULL;
XtDestroyWidget(w);
}
static void
DiskRemovedDialog(const char *device, const char *mountpoint)
{
char text[700];
XmString m, t;
Arg a[4];
int n = 0;
Widget d;
if (diskRemovedDialog != NULL && XtIsManaged(diskRemovedDialog))
return;
snprintf(text, sizeof text,
"The device \"%s\" was removed while it was still\n"
"mounted at \"%s\".\n\n"
"Data might have been lost or damaged. Before you\n"
"unplug a device, unmount it first -- Eject in the file\n"
"manager (Command+E) or Unmount in the Disk Utility.",
device, mountpoint);
m = XmStringCreateLocalized(text);
t = XmStringCreateLocalized("Disk Not Ejected");
XtSetArg(a[n], XmNdialogTitle, t); n++;
XtSetArg(a[n], XmNdefaultPosition, False); n++;
XtSetArg(a[n], XmNautoUnmanage, False); n++;
XtSetArg(a[n], XmNuseAsyncGeometry, True); n++;
d = XmCreateErrorDialog(wmGD.topLevelW, "diskRemoved", a, n);
/* the message label itself (as _DtMessageDialog does): the newlines
* of the text are the lines of the box */
XtSetArg(a[0], XmNlabelString, m);
XtSetValues(XmMessageBoxGetChild(d, XmDIALOG_MESSAGE_LABEL), a, 1);
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, DiskRemovedClosed, NULL);
XtManageChild(d);
{
/* dtwm is the window manager: its own dialog gets no frame,
* so it is placed here (the middle of the screen) */
Widget shell = XtParent(d);
Dimension w = 1, h = 1;
XtVaGetValues(d, XmNwidth, &w, XmNheight, &h, NULL);
if (shell != NULL && XtWindow(shell) != None)
XMoveWindow(deskDpy, XtWindow(shell),
(deskScreenW - (int) w) / 2,
(deskScreenH - (int) h) / 2);
}
XmStringFree(m);
XmStringFree(t);
diskRemovedDialog = d;
(void) McdeSoundEvent(deskDpy, "Warning");
}
/* what an event line means for the desktop: a device that went away while a
* partition of it was still mounted gets the warning; the icon refresh is
* the caller's (DeskDeviceCB) */
static void
DiskEventSeen(const char *line)
{
char action[32], device[64], mountpoint[256];
if (mcde_sys_disk_event_info(line, action, sizeof action, device,
sizeof device) != 1 || device[0] == '\0')
return;
if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: %s (%s)\n", device, action);
if (mcde_sys_disk_detached(device, mountpoint, sizeof mountpoint) == 1) {
if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: %s was mounted at %s\n",
device, mountpoint);
DiskRemovedDialog(device, mountpoint);
}
}
static void
DeskTestDeviceCB(XtPointer client, XtIntervalId *id)
{
char *line = client;
(void) id;
DiskEventSeen(line);
XtFree(line);
}
static void
DeskDeviceCB(XtPointer client, int *fd, XtInputId *id)
{
char buf[4096];
(void) client;
(void) id;
/* one line per event (Linux: the uevent, NetBSD: DRVGETEVENT) */
while (mcde_sys_disk_event_read(*fd, buf, sizeof buf) > 0)
DiskEventSeen(buf);
deskDirty = 1;
if (deskTimer == 0)
deskTimer = XtAppAddTimeOut(wmGD.mwmAppContext, 500,
DeskTimerCB, NULL);
}
void
McdeDesktopRebuild(void)
{
int i;
DeskFreeWindows();
deskCount = 0;
DeskAddMounts();
DeskAddDisks();
DeskAddUser();
DeskAddTrash();
DeskConfLoad();
qsort(deskItem, (size_t) deskCount, sizeof deskItem[0], DeskCompare);
DeskLayout();
DeskConfSave();
for (i = 0; i < deskCount; i++) {
deskItem[i].hasPos = 1; /* DeskLayout gave every one a place */
DeskWindow(i);
DeskMap(i);
}
if (deskSel >= deskCount)
deskSel = -1;
if (getenv("MCDE_DESK_DEBUG") != NULL) {
for (i = 0; i < deskCount; i++)
fprintf(stderr, "desktop: %s at %d,%d ws=%d\n",
deskItem[i].key, deskItem[i].x, deskItem[i].y,
deskItem[i].workspace);
}
}
/* ------------------------------------------------------------------ */
/* init and the workspace hook */
/* ------------------------------------------------------------------ */
void
McdeDesktopWorkspaceChanged(WmWorkspaceData *pWS)
{
int i;
(void) pWS;
for (i = 0; i < deskCount; i++)
DeskMap(i);
}
void
McdeDesktopInit(void)
{
XColor c;
Colormap cmap;
XGCValues gc;
deskDpy = DISPLAY;
pSD = DeskScreen();
if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: init pSD=%p\n", (void *) pSD);
if (pSD == NULL)
return;
deskRoot = pSD->rootWindow;
deskScreenW = DisplayWidth(deskDpy, pSD->screen);
deskScreenH = DisplayHeight(deskDpy, pSD->screen);
cmap = DefaultColormap(deskDpy, pSD->screen);
deskBg = WhitePixel(deskDpy, pSD->screen);
deskFg = BlackPixel(deskDpy, pSD->screen);
deskShadow = BlackPixel(deskDpy, pSD->screen);
deskMid = WhitePixel(deskDpy, pSD->screen);
if (XParseColor(deskDpy, cmap, "#C6C6B2B2A8A8", &c) &&
XAllocColor(deskDpy, cmap, &c))
deskBg = c.pixel;
if (XParseColor(deskDpy, cmap, "#ADADADADADAD", &c) &&
XAllocColor(deskDpy, cmap, &c))
deskMid = c.pixel;
deskFont = XLoadQueryFont(deskDpy, "-*-helvetica-medium-r-normal--12-*");
if (deskFont == NULL)
deskFont = XLoadQueryFont(deskDpy, "fixed");
gc.font = deskFont != NULL ? deskFont->fid : None;
gc.foreground = deskFg;
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();
if (deskEventFd >= 0)
(void) XtAppAddInput(wmGD.mwmAppContext, deskEventFd,
(XtPointer) XtInputReadMask, DeskDeviceCB, NULL);
else if (getenv("MCDE_DESK_DEBUG") != NULL)
fprintf(stderr, "desktop: no device events: %s\n",
mcde_sys_error());
/* tests and screenshots hand the desktop one event line without a
* kernel (the same path a real event takes, after the read) */
if (getenv("MCDE_DESK_TEST_DEVICE") != NULL)
(void) XtAppAddTimeOut(wmGD.mwmAppContext, 1000,
DeskTestDeviceCB,
(XtPointer) XtNewString(getenv("MCDE_DESK_TEST_DEVICE")));
}