mirror of
https://github.com/Sn3akyy1/lucid.git
synced 2026-11-19 21:27:56 +03:00
545 lines
17 KiB
QML
545 lines
17 KiB
QML
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
pragma Singleton
|
|
|
|
Singleton {
|
|
id: root
|
|
|
|
readonly property string marker: "# generated by lucid settings"
|
|
readonly property string confPath: Quickshell.env("HOME") + "/.config/hypr/hypridle.conf"
|
|
|
|
property bool probed: false
|
|
property bool installed: false
|
|
property bool running: false
|
|
property bool startsAtLogin: false
|
|
// filled by the probe, so the generated file names this machine's devices
|
|
property string backlight: ""
|
|
property string kbdBacklight: ""
|
|
property string acOnline: ""
|
|
property bool hasBattery: false
|
|
property string lastError: ""
|
|
// the settings page polls faster while it is on screen
|
|
property bool watching: false
|
|
|
|
readonly property bool active: Prefs.loaded && Prefs.idleEnabled && root.installed
|
|
readonly property bool paused: Prefs.idleKeepAwake
|
|
readonly property bool wanted: root.active && !root.paused
|
|
readonly property bool hasKbdBacklight: root.kbdBacklight !== ""
|
|
readonly property bool hasBacklight: root.backlight !== ""
|
|
|
|
readonly property string summary: {
|
|
if (!root.probed)
|
|
return "Looking for hypridle…";
|
|
|
|
if (!root.installed)
|
|
return "Not installed";
|
|
|
|
if (!Prefs.idleEnabled)
|
|
return "Turned off — this machine never goes idle on its own";
|
|
|
|
if (root.paused)
|
|
return "Paused — keep awake is on";
|
|
|
|
if (!root.running)
|
|
return "Turned on, but hypridle is not answering";
|
|
|
|
return root.stages.length > 0 ? "Watching — " + root.stages.length + (root.stages.length === 1 ? " step" : " steps") : "Watching, but nothing is set to happen";
|
|
}
|
|
|
|
// 30 s to 2 h, coarse enough that the slider lands on round numbers
|
|
readonly property var steps: [30, 60, 120, 180, 300, 600, 900, 1200, 1800, 2700, 3600, 5400, 7200]
|
|
readonly property var stepLabels: ["30 sec", "1 min", "2 min", "3 min", "5 min", "10 min", "15 min", "20 min", "30 min", "45 min", "1 hour", "90 min", "2 hours"]
|
|
|
|
function stepIndex(sec) {
|
|
var best = 0;
|
|
var dist = Math.abs(root.steps[0] - sec);
|
|
for (var i = 1; i < root.steps.length; i++) {
|
|
var d = Math.abs(root.steps[i] - sec);
|
|
if (d < dist) {
|
|
dist = d;
|
|
best = i;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
function stepAt(i) {
|
|
return root.steps[Math.max(0, Math.min(root.steps.length - 1, Math.round(i)))];
|
|
}
|
|
|
|
function durationText(sec) {
|
|
if (sec < 60)
|
|
return sec + " sec";
|
|
|
|
var m = Math.round(sec / 60);
|
|
if (m < 60)
|
|
return m + (m === 1 ? " min" : " min");
|
|
|
|
var h = m / 60;
|
|
return (h === Math.round(h) ? String(h) : h.toFixed(1)) + (h === 1 ? " hour" : " hours");
|
|
}
|
|
|
|
// the ladder, in the order it actually fires
|
|
readonly property var stages: {
|
|
var out = [];
|
|
if (Prefs.idleDim && root.hasBacklight)
|
|
out.push({
|
|
"key": "dim",
|
|
"name": "Dim",
|
|
"after": Prefs.idleDimAfter
|
|
});
|
|
|
|
if (Prefs.idleLock)
|
|
out.push({
|
|
"key": "lock",
|
|
"name": "Lock",
|
|
"after": Prefs.idleLockAfter
|
|
});
|
|
|
|
if (Prefs.idleScreenOff)
|
|
out.push({
|
|
"key": "screen",
|
|
"name": "Screen off",
|
|
"after": Prefs.idleScreenOffAfter
|
|
});
|
|
|
|
if (Prefs.idleSuspend)
|
|
out.push({
|
|
"key": "suspend",
|
|
"name": "Suspend",
|
|
"after": Prefs.idleSuspendAfter
|
|
});
|
|
|
|
return out;
|
|
}
|
|
|
|
// a step that fires before the one above it in the list never makes sense
|
|
readonly property bool outOfOrder: {
|
|
for (var i = 1; i < root.stages.length; i++) {
|
|
if (root.stages[i].after <= root.stages[i - 1].after)
|
|
return true;
|
|
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// hypridle hands every command to a shell, so a `||` chain is enough to
|
|
// skip one. it is only ever tested once, when the timer runs out
|
|
function guarded(cmd, allowAcSkip) {
|
|
var skips = [];
|
|
if (Prefs.idleWhileMedia)
|
|
skips.push("playerctl -as status 2>/dev/null | grep -qx Playing");
|
|
|
|
if (allowAcSkip && !Prefs.idleSuspendOnAc && root.acOnline !== "")
|
|
skips.push("grep -qx 1 " + root.acOnline);
|
|
|
|
return skips.length > 0 ? skips.join(" || ") + " || " + cmd : cmd;
|
|
}
|
|
|
|
readonly property string lockCmd: "qs ipc call lock lock"
|
|
readonly property string dpmsOn: "hyprctl dispatch 'hl.dsp.dpms({action = \"on\"})'"
|
|
readonly property string dpmsOff: "hyprctl dispatch 'hl.dsp.dpms({action = \"off\"})'"
|
|
|
|
function listener(timeout, onTimeout, onResume) {
|
|
var out = "listener {\n timeout = " + timeout + "\n on-timeout = " + onTimeout + "\n";
|
|
if (onResume !== "")
|
|
out += " on-resume = " + onResume + "\n";
|
|
|
|
return out + "}\n";
|
|
}
|
|
|
|
readonly property string conf: {
|
|
var out = root.marker + ". edits are overwritten.\n";
|
|
out += "# turn Idle off in Lucid Settings to take this file back.\n\n";
|
|
out += "general {\n";
|
|
out += " lock_cmd = " + root.lockCmd + "\n";
|
|
if (Prefs.idleLockBeforeSleep)
|
|
out += " before_sleep_cmd = " + root.lockCmd + "\n";
|
|
|
|
if (Prefs.idleWakeAfterSleep)
|
|
out += " after_sleep_cmd = " + root.dpmsOn + "\n";
|
|
|
|
out += " ignore_dbus_inhibit = " + (Prefs.idleRespectInhibitors ? "false" : "true") + "\n";
|
|
out += " ignore_systemd_inhibit = " + (Prefs.idleRespectInhibitors ? "false" : "true") + "\n";
|
|
out += "}\n";
|
|
if (Prefs.idleDim && root.hasBacklight) {
|
|
out += "\n";
|
|
out += root.listener(Prefs.idleDimAfter, root.guarded("brightnessctl -s set " + Prefs.idleDimLevel + "%", false), "brightnessctl -r");
|
|
if (Prefs.idleDimKeyboard && root.hasKbdBacklight)
|
|
out += root.listener(Prefs.idleDimAfter, root.guarded("brightnessctl -sd " + root.kbdBacklight + " set 0", false), "brightnessctl -rd " + root.kbdBacklight);
|
|
|
|
}
|
|
if (Prefs.idleLock)
|
|
out += "\n" + root.listener(Prefs.idleLockAfter, root.guarded(root.lockCmd, false), "");
|
|
|
|
if (Prefs.idleScreenOff)
|
|
out += "\n" + root.listener(Prefs.idleScreenOffAfter, root.guarded(root.dpmsOff, false), root.dpmsOn);
|
|
|
|
if (Prefs.idleSuspend)
|
|
out += "\n" + root.listener(Prefs.idleSuspendAfter, root.guarded("systemctl suspend", true), "");
|
|
|
|
return out;
|
|
}
|
|
|
|
// what hypridle was told last, so a reload does not restart it for nothing
|
|
property string applied: ""
|
|
|
|
function apply() {
|
|
if (!Prefs.loaded || !Prefs.idleAdopted || !root.installed)
|
|
return ;
|
|
|
|
// paused counts as off: the file stays, the daemon does not. a hypridle
|
|
// Lucid never started is left alone, so an untouched page kills nothing
|
|
if (!root.wanted) {
|
|
if (Prefs.idleEnabled || root.applied !== "") {
|
|
root.applied = "";
|
|
stopProc.running = false;
|
|
stopProc.running = true;
|
|
}
|
|
return ;
|
|
}
|
|
var want = root.conf;
|
|
if (want !== root.applied) {
|
|
root.applied = want;
|
|
confFile.setText(want);
|
|
}
|
|
runProc.running = false;
|
|
runProc.running = true;
|
|
}
|
|
|
|
Timer {
|
|
id: applyDebounce
|
|
|
|
interval: 700
|
|
repeat: false
|
|
onTriggered: root.apply()
|
|
}
|
|
|
|
onConfChanged: {
|
|
if (root.active)
|
|
applyDebounce.restart();
|
|
|
|
}
|
|
onActiveChanged: {
|
|
root.syncAutostart();
|
|
applyDebounce.restart();
|
|
}
|
|
onPausedChanged: {
|
|
root.syncAutostart();
|
|
applyDebounce.restart();
|
|
}
|
|
|
|
// one shell round-trip for everything the generated file has to name
|
|
Process {
|
|
id: probe
|
|
|
|
running: true
|
|
command: ["sh", "-c", "h=$(command -v hypridle || echo '');" + "bl=$(brightnessctl -m --list 2>/dev/null | awk -F, '$2==\"backlight\"{print $1; exit}');" + "kb=$(brightnessctl -m --list 2>/dev/null | awk -F, '$2==\"leds\" && $1 ~ /kbd_backlight/ {print $1; exit}');" + "ac=''; bat=false;" + "for d in /sys/class/power_supply/*; do [ -f \"$d/type\" ] || continue; t=$(cat \"$d/type\");" + "[ \"$t\" = Mains ] && [ -f \"$d/online\" ] && [ -z \"$ac\" ] && ac=\"$d/online\";" + "[ \"$t\" = Battery ] && [ -f \"$d/capacity\" ] && bat=true; done;" + "printf '%s\\n%s\\n%s\\n%s\\n%s\\n' \"$h\" \"$bl\" \"$kb\" \"$ac\" \"$bat\""]
|
|
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
var l = this.text.split("\n");
|
|
root.installed = (l[0] || "").trim() !== "";
|
|
root.backlight = (l[1] || "").trim();
|
|
root.kbdBacklight = (l[2] || "").trim();
|
|
root.acOnline = (l[3] || "").trim();
|
|
root.hasBattery = (l[4] || "").trim() === "true";
|
|
root.probed = true;
|
|
root.apply();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
Process {
|
|
id: statusProc
|
|
|
|
command: ["sh", "-c", "pgrep -x hypridle >/dev/null && echo yes || echo no;" + "systemctl --user is-enabled hypridle >/dev/null 2>&1 && echo yes || echo no"]
|
|
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
var l = this.text.split("\n");
|
|
root.running = (l[0] || "").trim() === "yes";
|
|
root.startsAtLogin = (l[1] || "").trim() === "yes";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
function refresh() {
|
|
statusProc.running = false;
|
|
statusProc.running = true;
|
|
}
|
|
|
|
Timer {
|
|
interval: root.watching ? 2500 : 30000
|
|
repeat: true
|
|
running: Prefs.loaded
|
|
triggeredOnStart: true
|
|
onTriggered: root.refresh()
|
|
}
|
|
|
|
// a stray hypridle started by hand would fight the unit, so clear it first
|
|
Process {
|
|
id: runProc
|
|
|
|
command: ["sh", "-c", "pkill -x hypridle 2>/dev/null; systemctl --user restart hypridle 2>&1"]
|
|
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
root.lastError = this.text.trim();
|
|
root.refresh();
|
|
settle.restart();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// systemctl returns before hypridle has claimed its name, so look again
|
|
Timer {
|
|
id: settle
|
|
|
|
interval: 1200
|
|
repeat: false
|
|
onTriggered: root.refresh()
|
|
}
|
|
|
|
Process {
|
|
id: stopProc
|
|
|
|
command: ["sh", "-c", "systemctl --user stop hypridle 2>/dev/null; pkill -x hypridle 2>/dev/null; exit 0"]
|
|
onExited: root.refresh()
|
|
}
|
|
|
|
Process {
|
|
id: loginProc
|
|
|
|
command: ["sh", "-c", "systemctl --user " + ((Prefs.idleAutostart && root.wanted) ? "enable" : "disable") + " hypridle 2>&1"]
|
|
|
|
stdout: StdioCollector {
|
|
onStreamFinished: root.refresh()
|
|
}
|
|
|
|
}
|
|
|
|
// deferred: a handler on `active` can still read the old `wanted`, and that
|
|
// is what the unit's enable/disable is decided from
|
|
function syncAutostart() {
|
|
if (!root.installed)
|
|
return ;
|
|
|
|
Qt.callLater(root.runAutostart);
|
|
}
|
|
|
|
function runAutostart() {
|
|
loginProc.running = false;
|
|
loginProc.running = true;
|
|
}
|
|
|
|
Connections {
|
|
function onIdleAutostartChanged() {
|
|
root.syncAutostart();
|
|
}
|
|
|
|
function onLoadedChanged() {
|
|
root.maybeAdopt();
|
|
}
|
|
|
|
target: Prefs
|
|
}
|
|
|
|
function restart() {
|
|
root.applied = "";
|
|
root.apply();
|
|
}
|
|
|
|
// the file was the user's before it was ours, so read their timings out of
|
|
// it once rather than quietly replacing them
|
|
function maybeAdopt() {
|
|
if (!Prefs.loaded || Prefs.idleAdopted || !confFile.ready)
|
|
return ;
|
|
|
|
var text = confFile.raw;
|
|
Prefs.idleAdopted = true;
|
|
if (text.trim() === "" || text.indexOf(root.marker) !== -1)
|
|
return ;
|
|
|
|
var lines = text.split("\n");
|
|
var inListener = false;
|
|
var timeout = -1;
|
|
var cmd = "";
|
|
var seen = {};
|
|
function flush() {
|
|
if (timeout > 0 && cmd !== "")
|
|
root.adoptOne(timeout, cmd, seen);
|
|
|
|
timeout = -1;
|
|
cmd = "";
|
|
}
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
var line = lines[i].split("#")[0].trim();
|
|
if (line === "")
|
|
continue;
|
|
|
|
if (line.indexOf("listener") === 0) {
|
|
inListener = true;
|
|
continue;
|
|
}
|
|
if (line.indexOf("general") === 0) {
|
|
inListener = false;
|
|
continue;
|
|
}
|
|
if (line.indexOf("}") === 0) {
|
|
if (inListener)
|
|
flush();
|
|
|
|
inListener = false;
|
|
continue;
|
|
}
|
|
var eq = line.indexOf("=");
|
|
if (eq < 0)
|
|
continue;
|
|
|
|
var key = line.substring(0, eq).trim();
|
|
var val = line.substring(eq + 1).trim();
|
|
if (inListener) {
|
|
if (key === "timeout")
|
|
timeout = parseInt(val, 10);
|
|
else if (key === "on-timeout")
|
|
cmd = val;
|
|
} else if (key === "before_sleep_cmd") {
|
|
Prefs.idleLockBeforeSleep = val !== "";
|
|
} else if (key === "after_sleep_cmd") {
|
|
Prefs.idleWakeAfterSleep = val !== "";
|
|
} else if (key === "ignore_dbus_inhibit" || key === "ignore_systemd_inhibit") {
|
|
if (val === "true")
|
|
Prefs.idleRespectInhibitors = false;
|
|
|
|
}
|
|
}
|
|
flush();
|
|
// anything the old file did not ask for is off, not left at its default
|
|
var all = ["dim", "lock", "screen", "suspend"];
|
|
for (var j = 0; j < all.length; j++) {
|
|
if (!seen[all[j]])
|
|
root.adoptOff(all[j]);
|
|
|
|
}
|
|
}
|
|
|
|
function adoptOne(timeout, cmd, seen) {
|
|
var t = root.stepAt(root.stepIndex(timeout));
|
|
if (cmd.indexOf("kbd_backlight") !== -1) {
|
|
Prefs.idleDimKeyboard = true;
|
|
return ;
|
|
}
|
|
if (cmd.indexOf("brightnessctl") !== -1) {
|
|
seen.dim = true;
|
|
Prefs.idleDim = true;
|
|
Prefs.idleDimAfter = t;
|
|
var m = cmd.match(/set\s+(\d+)/);
|
|
if (m)
|
|
Prefs.idleDimLevel = Math.max(1, Math.min(50, parseInt(m[1], 10)));
|
|
|
|
return ;
|
|
}
|
|
if (cmd.indexOf("dpms") !== -1) {
|
|
seen.screen = true;
|
|
Prefs.idleScreenOff = true;
|
|
Prefs.idleScreenOffAfter = t;
|
|
return ;
|
|
}
|
|
if (cmd.indexOf("suspend") !== -1) {
|
|
seen.suspend = true;
|
|
Prefs.idleSuspend = true;
|
|
Prefs.idleSuspendAfter = t;
|
|
return ;
|
|
}
|
|
if (cmd.indexOf("lock") !== -1) {
|
|
seen.lock = true;
|
|
Prefs.idleLock = true;
|
|
Prefs.idleLockAfter = t;
|
|
}
|
|
}
|
|
|
|
function adoptOff(key) {
|
|
if (key === "dim")
|
|
Prefs.idleDim = false;
|
|
else if (key === "lock")
|
|
Prefs.idleLock = false;
|
|
else if (key === "screen")
|
|
Prefs.idleScreenOff = false;
|
|
else if (key === "suspend")
|
|
Prefs.idleSuspend = false;
|
|
}
|
|
|
|
// the file on disk, kept only so adoption and the backup can read it
|
|
FileView {
|
|
id: confFile
|
|
|
|
property bool ready: false
|
|
property string raw: ""
|
|
|
|
path: root.confPath
|
|
blockLoading: true
|
|
watchChanges: true
|
|
onFileChanged: reload()
|
|
onLoaded: {
|
|
confFile.raw = confFile.text();
|
|
confFile.ready = true;
|
|
root.maybeAdopt();
|
|
}
|
|
onLoadFailed: {
|
|
confFile.raw = "";
|
|
confFile.ready = true;
|
|
root.maybeAdopt();
|
|
}
|
|
onSaveFailed: root.lastError = "Could not write " + root.confPath
|
|
}
|
|
|
|
// one copy of whatever was there before Lucid ever wrote the file
|
|
Process {
|
|
id: backup
|
|
|
|
running: true
|
|
command: ["sh", "-c", "f=\"" + root.confPath + "\"; [ -f \"$f\" ] || exit 0;" + "grep -q '" + root.marker + "' \"$f\" && exit 0;" + "[ -f \"$f.pre-lucid\" ] && exit 0;" + "cp \"$f\" \"$f.pre-lucid\"; exit 0"]
|
|
}
|
|
|
|
IpcHandler {
|
|
target: "idle"
|
|
|
|
// keep awake, the caffeine toggle
|
|
function keepawake(): void {
|
|
Prefs.idleKeepAwake = !Prefs.idleKeepAwake;
|
|
}
|
|
|
|
function awake(): void {
|
|
Prefs.idleKeepAwake = true;
|
|
}
|
|
|
|
function normal(): void {
|
|
Prefs.idleKeepAwake = false;
|
|
}
|
|
|
|
function on(): void {
|
|
Prefs.idleEnabled = true;
|
|
}
|
|
|
|
function off(): void {
|
|
Prefs.idleEnabled = false;
|
|
}
|
|
|
|
function restart(): void {
|
|
root.restart();
|
|
}
|
|
|
|
function status(): string {
|
|
return root.summary;
|
|
}
|
|
|
|
}
|
|
|
|
}
|