Skip to content
Closed
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
10 changes: 10 additions & 0 deletions shell/plugins/services/idle/IdleModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ function eventParts(event, count) {
return String(event && event.data ? event.data : "").split(",")
}

function hasRealScreen(screens) {
var list = screens || []
for (var i = 0; i < list.length; i++) {
var screen = list[i]
if (screen && screen.name && screen.name !== "FALLBACK" && screen.width > 0 && screen.height > 0) return true
}
return false
}

function screensaverWindowsAfter(windows, address, visible) {
var key = String(address || "")
if (!key) {
Expand Down Expand Up @@ -69,6 +78,7 @@ if (typeof module !== "undefined") {
firstIdleTimeout: firstIdleTimeout,
delayAfterFirstIdle: delayAfterFirstIdle,
eventParts: eventParts,
hasRealScreen: hasRealScreen,
screensaverWindowsAfter: screensaverWindowsAfter
}
}
39 changes: 39 additions & 0 deletions shell/plugins/services/idle/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,32 @@ Item {
if (!root.idleEnabled || !root.idledThisCycle || !root.screensaverStartedThisCycle) return
if (root.screensaverWindowCount > 0) return

// The window went because its output did (a monitor link flapping during
// the blank). That is not the user, and waking here would undo the blank.
if (outputsLost()) {
holdForOutputLoss("screensaver-closed-with-output")
return
}

// The user dismissed the screensaver before the lock deadline. Treat that
// as activity and cancel the pending lock; the lock timer is only allowed
// to fire while the screensaver remains up.
root.cancelIdleCycle("screensaver-dismissed")
}

// Screens the compositor can show something on: not Hyprland's FALLBACK
// placeholder and not a screen without a valid output behind it.
property var outputScreens: Quickshell.screens

function outputsLost() {
return outputLossTimer.running || !IdleModel.hasRealScreen(root.outputScreens)
}

function holdForOutputLoss(reason) {
logEvent("output-loss", reason)
outputLossTimer.restart()
}

function eventParts(event, count) {
return IdleModel.eventParts(event, count)
}
Expand All @@ -164,6 +184,9 @@ Item {
if (name === "openwindow") {
var open = eventParts(event, 4)
if (String(open[2] || "") === root.screensaverClass) root.handleScreensaverWindowOpened(open[0])
} else if (name === "monitorremoved") {
var removed = String(eventParts(event, 1)[0] || "")
if (root.idledThisCycle && removed !== "FALLBACK") root.holdForOutputLoss("monitorremoved " + removed)
Comment on lines +187 to +189

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 Dismissal still leads to locking

Removing one monitor starts outputLossTimer even when another monitor remains connected. For the next three seconds, outputsLost() ignores genuine screensaver dismissal and activity on that remaining monitor. The pending lock stays armed and can lock the session after the user has returned. Limit the hold to losing all usable outputs, and stop suppressing real dismissal once an output is available.

Knowledge Base Used: Shell UI and desktop interactions

} else if (name === "closewindow") {
var close = eventParts(event, 1)
var address = String(close[0] || "")
Expand All @@ -174,6 +197,14 @@ Item {
function handleActiveSignal() {
if (!root.idledThisCycle) return

// Without an output to show anything on, the compositor's own re-check
// (a window closing with its output) is the only thing that can report
// activity. It is not the user, and waking would undo the blank.
if (outputsLost()) {
logEvent("idle-monitor-active", "ignored while outputs are lost")
return
}

// Starting the screensaver can make the compositor report activity. Keep
// the lock timer running once the screensaver exists (or during its short
// launch grace); Hyprland window events cancel the cycle if it exits before
Expand Down Expand Up @@ -287,6 +318,14 @@ Item {
onTriggered: if (root.idleEnabled && root.idledThisCycle && root.lockEnabled) root.lockSystem("lock-timeout")
}

// The screen list and the compositor's resumed signal can lag the monitor
// removal, so the loss is held for a moment past the event that reports it.
Timer {
id: outputLossTimer
interval: 3000
repeat: false
}

Timer {
id: screensaverLaunchGraceTimer
interval: 3000
Expand Down
119 changes: 119 additions & 0 deletions test/shell.d/fixtures/idle-output-loss/shell.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import QtQuick
import Quickshell

ShellRoot {
id: root

readonly property string rootPath: Quickshell.env("OMARCHY_PATH")
readonly property string resultPath: Quickshell.env("OMARCHY_QML_TEST_RESULT")
readonly property string wakeLog: Quickshell.env("OMARCHY_WAKE_LOG")
property var failures: []
property int checks: 0
property int step: 0
property var service: null

readonly property var realScreen: ({ name: "DP-1", width: 1920, height: 1080 })
readonly property var fallbackScreen: ({ name: "FALLBACK", width: 1920, height: 1080 })
readonly property string saver: "org.omarchy.screensaver"

Item { id: host }

function check(condition, message) {
checks += 1
if (!condition) failures.push(message)
}

function mark(name) {
var quoted = "'" + name.replace(/'/g, "'\\''") + "'"
Quickshell.execDetached(["bash", "-c", "echo \"S " + name + "\" >> \"$1\"", "_", wakeLog])

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.

P2 Wake log order can change

mark() appends each scenario label through a detached process, then the scenario immediately starts wake through another process. Either process can write first, but the test requires one exact order. The 700 ms pause between scenarios does not order these two writes. Wait for the marker to finish before triggering wake, or record and check each scenario separately.

}

// A cycle that idled with the screensaver up on a window at 0x1, as the
// idle service sees it once the screensaver has opened.
function begin(name, screens) {
if (service) service.destroy()
var component = Qt.createComponent("file://" + rootPath + "/shell/plugins/services/idle/Service.qml", Component.PreferSynchronous)
if (component.status !== Component.Ready) throw new Error(component.errorString())
service = component.createObject(host, { outputScreens: screens })
if (!service) throw new Error(component.errorString())
service.stayAwakeStateLoaded = true
service.idledThisCycle = true
service.screensaverStartedThisCycle = true
service.handleHyprlandEvent({ name: "openwindow", data: "0x1,1,org.omarchy.screensaver,t" })
check(service.screensaverWindowCount === 1, name + ": the screensaver window is tracked")
mark(name)
}

function closeSaver() { service.handleHyprlandEvent({ name: "closewindow", data: "0x1" }) }

property var scenarios: [
function() {
begin("no-outputs-close", [])
closeSaver()
check(service.idledThisCycle, "closing with no outputs keeps the cycle")
service.handleActiveSignal()
check(service.idledThisCycle, "resumed after the output loss keeps the cycle")
},
function() {
begin("fallback-only-close", [fallbackScreen])
closeSaver()
check(service.idledThisCycle, "a FALLBACK-only screen list counts as no outputs")
service.handleActiveSignal()
check(service.idledThisCycle, "resumed with only FALLBACK keeps the cycle")
},
function() {
begin("stale-list-monitorremoved", [realScreen])
service.handleHyprlandEvent({ name: "monitorremoved", data: "DP-1" })
closeSaver()
check(service.idledThisCycle, "monitorremoved before a stale screen list keeps the cycle")
service.handleActiveSignal()
check(service.idledThisCycle, "resumed right after monitorremoved keeps the cycle")
},
function() {
begin("fallback-removed-close", [realScreen])
service.handleHyprlandEvent({ name: "monitorremoved", data: "FALLBACK" })
closeSaver()
check(!service.idledThisCycle, "removing only FALLBACK is not an output loss")
},
function() {
begin("real-dismissal", [realScreen])
closeSaver()
check(!service.idledThisCycle, "closing the screensaver with outputs present still cancels")
},
function() {
begin("real-activity", [realScreen])
service.handleHyprlandEvent({ name: "closewindow", data: "0x9" })
check(service.idledThisCycle, "closing an unrelated window leaves the cycle")
service.screensaverStartedThisCycle = false
service.handleActiveSignal()
check(!service.idledThisCycle, "resumed with outputs present is real activity")
}
]

function finish() {
if (service) service.destroy()
var payload = JSON.stringify({ ok: failures.length === 0, checks: checks, failures: failures })
var quoted = "'" + payload.replace(/'/g, "'\\''") + "'"
Quickshell.execDetached(["bash", "-c", "printf '%s' " + quoted + " > \"$1\"", "_", resultPath])
}

// Each scenario gets time for its wake stub (if any) to log before the next.
Timer {
interval: 700
repeat: true
running: true
onTriggered: {
if (root.step >= root.scenarios.length) {
stop()
root.finish()
return
}
try {
root.scenarios[root.step]()
} catch (error) {
root.failures.push(String(error))
}
root.step += 1
}
}
}
89 changes: 89 additions & 0 deletions test/shell.d/idle-output-loss-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

TMPDIR=""
QS_PID=""

cleanup() {
if [[ -n $QS_PID ]] && kill -0 "$QS_PID" 2>/dev/null; then
kill "$QS_PID" 2>/dev/null || true
wait "$QS_PID" 2>/dev/null || true
fi
if [[ -n $TMPDIR && -d $TMPDIR ]]; then
rm -rf "$TMPDIR"
fi
}
trap cleanup EXIT

require_compositor "idle output loss test"

if ! command -v quickshell >/dev/null 2>&1; then
skip "quickshell not installed; skipping idle output loss test"
exit 0
fi

require_command jq

TMPDIR=$(mktemp -d)
result="$TMPDIR/result.json"
log="$TMPDIR/quickshell.log"
wakes="$TMPDIR/wakes.log"
config_dir="$TMPDIR/idle-output-loss"
mkdir -p "$config_dir" "$TMPDIR/home" "$TMPDIR/stubs"
cp "$SHELL_TEST_DIR/fixtures/idle-output-loss/shell.qml" "$config_dir/shell.qml"
ln -s "$ROOT/shell/Ui" "$config_dir/Ui"
ln -s "$ROOT/shell/Commons" "$config_dir/Commons"

# Stub the boundary: the idle service's wake command only records that it ran.
cat >"$TMPDIR/stubs/omarchy-system-wake" <<STUB
#!/bin/bash
echo wake >> "$wakes"
STUB
chmod +x "$TMPDIR/stubs/omarchy-system-wake"

OMARCHY_PATH="$ROOT" \
OMARCHY_QML_TEST_RESULT="$result" \
OMARCHY_WAKE_LOG="$wakes" \
HOME="$TMPDIR/home" \
XDG_CONFIG_HOME="$TMPDIR/home/.config" \
XDG_CACHE_HOME="$TMPDIR/home/.cache" \
XDG_STATE_HOME="$TMPDIR/home/.local/state" \
QML2_IMPORT_PATH="$ROOT/shell${QML2_IMPORT_PATH:+:$QML2_IMPORT_PATH}" \
QML_IMPORT_PATH="$ROOT/shell${QML_IMPORT_PATH:+:$QML_IMPORT_PATH}" \
PATH="$TMPDIR/stubs:$ROOT/bin:$PATH" \
quickshell -p "$config_dir" --no-color >"$log" 2>&1 &
Comment on lines +56 to +57

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 Test runs the real wake

The test puts the wake stub first in PATH, but Service.qml runs it through bash -lc. On an installed Omarchy session, the login shell resets PATH, so it can run the real omarchy-system-wake instead. The test then changes the desktop and fails because the expected wake entries are missing. Add the existing bash stub pattern that replaces -lc with -c.

QS_PID=$!

for _ in {1..120}; do
[[ -s $result ]] && break
if ! kill -0 "$QS_PID" 2>/dev/null; then
sed -n '1,220p' "$log" >&2
fail "idle output loss quickshell exited before writing result"
fi
sleep 0.1
done

[[ -s $result ]] || {
sed -n '1,220p' "$log" >&2
fail "idle output loss test timed out"
}

if ! jq -e '.ok == true' "$result" >/dev/null; then
jq . "$result" >&2
sed -n '1,220p' "$log" >&2
fail "idle service keeps the cycle through output loss and still cancels on real activity"
fi

# The wake stub runs asynchronously; each scenario marks its own section.
expected=$'S no-outputs-close\nS fallback-only-close\nS stale-list-monitorremoved\nS fallback-removed-close\nwake\nS real-dismissal\nwake\nS real-activity\nwake'
if [[ $(cat "$wakes") != "$expected" ]]; then
printf 'Wake log:\n' >&2
cat "$wakes" >&2
sed -n '1,120p' "$log" >&2
fail "wake runs only for real dismissal and activity, never for output loss"
fi

pass "idle service keeps the cycle through output loss and still cancels on real activity"
5 changes: 5 additions & 0 deletions test/shell.d/idle-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ assert(
'idle ends a running cycle when both timeouts are set to 0'
)

assert(idle.hasRealScreen([{ name: 'DP-1', width: 1920, height: 1080 }]), 'idle counts a real output')
assert(!idle.hasRealScreen([]), 'idle sees no output in an empty list')
assert(!idle.hasRealScreen([{ name: 'FALLBACK', width: 1920, height: 1080 }]), 'idle ignores the FALLBACK placeholder')
assert(!idle.hasRealScreen([{ name: '', width: 0, height: 0 }]), 'idle ignores a screen without a valid output')

assertDeepEqual(idle.eventParts({ data: 'a,b,c' }, 2), ['a', 'b', 'c'], 'idle parses raw event data')
assertDeepEqual(
idle.eventParts({ parse: function(count) { return ['parsed', count] } }, 4),
Expand Down