From 048e842776b86a78a7d537525e8574ea2e98bcfc Mon Sep 17 00:00:00 2001 From: lotherk Date: Sat, 14 Nov 2026 22:28:06 +0000 Subject: [PATCH] MCDE dtlaunch: the panes of System Settings are results; dtsystemsettings -list-panes (t0185, first part) dtsystemsettings -list-panes prints the panes (ID, name, category, keywords; a PANE_LATER pane is left out) before any X setup. dtlaunch asks for them once (pipe and exec, no shell, a 3 s poll limit; no waitpid, SIGCHLD is ignored there) and model.c parses and matches them: the name at full score, a keyword at half, the ID checked because it ends up in dtsystemsettings -pane ID. A result reads '[settings] Displays (Hardware)' and starts through the same Parental Controls as a program (run_args). dtlaunch -text STRING fills the field of the first opening, for tests and tools/mcde-shot.sh. Measured: make mcde-linux 'check programs/dtlaunch programs/dtsystemsettings' exit=0, test_model PASS (test_panes: parse incl. unsafe IDs, matching incl. a German keyword); -list-panes: 24 lines, exit 0 with DISPLAY unset, no dock; Spotlight-mcde.png: typing 'disp' lists '[settings] Displays (Hardware)' second; Return on 'displays' starts 'dtsystemsettings -pane displays' and a window 'Displays' appears (Xvfb). Not done: the grouped two-column list, documents/mail/PDF/web results, the dtbar magnifier, the clipboard without the 1 s poll (t0185, t0080). Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 17 ++++ programs/dtlaunch/dtlaunch.c | 148 +++++++++++++++++++++++++++++-- programs/dtlaunch/model.c | 111 +++++++++++++++++++++++ programs/dtlaunch/model.h | 23 +++++ programs/dtlaunch/test_model.c | 62 +++++++++++++ programs/dtsystemsettings/main.c | 25 +++++- 6 files changed, 376 insertions(+), 10 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 54fd0260..2813185b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -331,6 +331,23 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt (Command+space)" (dtlaunch -show) and the settings. Both must be cascades: the status area is a homogeneous menu bar and refuses any other child ("Attempt to add wrong child type", measured). +- **The launcher finds settings** (t0185, first part): `dtsystemsettings + -list-panes` prints the panes (`IDnamecategorykeywords`, a + pane that is only named, `PANE_LATER`, left out; no display needed, measured + 24 lines with DISPLAY unset), dtlaunch asks for them once, by pipe and exec + (SIGCHLD is ignored there, so **no waitpid** -- it would wait for every + program the launcher ever started; a 3 s poll limit stops a stuck child), + and `model.c` (`mx_panes_parse`, `mx_pane_match`, `test_panes`) turns the text + into results: the name at full score, a keyword at half, the ID checked + (letters, digits, - and _) because it ends up in `dtsystemsettings -pane ID`, + started with the same Parental Controls as a program. A result reads + `[settings] Displays (Hardware)`. `dtlaunch -text STRING` fills the field of + the first opening (tests, `tools/mcde-shot.sh`). Measured: typing `disp` + lists the pane second (`docs/screenshots/leopard/Spotlight-mcde.png`); + Return on `displays` starts `dtsystemsettings -pane displays` and its + window "Displays" appears. **Not done yet:** the two-column list grouped by + category, documents/mail/PDF/web results, the magnifier button in dtbar with + `-at X,Y`, and the clipboard without the 1 s poll (XFixes). - **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/dtlaunch/dtlaunch.c b/programs/dtlaunch/dtlaunch.c index ebf464cc..9af69b6b 100644 --- a/programs/dtlaunch/dtlaunch.c +++ b/programs/dtlaunch/dtlaunch.c @@ -5,11 +5,15 @@ * * One key (default Cmd+space -- the key left of the space bar and the space * bar, as on the desktops of that kind; resource Dtlaunch*hotkey, names Cmd, - * Option, Ctrl, Shift) opens a field and a list. What is typed is matched against the programs in $PATH, the snippets + * Option, Ctrl, Shift) opens a field and a list. What is typed is matched against the programs in $PATH, the panes of + * System Settings, the snippets * of ~/.dt/snippets, the history of the clipboard, and read as a calculation. - * Return starts the program, or puts the snippet, the clipboard entry or the + * Return starts the program, or opens the pane (`dtsystemsettings -pane ID', + * through the same Parental Controls as a program), or puts the snippet, the + * clipboard entry or the * result on the clipboard (CLIPBOARD and PRIMARY), ready to paste; Escape - * closes. Up and Down move in the list. + * closes. Up and Down move in the list. The panes are asked for once, from + * `dtsystemsettings -list-panes' (a pipe and exec, no shell, no display). * * The clipboard history: every second the owner of CLIPBOARD is asked for its * text (Xt selections, ICCCM); a new text goes to the top, at most HISTORY @@ -18,12 +22,17 @@ * One launcher per display: the first one owns the selection _MCDE_DTLAUNCH; * `dtlaunch -show' asks that one to open and exits; `dtlaunch -quit' asks it * to exit and waits until it has (System Settings changes the hot key). - * -nograb: no hot key (for tests); -show: open at once. + * -nograb: no hot key (for tests); -show: open at once; -text STRING: the + * field of the first opening starts with STRING (for tests, and for a caller + * that knows what to look for). * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md); the parts without X * are in model.c. */ +#include +#include +#include #include #include #include @@ -52,7 +61,7 @@ #define MAXCLIP (64 * 1024) #define POLL_MS 1000 -typedef enum { K_APP, K_SNIPPET, K_CLIP, K_CALC } Kind; +typedef enum { K_APP, K_SNIPPET, K_CLIP, K_CALC, K_PANE } Kind; typedef struct { Kind kind; @@ -74,6 +83,10 @@ static struct { size_t nprograms; MxSnippet *snippets; size_t nsnippets; + MxPane *panes; /* System Settings, asked once */ + size_t npanes; + int panes_loaded; + char *initial_text; /* -text: the field of the first opening */ char *history[HISTORY]; size_t nhistory; Item items[MAXITEMS]; @@ -95,12 +108,13 @@ static XrmOptionDescRec options[] = { { "-show", "*show", XrmoptionNoArg, "True" }, { "-quit", "*quit", XrmoptionNoArg, "True" }, { "-nograb", "*nograb", XrmoptionNoArg, "True" }, + { "-text", "*text", XrmoptionSepArg, NULL }, }; static String fallback[] = { "Dtlaunch*field.columns: 48", "Dtlaunch*list.visibleItemCount: 10", - "Dtlaunch*hint.labelString: Type to find a program, a snippet, the clipboard, or =1+2", + "Dtlaunch*hint.labelString: Type to find a program, a setting, a snippet, the clipboard, or =1+2", NULL }; @@ -233,6 +247,16 @@ refresh(void) add_item(K_APP, mx_match(q, L.programs[i]), xstrdup(L.programs[i]), L.programs[i]); + /* the panes of System Settings; the label carries the + * category until the list is grouped (t0185) */ + for (i = 0; i < L.npanes; i++) { + char lab[160]; + + (void)snprintf(lab, sizeof lab, "[settings] %s (%s)", + L.panes[i].name, L.panes[i].category); + add_item(K_PANE, mx_pane_match(q, &L.panes[i]), + xstrdup(lab), L.panes[i].id); + } } XtFree(q); qsort(L.items, L.nitems, sizeof L.items[0], by_score); @@ -349,12 +373,82 @@ poll_clipboard(XtPointer client, XtIntervalId *id) /* ---- showing, hiding, doing -------------------------------------------- */ +/* The panes of System Settings: what `dtsystemsettings -list-panes' prints, + * read once (t0185). A pipe and exec, no shell; the program needs no display. + * SIGCHLD is ignored (main), so there is no waitpid -- it would wait for every + * program the launcher ever started: the end of the output is the child + * closing its side. A child that says nothing for 3 s is given up on, so a + * stuck dtsystemsettings cannot hang the launcher. */ +static void +load_panes(void) +{ + static char buf[64 * 1024]; + size_t len = 0; + int fd[2]; + pid_t pid; + + L.panes_loaded = 1; + if (pipe(fd) != 0) + return; + pid = fork(); + if (pid < 0) { + (void)close(fd[0]); + (void)close(fd[1]); + return; + } + if (pid == 0) { + int nul = open("/dev/null", O_RDWR); + + (void)close(fd[0]); + if (nul >= 0) { + (void)dup2(nul, 0); + (void)dup2(nul, 2); + } + (void)dup2(fd[1], 1); + execlp("dtsystemsettings", "dtsystemsettings", "-list-panes", + (char *)NULL); + _exit(127); + } + (void)close(fd[1]); + while (len < sizeof buf - 1) { + struct pollfd p; + ssize_t r; + int pr; + + p.fd = fd[0]; + p.events = POLLIN; + pr = poll(&p, 1, 3000); + if (pr < 0 && errno == EINTR) + continue; /* errno only means something on -1 */ + if (pr <= 0) + break; /* silence for 3 s, or an error */ + r = read(fd[0], buf + len, sizeof buf - 1 - len); + if (r < 0 && errno == EINTR) + continue; + if (r <= 0) + break; /* EOF: the child has finished */ + len += (size_t)r; + } + (void)close(fd[0]); + buf[len] = '\0'; + mx_panes_free(L.panes, L.npanes); + L.panes = NULL; + L.npanes = 0; + if (mx_panes_parse(buf, &L.panes, &L.npanes) != 0) { + L.panes = NULL; + L.npanes = 0; + } +} + static void load_sources(void) { const char *home = getenv("HOME"); char path[1024]; + if (!L.panes_loaded) + load_panes(); + mx_strv_free(L.programs, L.nprograms); (void)mx_path_programs(getenv("PATH"), &L.programs, &L.nprograms); mx_snippets_free(L.snippets, L.nsnippets); @@ -374,6 +468,14 @@ show(void) load_sources(); /* PATH and snippets may have changed */ XmTextFieldSetString(L.field, ""); + if (L.initial_text != NULL) { + /* -text: only the first opening starts with it */ + XmTextFieldSetString(L.field, L.initial_text); + XmTextFieldSetInsertionPosition(L.field, + (XmTextPosition)strlen(L.initial_text)); + free(L.initial_text); + L.initial_text = NULL; + } refresh(); if (!XtIsRealized(L.shell)) XtRealizeWidget(L.shell); @@ -524,9 +626,9 @@ hint_text(const char *text) } /* 1 when the program was started, 0 when Parental Controls refused it (the - * window stays open and says why) */ + * window stays open and says why). ARGV is what execvp(3) takes, no shell. */ static int -run(const char *program) +run_args(const char *program, char *const argv[]) { pid_t pid; @@ -544,12 +646,22 @@ run(const char *program) if (pid == 0) { /* a session of its own, so the program outlives the launcher */ (void)setsid(); - execlp(program, program, (char *)NULL); + execvp(program, argv); _exit(127); } return 1; } +static int +run(const char *program) +{ + char *argv[2]; + + argv[0] = (char *)program; + argv[1] = NULL; + return run_args(program, argv); +} + static void choose(int pos) { @@ -561,6 +673,16 @@ choose(int pos) if (it->kind == K_APP) { if (!run(it->value)) return; /* refused: the hint says why */ + } else if (it->kind == K_PANE) { + /* the ID went through mx_panes_parse: letters, digits, - and _ */ + char *argv[4]; + + argv[0] = "dtsystemsettings"; + argv[1] = "-pane"; + argv[2] = it->value; + argv[3] = NULL; + if (!run_args("dtsystemsettings", argv)) + return; /* refused: the hint says why */ } else offer(it->value); hide(); @@ -759,6 +881,14 @@ main(int argc, char **argv) "Dtlaunch.Nograb", &s, &(XrmValue){ 0, NULL }) ? True : False; quit_now = XrmGetResource(XtDatabase(L.dpy), "dtlaunch.quit", "Dtlaunch.Quit", &s, &(XrmValue){ 0, NULL }) ? True : False; + { + XrmValue tv; + + tv.addr = NULL; + if (XrmGetResource(XtDatabase(L.dpy), "dtlaunch.text", + "Dtlaunch.Text", &s, &tv) && tv.addr != NULL) + L.initial_text = xstrdup((const char *)tv.addr); + } XtRealizeWidget(L.top); /* one launcher per display: ask the running one to open or to exit */ diff --git a/programs/dtlaunch/model.c b/programs/dtlaunch/model.c index 0f7c2854..0dab3b2d 100644 --- a/programs/dtlaunch/model.c +++ b/programs/dtlaunch/model.c @@ -347,3 +347,114 @@ mx_strv_free(char **v, size_t n) free(v[i]); free(v); } + +/* ---- the panes of System Settings -------------------------------------- */ + +static int +pane_id_ok(const char *id) +{ + size_t i, len = strlen(id); + + if (len == 0 || len > 40) + return 0; + for (i = 0; i < len; i++) + if (!isalnum((unsigned char)id[i]) && id[i] != '-' && + id[i] != '_') + return 0; + return 1; +} + +int +mx_panes_parse(const char *text, MxPane **out, size_t *n) +{ + char *copy, *line, *next; + MxPane *v = NULL, *nv; + size_t cnt = 0; + + *out = NULL; + *n = 0; + if (text == NULL || (copy = strdup(text)) == NULL) + return text == NULL ? 0 : -1; + for (line = copy; line != NULL && *line != '\0'; line = next) { + char *f[4] = { NULL, NULL, NULL, NULL }; + int nf, k; + + if ((next = strchr(line, '\n')) != NULL) + *next++ = '\0'; + /* the fields: ID, name, category, keywords */ + for (nf = 0, f[0] = line; nf < 3; nf++) { + char *tab = strchr(f[nf], '\t'); + + if (tab == NULL) + break; + *tab = '\0'; + f[nf + 1] = tab + 1; + } + if (nf < 2) + continue; /* fewer than three fields */ + k = nf == 3 ? 1 : 0; + if (!pane_id_ok(f[0]) || f[1][0] == '\0' || f[2][0] == '\0') + continue; + if ((nv = realloc(v, (cnt + 1) * sizeof *v)) == NULL) + break; + v = nv; + v[cnt].id = strdup(f[0]); + v[cnt].name = strdup(f[1]); + v[cnt].category = strdup(f[2]); + v[cnt].keywords = strdup(k ? f[3] : ""); + if (v[cnt].id == NULL || v[cnt].name == NULL || + v[cnt].category == NULL || v[cnt].keywords == NULL) { + free(v[cnt].id); + free(v[cnt].name); + free(v[cnt].category); + free(v[cnt].keywords); + break; + } + cnt++; + } + free(copy); + *out = v; + *n = cnt; + return 0; +} + +void +mx_panes_free(MxPane *p, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) { + free(p[i].id); + free(p[i].name); + free(p[i].category); + free(p[i].keywords); + } + free(p); +} + +int +mx_pane_match(const char *query, const MxPane *p) +{ + int best = mx_match(query, p->name); + char *kw, *w, *save; + + if (*query == '\0' || p->keywords[0] == '\0') + return best; + if ((kw = strdup(p->keywords)) == NULL) + return best; + for (w = kw; w != NULL && *w != '\0'; w = save) { + int s; + + while (*w == ' ') + w++; + if (*w == '\0') + break; + if ((save = strchr(w, ' ')) != NULL) + *save++ = '\0'; + s = mx_match(query, w) / 2; + if (s > best) + best = s; + } + free(kw); + return best; +} diff --git a/programs/dtlaunch/model.h b/programs/dtlaunch/model.h index 8d279ca8..7679c5d1 100644 --- a/programs/dtlaunch/model.h +++ b/programs/dtlaunch/model.h @@ -50,4 +50,27 @@ char *mx_unescape(const char *s); int mx_path_programs(const char *path, char ***out, size_t *n); void mx_strv_free(char **v, size_t n); +/* + * The panes of System Settings, as `dtsystemsettings -list-panes' prints them: + * one a line, "IDnamecategorykeywords" (keywords may be + * missing). ID is what `dtsystemsettings -pane ID' takes and ends up in an + * exec, so the text is not trusted: an ID of anything but letters, digits, '-' + * and '_' (at most 40), an empty name or category, or fewer than three fields + * skips the line. *out is malloc'd; free it with mx_panes_free. + */ +typedef struct { + char *id; + char *name; + char *category; + char *keywords; +} MxPane; + +int mx_panes_parse(const char *text, MxPane **out, size_t *n); +void mx_panes_free(MxPane *p, size_t n); +/* + * How well QUERY matches a pane: its name as mx_match scores it, or a word of + * its keywords at half that. 0 for no match. + */ +int mx_pane_match(const char *query, const MxPane *p); + #endif /* DTLAUNCH_MODEL_H */ diff --git a/programs/dtlaunch/test_model.c b/programs/dtlaunch/test_model.c index 3f950163..c9a51963 100644 --- a/programs/dtlaunch/test_model.c +++ b/programs/dtlaunch/test_model.c @@ -157,6 +157,67 @@ test_programs(const char *dir) mx_strv_free(v, n); } +/* the panes of System Settings (t0185): the text of `dtsystemsettings + * -list-panes', untrusted because the ID ends up in an exec */ +static void +test_panes(void) +{ + static const char text[] = + "displays\tDisplays\tHardware\tdisplays monitors resolution aufloesung bildschirm\n" + "sound\tSound\tHardware\tsound audio lautstaerke volume ton\n" + "desktop\tDesktop & Screen Saver\tPersonal\tdesktop background bildschirmschoner\n" + "network\tNetwork\tInternet & Network\n" /* no keywords */ + "\n" /* blank */ + "bad id\tBad\tSystem\tx\n" /* blank in the ID */ + "a;b\tBad\tSystem\tx\n" + "$(id)\tBad\tSystem\tx\n" + "-x\tDash first is fine\tSystem\tx\n" /* '-' is allowed */ + "short\tonly two fields\n" + "noname\t\tSystem\tx\n" + "nocat\tName\t\tx\n" + "last\tLast One\tSystem\tkeyword with tab\tand more"; /* no newline */ + MxPane *p; + size_t n, i; + int best, s_disp, s_bild, s_none; + + CHECK(mx_panes_parse(NULL, &p, &n) == 0 && n == 0 && p == NULL); + CHECK(mx_panes_parse("", &p, &n) == 0 && n == 0); + CHECK(mx_panes_parse(text, &p, &n) == 0); + CHECK(n == 6); + if (n != 6) + return; + CHECK(strcmp(p[0].id, "displays") == 0); + CHECK(strcmp(p[0].name, "Displays") == 0); + CHECK(strcmp(p[0].category, "Hardware") == 0); + CHECK(strncmp(p[0].keywords, "displays monitors", 17) == 0); + CHECK(strcmp(p[2].name, "Desktop & Screen Saver") == 0); + CHECK(strcmp(p[3].id, "network") == 0 && p[3].keywords[0] == '\0'); + CHECK(strcmp(p[4].id, "-x") == 0); + CHECK(strcmp(p[5].id, "last") == 0); + /* the fourth tab stays in the keywords */ + CHECK(strchr(p[5].keywords, '\t') != NULL); + for (i = 0; i < n; i++) /* none of the bad IDs got through */ + CHECK(strpbrk(p[i].id, " ;$()") == NULL); + + /* matching: the name, then a keyword at half the score */ + s_disp = mx_pane_match("disp", &p[0]); + CHECK(s_disp > 0); + CHECK(s_disp == mx_match("disp", "Displays")); /* the name wins */ + s_bild = mx_pane_match("bildschirm", &p[0]); /* German keyword */ + CHECK(s_bild > 0); + CHECK(s_bild == mx_match("bildschirm", "bildschirm") / 2); + CHECK(mx_pane_match("bildschirm", &p[2]) > 0); /* another pane's word */ + s_none = mx_pane_match("zzz", &p[0]); + CHECK(s_none == 0); + best = mx_pane_match("sound", &p[1]); + CHECK(best == mx_match("sound", "Sound")); + CHECK(mx_pane_match("net", &p[3]) > 0); /* no keywords: name only */ + CHECK(mx_pane_match("zzz", &p[3]) == 0); + /* a pane's keyword does not make another pane match */ + CHECK(mx_pane_match("lautstaerke", &p[0]) == 0); + mx_panes_free(p, n); +} + /* remove what test_programs made; test_snippets removes its own file */ static void cleanup(const char *dir) @@ -187,6 +248,7 @@ main(void) test_calc(); test_snippets(dir); test_programs(dir); + test_panes(); cleanup(dir); printf("%s: %d failures\n", failures ? "FAIL" : "PASS", failures); return failures ? 1 : 0; diff --git a/programs/dtsystemsettings/main.c b/programs/dtsystemsettings/main.c index 14b9c687..02313b19 100644 --- a/programs/dtsystemsettings/main.c +++ b/programs/dtsystemsettings/main.c @@ -1310,6 +1310,26 @@ RemovePrinterCommand(const char *name) return r == MCDE_SYS_OK ? 0 : 1; } +/* -list-panes: the panes a search can offer (dtlaunch asks once at start), + * one line each, fields separated by a TAB: ID, name, category, keywords. + * A pane that is only named, not there yet (PANE_LATER), is left out: -pane + * could not open it. No display needed. */ +static int +ListPanesCommand(void) +{ + int i; + + for (i = 0; i < paneCount; i++) { + const Pane *p = &panes[i]; + + if (p->kind == PANE_LATER) + continue; + printf("%s\t%s\t%s\t%s\n", p->id, p->label, + categoryNames[p->category], p->keywords ? p->keywords : ""); + } + return 0; +} + int main(int argc, char **argv) { @@ -1322,6 +1342,9 @@ main(int argc, char **argv) for (i = 1; i + 1 < argc; i++) if (strcmp(argv[i], "-open-web") == 0) return OpenWebCommand(argv[i + 1]); + for (i = 1; i < argc; i++) + if (strcmp(argv[i], "-list-panes") == 0) + return ListPanesCommand(); XtSetLanguageProc(NULL, NULL, NULL); shell = XtVaAppInitialize(&app, "Dtsystemsettings", NULL, 0, &argc, @@ -1365,7 +1388,7 @@ main(int argc, char **argv) return RenewDhcpCommand(argv[i + 1]); else { fprintf(stderr, "usage: dtsystemsettings [-pane ID | " - "-apply-update | -apply-displays | -set-timezone ZONE | " + "-list-panes | -apply-update |-apply-displays | -set-timezone ZONE | " "-set-time EPOCH | -set-ntp on|off [SERVER] | " "-set-hostname NAME | " "-join-wifi IF [SSID-HEX] | -renew-dhcp IF]\n");