Files
mcde/programs/dttoolbox/dttoolbox.c
T
lotherkandClaude Opus 5.5 9fd4818051 MCDE: the review's open points -- sound through McdeSys, dtwm plug and play, the rest
From the review of 2026-11-12's commits (the points left open in
1b524eb94):

Sound behind McdeSys (ext/src/mcde/AGENTS.md, philosophy 3): libMcdeSound
named /usr/bin/paplay, aplay, /usr/pkg/bin/audioplay and ffplay itself and
waited for the player, so mcdesoundd heard nothing while a sound played.
mcde_sys_sound_play (sound.c) takes paplay where a PulseAudio server
answers, else the backend's player (mcde_sys_player: audioplay(1) on
NetBSD, aplay(1) on Linux), started in a grandchild: no wait, no zombie.
Measured against a private PulseAudio with a null sink: OpenFolder and
TerminalBell sent back to back play at the same time (pactl list short
sink-inputs: two streams), mcdesoundd -selftest PASS, a second mcdesoundd
says "a server runs already".  The /usr/pkg/dt fallbacks of Sound.c and
dtphoto.c are gone (CDE_INSTALLATION_TOP is the prefix);
McdeSoundFile's dead buffer is gone; mcdesoundd ends the 20 bytes of a
message itself; Sound.h is installed (include/Makefile.am, PLIST).

Linking: LIBMCDESOUND and LIBTERM carry the libraries below them
(configure.ac).  Debian's libtool hands no indirect library to the
linker, which then took the installed, older libMcdeSys through
libMcdeSound's RUNPATH and failed (dtterm: undefined
mcde_sys_sound_play).

dtwm plug and play (WmMcdeDisplays.c, written 2026-11-11, uncommitted and
not compiling): the ClientData fields it uses, McdeHeadsChanged (Xinerama's
list read anew, its XineramaQueryScreens memory given back with XFree),
McdePanelMonitorsChanged (the panel centres on the main display), the
RandR events taken in WmMain.c's loop, McdeDisplaysInit after the EWMH
setup, the file in Makefile.am.  Measured on Xvfb: dtwm starts with it,
the check at login leaves the outputs as they were (xrandr before and
after: screen connected 1024x700+0+0), dtwm runs on.  Plugging itself was
not measured: Xvfb cannot change its outputs, and Xorg's dummy driver
refuses a user who is not at the console.

Smaller: dttoolbox ignores SIGCHLD from the start (set after the first
fork it missed the first child) and lost its test hook
/tmp/dttoolbox-no-home; dtfile plays OpenFolder once per folder
(FileMgr.c), not twice; dtpaint's unused variable; dttoolbox frees
XGetAtomName's string without a cast-away const.  AGENTS.md describes
McdeSound, mcdesoundd, dtphoto, dtpaint, dttoolbox and the plug and play;
the WAV tones' maker was not kept, which it says.  The two stale citations
(53b852bb2, 945da0f4c) are corrected in git notes.

Built with make mcde-host, exit 0 (build/logs/mcde-host-2026-11-13q.log).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 06:41:53 +00:00

600 lines
15 KiB
C

/*
* dttoolbox.c -- MCDE: the toolchest
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The chest of the IRIX desktop: a window of menus, read from a chestrc file
* (docs/MCDE-PROGRAMS.md, B1-B5). Stage T1: the parser of the useful
* subset, the window, and the dynamic menus over the desktop's rendezvous:
* the root window's _SGI__MENU_WINDOW carries a property _SGI_MENU_<name>
* with the menu text; this program owns _SGI_TOOLCHEST_WINDOW and reads
* _SGI_TOOLCHEST_CONFIG ClientMessages (research/notes/TOOLCHEST_PROTOCOL.md).
*
* Grammar read: "Menu NAME [dynamic] {", entries "LABEL f.menu NAME",
* "LABEL f.exec \"command\"", "LABEL f.checkexec.sh \"command\"",
* "no-label f.separator", "}" and # comments; "sinclude"/"include" are
* skipped (a note goes to stderr), as the reference's extra chests are not
* shipped yet.
*
* C99 and POSIX, Motif and Xlib.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <Xm/Xm.h>
#include <Xm/CascadeB.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#define MAXMENU 64
#define MAXENTRY 64
enum { E_EXEC, E_MENU, E_SEP };
typedef struct {
char label[128];
int type;
char value[512]; /* command or menu name */
} Entry;
typedef struct {
char name[64];
int dynamic;
int n;
Entry e[MAXENTRY];
} Menu;
static Menu menus[MAXMENU];
static int nmenus;
static Display *dpy;
static Widget top, chest, selectedSub;
static Atom aMenuWindow, aChestWindow, aConfig, aDesk;
static int dynamicOn = 1;
static char chestFile[512];
static char lastSelected[4096];
static Menu *
FindMenu(const char *name)
{
int i;
for (i = 0; i < nmenus; i++)
if (strcmp(menus[i].name, name) == 0)
return &menus[i];
return NULL;
}
static Menu *
NewMenu(const char *name)
{
static Menu spill; /* past MAXMENU: filled, never shown */
Menu *m;
if ((m = FindMenu(name)) != NULL)
return m;
if (nmenus >= MAXMENU) {
memset(&spill, 0, sizeof spill);
return &spill;
}
m = &menus[nmenus++];
memset(m, 0, sizeof *m);
snprintf(m->name, sizeof m->name, "%s", name);
return m;
}
static void
Trim(char *p)
{
size_t n = strlen(p);
while (n > 0 && (p[n - 1] == '\n' || p[n - 1] == '\r' ||
p[n - 1] == ' ' || p[n - 1] == '\t'))
p[--n] = '\0';
}
/* the label of an entry: quoted, or the first word */
static char *
TakeLabel(char **pp, char *out, size_t size)
{
char *p = *pp;
while (*p == ' ' || *p == '\t')
p++;
if (*p == '"') {
char *q = ++p;
while (*q != '\0' && *q != '"')
q++;
snprintf(out, size, "%.*s", (int) (q - p), p);
if (*q == '"')
q++;
*pp = q;
} else {
char *q = p;
while (*q != '\0' && *q != ' ' && *q != '\t')
q++;
snprintf(out, size, "%.*s", (int) (q - p), p);
*pp = q;
}
return out;
}
/* the quoted command after the function word */
static void
TakeCommand(char **pp, char *out, size_t size)
{
char *p = *pp, *q;
if ((q = strchr(p, '"')) == NULL) {
out[0] = '\0';
return;
}
q++;
p = q;
while (*q != '\0' && *q != '"')
q++;
snprintf(out, size, "%.*s", (int) (q - p), p);
}
static void
AddEntry(Menu *m, const char *label, int type, const char *value)
{
Entry *e;
if (m->n >= MAXENTRY)
return;
e = &m->e[m->n++];
snprintf(e->label, sizeof e->label, "%s", label);
e->type = type;
snprintf(e->value, sizeof e->value, "%s", value ? value : "");
}
/* one line of a menu body, for the chestrc and for a published menu */
static void
ParseEntry(Menu *m, char *line)
{
char label[128] = "", *p = line;
Trim(p);
if (*p == '\0' || *p == '#' || *p == '{' || *p == '}')
return;
if (strncmp(p, "no-label", 8) == 0) {
AddEntry(m, "", E_SEP, NULL);
return;
}
if (strncmp(p, "sinclude", 8) == 0 || strncmp(p, "include", 7) == 0) {
fprintf(stderr, "dttoolbox: %s is not read yet: %s\n",
strncmp(p, "sinclude", 8) == 0 ? "sinclude" : "include", p);
return;
}
TakeLabel(&p, label, sizeof label);
if (strstr(p, "f.menu") != NULL) {
char *q = strstr(p, "f.menu") + 6, name[64];
int n = 0;
while (*q == ' ' || *q == '\t')
q++;
while (*q != '\0' && *q != ' ' && *q != '\t' && n < 63)
name[n++] = *q++;
name[n] = '\0';
AddEntry(m, label, E_MENU, name);
} else if (strstr(p, "f.exec") != NULL ||
strstr(p, "f.checkexec") != NULL) {
char cmd[512];
TakeCommand(&p, cmd, sizeof cmd);
AddEntry(m, label, E_EXEC, cmd);
} else if (*p == '\0') {
AddEntry(m, label, E_EXEC, "");
}
}
static int
ReadChest(const char *path)
{
FILE *f;
char line[1024];
Menu *cur = NULL;
int inblock = 0;
if ((f = fopen(path, "r")) == NULL)
return -1;
while (fgets(line, sizeof line, f) != NULL) {
char *p = line;
while (*p == ' ' || *p == '\t')
p++;
Trim(p);
if (*p == '\0' || *p == '#')
continue;
if (strncmp(p, "Menu ", 5) == 0) {
char name[64], *q = p + 5;
int n = 0;
while (*q != '\0' && *q != ' ' && *q != '\t' && n < 63)
name[n++] = *q++;
name[n] = '\0';
cur = NewMenu(name);
while (*q == ' ' || *q == '\t')
q++;
if (strncmp(q, "dynamic", 7) == 0)
cur->dynamic = 1;
inblock = 0;
continue;
}
if (*p == '{') {
inblock = 1;
continue;
}
if (*p == '}') {
inblock = 0;
cur = NULL;
continue;
}
if (inblock && cur != NULL)
ParseEntry(cur, p);
}
fclose(f);
return 0;
}
/* ------------------------------------------------------------------ build */
static void RunCommand(char *cmd);
static void
EntryCallback(Widget w, XtPointer data, XtPointer call)
{
(void) w; (void) call;
RunCommand((char *) data);
}
static void BuildMenuWidget(Widget parent, const char *name, int depth);
static void
AddWidgets(Widget shell, Menu *m, int depth)
{
int i;
for (i = 0; i < m->n; i++) {
Entry *e = &m->e[i];
Widget b;
if (e->type == E_SEP) {
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass,
shell, NULL);
continue;
}
if (e->type == E_MENU) {
Widget sub = XmCreatePulldownMenu(shell,
(char *) e->label, NULL, 0);
XtVaCreateManagedWidget(e->label,
xmCascadeButtonWidgetClass, shell,
XmNsubMenuId, sub, NULL);
BuildMenuWidget(sub, e->value, depth + 1);
continue;
}
b = XtVaCreateManagedWidget(e->label, xmPushButtonWidgetClass,
shell, NULL);
XtAddCallback(b, XmNactivateCallback, EntryCallback, e->value);
}
}
static Menu *published;
static void
BuildMenuWidget(Widget shell, const char *name, int depth)
{
Menu *m;
if (depth > 6)
return;
m = FindMenu(name);
if (m == NULL) {
/* a dynamic menu that nothing has published yet */
if (published == NULL || strcmp(published->name, name) != 0) {
published = NewMenu(name);
published->dynamic = 1;
}
m = published;
}
AddWidgets(shell, m, depth);
}
/* the children are not waited for: SIGCHLD is ignored from the start
* (main), so none stays a zombie -- set after the first fork it missed
* the first child */
static void
RunCommand(char *cmd)
{
if (cmd == NULL || *cmd == '\0')
return;
fprintf(stderr, "dttoolbox: %s\n", cmd);
if (fork() == 0) {
setsid();
execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
_exit(127);
}
}
/* --------------------------------------------------- dynamic menus (poll) */
/*
* The desktop publishes a menu as a string property on the menu window:
* _SGI_MENU_Selected, with chestrc lines. Reading it on a timer keeps this
* simple and safe: a property event for a foreign window would have to be
* routed by hand through Xt. Only the Selected menu is published today;
* the others are their chestrc.
*/
static void
PollPublished(XtPointer data, XtIntervalId *id)
{
Window root = DefaultRootWindow(dpy), menuWin = None;
Atom aWin = XInternAtom(dpy, "_SGI__MENU_WINDOW", False);
Atom aSel = XInternAtom(dpy, "_SGI_MENU_Selected", False);
Atom type;
int format;
unsigned long n, after;
unsigned char *data2 = NULL;
(void) data; (void) id;
if (!dynamicOn || selectedSub == NULL) {
XtAppAddTimeOut(XtWidgetToApplicationContext(top), 500,
PollPublished, NULL);
return;
}
if (XGetWindowProperty(dpy, root, aWin, 0, 1, False,
AnyPropertyType, &type, &format, &n, &after, &data2) == Success &&
data2 != NULL) {
if (format == 32 && n >= 1 &&
(type == XA_WINDOW || type == XA_CARDINAL))
menuWin = *(Window *) data2;
XFree(data2);
data2 = NULL;
}
if (getenv("DTTB_DEBUG") != NULL)
fprintf(stderr, "dttoolbox: poll menuWin=%lx sub=%p\n",
(unsigned long) menuWin, (void *) selectedSub);
if (menuWin != None &&
XGetWindowProperty(dpy, menuWin, aSel, 0, 4096, False,
AnyPropertyType, &type, &format, &n, &after, &data2) == Success &&
data2 != NULL && n > 0) {
char *text = (char *) data2;
if (strcmp(text, lastSelected) != 0) {
Menu *m = FindMenu("Selected");
char *line, *save = NULL;
Widget *kids = NULL;
Cardinal nk = 0;
if (m == NULL)
m = NewMenu("Selected");
m->n = 0;
/* kept before strtok_r cuts the text into lines: kept
* after, it held the first line only and every poll
* rebuilt the menu */
snprintf(lastSelected, sizeof lastSelected, "%s", text);
for (line = strtok_r(text, "\n", &save); line != NULL;
line = strtok_r(NULL, "\n", &save))
ParseEntry(m, line);
if (XtIsManaged(selectedSub) &&
XtParent(selectedSub) != NULL &&
XtIsManaged(XtParent(selectedSub)))
fprintf(stderr,
"dttoolbox: Selected is open, deferred\n");
else {
Cardinal i;
if (XmIsRowColumn(selectedSub))
XtUnmanageChild(selectedSub);
kids = NULL;
nk = 0;
XtVaGetValues(selectedSub, XmNchildren, &kids,
XmNnumChildren, &nk, NULL);
for (i = 0; i < nk; i++)
XtDestroyWidget(kids[i]);
AddWidgets(selectedSub, m, 1);
}
fprintf(stderr,
"dttoolbox: menu Selected published (%d entries)\n",
m->n);
}
XFree(data2);
}
XtAppAddTimeOut(XtWidgetToApplicationContext(top), 500, PollPublished,
NULL);
}
/* ------------------------------------------------------------------- main */
static Widget
BuildChest(void)
{
Widget row;
Menu *root = FindMenu("ToolChest");
int i;
row = XtVaCreateManagedWidget("chest", xmRowColumnWidgetClass, top,
XmNorientation, XmVERTICAL, XmNpacking, XmPACK_COLUMN,
XmNnumColumns, 1, XmNmarginWidth, 4, XmNmarginHeight, 4,
XmNspacing, 2, NULL);
if (root != NULL) {
for (i = 0; i < root->n; i++) {
Entry *e = &root->e[i];
if (e->type == E_SEP) {
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass,
row, NULL);
continue;
}
if (e->type == E_MENU) {
Widget sub = XmCreatePulldownMenu(row, e->label,
NULL, 0);
XtVaCreateManagedWidget(e->label,
xmCascadeButtonWidgetClass, row,
XmNsubMenuId, sub, NULL);
if (strcmp(e->value, "Selected") == 0)
selectedSub = sub;
BuildMenuWidget(sub, e->value, 1);
} else {
Widget b = XtVaCreateManagedWidget(e->label,
xmPushButtonWidgetClass, row, NULL);
XtAddCallback(b, XmNactivateCallback,
EntryCallback, e->value);
}
}
} else {
Widget b = XtVaCreateManagedWidget("No ToolChest menu",
xmPushButtonWidgetClass, row, NULL);
(void) b;
}
return row;
}
/* dynamic menus: the root property _SGI_MENU_<name> holds chestrc lines */
static void
ReadPublished(Window root, const char *name)
{
Menu *m;
Atom atom;
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
Window menuWin = None;
m = FindMenu(name);
if (m == NULL) {
m = NewMenu(name);
m->dynamic = 1;
}
/* the text lives on the _SGI__MENU_WINDOW, else on the root */
atom = XInternAtom(dpy, "_SGI__MENU_WINDOW", False);
if (XGetWindowProperty(dpy, root, atom, 0, 1, False, XA_WINDOW,
&type, &format, &n, &after, &data) == Success && data != NULL) {
menuWin = *(Window *) data;
XFree(data);
data = NULL;
}
if (menuWin == None)
return;
snprintf(m->name, sizeof m->name, "%s", name);
(void) atom;
{
char prop[64];
snprintf(prop, sizeof prop, "_SGI_MENU_%s", name);
atom = XInternAtom(dpy, prop, False);
}
if (XGetWindowProperty(dpy, menuWin, atom, 0, 8192, False, XA_STRING,
&type, &format, &n, &after, &data) != Success || data == NULL)
return;
m->n = 0;
{
char *text = (char *) data, *line, *save = NULL;
for (line = strtok_r(text, "\n", &save); line != NULL;
line = strtok_r(NULL, "\n", &save))
ParseEntry(m, line);
}
XFree(data);
fprintf(stderr, "dttoolbox: menu %s published (%d entries)\n",
name, m->n);
}
static void
RootEvent(Widget w, XtPointer data, XEvent *ev, Boolean *keep)
{
(void) w; (void) data; (void) keep;
if (ev->type == PropertyNotify) {
char *name = XGetAtomName(dpy, ev->xproperty.atom);
if (name != NULL &&
strncmp(name, "_SGI_MENU_", 10) == 0)
ReadPublished(ev->xproperty.window,
name + 10);
if (name != NULL)
XFree(name);
}
}
static void
ChestEvent(Widget w, XtPointer data, XEvent *ev, Boolean *keep)
{
(void) w; (void) data; (void) keep;
if (ev->type == ClientMessage &&
ev->xclient.message_type == aConfig) {
dynamicOn = ev->xclient.data.b[0] != 0;
fprintf(stderr, "dttoolbox: dynamic menus %s\n",
dynamicOn ? "on" : "off");
}
}
int
main(int argc, char **argv)
{
XtAppContext app;
Window root;
const char *home;
signal(SIGCHLD, SIG_IGN);
top = XtVaAppInitialize(&app, "Dttoolbox", NULL, 0, &argc, argv, NULL,
NULL);
dpy = XtDisplay(top);
root = DefaultRootWindow(dpy);
if ((home = getenv("DTTB_CHESTRC")) != NULL) {
snprintf(chestFile, sizeof chestFile, "%s", home);
} else if ((home = getenv("HOME")) != NULL) {
char p[512];
snprintf(p, sizeof p, "%s/.chestrc", home);
if (access(p, R_OK) == 0)
snprintf(chestFile, sizeof chestFile, "%s", p);
}
if (chestFile[0] == '\0') {
snprintf(chestFile, sizeof chestFile, "%s",
CDE_INSTALLATION_TOP "/share/toolchest/system.chestrc");
if (access(chestFile, R_OK) != 0)
snprintf(chestFile, sizeof chestFile, "system.chestrc");
}
if (ReadChest(chestFile) != 0)
fprintf(stderr, "dttoolbox: cannot read %s\n", chestFile);
aMenuWindow = XInternAtom(dpy, "_SGI__MENU_WINDOW", False);
aChestWindow = XInternAtom(dpy, "_SGI_TOOLCHEST_WINDOW", False);
aConfig = XInternAtom(dpy, "_SGI_TOOLCHEST_CONFIG", False);
aDesk = XInternAtom(dpy, "_SGI_DESKS_DESKTOP_WINDOW", False);
chest = BuildChest();
XtVaSetValues(top, XmNtitle, "ToolChest", NULL);
XtRealizeWidget(top);
XSetSelectionOwner(dpy, aChestWindow, XtWindow(top), CurrentTime);
XSelectInput(dpy, root, PropertyChangeMask);
XtAddEventHandler(top, PropertyChangeMask, False, RootEvent, NULL);
XtAddEventHandler(top, NoEventMask, True, ChestEvent, NULL);
fprintf(stderr, "dttoolbox: %d menus from %s\n", nmenus, chestFile);
XtAppAddTimeOut(app, 500, PollPublished, NULL);
XtAppMainLoop(app);
return 0;
}