diff options
Diffstat (limited to 'desktop/modules/services/ServicesModule.qml')
| -rw-r--r-- | desktop/modules/services/ServicesModule.qml | 135 |
1 files changed, 135 insertions, 0 deletions
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 } + } + } +} |
