aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/services/ServicesModule.qml
blob: ffc25fdc7115b2837074b00a83aad1dbab5d9a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 }
        }
    }
}