Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions shell/plugins/services/idle/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Item {

onFirstIdleTimeoutSecondsChanged: retimeIdleMonitor()

// The lock service is never handed to other plugins, so this is the only sign a lock is on its way.
property bool lockCommandPending: false
property bool stayAwake: false
property bool stayAwakeStateLoaded: false
property bool hasPendingStayAwakePersist: false
Expand Down Expand Up @@ -79,6 +81,11 @@ Item {
}

function launchScreensaver() {
if (root.lockCommandPending) {
logEvent("screensaver-skip", "lock-in-flight")
return
}

root.screensaverStartedThisCycle = true
screensaverLaunchGraceTimer.restart()
runProcess(screensaverProcess, "screensaver", "[[ $(omarchy-shell lock isLocked 2>/dev/null) == \"true\" ]] || omarchy-launch-screensaver")
Expand All @@ -91,7 +98,12 @@ Item {
screensaverLaunchGraceTimer.stop()
root.idledThisCycle = false
root.screensaverStartedThisCycle = false
runProcess(lockProcess, "lock", "omarchy-system-lock")
// The lock is invisible until omarchy-system-lock reaches the lock service, about a
// second later; bounded, so a lock command that never returns cannot park idle handling.
if (runProcess(lockProcess, "lock", "omarchy-system-lock")) {
root.lockCommandPending = true
lockCommandTimer.restart()
}
}

function startIdleCycle() {
Expand All @@ -100,6 +112,13 @@ Item {
return
}

// lockSystem() clears idledThisCycle before the lock exists, so an idle re-assertion
// would start a new cycle and launch a screensaver into the pending lock.
if (root.lockCommandPending) {
logEvent("idle-cycle-skip", "lock-in-flight")
return
}

logEvent("idle-cycle-start", "screensaver=" + root.screensaverTimeoutSeconds + " lock=" + root.lockTimeoutSeconds)
root.idledThisCycle = true
root.screensaverStartedThisCycle = false
Expand Down Expand Up @@ -176,6 +195,7 @@ Item {
stayAwakeStateLoaded: root.stayAwakeStateLoaded,
stayAwakeStatePath: root.stayAwakeStatePath,
idle: idleMonitor.isIdle,
lockCommandPending: root.lockCommandPending,
inIdleCycle: root.idledThisCycle,
screensaverStarted: root.screensaverStartedThisCycle,
screensaver: root.screensaverTimeoutSeconds,
Expand Down Expand Up @@ -275,6 +295,16 @@ Item {
}
}

Timer {
id: lockCommandTimer
interval: 15000
repeat: false
onTriggered: {
root.lockCommandPending = false
root.logEvent("lock-command-timeout")
}
Comment on lines +302 to +305

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Idle actions never restart

If an idle event arrives while lockCommandPending is true, startIdleCycle() drops it. If the lock command then fails or hangs before requesting the lock, clearing the flag here or in onExited does not check whether the user is still idle. lockSystem() has already stopped both timers and cleared idledThisCycle, so the session can remain unlocked with idle actions unarmed until another activity-to-idle change.

Recheck the current idle and lock state when releasing the flag. Add coverage that checks recovery without manually calling startIdleCycle().

}
Comment on lines +298 to +306

Timer {
id: screensaverLaunchGraceTimer
interval: 3000
Expand All @@ -297,7 +327,11 @@ Item {
}
Process {
id: lockProcess
onExited: function(exitCode, exitStatus) { root.logEvent("process-exit", "lock exitCode=" + exitCode + " status=" + exitStatus) }
onExited: function(exitCode, exitStatus) {
root.lockCommandPending = false
lockCommandTimer.stop()
root.logEvent("process-exit", "lock exitCode=" + exitCode + " status=" + exitStatus)
}
}
Process {
id: wakeProcess
Expand Down
75 changes: 75 additions & 0 deletions test/shell.d/fixtures/idle-lock-pending/shell.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import QtQuick
import Quickshell

// Drives the real idle service through a lock whose command is still on its way
// to the lock service (a stub omarchy-system-lock that sleeps), re-asserting idle
// meanwhile as the compositor does, and again once the command has returned.
ShellRoot {
id: root

property string resultPath: Quickshell.env("OMARCHY_QML_TEST_RESULT")
property var failures: []
property var service: null

function fail(message) {
failures.push(String(message))
}

function assertTrue(condition, message) {
if (!condition) fail(message)
}

function shellQuote(value) {
return "'" + String(value).replace(/'/g, "'\\''") + "'"
}

function writeResult() {
var payload = JSON.stringify({ ok: failures.length === 0, failures: failures, lastEvent: service ? service.lastEvent : "" })
Quickshell.execDetached(["bash", "-c", "printf '%s' " + shellQuote(payload) + " > " + shellQuote(resultPath)])
}

function processes() {
return JSON.parse(service.statusJson()).processes
}

// A screensaver timeout below the lock timeout launches it the moment a cycle starts, as the defaults do.
QtObject {
id: fakeShell
property var shellConfig: ({ idle: { screensaver: 86400, lock: 172800 } })
}

Timer {
id: whilePending
interval: 500
onTriggered: {
assertTrue(root.processes().lock, "the lock command is still running")
service.startIdleCycle()
assertTrue(!service.screensaverStartedThisCycle, "no screensaver launches while the lock command is pending")
assertTrue(!service.idledThisCycle, "no idle cycle starts while the lock command is pending")
afterLock.start()
}
}

Timer {
id: afterLock
interval: 2000
onTriggered: {
assertTrue(!root.processes().lock, "the lock command has exited")
service.startIdleCycle()
assertTrue(service.idledThisCycle && service.screensaverStartedThisCycle, "an idle cycle starts again once the lock command has exited")
root.writeResult()
}
}

Component.onCompleted: {
var component = Qt.createComponent("file://" + Quickshell.env("OMARCHY_PATH") + "/shell/plugins/services/idle/Service.qml")
if (component.status !== Component.Ready) {
fail("idle service failed to load: " + component.errorString())
writeResult()
return
}
service = component.createObject(root, { shell: fakeShell })
service.lockSystem("test")
whilePending.start()
}
}
17 changes: 17 additions & 0 deletions test/shell.d/idle-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ HOME="$test_home" "$ROOT/bin/omarchy-toggle-idle" allow-idle >/dev/null
[[ ! -f $test_home/.local/state/omarchy/indicators/stay-awake ]] || fail "Stay Awake toggle persists disabled state"

pass "Stay Awake toggle persists state"

require_quickshell "idle lock-pending test"

qml_fixture_scratch
trap 'rm -rf "$test_tmp"; qml_fixture_cleanup' EXIT
printf '#!/bin/bash\nsleep 1.5\n' >"$QML_FIXTURE_TMP/bin/omarchy-system-lock"
printf '#!/bin/bash\n[[ $* == "lock isLocked" ]] && echo false\nexit 0\n' >"$QML_FIXTURE_TMP/bin/omarchy-shell"
printf '#!/bin/bash\nexit 0\n' >"$QML_FIXTURE_TMP/bin/omarchy-launch-screensaver"
printf '#!/bin/bash\nexit 0\n' >"$QML_FIXTURE_TMP/bin/omarchy-system-wake"
# The service runs its commands under `bash -lc`, and a login shell would put the real commands back ahead of these stubs.
printf '#!/bin/bash\n[[ ${1:-} == "-lc" ]] && shift && set -- -c "$@"\nexec /bin/bash "$@"\n' >"$QML_FIXTURE_TMP/bin/bash"
chmod +x "$QML_FIXTURE_TMP"/bin/*

resolved=$(env HOME="$QML_FIXTURE_TMP/home" PATH="$QML_FIXTURE_TMP/bin:$ROOT/bin:$PATH" bash -lc 'command -v omarchy-system-lock')
[[ $resolved == "$QML_FIXTURE_TMP/bin/omarchy-system-lock" ]] || fail "a login shell finds the stub lock command" "It found $resolved."

run_qml_fixture idle-lock-pending "idle starts no cycle on top of a lock still on its way" Commons
Loading