LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
Dock model with typed ids and desktop actions in the Linux app launcher
Federico Pereirade80e040f2f3
diff --git a/sdk/dockmodel.js b/sdk/dockmodel.jsnew file mode 100644index 0000000..1ea5884--- /dev/null+++ b/sdk/dockmodel.js@@ -0,0 +1,152 @@+// dockmodel.js — the Dock's pinned list as plain data, with no runtime or React (so it can be+// unit tested with node: server/tests/dock_model_test.mjs).+//+// A pinned item is a typed id, so the three kinds of app can never collide:+// "app:<id>" a LordOS app bundled with the theme (apps/<id>), opened with wm.open+// "lpk:<id>" an installed .lpk package, launched with lord.apps.launchPackage(id)+// "desktop:<id>" a Linux app's .desktop entry, launched with lord.apps.launch(id)+// Lists written before 0.8.1 hold untyped ids ("files", "store"); normalizePinned() migrates them.+//+// Positions passed to moveItem/pinItem count only the items the Dock shows (isShown): an+// uninstalled app keeps its place in settings.json, hidden, and comes back if it is reinstalled.++// "shell:launcher" the theme's app launcher (the only "shell" item): it moves like an app+export const DOCK_KINDS = ['app', 'lpk', 'desktop', 'shell'];+export const LAUNCHER = 'shell:launcher';+// The Dock's rules:+// - Files is always the first item: it can't be moved or removed;+// - the Trash is always the last one, drawn by the theme after a separator (not a pinned item);+// - Settings and the app launcher can't be removed (no «Remove from Dock»; dragging them out or+// onto the Trash puts them back) but move freely between Files and the Trash;+// - every other app is added, moved and removed freely.+// "First" and "last" follow the Dock's reading order (the theme mirrors a bottom Dock in RTL).+export const FIRST = 'app:files';+export const LOCKED = [FIRST, LAUNCHER, 'app:settings'];++// The same rules as the server: lpk_valid_id (server/src/lpk.c) and valid_id (server/src/apps.c).+const APP_RE = /^[a-z][a-z0-9-]{0,39}$/;+const LPK_RE = /^[a-z][a-z0-9-]{1,39}$/;+const DESKTOP_RE = /^[A-Za-z0-9_][A-Za-z0-9._-]{0,61}$/;+const validKey = {+ app: (k) => APP_RE.test(k),+ shell: (k) => k === 'launcher',+ lpk: (k) => LPK_RE.test(k),+ desktop: (k) => DESKTOP_RE.test(k) && !k.includes('..') && !k.startsWith('lpk-'),+};++// "lpk:notes" → { kind: 'lpk', key: 'notes' }; null if it isn't a valid typed id.+export function parseDockId(id) {+ if (typeof id !== 'string') return null;+ const i = id.indexOf(':');+ if (i <= 0) return null;+ const kind = id.slice(0, i), key = id.slice(i + 1);+ return DOCK_KINDS.includes(kind) && validKey[kind](key) ? { kind, key } : null;+}++export const dockId = (kind, key) => (DOCK_KINDS.includes(kind) && typeof key === 'string' && validKey[kind](key) ? `${kind}:${key}` : null);+export const isDockId = (id) => parseDockId(id) !== null;++// One stored entry → a typed id, or null to drop it. classify(untypedId) says what an old untyped+// id is now: 'app', 'lpk' or null (unknown: kept as a LordOS app id if it looks like one).+export function toTypedId(id, classify = () => null) {+ if (typeof id !== 'string' || !id) return null;+ if (parseDockId(id)) return id;+ if (id.includes(':')) return null; // "linux:…" (a window with no .desktop) or junk+ const kind = classify(id) || 'app';+ return dockId(kind, id);+}++// The stored list → { list, changed }: typed, valid and without repeats.+export function normalizePinned(list, classify) {+ if (!Array.isArray(list)) return { list: [], changed: list != null };+ const out = [];+ for (const raw of list) {+ const id = toTypedId(raw, classify);+ if (id && !out.includes(id)) out.push(id);+ }+ return { list: out, changed: out.length !== list.length || out.some((x, i) => x !== list[i]) };+}++// Files is always the first item: whatever an operation does, it goes back to the front.+const filesFirst = (list) => {+ const i = list.indexOf(FIRST);+ return i <= 0 ? list : [FIRST, ...list.slice(0, i), ...list.slice(i + 1)];+};++// Inserts id before the shown item at position index (null, past the end or not a number: after the+// last item). list must not contain id. Nothing lands before Files: index 0 becomes "right after it".+function insertAt(list, id, index, isShown) {+ const shown = list.filter(isShown);+ let k = Number.isFinite(+index) && index !== null ? Math.max(0, Math.round(+index)) : shown.length;+ if (shown[0] === FIRST) k = Math.max(1, k);+ if (k >= shown.length) return [...list, id];+ const at = list.indexOf(shown[k]);+ return [...list.slice(0, at), id, ...list.slice(at)];+}++const same = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);++// Moves a pinned item to position toIndex among the shown items (counted without it). Files never+// moves.+export function moveItem(list, id, toIndex, isShown = () => true) {+ if (!list.includes(id) || id === FIRST) return list;+ const next = filesFirst(insertAt(list.filter((x) => x !== id), id, toIndex, isShown));+ return same(next, list) ? list : next;+}++// Pins id at position atIndex among the shown items (at the end without it); already pinned: moves.+export function pinItem(list, id, atIndex, isShown = () => true) {+ if (!isDockId(id)) return list;+ if (list.includes(id)) return atIndex == null ? list : moveItem(list, id, atIndex, isShown);+ return filesFirst(insertAt(list, id, atIndex, isShown));+}++export const isLocked = (id, locked = LOCKED) => locked.includes(id);+// Can the item change place? Everything but Files (the Trash is not in the list at all).+export const isMovable = (id) => id !== FIRST;+export const unpinItem = (list, id, locked = LOCKED) => (list.includes(id) && !isLocked(id, locked) ? list.filter((x) => x !== id) : list);++// Puts back every required item missing from list (an older settings file, or one edited by+// hand), each after the item that comes before it in defaults; Files goes first.+export function ensureItems(list, required = LOCKED, defaults = []) {+ let out = list;+ for (const id of required) {+ if (out.includes(id) || !isDockId(id)) continue;+ const d = defaults.indexOf(id);+ let at = out.length;+ if (d >= 0) {+ at = 0;+ for (let i = d - 1; i >= 0; i--) { const k = out.indexOf(defaults[i]); if (k >= 0) { at = k + 1; break; } }+ }+ out = [...out.slice(0, at), id, ...out.slice(at)];+ }+ return filesFirst(out);+}++// Quick actions at the top of an app's Dock menu: [{ id, label, args }], from its manifest+// ("dockActions") or its module (registerDockActions). Anything malformed is left out.+const ACTION_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,47}$/;+const plainArgs = (a) => {+ if (a == null) return {};+ if (typeof a !== 'object' || Array.isArray(a)) return null;+ const out = {};+ for (const [k, v] of Object.entries(a)) {+ if (!/^[A-Za-z_][A-Za-z0-9_]{0,31}$/.test(k) || k === 'nonce') return null;+ if (v === null || ['string', 'number', 'boolean'].includes(typeof v)) out[k] = v;+ else return null;+ }+ return out;+};+export function cleanActions(list, max = 40) {+ if (!Array.isArray(list)) return [];+ const out = [];+ for (const a of list) {+ if (!a || typeof a !== 'object' || !ACTION_RE.test(String(a.id ?? '')) || out.some((x) => x.id === a.id)) continue;+ const label = typeof a.label === 'string' ? a.label : '';+ const args = plainArgs(a.args);+ if (!label || !args) continue;+ out.push({ id: a.id, label, args, new: !!a.new });+ if (out.length >= max) break;+ }+ return out;+}diff --git a/server/Makefile b/server/Makefileindex a2dd3ae..d393d90 100644--- a/server/Makefile+++ b/server/Makefile@@ -245,6 +245,12 @@ test: build/launch-clibuild/launch-cli: tests/launch_cli.c $(LAUNCH_CLI_SRC) src/apps.h $(wildcard src/sec/*.h src/fw/*.h src/hw/*.h)@mkdir -p build$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/launch_cli.c $(LAUNCH_CLI_SRC)+# tests/apps_actions_test.c: desktop actions (Actions= and [Desktop Action …]) in the real launcher+test: build/apps-actions-test+build/apps-actions-test: tests/apps_actions_test.c $(LAUNCH_CLI_SRC) src/apps.h $(wildcard src/sec/*.h src/fw/*.h src/hw/*.h)+ @mkdir -p build+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/apps_actions_test.c $(LAUNCH_CLI_SRC)+ ./$@ || { rm -f $@; exit 1; }# tests/notifyd_cli.c: the real notification server on a private bus, for tests/fwlinux/notify.shNOTIFYD_CLI_SRC := src/hw/notifyd.c src/sec/appid.c src/hw/jbuf.c src/vault/dbus.c src/sec/restrict.c src/fw/rules.c src/fw/fwmap.c src/sec/json.ctest: build/notifyd-cli build/notifyd-testdiff --git a/server/src/apps.c b/server/src/apps.cindex 1fb3cd7..3d73c33 100644--- a/server/src/apps.c+++ b/server/src/apps.c@@ -74,14 +74,53 @@ static int locale_rank(const char *loc, const char *lang) {return 0;}+/* A desktop action ("[Desktop Action <name>]", Desktop Entry Specification, "Additional+ * applications actions"): its name, translated label and Exec line. Only the actions listed in+ * the main group's Actions= key count. */+typedef struct {+ char name[APPS_ACTION_NAME_MAX], label[160], exec[1024];+ int rank, has_label;+} Action;+typedef struct {DesktopApp a;char exec[1024], tryexec[256], icon_name[256], path[512];- char only[256], not_[256];+ char only[256], not_[256], actions[512];int ranks[5]; /* name generic comment keywords: language priority */int hidden, nodisplay, terminal, is_app, has_name;+ Action act[APPS_MAX_ACTIONS]; /* the [Desktop Action …] groups seen (valid names only) */+ int nact;} Entry;+/* An action identifier the launcher accepts: [A-Za-z0-9-] (the specification's charset), at most+ * 63 characters. */+static int valid_action(const char *s) {+ size_t n = strlen(s);+ if (!n || n >= APPS_ACTION_NAME_MAX) return 0;+ for (const char *p = s; *p; p++) if (!isalnum((unsigned char)*p) && *p != '-') return 0;+ return 1;+}++/* "Key[loc]=value" → key, loc (or NULL) and value, in place. 0 if the line has no '='. */+static int split_kv(char *line, char **k, char **loc, char **v) {+ char *eq = strchr(line, '=');+ if (!eq) return 0;+ *k = line; *v = eq + 1;+ char *ke = eq;+ while (ke > *k && ke[-1] == ' ') ke--;+ *ke = 0;+ while (**v == ' ') (*v)++;+ *loc = NULL;+ char *br = strchr(*k, '[');+ if (br) {+ *br = 0; *loc = br + 1;+ char *cb = strchr(*loc, ']'); if (cb) *cb = 0;+ char *at = strchr(*loc, '@'); if (at) *at = 0;+ char *dot = strchr(*loc, '.'); if (dot) *dot = 0;+ }+ return 1;+}+static void set_tr(Entry *e, int slot, char *dst, size_t n, const char *val, const char *loc, const char *lang) {int r = locale_rank(loc, lang);if (!r && loc) return;@@ -96,19 +135,36 @@ static int parse_desktop(const char *file, const char *lang, Entry *e) {memset(e, 0, sizeof *e);char line[2048];int in_main = 0;+ Action *act = NULL; /* inside a [Desktop Action …] group that is kept */while (fgets(line, sizeof line, f)) {line[strcspn(line, "\r\n")] = 0;- if (line[0] == '[') { in_main = !strcmp(line, "[Desktop Entry]"); continue; }- if (!in_main || line[0] == '#' || !line[0]) continue;- char *eq = strchr(line, '=');- if (!eq) continue;- char *k = line, *v = eq + 1;- char *ke = eq;- while (ke > k && ke[-1] == ' ') ke--;- *ke = 0;- while (*v == ' ') v++;- char *loc = NULL, *br = strchr(k, '[');- if (br) { *br = 0; loc = br + 1; char *cb = strchr(loc, ']'); if (cb) *cb = 0; char *at = strchr(loc, '@'); if (at) *at = 0; char *dot = strchr(loc, '.'); if (dot) *dot = 0; }+ if (line[0] == '[') {+ static const char pre[] = "[Desktop Action ";+ size_t len = strlen(line);+ in_main = !strcmp(line, "[Desktop Entry]");+ act = NULL;+ if (!strncmp(line, pre, sizeof pre - 1) && len > sizeof pre && line[len - 1] == ']') {+ line[len - 1] = 0;+ const char *name = line + sizeof pre - 1;+ int dup = 0;+ for (int i = 0; i < e->nact; i++) if (!strcmp(e->act[i].name, name)) dup = 1;+ if (valid_action(name) && !dup && e->nact < APPS_MAX_ACTIONS) {+ act = &e->act[e->nact++];+ snprintf(act->name, sizeof act->name, "%s", name);+ }+ }+ continue;+ }+ if ((!in_main && !act) || line[0] == '#' || !line[0]) continue;+ char *k, *loc, *v;+ if (!split_kv(line, &k, &loc, &v)) continue;+ if (act) {+ if (!strcmp(k, "Name")) {+ int r = locale_rank(loc, lang);+ if ((r || !loc) && r >= act->rank) { act->rank = r; snprintf(act->label, sizeof act->label, "%s", v); act->has_label = 1; }+ } else if (!loc && !strcmp(k, "Exec")) snprintf(act->exec, sizeof act->exec, "%s", v);+ continue;+ }if (!strcmp(k, "Name")) { set_tr(e, 0, e->a.name, sizeof e->a.name, v, loc, lang); e->has_name = 1; }else if (!strcmp(k, "GenericName")) set_tr(e, 1, e->a.generic, sizeof e->a.generic, v, loc, lang);else if (!strcmp(k, "Comment")) set_tr(e, 2, e->a.comment, sizeof e->a.comment, v, loc, lang);@@ -127,9 +183,11 @@ static int parse_desktop(const char *file, const char *lang, Entry *e) {else if (!strcmp(k, "Hidden")) e->hidden = !strcmp(v, "true");else if (!strcmp(k, "NoDisplay")) e->nodisplay = !strcmp(v, "true");else if (!strcmp(k, "Terminal")) e->terminal = !strcmp(v, "true");+ else if (!strcmp(k, "Actions")) snprintf(e->actions, sizeof e->actions, "%s", v);}fclose(f);unescape(e->a.name); unescape(e->a.generic); unescape(e->a.comment);+ for (int i = 0; i < e->nact; i++) unescape(e->act[i].label);snprintf(e->a.file, sizeof e->a.file, "%s", file);return 0;}@@ -601,8 +659,64 @@ static FilesPlan *view_plan(const char *cwd) {static char last_error[64];const char *apps_last_error(void) { return last_error; }-static int launch(const char *id, const char *file);-int apps_launch(const char *id) { return launch(id, NULL); }+static int launch(const char *id, const char *file, const char *action);+int apps_launch(const char *id) { return launch(id, NULL, NULL); }++/* A desktop action of the entry that may run: listed in its Actions= key, with a valid name, a+ * label and an Exec line. NULL otherwise. */+static const Action *entry_action(const Entry *e, const char *name) {+ if (!name || !valid_action(name) || !list_has(e->actions, name)) return NULL;+ for (int i = 0; i < e->nact; i++)+ if (!strcmp(e->act[i].name, name)) return e->act[i].has_label && e->act[i].exec[0] ? &e->act[i] : NULL;+ return NULL;+}++/* The listed app with that id, or NULL (the same lookup as the launcher). */+static const DesktopApp *find_app(const char *id) {+ if (!valid_id(id)) return NULL;+ const DesktopApp *list;+ int n = apps_list(cached_lang[0] ? cached_lang : "en", &list);+ for (int i = 0; i < n; i++) if (!strcmp(list[i].id, id)) return &list[i];+ return NULL;+}++int apps_actions(const char *id, const char *lang, AppAction *out, int max) {+ const DesktopApp *a = find_app(id);+ if (!a || max <= 0) return 0;+ Entry *e = malloc(sizeof *e);+ if (!e) return 0;+ int n = 0;+ if (!parse_desktop(a->file, lang && lang[0] ? lang : "en", e)) {+ /* in the order of the Actions= key */+ for (const char *p = e->actions; *p && n < max;) {+ const char *q = strchr(p, ';');+ size_t len = q ? (size_t)(q - p) : strlen(p);+ char name[APPS_ACTION_NAME_MAX];+ if (len && len < sizeof name) {+ memcpy(name, p, len);+ name[len] = 0;+ const Action *act = entry_action(e, name);+ int dup = 0;+ for (int i = 0; i < n; i++) if (!strcmp(out[i].name, name)) dup = 1;+ if (act && !dup) {+ snprintf(out[n].name, sizeof out[n].name, "%s", act->name);+ snprintf(out[n].label, sizeof out[n].label, "%s", act->label);+ n++;+ }+ }+ if (!q) break;+ p = q + 1;+ }+ }+ free(e);+ return n;+}++int apps_launch_action(const char *id, const char *action) {+ last_error[0] = 0;+ if (!action || !valid_action(action)) return -1;+ return launch(id, NULL, action);+}/* Opening a file with an app: only an app that declares MimeType (accepts files), a real* path inside the home folder or /media/$USER (fsops_allowed), a regular file or folder,@@ -615,16 +729,16 @@ int apps_launch_with(const char *id, const char *path) {* -1 if that is one of LordOS's own apps (the theme opens those) or there is none */char def[160];if (id && !id[0]) { if (defaults_app_for(real, def, sizeof def)) return -1; id = def; }- return launch(id, real);+ return launch(id, real, NULL);}-static int launch(const char *id, const char *file) {+/* action: the name of one of the entry's desktop actions (apps_launch_action), whose Exec line+ * replaces the main one; never together with a file. Everything else (the id, the listing filters,+ * the scope, the restrictions) is the same as a plain launch. */+static int launch(const char *id, const char *file, const char *action) {last_error[0] = 0;- if (!valid_id(id)) return -1;- const DesktopApp *list;- int n = apps_list(cached_lang[0] ? cached_lang : "en", &list);- const DesktopApp *a = NULL;- for (int i = 0; i < n; i++) if (!strcmp(list[i].id, id)) { a = &list[i]; break; }+ if (file && action) return -1;+ const DesktopApp *a = find_app(id);if (!a) return -1;/* the file is read again: what counts is what it says now */Entry *e = malloc(sizeof *e);@@ -633,8 +747,13 @@ static int launch(const char *id, const char *file) {char prog[PATH_MAX];int used = 0;int ok = e && buf && !parse_desktop(a->file, cached_lang, e) && e->is_app && !e->terminal && !e->hidden &&- (!file || e->a.mime[0]) &&- split_exec(e->exec, e, argv, buf, 4096, file, &used) > 0 && (!file || used) && !find_program(argv[0], a->user, prog, sizeof prog);+ (!file || e->a.mime[0]);+ const char *exec = e ? e->exec : NULL;+ if (ok && action) {+ const Action *act = entry_action(e, action);+ if (act) exec = act->exec; else ok = 0;+ }+ ok = ok && split_exec(exec, e, argv, buf, 4096, file, &used) > 0 && (!file || used) && !find_program(argv[0], a->user, prog, sizeof prog);char cwd[PATH_MAX] = "";if (ok && e->path[0] == '/') snprintf(cwd, sizeof cwd, "%s", e->path);free(e);diff --git a/server/src/apps.h b/server/src/apps.hindex 0f9a27c..3ca33d1 100644--- a/server/src/apps.h+++ b/server/src/apps.h@@ -26,6 +26,20 @@ int apps_launch(const char *id);/* Same, but opening a file or folder (see apps.c: only apps with MimeType and paths in the* home folder or the user's mounts, as a single argument). */int apps_launch_with(const char *id, const char *path);+/* Desktop actions ("Actions=" + "[Desktop Action <name>]", e.g. a browser's "New Private Window"):+ * only those listed in Actions=, with a [A-Za-z0-9-] name, a Name and an Exec line. */+#define APPS_MAX_ACTIONS 16+#define APPS_ACTION_NAME_MAX 64+typedef struct {+ char name[APPS_ACTION_NAME_MAX]; /* the action's identifier */+ char label[160]; /* its Name, translated for lang */+} AppAction;+/* Lists the desktop actions of the listed app with that id (up to max). Returns the count. */+int apps_actions(const char *id, const char *lang, AppAction *out, int max);+/* Launches one of them: the same launcher as apps_launch (same id rules, scope, restrictions, no+ * shell, no caller arguments), with that action's Exec line instead of the main one. The action+ * must be listed by apps_actions. 0 if the process started. */+int apps_launch_action(const char *id, const char *action);/* Why the last apps_launch/apps_launch_with refused, as an error code ("" = no specific reason). */const char *apps_last_error(void);diff --git a/server/src/ui/js_sys.c b/server/src/ui/js_sys.cindex 844017a..ccaa25b 100644--- a/server/src/ui/js_sys.c+++ b/server/src/ui/js_sys.c@@ -1414,6 +1414,45 @@ static JSValue f_apps_launch(JSContext *c, JSValueConst this_val, int argc, JSVareturn JS_NewBool(c, ok);}+/* lord.apps.actions(id, language) → [{ id, name }]: the desktop actions of an installed .desktop+ * (apps.c, apps_actions); [] for an unknown id. */+static JSValue f_apps_actions(JSContext *c, JSValueConst this_val, int argc, JSValueConst *argv) {+ (void)this_val;+ JSValue arr = JS_NewArray(c);+ const char *id = argc > 0 && JS_IsString(argv[0]) ? JS_ToCString(c, argv[0]) : NULL;+ if (!id) return arr;+ const char *lang = argc > 1 && JS_IsString(argv[1]) ? JS_ToCString(c, argv[1]) : NULL;+ char l[32] = "en";+ if (lang) {+ if (strlen(lang) < sizeof l && valid_id_dot(lang)) snprintf(l, sizeof l, "%s", lang);+ JS_FreeCString(c, lang);+ }+ AppAction acts[APPS_MAX_ACTIONS];+ int n = apps_actions(id, l, acts, APPS_MAX_ACTIONS);+ JS_FreeCString(c, id);+ for (int i = 0; i < n; i++) {+ JSValue o = JS_NewObject(c);+ JS_SetPropertyStr(c, o, "id", JS_NewString(c, acts[i].name));+ JS_SetPropertyStr(c, o, "name", JS_NewString(c, acts[i].label));+ JS_SetPropertyUint32(c, arr, (uint32_t)i, o);+ }+ return arr;+}++/* lord.apps.launchAction(id, action): runs one desktop action of an installed .desktop, through+ * the same launcher as lord.apps.launch (apps_launch_action): only an action the file lists,+ * never caller arguments or commands. */+static JSValue f_apps_launch_action(JSContext *c, JSValueConst this_val, int argc, JSValueConst *argv) {+ (void)this_val;+ if (argc < 2 || !JS_IsString(argv[0]) || !JS_IsString(argv[1])) return JS_FALSE;+ const char *id = JS_ToCString(c, argv[0]);+ const char *act = JS_ToCString(c, argv[1]);+ int ok = id && act && apps_launch_action(id, act) == 0;+ if (id) JS_FreeCString(c, id);+ if (act) JS_FreeCString(c, act);+ return JS_NewBool(c, ok);+}+/* lord.apps.lastError(): the error code of the last refused launch ("" if none), no arguments */static JSValue f_apps_last_error(JSContext *c, JSValueConst this_val, int argc, JSValueConst *argv) {(void)this_val; (void)argc; (void)argv;@@ -1796,6 +1835,8 @@ static const JSCFunctionListEntry apps_funcs[] = {JS_CFUNC_DEF("linux", 1, f_apps_linux),JS_CFUNC_DEF("launch", 1, f_apps_launch),JS_CFUNC_DEF("launchWith", 2, f_apps_launch_with),+ JS_CFUNC_DEF("actions", 2, f_apps_actions),+ JS_CFUNC_DEF("launchAction", 2, f_apps_launch_action),JS_CFUNC_DEF("lastError", 0, f_apps_last_error),JS_CFUNC_DEF("packages", 0, f_apps_packages),JS_CFUNC_DEF("inspectPackage", 1, f_apps_inspect),diff --git a/server/tests/apps_actions_test.c b/server/tests/apps_actions_test.cnew file mode 100644index 0000000..56e8c4e--- /dev/null+++ b/server/tests/apps_actions_test.c@@ -0,0 +1,99 @@+/* apps_actions_test.c — desktop actions of the Linux app launcher (src/apps.c): which+ * "[Desktop Action …]" groups are listed, their translated names, and that apps_launch_action only+ * runs an action the .desktop lists (with its own Exec line, through the normal launcher).+ *+ * Uses a temporary HOME with one .desktop in ~/.local/share/applications whose actions touch+ * marker files. Outside a session the launched process stays in the caller's cgroup. */+#include "apps.h"+#include "fsops.h"+#include "fw/rules.h"+#include "sec/broker.h"+#include "sec/perms.h"+#include "sec/restrict.h"++#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <time.h>+#include <unistd.h>++void brokersrv_revoke(const char *app, const char *perm) { (void)app; (void)perm; }+PermState perm_get(const char *app_id, Perm p) { (void)app_id; (void)p; return PERM_ASK; }++static int n, bad;+#define CHECK(name, cond) do { n++; if (!(cond)) { bad++; printf("✗ %s\n", name); } } while (0)++static int appeared(const char *path) {+ for (int i = 0; i < 100; i++) {+ if (!access(path, F_OK)) return 1;+ struct timespec ts = { 0, 20 * 1000 * 1000 };+ nanosleep(&ts, NULL);+ }+ return 0;+}++int main(void) {+ char home[] = "/tmp/lord-actions-XXXXXX";+ if (!mkdtemp(home)) { perror("mkdtemp"); return 1; }+ setenv("HOME", home, 1);+ unsetenv("LORDOS_SESSIOND");+ char path[512];+ const char *dirs[] = { "%s/.local", "%s/.local/share", "%s/.local/share/applications", NULL };+ for (int i = 0; dirs[i]; i++) { snprintf(path, sizeof path, dirs[i], home); mkdir(path, 0700); }+ snprintf(path, sizeof path, "%s/.local/share/applications/lord-demo.desktop", home);+ FILE *f = fopen(path, "w");+ if (!f) { perror(path); return 1; }+ fprintf(f,+ "[Desktop Entry]\nType=Application\nName=Demo\nExec=/usr/bin/touch %s/main\n"+ "Actions=new-window;private;bad_name;noexec;nolabel;new-window;\n\n"+ "[Desktop Action new-window]\nName=New Window\nName[es]=Nueva ventana\nExec=/usr/bin/touch %s/act-new\n\n"+ "[Desktop Action private]\nName=Private\\sWindow\nExec=/usr/bin/touch %s/act-private\n\n"+ "[Desktop Action bad_name]\nName=Bad\nExec=/usr/bin/touch %s/act-bad\n\n"+ "[Desktop Action noexec]\nName=No exec\n\n"+ "[Desktop Action nolabel]\nExec=/usr/bin/touch %s/act-nolabel\n\n"+ "[Desktop Action unlisted]\nName=Unlisted\nExec=/usr/bin/touch %s/act-unlisted\n",+ home, home, home, home, home, home);+ fclose(f);+ restrict_set_store(fw_store_new());++ AppAction acts[APPS_MAX_ACTIONS];+ int k = apps_actions("lord-demo", "en", acts, APPS_MAX_ACTIONS);+ CHECK("two valid listed actions", k == 2);+ CHECK("first is new-window", k > 0 && !strcmp(acts[0].name, "new-window") && !strcmp(acts[0].label, "New Window"));+ CHECK("second is private, unescaped", k > 1 && !strcmp(acts[1].name, "private") && !strcmp(acts[1].label, "Private Window"));+ k = apps_actions("lord-demo", "es-AR", acts, APPS_MAX_ACTIONS);+ CHECK("translated label", k > 0 && !strcmp(acts[0].label, "Nueva ventana"));+ CHECK("max is honoured", apps_actions("lord-demo", "en", acts, 1) == 1);+ CHECK("unknown app: none", apps_actions("lord-nope", "en", acts, APPS_MAX_ACTIONS) == 0);+ CHECK("invalid id: none", apps_actions("../lord-demo", "en", acts, APPS_MAX_ACTIONS) == 0);++ CHECK("unlisted action refused", apps_launch_action("lord-demo", "unlisted") != 0);+ CHECK("invalid action name refused", apps_launch_action("lord-demo", "bad_name") != 0);+ CHECK("action without Exec refused", apps_launch_action("lord-demo", "noexec") != 0);+ CHECK("action without Name refused", apps_launch_action("lord-demo", "nolabel") != 0);+ CHECK("path-like action refused", apps_launch_action("lord-demo", "../new-window") != 0);+ CHECK("empty action refused", apps_launch_action("lord-demo", "") != 0);+ CHECK("NULL action refused", apps_launch_action("lord-demo", NULL) != 0);+ CHECK("unknown app refused", apps_launch_action("lord-nope", "private") != 0);+ CHECK("action on a path id refused", apps_launch_action("../x", "private") != 0);++ CHECK("listed action launches", apps_launch_action("lord-demo", "private") == 0);+ snprintf(path, sizeof path, "%s/act-private", home);+ CHECK("it ran that action's Exec", appeared(path));+ snprintf(path, sizeof path, "%s/main", home);+ CHECK("not the main Exec", access(path, F_OK) != 0);+ snprintf(path, sizeof path, "%s/act-unlisted", home);+ CHECK("the unlisted action never ran", access(path, F_OK) != 0);+ snprintf(path, sizeof path, "%s/act-bad", home);+ CHECK("the badly named action never ran", access(path, F_OK) != 0);+ CHECK("plain launch still works", apps_launch("lord-demo") == 0);+ snprintf(path, sizeof path, "%s/main", home);+ CHECK("main Exec ran", appeared(path));++ char cmd[600];+ snprintf(cmd, sizeof cmd, "rm -rf '%s'", home);+ if (system(cmd)) {}+ printf(bad ? "apps-actions: %d of %d failed\n" : "apps-actions: %d/%d ok\n", bad ? bad : n, n);+ return bad ? 1 : 0;+}diff --git a/server/tests/dock_model_test.mjs b/server/tests/dock_model_test.mjsnew file mode 100644index 0000000..131518b--- /dev/null+++ b/server/tests/dock_model_test.mjs@@ -0,0 +1,124 @@+// dock_model_test.mjs — the Dock's pinned-list model (sdk/dockmodel.js): typed ids, migration of+// untyped lists, move/pin/unpin edge cases, hidden (uninstalled) items and quick actions.+//+// node tests/dock_model_test.mjs (from server/; run_suites.sh runs it when node exists)+import path from 'node:path';+import { fileURLToPath } from 'node:url';++const here = path.dirname(fileURLToPath(import.meta.url));+const M = await import(path.join(here, '../../sdk/dockmodel.js'));+const { parseDockId, dockId, toTypedId, normalizePinned, moveItem, pinItem, unpinItem, cleanActions } = M;++let n = 0, bad = 0;+const eq = (name, got, want) => {+ n++;+ const g = JSON.stringify(got), w = JSON.stringify(want);+ if (g !== w) { bad++; console.log(`✗ ${name}: want ${w}, got ${g}`); }+};++// ---- typed ids ----+eq('parse app', parseDockId('app:files'), { kind: 'app', key: 'files' });+eq('parse lpk', parseDockId('lpk:hello-world'), { kind: 'lpk', key: 'hello-world' });+eq('parse desktop', parseDockId('desktop:org.mozilla.firefox'), { kind: 'desktop', key: 'org.mozilla.firefox' });+eq('parse desktop underscore', parseDockId('desktop:chromium_browser'), { kind: 'desktop', key: 'chromium_browser' });+for (const bad of ['files', 'app:', ':files', 'app:Files', 'app:-x', 'lpk:a', 'lpk:Up', 'lpk:a_b', 'desktop:..x', 'desktop:a..b',+ 'desktop:.hidden', 'desktop:-x', 'desktop:lpk-notes', 'desktop:a/b', 'desktop:a b', 'desktop:' + 'x'.repeat(63), 'linux:foot',+ 'web:x', 'app:files:x', 'APP:files', null, 42, undefined, {}]) eq(`parse rejects ${JSON.stringify(bad)}`, parseDockId(bad), null);+eq('dockId ok', dockId('lpk', 'notes'), 'lpk:notes');+eq('dockId bad kind', dockId('web', 'x'), null);+eq('dockId bad key', dockId('desktop', '../x'), null);+eq('desktop 62 chars ok', parseDockId('desktop:' + 'x'.repeat(62))?.kind, 'desktop');++// ---- migration of untyped lists ----+const classify = (id) => ({ files: 'app', terminal: 'app', hello: 'lpk', notes: 'lpk' })[id] || null;+eq('typed stays', toTypedId('desktop:firefox', classify), 'desktop:firefox');+eq('untyped native', toTypedId('files', classify), 'app:files');+eq('untyped package', toTypedId('hello', classify), 'lpk:hello');+eq('untyped unknown → app', toTypedId('gone', classify), 'app:gone');+eq('untyped invalid dropped', toTypedId('Bad Id', classify), null);+eq('linux: dropped', toTypedId('linux:foot', classify), null);+eq('migrate', normalizePinned(['files', 'desktop:firefox', 'hello', 'linux:x', 'files', 'app:files', 'terminal', 7, ''], classify),+ { list: ['app:files', 'desktop:firefox', 'lpk:hello', 'app:terminal'], changed: true });+eq('already typed: unchanged', normalizePinned(['app:files', 'lpk:notes'], classify), { list: ['app:files', 'lpk:notes'], changed: false });+eq('not a list', normalizePinned('files', classify), { list: [], changed: true });+eq('null (theme defaults)', normalizePinned(null, classify), { list: [], changed: false });+eq('empty', normalizePinned([], classify), { list: [], changed: false });++// ---- move ----+const L = ['app:a', 'app:b', 'app:c', 'app:d'];+eq('move to front', moveItem(L, 'app:c', 0), ['app:c', 'app:a', 'app:b', 'app:d']);+eq('move to end', moveItem(L, 'app:a', 3), ['app:b', 'app:c', 'app:d', 'app:a']);+eq('move past end', moveItem(L, 'app:a', 99), ['app:b', 'app:c', 'app:d', 'app:a']);+eq('move negative', moveItem(L, 'app:d', -5), ['app:d', 'app:a', 'app:b', 'app:c']);+eq('move same place keeps identity', moveItem(L, 'app:b', 1) === L, true);+eq('move unknown id', moveItem(L, 'app:zz', 0) === L, true);+eq('move fractional index', moveItem(L, 'app:d', 0.6), ['app:a', 'app:d', 'app:b', 'app:c']);+eq('move NaN → end', moveItem(L, 'app:a', NaN), ['app:b', 'app:c', 'app:d', 'app:a']);+eq('move single', moveItem(['app:a'], 'app:a', 0), ['app:a']);+// hidden items (uninstalled) keep their place and don't count+const H = ['app:a', 'lpk:gone', 'app:b', 'app:c'];+const shown = (id) => id !== 'lpk:gone';+eq('move with hidden: index counts shown only', moveItem(H, 'app:c', 1, shown), ['app:a', 'lpk:gone', 'app:c', 'app:b']);+eq('move with hidden to front', moveItem(H, 'app:b', 0, shown), ['app:b', 'app:a', 'lpk:gone', 'app:c']);+eq('move with hidden to end', moveItem(H, 'app:a', 2, shown), ['lpk:gone', 'app:b', 'app:c', 'app:a']);++// ---- pin ----+eq('pin at end', pinItem(L, 'desktop:firefox'), [...L, 'desktop:firefox']);+eq('pin at index', pinItem(L, 'lpk:notes', 1), ['app:a', 'lpk:notes', 'app:b', 'app:c', 'app:d']);+eq('pin at 0', pinItem(L, 'lpk:notes', 0), ['lpk:notes', ...L]);+eq('pin past end', pinItem(L, 'lpk:notes', 50), [...L, 'lpk:notes']);+eq('pin already pinned, no index: unchanged', pinItem(L, 'app:b') === L, true);+eq('pin already pinned, with index: moves', pinItem(L, 'app:d', 0), ['app:d', 'app:a', 'app:b', 'app:c']);+eq('pin invalid id refused', pinItem(L, 'files') === L, true);+eq('pin linux: refused', pinItem(L, 'linux:foot', 0) === L, true);+eq('pin into empty', pinItem([], 'app:files', 3), ['app:files']);+eq('pin with hidden', pinItem(H, 'app:x', 1, shown), ['app:a', 'lpk:gone', 'app:x', 'app:b', 'app:c']);++// ---- unpin ----+eq('unpin', unpinItem(L, 'app:b'), ['app:a', 'app:c', 'app:d']);+eq('unpin missing keeps identity', unpinItem(L, 'app:zz') === L, true);+eq('unpin last', unpinItem(['app:a'], 'app:a'), []);++// ---- locked items (launcher, Files, Settings) ----+const { LOCKED, LAUNCHER, ensureItems, isLocked } = M;+eq('launcher id parses', parseDockId(LAUNCHER), { kind: 'shell', key: 'launcher' });+eq('only one shell item', parseDockId('shell:trash'), null);+eq('locked set', LOCKED, ['app:files', 'shell:launcher', 'app:settings']);+eq('locked: files', isLocked('app:files'), true);+eq('not locked: terminal', isLocked('app:terminal'), false);+eq('unpin files refused', unpinItem(['app:files', 'shell:launcher', 'app:x'], 'app:files'), ['app:files', 'shell:launcher', 'app:x']);+eq('unpin launcher refused', unpinItem(['shell:launcher', 'app:x'], 'shell:launcher'), ['shell:launcher', 'app:x']);+eq('unpin settings refused', unpinItem(['app:settings'], 'app:settings'), ['app:settings']);+eq('launcher can move', moveItem(['app:files', 'shell:launcher', 'app:x'], 'shell:launcher', 2), ['app:files', 'app:x', 'shell:launcher']);+eq('settings can move', moveItem(['app:files', 'app:x', 'app:settings'], 'app:settings', 1), ['app:files', 'app:settings', 'app:x']);+// Files is first and fixed+const F = ['app:files', 'app:a', 'app:b'];+eq('files never moves', moveItem(F, 'app:files', 2) === F, true);+eq('nothing before files (move)', moveItem(F, 'app:b', 0), ['app:files', 'app:b', 'app:a']);+eq('nothing before files (pin)', pinItem(F, 'lpk:notes', 0), ['app:files', 'lpk:notes', 'app:a', 'app:b']);+eq('pin files again at the end keeps it first', pinItem(F, 'app:files', 2) === F, true);+eq('files put back in front by ensure', ensureItems(['app:a', 'app:files'], ['app:files'], []), ['app:files', 'app:a']);+eq('isMovable', [M.isMovable('app:files'), M.isMovable('shell:launcher'), M.isMovable('app:settings'), M.isMovable('desktop:x')], [false, true, true, true]);+const DEF = ['app:files', 'shell:launcher', 'app:terminal', 'app:settings', 'app:about'];+eq('ensure: old list without any', ensureItems(['app:terminal', 'desktop:firefox'], LOCKED, DEF),+ ['app:files', 'shell:launcher', 'app:terminal', 'app:settings', 'desktop:firefox']);+eq('ensure: settings after its default predecessor', ensureItems(['app:files', 'shell:launcher', 'app:about', 'app:terminal'], LOCKED, DEF),+ ['app:files', 'shell:launcher', 'app:about', 'app:terminal', 'app:settings']);+eq('ensure: complete list unchanged', ensureItems(DEF, LOCKED, DEF), DEF);+eq('ensure: moved launcher stays', ensureItems(['app:files', 'app:settings', 'shell:launcher'], LOCKED, DEF), ['app:files', 'app:settings', 'shell:launcher']);+eq('ensure: empty list', ensureItems([], LOCKED, DEF), ['app:files', 'shell:launcher', 'app:settings']);+eq('ensure: not in defaults → end', ensureItems(['app:x'], ['app:y'], []), ['app:x', 'app:y']);++// ---- quick actions ----+eq('actions ok', cleanActions([{ id: 'network', label: 'settings.section.network', args: { section: 'network' } }]),+ [{ id: 'network', label: 'settings.section.network', args: { section: 'network' }, new: false }]);+eq('actions drop malformed', cleanActions([+ { id: '', label: 'x' }, { id: 'a b', label: 'x' }, { id: 'ok', label: '' }, { id: 'nested', label: 'x', args: { a: { b: 1 } } },+ { id: 'arr', label: 'x', args: [1] }, { id: 'nonce', label: 'x', args: { nonce: 1 } }, { id: 'fn', label: 'x', args: { f: () => 1 } },+ null, 'str', { id: 'good', label: 'x', new: 1 }, { id: 'good', label: 'dup' },+]), [{ id: 'good', label: 'x', args: {}, new: true }]);+eq('actions not a list', cleanActions({ id: 'x' }), []);+eq('actions max', cleanActions(Array.from({ length: 60 }, (_, i) => ({ id: `a${i}`, label: 'x' }))).length, 40);++console.log(bad ? `dock-model: ${bad} of ${n} failed` : `dock-model: ${n}/${n} ok`);+process.exit(bad ? 1 : 0);diff --git a/server/tests/run_suites.sh b/server/tests/run_suites.shindex 348f25e..b47e29e 100644--- a/server/tests/run_suites.sh+++ b/server/tests/run_suites.sh@@ -150,6 +150,9 @@ EOF# the firewall rule sheet's parser (sdk/fwrule.js) against the same cases as build/fwdest-testif have node; then run "fwrule-input-test (sdk/fwrule.js)" node tests/fwrule_input_test.mjselse skip "fwrule-input-test" "node"; fi+ # the Dock's pinned-list model (sdk/dockmodel.js)+ if have node; then run "dock-model-test (sdk/dockmodel.js)" node tests/dock_model_test.mjs+ else skip "dock-model-test" "node"; fi}full() {