diff options
Diffstat (limited to 'desktop/modules/services')
| -rw-r--r-- | desktop/modules/services/README.md | 41 | ||||
| -rw-r--r-- | desktop/modules/services/Services.js | 43 | ||||
| -rw-r--r-- | desktop/modules/services/ServicesModule.qml | 135 | ||||
| -rw-r--r-- | desktop/modules/services/ServicesPage.qml | 92 | ||||
| -rwxr-xr-x | desktop/modules/services/restart.sh | 56 | ||||
| -rw-r--r-- | desktop/modules/services/test_services.js | 46 |
6 files changed, 413 insertions, 0 deletions
diff --git a/desktop/modules/services/README.md b/desktop/modules/services/README.md new file mode 100644 index 0000000..982a6b8 --- /dev/null +++ b/desktop/modules/services/README.md @@ -0,0 +1,41 @@ +# services + +The drawer's Services tile: every quickshell shell, deck-it and the desktop's +daemons, one row each, with Start when stopped and Stop and Restart when +running. Their state is read every 2s while the page is open. Runtime only: +what starts at login is the settings window's Autostart page, deck-it's +settings are its four pages there. + +Shells are the directories in the repo holding a `shell.qml`, running when +`qs list --all` shows an instance on one. Start, Restart and Restart all run +`reload.sh`, wrapped by `restart.sh`, which then sends one notification +naming the shells that came back, or a critical one naming those that did +not. Back means a `qs` instance on the shell's config path under a pid it did +not have before, within 5s: the old instance can linger in the list for a +moment after `qs kill`. It is the process, not the config: a shell that starts +with a broken component counts as back. Stop is `qs kill -p <dir>`. The +desktop row has no Stop, since that shell draws the drawer; its Restart works +because every action is started with `Quickshell.execDetached`, outside the +desktop shell's process, so the script carries on and starts the new one. A +hung desktop shell cannot be restarted from here; `./reload.sh` from a +terminal remains the way out. + +deck-it's row goes through `DeckIt.control()` (`modules/deckit`): Start runs +the login command, Stop `deck-it stop`, Restart both. Its detail line is +`deck-it status`, and the buttons wait until it shows the result or 30s pass. + +The daemons are listed in `Services.js`: a label, a piece of the daemon's +`autostart.lua` line and its `pgrep`/`pkill` arguments. Start runs that +enabled line through `hyprctl dispatch hl.dsp.exec_cmd(...)`, as the Autostart +page's Try does, so a `[workspace]` rule applies as at login and the command +lives only in `autostart.lua`; with no enabled line the row says "not in +autostart" and offers no Start. Stop is `pkill`, then up to 5s for the +process to exit, which hyprsunset needs: it holds Hyprland's CTM manager until +it is gone, and a second one dies. Restart is Stop then Start. A `-f` pattern +brackets one letter (`[t]v-mpris\.py`) so it never matches the `sh -c` that +passes it to pgrep or pkill. + +The Hypridle and Hyprsunset settings pages restart their daemon to apply a +save; that is theirs, not a duplicate of these rows. + +Check: `node desktop/modules/services/test_services.js`. diff --git a/desktop/modules/services/Services.js b/desktop/modules/services/Services.js new file mode 100644 index 0000000..d9aab80 --- /dev/null +++ b/desktop/modules/services/Services.js @@ -0,0 +1,43 @@ +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +.pragma library + +// The daemons the drawer's Services page starts and stops, tested by +// test_services.js under node. +// +// Each is [label, a piece of its autostart.lua line, pgrep/pkill arguments]. +// Its start command is that enabled line, so the command lives only in +// autostart.lua. A -f pattern brackets one letter, so it never matches the +// sh -c that runs pgrep or pkill with the pattern as an argument. +const DAEMONS = [ + ["desktop-assistant", "desktop-assistant/assistant.py", ["-f", "desktop-assistant/[a]ssistant\\.py"]], + ["cal-notif", "cal-notif daemon", ["-f", "[c]al-notif daemon"]], + ["tv-mpris", "tv-mpris.py", ["-f", "[t]v-mpris\\.py"]], + ["mail-watcher", "mail-watcher.sh", ["-f", "[m]ail-watcher\\.sh"]], + ["mail-notify", "mail-notify.sh", ["-f", "[m]ail-notify\\.sh"]], + ["hypridle", "hypridle", ["-x", "hypridle"]], + ["hyprsunset", "hyprsunset", ["-x", "hyprsunset"]], + ["polkit agent", "hyprpolkitagent", ["-x", "hyprpolkitagent"]], + ["notifyd", "notifyd", ["-x", "notifyd"]], + ["playerctld", "playerctld", ["-x", "playerctld"]], + ["conky", "conky", ["-x", "conky"]], + ["clipboard history", "cliphist store", ["-f", "[w]l-paste --watch cliphist"]], +]; + +// [{label, args, start}] from Autostart.js's parsed entries: start is the +// first enabled line holding the daemon's piece, "" when there is none. +function daemons(entries) { + return DAEMONS.map(([label, piece, args]) => { + const e = entries.find(e => e.enabled && e.cmd.includes(piece)); + return { label, args, start: e ? e.cmd : "" }; + }); +} diff --git a/desktop/modules/services/ServicesModule.qml b/desktop/modules/services/ServicesModule.qml new file mode 100644 index 0000000..ffc25fd --- /dev/null +++ b/desktop/modules/services/ServicesModule.qml @@ -0,0 +1,135 @@ +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +import Quickshell +import Quickshell.Io +import QtQuick +import "../.." +import "../deckit" +import "../autostart/Autostart.js" as A +import "Services.js" as S + +// The drawer's Services: every quickshell shell, deck-it and the desktop's +// daemons, each with Start, Stop and Restart. Their state is read every 2s +// while the page is open. Started detached, so an action outlives the qs that +// started it: restarting the desktop shell from its own drawer works, the +// drawer just closes until the new process is up. +Module { + id: sm + name: "services" + label: "Services" + // nf-fa-refresh, present in Inconsolata Nerd Font. + icon: "" + keywords: ["services", "restart", "reload", "start", "stop", "quickshell", "qs", "shells", + "daemon", "deck-it", "process"] + + readonly property string root: Quickshell.shellDir + "/.." + // [{name, running}], one per directory holding a shell.qml. + property var shells: [] + // [{label, args, start, running}], Services.js's daemons. + property var daemons: [] + property int watchers: 0 + + // An empty name restarts every shell the page lists. A stopped shell + // restarts into a start: reload.sh only starts what is not running. + function restartShell(name) { + Quickshell.execDetached(["sh", Quickshell.shellDir + "/modules/services/restart.sh", sm.root] + .concat(name ? [name] : sm.shells.map(s => s.name))); + repoll.restart(); + } + + function stopShell(name) { + Quickshell.execDetached(["qs", "kill", "-p", sm.root + "/" + name]); + repoll.restart(); + } + + // Stop is pkill, then up to 5s for the process to go: hyprsunset holds + // Hyprland's CTM manager until it exits, and a second one dies. Start runs + // the autostart.lua line through Hyprland, so its [workspace] rule + // applies as at login. + function daemon(d, op) { + const start = op === "stop" || !d.start ? "" : 'hl.dsp.exec_cmd("' + A.quote(d.start) + '")'; + Quickshell.execDetached(["sh", "-c", + 'start=$1; kill=$2; shift 2; ' + + 'if [ "$kill" = 1 ]; then pkill "$@"; ' + + 'for i in $(seq 50); do pgrep "$@" >/dev/null || break; sleep 0.1; done; fi; ' + + '[ -z "$start" ] || hyprctl dispatch "$start"', + "sh", start, op === "start" ? "0" : "1"].concat(d.args)); + repoll.restart(); + } + + function refresh() { + shellProc.rows = []; + shellProc.running = false; + shellProc.running = true; + if (!daemonProc.running) daemonProc.running = true; + } + + // reload.sh waits a second before it returns; read again after that. + property Timer repoll: Timer { + interval: 2000 + onTriggered: sm.refresh() + } + property Timer poll: Timer { + interval: 2000 + repeat: true + running: sm.watchers > 0 + onTriggered: sm.refresh() + } + + property FileView autostart: FileView { + path: DeckIt.autostartFile + watchChanges: true + printErrors: false + onFileChanged: reload() + onLoaded: sm.refresh() + } + + // A shell is running when qs lists an instance on its config path. + property Process shellProc: Process { + property var rows: [] + command: ["sh", "-c", 'cd "$1" || exit 1; up=$(qs list --all 2>/dev/null); ' + + 'for f in */shell.qml; do case "$up" in ' + + '*"Config path: $PWD/$f"*) s=running ;; *) s=stopped ;; esac; ' + + 'printf "%s\\t%s\\n" "${f%/shell.qml}" "$s"; done', "sh", sm.root] + stdout: SplitParser { + onRead: line => { + const f = line.split("\t"); + if (f.length === 2) shellProc.rows.push({ name: f[0], running: f[1] === "running" }); + } + } + onExited: { + sm.shells = shellProc.rows; + sm.searchItems = shellProc.rows.map(s => ({ text: s.name, key: "shell:" + s.name })) + .concat(S.DAEMONS.map(x => ({ text: x[0], key: "daemon:" + x[0] }))); + } + } + + // One 1 or 0 per daemon, in Services.js's order; every daemon has a + // flag and a pattern. + property Process daemonProc: Process { + command: ["sh", "-c", 'while [ $# -gt 1 ]; do pgrep "$1" "$2" >/dev/null && echo 1 || echo 0; shift 2; done', + "sh"].concat([].concat(...S.DAEMONS.map(x => x[2]))) + stdout: StdioCollector { id: daemonOut } + onExited: { + const up = daemonOut.text.split("\n"); + sm.daemons = S.daemons(A.parse(sm.autostart.text())) + .map((d, i) => Object.assign(d, { running: up[i] === "1" })); + } + } + + page: Component { + Page { + title: "Services" + ServicesPage { width: parent.width; services: sm } + } + } +} diff --git a/desktop/modules/services/ServicesPage.qml b/desktop/modules/services/ServicesPage.qml new file mode 100644 index 0000000..cc392db --- /dev/null +++ b/desktop/modules/services/ServicesPage.qml @@ -0,0 +1,92 @@ +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +import QtQuick +import "../.." +import "../ai" +import "../deckit" +import "../qtmaildir" + +// Shells, deck-it and the daemons, one row each: Start when stopped, Stop and +// Restart when running. The desktop shell has no Stop: it draws this page. +Column { + id: page + + required property var services + + spacing: 8 + + Component.onCompleted: { page.services.watchers++; DeckIt.watchers++; page.services.refresh(); } + Component.onDestruction: { page.services.watchers--; DeckIt.watchers--; } + + Heading { text: "Shells" } + + AiRow { + width: page.width + title: "All shells" + detail: "Restarts every one, this drawer included" + on: true + action: "Restart all" + onTriggered: page.services.restartShell("") + } + + Repeater { + model: page.services.shells + + AiRow { + required property var modelData + readonly property bool self: modelData.name === "desktop" + objectName: "shell:" + modelData.name + width: page.width + title: modelData.name + on: modelData.running + detail: (modelData.running ? "Running" : "Stopped") + (self ? " · hosts this drawer" : "") + action: !modelData.running ? "Start" : self ? "" : "Stop" + restartable: modelData.running + onTriggered: modelData.running ? page.services.stopShell(modelData.name) + : page.services.restartShell(modelData.name) + onRestart: page.services.restartShell(modelData.name) + } + } + + Heading { text: "deck-it" } + + AiRow { + width: page.width + title: "deck-it" + detail: ({ start: "Starting…", stop: "Stopping…", restart: "Restarting…" })[DeckIt.busy] ?? DeckIt.summary + on: DeckIt.running + busy: DeckIt.busy !== "" + action: DeckIt.busy ? "" : DeckIt.running ? "Stop" : "Start" + restartable: DeckIt.running && !DeckIt.busy + onTriggered: DeckIt.control(DeckIt.running ? "stop" : "start") + onRestart: DeckIt.control("restart") + } + + Heading { text: "Daemons" } + + Repeater { + model: page.services.daemons + + AiRow { + required property var modelData + objectName: "daemon:" + modelData.label + width: page.width + title: modelData.label + on: modelData.running + detail: modelData.running ? "Running" : modelData.start ? "Stopped" : "Stopped · not in autostart" + action: modelData.running ? "Stop" : modelData.start ? "Start" : "" + restartable: modelData.running && modelData.start !== "" + onTriggered: page.services.daemon(modelData, modelData.running ? "stop" : "start") + onRestart: page.services.daemon(modelData, "restart") + } + } +} diff --git a/desktop/modules/services/restart.sh b/desktop/modules/services/restart.sh new file mode 100755 index 0000000..2975d41 --- /dev/null +++ b/desktop/modules/services/restart.sh @@ -0,0 +1,56 @@ +#!/bin/sh +# +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# restart.sh <repo> <shell>...: reload.sh on the named shells, then one +# notification saying which came back. A shell came back when qs lists an +# instance on its config path under a pid it did not have before, within 5s: +# the old instance can still be listed for a moment after qs kill, and a +# config that fails to load exits rather than lingering. +# +# ponytail: "came back" is the process, not the config. A shell that loads +# with a broken component still counts; its log says so. + +root=$(cd "$1" && pwd) || exit 1 +shift + +# The pids qs lists for <shell>'s config path, one per line. +pids() { + qs list --all 2>/dev/null | awk -v p="Config path: $root/$1/shell.qml" ' + /Process ID:/ { id = $3 } index($0, p) { print id }' +} + +old="" +for n; do old="$old $(pids "$n")"; done + +"$root/reload.sh" "$@" >/dev/null 2>&1 + +good="" +bad="" +for n; do + back="" + for _ in 1 2 3 4 5 6 7 8 9 10; do + for p in $(pids "$n"); do + case " $old " in *" $p "*) ;; *) back=1 ;; esac + done + [ -n "$back" ] && break + sleep 0.5 + done + if [ -n "$back" ]; then good="$good $n"; else bad="$bad $n"; fi +done + +if [ -n "$bad" ]; then + notify-send --app-name=Keystone --urgency=critical --icon=dialog-error \ + "Shells" "Did not come back:$bad${good:+. Restarted:$good}" +else + notify-send --app-name=Keystone --icon=dialog-information "Shells" "Restarted:$good" +fi diff --git a/desktop/modules/services/test_services.js b/desktop/modules/services/test_services.js new file mode 100644 index 0000000..9f9be4e --- /dev/null +++ b/desktop/modules/services/test_services.js @@ -0,0 +1,46 @@ +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Run: node desktop/modules/services/test_services.js +// Both files are QML libraries, so their .pragma lines are stripped first. + +const fs = require("fs"); +const assert = require("assert"); + +const lib = (f, names) => new Function(fs.readFileSync(f, "utf8").replace(/^\.pragma library$/m, "") + + `\nreturn { ${names} };`)(); +const A = lib(__dirname + "/../autostart/Autostart.js", "parse"); +const S = lib(__dirname + "/Services.js", "daemons, DAEMONS"); + +const entries = A.parse([ + 'hl.on("hyprland.start", function()', + ' hl.exec_cmd("[workspace special:dash silent] conky -c ~/.config/conky/conky.conf")', + ' -- hl.exec_cmd("hypridle")', + ' hl.exec_cmd("python3 -u ~/x/desktop-assistant/assistant.py >> \\"$XDG_RUNTIME_DIR/a.log\\" 2>&1")', + 'end)', +].join("\n")); + +const d = Object.fromEntries(S.daemons(entries).map(x => [x.label, x])); +assert.strictEqual(d.conky.start, "[workspace special:dash silent] conky -c ~/.config/conky/conky.conf"); +assert.strictEqual(d["desktop-assistant"].start, + 'python3 -u ~/x/desktop-assistant/assistant.py >> "$XDG_RUNTIME_DIR/a.log" 2>&1'); +assert.strictEqual(d.hypridle.start, ""); // commented out +assert.strictEqual(d.notifyd.start, ""); // absent +assert.deepStrictEqual(d.hypridle.args, ["-x", "hypridle"]); + +// Every -f pattern matches its line but not itself (the sh running pgrep). +for (const [label, piece, [flag, pat]] of S.DAEMONS) { + if (flag !== "-f") continue; + const re = new RegExp(pat); + assert.ok(!re.test(pat), label + " matches its own pattern"); + assert.ok(re.test(piece) || re.test("wl-paste --watch " + piece), label + " misses its line"); +} +console.log("services ok"); |
