diff --git a/AGENTS.md b/AGENTS.md index 6956ef72..c36c96a5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -363,6 +363,22 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt not yet through a real cross build. **Not done yet:** the two-column list grouped by category, documents/mail/PDF/web results, and the magnifier button in dtbar with `-at X,Y`. +- **dttoolbox is told, it does not poll** (t0080): the menu the desktop + publishes (`_SGI_MENU_Selected` on the window that the root property + `_SGI__MENU_WINDOW` names) was read every 500 ms with two atom lookups and + two property reads; now `main` looks at PropertyNotify itself (Xt routes no + event for a foreign window to a widget -- dtlaunch's loop does the same) and + asks for the menu window's property events before it reads the text. Idle + writes to the X socket in 10 s (`strace`): **20 before, 0 after**. A change + shows at once (0.01 s after start, 3.001 s and 7.001 s for the changes made + at those times; the poll needed up to 500 ms). A rebuild while the menu is + open is deferred; the text is then forgotten and **one** timer (1 s) retries, + only while that waits -- the old code recorded the text as seen and never + built it. **A crash that was there before:** the third change of a menu with + children died in `XtDestroyWidget` (SIGSEGV, measured) because the loop walked + the parent's own `XmNchildren` array while each destroy shortened it; the list + is copied first now. Not exercised: the deferred-rebuild path (it needs an + open menu). - **Printers**: `dtsystemsettings/printers.c` lists the CUPS queues (`mcde_sys_printers`: lpstat -p/-d, parsed), shows the chosen one (state, location, model, default, shared), and adds one from what mDNS finds diff --git a/programs/dttoolbox/dttoolbox.c b/programs/dttoolbox/dttoolbox.c index a3c72858..6d4bfd60 100644 --- a/programs/dttoolbox/dttoolbox.c +++ b/programs/dttoolbox/dttoolbox.c @@ -60,7 +60,8 @@ static Menu menus[MAXMENU]; static int nmenus; static Display *dpy; static Widget top, chest, selectedSub; -static Atom aMenuWindow, aChestWindow, aConfig, aDesk; +static Atom aMenuWindow, aChestWindow, aConfig, aDesk, aSelected; +static Window watchedMenu; /* the menu window whose property events we get */ static int dynamicOn = 1; static char chestFile[512]; static char lastSelected[4096]; @@ -331,32 +332,34 @@ RunCommand(char *cmd) } } -/* --------------------------------------------------- dynamic menus (poll) */ +/* ------------------------------------------------ dynamic menus (events) */ /* * 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. + * _SGI_MENU_Selected, with chestrc lines. It was read on a timer, every + * 500 ms for good: two atom lookups and two property reads each time, because + * a property event for a foreign window is not routed through Xt. Now the + * main loop (main) looks at PropertyNotify itself, as dtlaunch's does, and calls + * this when the desktop names another menu window (root property) or changes + * the text (property of the menu window, whose events it asks for here): idle, + * the toolbox sends nothing (tasksd t0080). Only the Selected menu is + * published today; the others are their chestrc. */ +static void RetryUpdate(XtPointer data, XtIntervalId *id); + static void -PollPublished(XtPointer data, XtIntervalId *id) +UpdateSelected(void) { Window root = DefaultRootWindow(dpy), menuWin = None; - Atom aWin = XInternAtom(dpy, "_SGI__MENU_WINDOW", False); - Atom aSel = XInternAtom(dpy, "_SGI_MENU_Selected", False); + Atom aWin = aMenuWindow; + Atom aSel = aSelected; 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); + if (!dynamicOn || selectedSub == NULL) return; - } if (XGetWindowProperty(dpy, root, aWin, 0, 1, False, AnyPropertyType, &type, &format, &n, &after, &data2) == Success && data2 != NULL) { @@ -366,8 +369,14 @@ PollPublished(XtPointer data, XtIntervalId *id) XFree(data2); data2 = NULL; } + /* ask for its property events BEFORE reading the text: a change between + * the two would be lost */ + if (menuWin != None && menuWin != watchedMenu) { + XSelectInput(dpy, menuWin, PropertyChangeMask); + watchedMenu = menuWin; + } if (getenv("DTTB_DEBUG") != NULL) - fprintf(stderr, "dttoolbox: poll menuWin=%lx sub=%p\n", + fprintf(stderr, "dttoolbox: update menuWin=%lx sub=%p\n", (unsigned long) menuWin, (void *) selectedSub); if (menuWin != None && XGetWindowProperty(dpy, menuWin, aSel, 0, 4096, False, @@ -393,11 +402,18 @@ PollPublished(XtPointer data, XtIntervalId *id) ParseEntry(m, line); if (XtIsManaged(selectedSub) && XtParent(selectedSub) != NULL && - XtIsManaged(XtParent(selectedSub))) + XtIsManaged(XtParent(selectedSub))) { fprintf(stderr, "dttoolbox: Selected is open, deferred\n"); - else { + /* the text is forgotten, not kept as seen: no + * event would come to build it once the menu is + * closed. One timer, only while this waits. */ + lastSelected[0] = '\0'; + XtAppAddTimeOut(XtWidgetToApplicationContext(top), + 1000, RetryUpdate, NULL); + } else { Cardinal i; + Widget *copy; if (XmIsRowColumn(selectedSub)) XtUnmanageChild(selectedSub); @@ -405,18 +421,34 @@ PollPublished(XtPointer data, XtIntervalId *id) nk = 0; XtVaGetValues(selectedSub, XmNchildren, &kids, XmNnumChildren, &nk, NULL); - for (i = 0; i < nk; i++) - XtDestroyWidget(kids[i]); + /* a copy of the list: XtDestroyWidget takes a child + * out of the parent's own array at once (no event is + * being dispatched here), so walking that array + * skipped and then freed widgets -- the third change + * of a menu with children crashed dttoolbox + * (measured: SIGSEGV in XtDestroyWidget) */ + if (nk > 0 && (copy = (Widget *)malloc(nk * + sizeof *copy)) != NULL) { + memcpy(copy, kids, nk * sizeof *copy); + for (i = 0; i < nk; i++) + XtDestroyWidget(copy[i]); + free(copy); + } AddWidgets(selectedSub, m, 1); + fprintf(stderr, + "dttoolbox: menu Selected published (%d entries)\n", + m->n); } - fprintf(stderr, - "dttoolbox: menu Selected published (%d entries)\n", - m->n); } XFree(data2); } - XtAppAddTimeOut(XtWidgetToApplicationContext(top), 500, PollPublished, - NULL); +} + +static void +RetryUpdate(XtPointer data, XtIntervalId *id) +{ + (void) data; (void) id; + UpdateSelected(); } /* ------------------------------------------------------------------- main */ @@ -582,6 +614,7 @@ main(int argc, char **argv) aChestWindow = XInternAtom(dpy, "_SGI_TOOLCHEST_WINDOW", False); aConfig = XInternAtom(dpy, "_SGI_TOOLCHEST_CONFIG", False); aDesk = XInternAtom(dpy, "_SGI_DESKS_DESKTOP_WINDOW", False); + aSelected = XInternAtom(dpy, "_SGI_MENU_Selected", False); chest = BuildChest(); XtVaSetValues(top, XmNtitle, "ToolChest", NULL); @@ -593,7 +626,27 @@ main(int argc, char **argv) XtAddEventHandler(top, NoEventMask, True, ChestEvent, NULL); fprintf(stderr, "dttoolbox: %d menus from %s\n", nmenus, chestFile); - XtAppAddTimeOut(app, 500, PollPublished, NULL); - XtAppMainLoop(app); + UpdateSelected(); /* what is published already */ + /* XtAppMainLoop with a look at the events Xt has no widget for: the + * desktop names another menu window (a property of the root), or changes + * the text of the one it named (a property of that window) */ + for (;;) { + XEvent ev; + + XtAppNextEvent(app, &ev); + if (ev.type == PropertyNotify && + ((ev.xproperty.window == root && + ev.xproperty.atom == aMenuWindow) || + (ev.xproperty.window == watchedMenu && + ev.xproperty.atom == aSelected))) { + UpdateSelected(); + continue; + } + XtDispatchEvent(&ev); + /* dynamic menus switched on: what was published meanwhile */ + if (ev.type == ClientMessage && + ev.xclient.message_type == aConfig) + UpdateSelected(); + } return 0; }