diff --git a/shell/plugins/lock/Service.qml b/shell/plugins/lock/Service.qml index 1005510ad987..723ce2a51e69 100644 --- a/shell/plugins/lock/Service.qml +++ b/shell/plugins/lock/Service.qml @@ -32,12 +32,20 @@ Item { property string lastEvent: "init" property string lastEventAt: "" property bool displaysBlank: false + // A lock that is not touched goes dark after blankRunUp. One that was just + // woken, from a blank panel or from suspend, stays lit for wakeRunUp first: + // the user is looking at it. + readonly property int blankRunUp: 5000 + readonly property int wakeRunUp: 30000 // displaysBlank tracks what the lock asked for; Hyprland reports what each // panel actually did. While a video is on show the two are reconciled, so a // blank that failed keeps playing and a panel woken behind the lock's back // (a resume that kept the same outputs) resumes instead of freezing. property var monitorDpms: ({}) property bool monitorDpmsKnown: false + // Bumped by every wake and blank, so a DPMS answer asked for before one of + // them is dropped instead of undoing it. + property int dpmsEpoch: 0 // Hyprland lights every panel for a key when any one is dark, so the key that // does it is a wake key whichever panel's lock surface holds keyboard focus. readonly property bool anyDisplayBlank: displaysBlank && (!monitorDpmsKnown || anyMonitorDark(monitorDpms)) @@ -172,19 +180,27 @@ Item { runWake() } - function armBlankTimer() { - idleBlankTimer.armedAt = Date.now() + // Input re-arms the short run-up but never cuts a longer one short. + function armBlankTimer(runUp) { + var interval = runUp || blankRunUp + var now = Date.now() + if (idleBlankTimer.running && idleBlankTimer.armedAt + idleBlankTimer.interval - now > interval) return + idleBlankTimer.interval = interval + idleBlankTimer.armedAt = now idleBlankTimer.restart() } - function runWake() { + function runWake(runUp) { + var fromBlank = root.displaysBlank + root.dpmsEpoch += 1 root.displaysBlank = false root.monitorDpmsKnown = false if (!wakeProcess.running) wakeProcess.running = true - if (lockRequested) armBlankTimer() + if (lockRequested) armBlankTimer(runUp || (fromBlank ? wakeRunUp : blankRunUp)) } function runBlank() { + root.dpmsEpoch += 1 root.displaysBlank = true root.monitorDpmsKnown = false if (!blankProcess.running) blankProcess.running = true @@ -222,6 +238,20 @@ Item { return true } + // Hyprland lights the panels for input that never reaches the lock, such as + // the power button, which is a locked bind. Every enabled panel lit while the + // lock thinks it is blank is such a wake, and gets the same run-up as a key. + function takeMonitorDpms(text, epoch) { + if (epoch !== dpmsEpoch || blankProcess.running || wakeProcess.running) return + // A panel coming back mid-blank is the screen-change settle's to judge. + if (screenDpmsSettleTimer.running || screenDpmsProcess.running) return + if (!applyMonitorDpms(text)) return + if (!displaysBlank || !lockRequested) return + if (Object.keys(monitorDpms).length === 0 || anyMonitorDark(monitorDpms)) return + logEvent("woken-behind-lock") + runWake(wakeRunUp) + } + // Give up the blank state only once no panel is dark. An answer that cannot be // read gives it up too, as a panel coming back always did. function settleScreenDpms(text) { @@ -467,32 +497,39 @@ Item { Process { id: wakeProcess command: ["bash", "-c", "omarchy-system-wake"] + onExited: root.dpmsEpoch += 1 } Process { id: blankProcess command: ["bash", "-c", "omarchy-brightness-keyboard off; omarchy-brightness-display off"] + onExited: root.dpmsEpoch += 1 } // Quickshell exposes no DPMS signal, so the panel state is polled while a - // video is the locked wallpaper. A wake or blank request drops the last - // answer, so its optimistic state applies until the next poll confirms it. + // video is the locked wallpaper, and while the lock is blank to notice a wake + // it did not see. A wake or blank request drops the last answer, so its + // optimistic state applies until the next poll confirms it. Process { id: monitorDpmsProcess + property int epoch: 0 command: ["hyprctl", "monitors", "-j"] stdout: StdioCollector { - onStreamFinished: root.applyMonitorDpms(text) + onStreamFinished: root.takeMonitorDpms(text, monitorDpmsProcess.epoch) } } Timer { id: monitorDpmsTimer - interval: 3000 + // Nothing re-blanks before a wake is noticed, so a still wallpaper can wait longer. + interval: root.videoBackground ? 3000 : 5000 repeat: true triggeredOnStart: true - running: root.locked && root.videoBackground + running: root.locked && (root.videoBackground || root.displaysBlank) onTriggered: { - if (!monitorDpmsProcess.running) monitorDpmsProcess.running = true + if (monitorDpmsProcess.running || blankProcess.running || wakeProcess.running) return + monitorDpmsProcess.epoch = root.dpmsEpoch + monitorDpmsProcess.running = true } onRunningChanged: { if (!running) root.monitorDpmsKnown = false @@ -524,7 +561,7 @@ Item { Timer { id: idleBlankTimer - interval: 5000 + interval: root.blankRunUp repeat: false property double armedAt: 0 onTriggered: { @@ -532,7 +569,7 @@ Item { // blank the freshly woken unlock screen under the user. Wall-clock time // exposes the gap: take a fresh run-up instead of blanking. if (Date.now() - armedAt > interval + 2000) { - root.armBlankTimer() + root.armBlankTimer(root.wakeRunUp) return } // Only a password check in flight should hold the display up. The @@ -550,15 +587,16 @@ Item { interval: 1000 repeat: true running: root.lockRequested + // Start the clock with the lock: sleep-lock suspends within a second of it. property double lastTick: 0 - onRunningChanged: lastTick = 0 + onRunningChanged: lastTick = running ? Date.now() : 0 onTriggered: { var now = Date.now() var resumed = lastTick > 0 && now - lastTick > interval + 2000 lastTick = now if (resumed) { root.logEvent("resume-detected") - root.runWake() + root.runWake(root.wakeRunUp) } } } diff --git a/test/shell.d/lock-lit-after-wake-test.sh b/test/shell.d/lock-lit-after-wake-test.sh new file mode 100755 index 000000000000..ae1dc8b954f8 --- /dev/null +++ b/test/shell.d/lock-lit-after-wake-test.sh @@ -0,0 +1,188 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +run_node_test <<'JS' +const fs = require('fs') +const service = fs.readFileSync(path.join(root, 'shell/plugins/lock/Service.qml'), 'utf8') + +function extract(name) { + const match = service.match(new RegExp(`\\n function ${name}\\([^)]*\\) \\{[\\s\\S]*?\\n \\}\\n`)) + if (!match) fail(`Service.qml defines ${name}`) + return match[0] +} + +function runUp(name) { + const match = service.match(new RegExp(`readonly property int ${name}: (\\d+)`)) + if (!match) fail(`Service.qml defines ${name}`) + return Number(match[1]) +} + +const blankRunUp = runUp('blankRunUp') +const wakeRunUp = runUp('wakeRunUp') +assert(blankRunUp === 5000, 'an untouched lock still goes dark after five seconds') +assert(wakeRunUp >= 30000, 'a woken lock stays lit for at least thirty seconds') + +// Run the service's own timer bookkeeping against a stand-in for the lock root +// and a clock the test moves by hand. +function makeLock() { + const clock = { now: 1000000 } + const timer = { + running: false, + interval: blankRunUp, + armedAt: 0, + restart() { this.running = true }, + // When the countdown would fire, or 0 if it is not running. + dueAt() { return this.running ? this.armedAt + this.interval : 0 } + } + const state = { + Date: { now: () => clock.now }, + dpmsEpoch: 0, + monitorDpms: {}, + blankProcess: { running: false }, + screenDpmsSettleTimer: { running: false }, + screenDpmsProcess: { running: false }, + events: [], + logEvent(event) { this.events.push(event) }, + displaysBlank: false, + monitorDpmsKnown: false, + lockRequested: true, + blankRunUp, + wakeRunUp, + wakeProcess: { running: false }, + idleBlankTimer: timer + } + state.root = state + const fns = new Function('state', `with (state) { + ${extract('armBlankTimer')} + ${extract('runWake')} + ${extract('anyMonitorDark')} + ${extract('applyMonitorDpms')} + ${extract('takeMonitorDpms')} + return { armBlankTimer, runWake, takeMonitorDpms } + }`)(state) + return { + state, timer, clock, ...fns, + after(ms) { clock.now += ms }, + blank() { timer.running = false; state.displaysBlank = true; state.dpmsEpoch += 2 }, + // A poll asked for now, answered with these panels. + poll(panels, epoch = state.dpmsEpoch) { + fns.takeMonitorDpms(JSON.stringify(panels.map(([name, dpmsStatus]) => ({ name, dpmsStatus, disabled: false }))), epoch) + } + } +} + +let lock = makeLock() +lock.armBlankTimer() +assert(lock.timer.dueAt() === lock.clock.now + blankRunUp, 'taking the lock arms the short run-up') + +lock.blank() +lock.after(60000) +lock.runWake() +assert(lock.timer.dueAt() === lock.clock.now + wakeRunUp, 'the key that wakes a blank panel keeps it lit for the wake run-up') +const wokeAt = lock.clock.now + +lock.after(10000) +lock.runWake() +assert(lock.timer.dueAt() === wokeAt + wakeRunUp, 'input soon after a wake does not cut the wake run-up short') + +lock.after(wakeRunUp - 10000 - 2000) +lock.runWake() +assert(lock.timer.dueAt() === lock.clock.now + blankRunUp, 'input near the end of the wake run-up re-arms the normal run-up') + +lock = makeLock() +lock.armBlankTimer() +lock.runWake() +assert(lock.timer.dueAt() === lock.clock.now + blankRunUp, 'input at a lit lock keeps the normal run-up') + +// Suspend froze the countdown: it still looks armed, but its wall-clock time has passed. +lock = makeLock() +lock.armBlankTimer() +lock.after(3600000) +lock.runWake(wakeRunUp) +assert(lock.timer.dueAt() === lock.clock.now + wakeRunUp, 'a resume re-arms the wake run-up over a countdown frozen by suspend') + +lock = makeLock() +lock.state.lockRequested = false +lock.runWake() +assert(!lock.timer.running, 'waking after unlock does not arm a blank') + +// The power button is a locked Hyprland bind: it lights the panels and the lock never sees a key. +lock = makeLock() +lock.blank() +lock.after(60000) +lock.poll([['eDP-1', true], ['USB-2', true]]) +assert(!lock.state.displaysBlank && lock.state.events.includes('woken-behind-lock'), 'panels lit without a key reaching the lock count as a wake') +assert(lock.timer.dueAt() === lock.clock.now + wakeRunUp, 'a wake the lock did not see gets the wake run-up') + +lock = makeLock() +lock.blank() +lock.poll([['eDP-1', false], ['USB-2', true]]) +assert(lock.state.displaysBlank && !lock.timer.running, 'one panel lit while another is dark is not a wake') + +lock = makeLock() +lock.blank() +const staleEpoch = lock.state.dpmsEpoch - 1 +lock.poll([['eDP-1', true]], staleEpoch) +assert(lock.state.displaysBlank && !lock.timer.running, 'an answer asked for before the blank landed is not a wake') + +lock = makeLock() +lock.blank() +lock.state.blankProcess.running = true +lock.poll([['eDP-1', true]]) +assert(lock.state.displaysBlank && !lock.timer.running, 'an answer that races the blank itself is not a wake') + +lock = makeLock() +lock.blank() +lock.state.wakeProcess.running = true +lock.poll([['eDP-1', true]]) +assert(lock.state.displaysBlank && !lock.timer.running, 'an answer that races a wake is left to that wake') + +lock = makeLock() +lock.blank() +lock.state.screenDpmsSettleTimer.running = true +lock.poll([['USB-2', true]]) +assert(lock.state.displaysBlank && !lock.timer.running, 'a panel coming back mid-blank is left to the screen-change settle') + +lock = makeLock() +lock.blank() +lock.poll([]) +assert(lock.state.displaysBlank && !lock.timer.running, 'no enabled panel is not a wake') + +lock = makeLock() +lock.armBlankTimer() +const due = lock.timer.dueAt() +lock.poll([['eDP-1', true]]) +assert(lock.timer.dueAt() === due && lock.state.events.length === 0, 'a lit lock polled for its video wallpaper is not woken again') + +assert( + /id: monitorDpmsTimer[\s\S]*?running: root\.locked && \(root\.videoBackground \|\| root\.displaysBlank\)[\s\S]*?if \(monitorDpmsProcess\.running \|\| blankProcess\.running \|\| wakeProcess\.running\) return\s*monitorDpmsProcess\.epoch = root\.dpmsEpoch/.test(service), + 'a blank lock polls the panels, tagging each answer with the state it was asked in' +) + +assert( + /id: blankProcess[\s\S]*?onExited: root\.dpmsEpoch \+= 1/.test(service) && + /id: wakeProcess[\s\S]*?onExited: root\.dpmsEpoch \+= 1/.test(service) && + /function runBlank\(\) \{\s*root\.dpmsEpoch \+= 1/.test(service) && + /function runWake\([^)]*\) \{[\s\S]*?root\.dpmsEpoch \+= 1/.test(service) && + /onStreamFinished: root\.takeMonitorDpms\(text, monitorDpmsProcess\.epoch\)/.test(service), + 'answers asked for before a blank or wake finished are dropped' +) + +assert( + /id: resumeWatchTimer[\s\S]*?onRunningChanged: lastTick = running \? Date\.now\(\) : 0/.test(service), + 'a suspend within a second of locking is still seen as a resume' +) + +assert( + /id: resumeWatchTimer[\s\S]*?if \(resumed\) \{[\s\S]*?root\.runWake\(root\.wakeRunUp\)/.test(service), + 'a resume wakes the lock with the wake run-up' +) + +assert( + /id: idleBlankTimer[\s\S]*?if \(Date\.now\(\) - armedAt > interval \+ 2000\) \{\s*root\.armBlankTimer\(root\.wakeRunUp\)/.test(service), + 'a blank countdown frozen by suspend takes the wake run-up instead of blanking' +) +JS diff --git a/test/shell.d/lock-wake-key-test.sh b/test/shell.d/lock-wake-key-test.sh index d1aff6e3f3ff..ecde90d7a964 100755 --- a/test/shell.d/lock-wake-key-test.sh +++ b/test/shell.d/lock-wake-key-test.sh @@ -17,7 +17,7 @@ assert( const service = fs.readFileSync(path.join(root, 'shell/plugins/lock/Service.qml'), 'utf8') assert( - /id: resumeWatchTimer[\s\S]*?running: root\.lockRequested[\s\S]*?now - lastTick > interval \+ 2000[\s\S]*?if \(resumed\) \{[\s\S]*?root\.runWake\(\)/.test(service), + /id: resumeWatchTimer[\s\S]*?running: root\.lockRequested[\s\S]*?now - lastTick > interval \+ 2000[\s\S]*?if \(resumed\) \{[\s\S]*?root\.runWake\(root\.wakeRunUp\)/.test(service), 'a resume clears the blank state, so the first key at a lit lock is typed' ) diff --git a/test/shell.d/video-background-test.sh b/test/shell.d/video-background-test.sh index b074c5f932a1..4d95233708da 100755 --- a/test/shell.d/video-background-test.sh +++ b/test/shell.d/video-background-test.sh @@ -157,9 +157,9 @@ assert( lockService.includes('function screenBlank(screenName)') && lockService.includes('function applyMonitorDpms(text)') && lockService.includes('command: ["hyprctl", "monitors", "-j"]') && - lockService.includes('running: root.locked && root.videoBackground') && + lockService.includes('running: root.locked && (root.videoBackground || root.displaysBlank)') && /displaysBlank: root\.screenBlank\(lockSurface\.screen/.test(lockService) && - /function runWake\(\) \{[\s\S]*?root\.monitorDpmsKnown = false/.test(lockService) && + /function runWake\([^)]*\) \{[\s\S]*?root\.monitorDpmsKnown = false/.test(lockService) && /function runBlank\(\) \{[\s\S]*?root\.monitorDpmsKnown = false/.test(lockService), 'a locked video wallpaper follows what each panel actually did, not only what the lock asked for' ) @@ -168,8 +168,8 @@ assert( lockView.includes('&& !root.powerSaverActive') && /displaysBlank: root\.screenBlank\(/.test(lockService) && /powerSaverActive: root\.powerSaverActive/.test(lockService) && - /function runBlank\(\) \{\s*\n\s*root\.displaysBlank = true/.test(lockService) && - /function runWake\(\) \{\s*\n\s*root\.displaysBlank = false/.test(lockService), + /function runBlank\(\) \{[\s\S]*?root\.displaysBlank = true/.test(lockService) && + /function runWake\([^)]*\) \{[\s\S]*?root\.displaysBlank = false/.test(lockService), 'the lock screen stops playback once displays go dark or power-saver is active' ) assert(