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
37 changes: 36 additions & 1 deletion shell/plugins/services/idle/IdleModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,47 @@ function screensaverWindowsAfter(windows, address, visible) {
}
}

// The screensaver windows still open, read from the live toplevels rather than
// from the openwindow events tracked so far. A close event can be missed, and a
// window tracked from an earlier event that has since gone must not keep a
// cycle armed, or hold off the launch grace fallback, for the rest of the
// session.
//
// A toplevel whose wayland handle has not been attached yet reports a null app
// id. That is unknown, not absent: the handle arrives milliseconds after the
// window maps, and reading it as "not the screensaver" drops a window that is
// really up, which on a lock path fails open. Such a window is kept when the
// openwindow events already track its address, and ignored when they do not, so
// a toplevel this cannot identify never becomes a screensaver window by itself.
function screensaverWindowsFrom(entries, className, tracked) {
var windows = {}
var count = 0

for (var i = 0; i < (entries ? entries.length : 0); i++) {
var entry = entries[i] || {}
var address = String(entry.address || "")
if (!address || windows[address]) continue

if (entry.appId === null) {
if (!tracked || !tracked[address]) continue
} else if (String(entry.appId || "") !== className) {
continue
}

windows[address] = true
count++
}

return { windows: windows, count: count }
}

if (typeof module !== "undefined") {
module.exports = {
secondsFromConfig: secondsFromConfig,
firstIdleTimeout: firstIdleTimeout,
delayAfterFirstIdle: delayAfterFirstIdle,
eventParts: eventParts,
screensaverWindowsAfter: screensaverWindowsAfter
screensaverWindowsAfter: screensaverWindowsAfter,
screensaverWindowsFrom: screensaverWindowsFrom
}
}
91 changes: 90 additions & 1 deletion shell/plugins/services/idle/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,53 @@ Item {
root.screensaverWindowCount = next.count
}

// The screensaver windows Hyprland has open right now. The openwindow and
// closewindow events are what arm a cycle, but one of them can go missing,
// and a window tracked from an event that has since closed must not keep a
// cycle armed, or hold off the launch grace fallback, for the rest of the
// session. Answers null when the list cannot be read, so a caller falls back
// to what the events tracked.
function liveScreensaverWindows() {
try {
var toplevels = Hyprland.toplevels.values
var entries = []
for (var i = 0; i < toplevels.length; i++) {
var toplevel = toplevels[i]
entries.push({
address: toplevel ? String(toplevel.address || "") : "",
// Null, not empty, when the wayland handle is not attached yet: the
// app id is unknown at that point, and the model keeps a window the
// raw openwindow already tracked rather than dropping it.
appId: toplevel && toplevel.wayland ? String(toplevel.wayland.appId || "") : null
})
}
return IdleModel.screensaverWindowsFrom(entries, root.screensaverClass, root.screensaverWindows)
} catch (error) {
return null
}
}

// Replace the tracked set with what Hyprland actually has, before a decision
// that rests on it, so a window whose close event never arrived cannot outlive
// the window itself.
function syncScreensaverWindows() {
var live = liveScreensaverWindows()
if (!live) return

if (live.count !== root.screensaverWindowCount) {
logEvent("screensaver-windows", "live=" + live.count + " tracked=" + root.screensaverWindowCount)
}

root.screensaverWindows = live.windows
root.screensaverWindowCount = live.count
}

// A window opening no longer cancels the launch grace re-check. By the time
// that fires the screensaver has either stayed up, which keeps the cycle
// armed, or gone with the user's return, which ends it, and the re-check is
// what ends a cycle whose closewindow event never arrives.
function handleScreensaverWindowOpened(address) {
setScreensaverWindow(address, true)
screensaverLaunchGraceTimer.stop()
}

function handleScreensaverWindowClosed(address) {
Expand All @@ -155,6 +199,33 @@ Item {
root.cancelIdleCycle("screensaver-dismissed")
}

// The toplevel list is the live record of which windows exist, and it changes
// when the screensaver window goes away even if its closewindow event never
// arrives. Watching it there ends a cycle whose close event was missed, so the
// pending lock cannot fire later while the user works. A window that is still
// up keeps the cycle armed.
//
// Every window the live list says has gone is handed to the same handler the
// closewindow path uses, rather than cancelling here: what a dismissal means
// stays in one place, and a reason recorded for it stays the same whichever
// path noticed first.
function handleToplevelsChanged() {
if (!root.idledThisCycle || !root.screensaverStartedThisCycle) return

// The launch window belongs to the grace timer, which is what decides
// whether the screensaver came up at all.
if (screensaverLaunchGraceTimer.running) return

var tracked = root.screensaverWindows
syncScreensaverWindows()

var gone = []
for (var address in tracked) {
if (tracked[address] && !root.screensaverWindows[address]) gone.push(address)
}
for (var i = 0; i < gone.length; i++) root.handleScreensaverWindowClosed(gone[i])
}

function eventParts(event, count) {
return IdleModel.eventParts(event, count)
}
Expand All @@ -174,12 +245,23 @@ Item {
function handleActiveSignal() {
if (!root.idledThisCycle) return

// Ask Hyprland what the screensaver is doing now rather than trusting the
// windows the events have accumulated: a closewindow that never arrived
// would otherwise keep an active user inside an idle cycle all session.
syncScreensaverWindows()

// 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
// the normal lock deadline.
if (root.screensaverStartedThisCycle && (root.screensaverWindowCount > 0 || screensaverLaunchGraceTimer.running)) {
logEvent("idle-monitor-active", "screensaver cycle remains armed")

// Re-check once the grace window has passed. Activity that dismissed the
// screensaver leaves it gone by then, so a cycle that should have ended
// does not wait for an idle transition that may never come: the monitor
// is already active and stays that way while the user works.
if (root.screensaverWindowCount > 0) screensaverLaunchGraceTimer.restart()
return
}

Expand Down Expand Up @@ -292,6 +374,8 @@ Item {
interval: 3000
repeat: false
onTriggered: {
root.syncScreensaverWindows()

if (root.idleEnabled && root.idledThisCycle && root.screensaverStartedThisCycle && root.screensaverWindowCount === 0 && !idleMonitor.isIdle) {
root.cancelIdleCycle("screensaver-not-running")
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}
Expand All @@ -303,6 +387,11 @@ Item {
function onRawEvent(event) { root.handleHyprlandEvent(event) }
}

Connections {
target: Hyprland.toplevels
function onValuesChanged() { root.handleToplevelsChanged() }
}

Process {
id: screensaverProcess
onExited: function(exitCode, exitStatus) { root.logEvent("process-exit", "screensaver exitCode=" + exitCode + " status=" + exitStatus) }
Expand Down
112 changes: 112 additions & 0 deletions test/shell.d/idle-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,46 @@ assert(
/onIdleTimersEnabledChanged: if \(!idleTimersEnabled\) cancelIdleCycle\(/.test(serviceSource),
'idle ends a running cycle when both timeouts are set to 0'
)
assert(
/Hyprland\.toplevels\.values/.test(serviceSource),
'idle reads the live toplevels when it asks which screensaver windows exist'
)
assert(
/id: screensaverLaunchGraceTimer[\s\S]*?onTriggered: \{\s*\n\s*root\.syncScreensaverWindows\(\)/.test(serviceSource),
'idle re-checks the live screensaver windows when the launch grace ends'
)
assert(
/function handleActiveSignal\(\) \{[\s\S]*?syncScreensaverWindows\(\)[\s\S]*?if \(root\.screensaverStartedThisCycle/.test(serviceSource),
'idle re-checks the live screensaver windows before holding a cycle open for activity'
)
assert(
/if \(root\.screensaverWindowCount > 0\) screensaverLaunchGraceTimer\.restart\(\)/.test(serviceSource),
'idle re-checks shortly after it holds a cycle open for activity'
)
assert(
/function handleScreensaverWindowOpened\(address\) \{\s*\n\s*setScreensaverWindow\(address, true\)\s*\n\s*\}/.test(serviceSource),
'idle keeps the launch grace re-check when a screensaver window opens'
)
assert(
/target: Hyprland\.toplevels\s*\n\s*function onValuesChanged\(\) \{ root\.handleToplevelsChanged\(\) \}/.test(serviceSource),
'idle watches the live toplevel list for a screensaver window that goes away'
)
assert(
/function handleToplevelsChanged\(\) \{[\s\S]*?if \(screensaverLaunchGraceTimer\.running\) return\s*\n\s*\n\s*var tracked = root\.screensaverWindows\s*\n\s*syncScreensaverWindows\(\)/.test(serviceSource),
'idle compares the live list against the tracked windows without cancelling on its own'
)
assert(
/function handleToplevelsChanged\(\) \{[\s\S]*?root\.handleScreensaverWindowClosed\(gone\[i\]\)/.test(serviceSource),
'idle hands a screensaver window that vanished without a closewindow to the close handler'
)
assert(
/root\.cancelIdleCycle\("screensaver-dismissed"\)/.test(serviceSource),
'idle keeps one dismissal decision for the closeevent and toplevels paths'
)
assert(
/if \(screensaverLaunchGraceTimer\.running\) return/.test(serviceSource),
'idle leaves the launch window to the grace timer'
)

assertDeepEqual(idle.eventParts({ data: 'a,b,c' }, 2), ['a', 'b', 'c'], 'idle parses raw event data')
assertDeepEqual(
Expand All @@ -78,6 +118,78 @@ assertDeepEqual(
{ windows: { a: true }, count: 1 },
'idle leaves screensaver windows unchanged without an address'
)

assertDeepEqual(
idle.screensaverWindowsFrom(
[{ address: 'a', appId: 'org.omarchy.screensaver' }, { address: 'b', appId: 'org.omarchy.screensaver' }],
'org.omarchy.screensaver'
),
{ windows: { a: true, b: true }, count: 2 },
'idle counts the live screensaver windows'
)
assertDeepEqual(
idle.screensaverWindowsFrom(undefined, 'org.omarchy.screensaver'),
{ windows: {}, count: 0 },
'idle has no screensaver windows without a toplevel list'
)
assertDeepEqual(
idle.screensaverWindowsFrom(
[{ address: 'a', appId: 'foot' }, { address: 'b', appId: 'org.omarchy.screensaver' }],
'org.omarchy.screensaver'
),
{ windows: { b: true }, count: 1 },
'idle ignores live windows that are not the screensaver'
)
assertDeepEqual(
idle.screensaverWindowsFrom([{ appId: 'org.omarchy.screensaver' }, { address: 'a' }], 'org.omarchy.screensaver'),
{ windows: {}, count: 0 },
'idle ignores a live window with no address or screensaver app id'
)
assertDeepEqual(
idle.screensaverWindowsFrom(
[{ address: 'a', appId: 'org.omarchy.screensaver' }, { address: 'a', appId: 'org.omarchy.screensaver' }],
'org.omarchy.screensaver'
),
{ windows: { a: true }, count: 1 },
'idle counts a live screensaver address once'
)
assertDeepEqual(
idle.screensaverWindowsFrom([{ address: 'a', appId: 'org.omarchy.screensaver' }], ''),
{ windows: {}, count: 0 },
'idle matches no window when the screensaver app id is empty'
)
assertDeepEqual(
idle.screensaverWindowsFrom([{ address: 'a', appId: null }], 'org.omarchy.screensaver', { a: true }),
{ windows: { a: true }, count: 1 },
'idle keeps a live window with no app id yet that the events already tracked'
)
assertDeepEqual(
idle.screensaverWindowsFrom([{ address: 'a', appId: null }], 'org.omarchy.screensaver', {}),
{ windows: {}, count: 0 },
'idle does not turn a live window with no app id yet into a screensaver window'
)
assertDeepEqual(
idle.screensaverWindowsFrom([{ address: 'a', appId: null }], 'org.omarchy.screensaver'),
{ windows: {}, count: 0 },
'idle keeps no unassociated window without a tracked set'
)
assertDeepEqual(
idle.screensaverWindowsFrom(
[{ address: 'a', appId: null }, { address: 'b', appId: 'foot' }],
'org.omarchy.screensaver',
{ a: true, b: true }
),
{ windows: { a: true }, count: 1 },
'idle still drops an unassociated window that is not the screensaver'
)
assert(
/appId: toplevel && toplevel\.wayland \? String\(toplevel\.wayland\.appId \|\| ""\) : null/.test(serviceSource),
'idle reports an unattached wayland handle as an unknown app id rather than an absent one'
)
assert(
/screensaverWindowsFrom\(entries, root\.screensaverClass, root\.screensaverWindows\)/.test(serviceSource),
'idle gives the live read the tracked windows so it keeps ones it cannot identify yet'
)
JS

test_tmp=$(mktemp -d)
Expand Down
Loading