Files
mcde/programs/dttoolbox/dttoolbox.c
T
lotherkandClaude Opus 5.5 0e81935ea0 MCDE: the bar after dtsession, its menus only for the focused program, a Network pane
Konrad, 2026-11-13: "panel fehlt, top leiste is lila", the network settings
"zu rudimentaer, keine ip config, kein dns", the global menu only while a
program that has one is in front.

The bar (and the launcher, the drop-down terminal, the login items) came
from Xsession.d, before dtsession had loaded the session's resources and
the colour server: dtbar took *background from ~/.Xdefaults (#6e1e7d, the
user's base16 theme; dtbar PID 1270788 started before dtsession 1270795).
Restarting dtbar in the running session gave the palette's colours.  Now
dtsession runs sys.sessionetc -- the first of CDE_CONFIGURATION_TOP/config
and CDE_INSTALLATION_TOP/config, as sys.session is found -- before the
user's sessionetc (SmRestore.c, StartEtc); it sources config/sessionetc.d,
where 0080.dtlaunch, 0085.dtdropdown, 0090.dtbar and 0095.loginitems moved.
The bar takes colour set 5, the programs' background, not set 6 of a menu
bar (Dtbar*secondaryColorSetId: 5; measured #c6b2a8 with Default.dp).

dtwm publishes the focused client as _NET_ACTIVE_WINDOW (EWMH, WmEwmh.c
SetNetActiveWindow from SetKeyboardFocus and where the focus is dropped);
dtbar shows a program's menus only while the active window is the menus'
owner, a dialog of it (WM_TRANSIENT_FOR) or a window of the same program
(WM_CLIENT_LEADER).  Measured on Xvfb with dtwm, dtbar, dtpad and xterm,
clicking: dtpad -> its menus, xterm -> M only, dtpad -> its menus again.

Network (dtsystemsettings/network.c, docs/reference/netconfig.txt): Show:
Network Status (a light and a sentence per card, Configure...) or one card
with the tabs TCP/IP (Using DHCP, with manual address, Manually, Off;
address, mask, router, DHCP client ID, Renew DHCP Lease; IPv6), DNS
(servers, search domains, what resolv.conf has now), Wi-Fi and Ethernet;
the lock and Apply Now as Accounts.  McdeSys mcde_sys_net_config,
_configure, _renew (replacing mcde_sys_net_dhcp, which was declared and
implemented nowhere): NetBSD one block per interface in dhcpcd.conf, as
ifconfig.if(5) recommends, sysinst's static address in ifconfig.IF read and
moved there, applied with dhcpcd -n IF (cross-compiled with the tree's
x86_64--netbsd-gcc, not run); Linux the NetworkManager profile (nmcli).
mcde_sys_interfaces took the last IPv4 address (the secondary one), now the
first.  Measured on this host, read only: the pane shows the three cards,
172.20.10.2/28 by DHCP, router 172.20.10.1, DNS 1.1.1.1, MAC, MTU 1500;
test_sysinfo (new checks: settings read, 300.1.2.3 refused) exit 0.
Nothing was applied on the development host.

From the review of 2026-11-12's commits: libDtTerm links libMcdeSound
(977a7347c made the bell call McdeSoundEvent; dtcodegen did not link); the
PLIST lacked dtphoto, dtpaint, dttoolbox, mcdesoundd, libMcdeSound, their
data and the sound themes (51 entries, from the installed tree); mcdesoundd
was started by nothing (sessionetc.d/0075.mcdesoundd) and took the
selection from a running server; dttoolbox kept only the first line of the
Selected menu and rebuilt it every 500 ms, and NewMenu returned an
uninitialised pointer past 64 menus.

Built and installed with make mcde-host MCDE_DIRS=install, exit 0.

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

601 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);
}
static void
RunCommand(char *cmd)
{
pid_t pid;
if (cmd == NULL || *cmd == '\0')
return;
fprintf(stderr, "dttoolbox: %s\n", cmd);
if ((pid = fork()) == 0) {
setsid();
execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
_exit(127);
}
if (pid > 0)
signal(SIGCHLD, SIG_IGN);
}
/* --------------------------------------------------- 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) {
const 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;
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 &&
access("/tmp/dttoolbox-no-home", F_OK) != 0) {
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;
}