aboutsummaryrefslogtreecommitdiffstats
path: root/appearance/Hypridle.qml
blob: e265b88b61baf9fcece1f86bbedbc4b59e306fbd (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 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 Singleton

import Quickshell
import Quickshell.Io
import QtQuick

// The hypridle side. Commands are fixed: only each listener's timeout and
// whether it is active are editable. Everything before the first listener is
// kept verbatim so the general block and the rationale comments survive.
Singleton {
    id: root

    readonly property string home: Quickshell.env("HOME")
    readonly property string confPath: `${home}/.config/hypr/hypridle.conf`

    property string prefix: ""
    property var listeners: []
    property string tail: ""

    function getField(body, key) {
        const m = body.match(new RegExp(`^\\s*${key}\\s*=\\s*(.+?)\\s*$`, "m"));
        return m ? m[1] : null;
    }

    // A disabled listener is written every line prefixed with "# ". Strip it
    // before reading fields.
    function uncomment(body) {
        return body.split("\n").map(l => l.replace(/^([ \t]*)#[ \t]?/, "$1")).join("\n");
    }

    // The closing brace is matched at the start of a line, because the dpms
    // command contains a brace mid-line and a non-greedy match would stop
    // inside it. A disabled listener has both its opening line and its closing
    // brace commented, so the closing pattern allows a leading "#". The first
    // listener's leading trivia is empty because its text is the prefix.
    function parse(text) {
        const re = /^([ \t]*)(#?)[ \t]*listener\s*\{([\s\S]*?)^[ \t]*#?[ \t]*\}/gm;
        const found = [];
        let firstStart = -1;
        let prevEnd = 0;
        let m;
        while ((m = re.exec(text)) !== null) {
            const start = m.index;
            if (firstStart < 0) { firstStart = start; prevEnd = start; }
            const leading = text.slice(prevEnd, start);
            const enabled = m[2] !== "#";
            const body = enabled ? m[3] : root.uncomment(m[3]);
            const t = root.getField(body, "timeout");
            found.push({
                leading: leading,
                timeout: t === null ? 0 : parseInt(t, 10),
                onTimeout: root.getField(body, "on-timeout") ?? "",
                onResume: root.getField(body, "on-resume") ?? "",
                enabled: enabled,
            });
            prevEnd = start + m[0].length;
        }
        if (firstStart < 0) return { prefix: text, listeners: [], tail: "" };
        return { prefix: text.slice(0, firstStart), listeners: found, tail: text.slice(prevEnd) };
    }

    function serialize(prefix, list, tail) {
        let out = prefix;
        for (const l of list) {
            const lines = ["listener {", `    timeout = ${l.timeout}`,
                           `    on-timeout = ${l.onTimeout}`];
            if (l.onResume) lines.push(`    on-resume = ${l.onResume}`);
            lines.push("}");
            const body = l.enabled ? lines.join("\n") : lines.map(x => "# " + x).join("\n");
            out += l.leading + body;
        }
        return out + tail;
    }

    function describe(l) {
        const cmd = l.onTimeout;
        if (cmd.indexOf("notify-send") === 0) return "notify before lock";
        if (cmd.indexOf("loginctl lock-session") >= 0) return "lock session";
        if (cmd.indexOf("dpms") >= 0) return "monitors off";
        if (cmd.indexOf("loginctl suspend") >= 0) return "suspend";
        return cmd;
    }

    function selftest(): string {
        const fixture = String.raw`general {
    lock_cmd = pidof hyprlock || hyprlock
}

# 10:00s - screen lock
listener {
    timeout = 600
    on-timeout = loginctl lock-session
}

# 10:30s - monitor off
listener {
    timeout = 630
    on-timeout = hyprctl dispatch "hl.dsp.dpms({ state = \"off\" })"
    on-resume = hyprctl dispatch "hl.dsp.dpms({ state = \"on\" })"
}
`;
        const p = root.parse(fixture);
        if (p.listeners.length !== 2) return `SELFTEST Hypridle FAIL: ${p.listeners.length} listeners`;
        if (p.listeners[0].timeout !== 600 || p.listeners[0].enabled !== true)
            return "SELFTEST Hypridle FAIL: first listener";
        if (p.listeners[1].onTimeout.indexOf(String.raw`state = \"off\"`) < 0)
            return "SELFTEST Hypridle FAIL: dpms command truncated";
        if (p.listeners[1].onResume.indexOf(String.raw`state = \"on\"`) < 0)
            return "SELFTEST Hypridle FAIL: dpms on-resume lost";
        if (root.serialize(p.prefix, p.listeners, p.tail) !== fixture)
            return "SELFTEST Hypridle FAIL: round-trip not byte-identical";
        const off = p.listeners.slice();
        off[1] = Object.assign({}, off[1], { enabled: false });
        const p2 = root.parse(root.serialize(p.prefix, off, p.tail));
        if (p2.listeners.length !== 2 || p2.listeners[1].enabled !== false ||
            p2.listeners[1].timeout !== 630)
            return "SELFTEST Hypridle FAIL: disabled listener not round-tripped";
        return "SELFTEST Hypridle PASS";
    }

    function refresh() {
        confProc.running = false;
        confProc.running = true;
    }

    Process {
        id: confProc
        command: ["cat", root.confPath]
        stdout: StdioCollector {
            onStreamFinished: {
                const p = root.parse(text);
                root.prefix = p.prefix;
                root.listeners = p.listeners;
                root.tail = p.tail;
            }
        }
    }

    FileView { id: confFile; path: root.confPath }

    function save() {
        if (root.prefix === "") return; // not loaded yet: writing would truncate the file
        confFile.setText(root.serialize(root.prefix, root.listeners, root.tail));
        Quickshell.execDetached(["sh", "-c", "pkill -x hypridle; setsid -f hypridle"]);
    }
}