diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-11 10:10:53 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-11 10:10:53 +0100 |
| commit | ed5ff682efbefd3e90d21ea618473af2844b774d (patch) | |
| tree | 9a25fca9e3aac26792c8eee40f33bf6b843d9cdb /deckit/sleep.py | |
| parent | 6a9a1ae47e758afb9a747a619b5665cb5908834a (diff) | |
| download | deck-it-ed5ff682efbefd3e90d21ea618473af2844b774d.tar.gz deck-it-ed5ff682efbefd3e90d21ea618473af2844b774d.zip | |
feat(sleep): tell the tablet when the PC shuts down or reboots
The delay inhibitor now covers shutdown too, and dbus-monitor also
watches PrepareForShutdown. X-Deckit-Sleep carries the reason
("asleep" or "shutting down") and the page shows "The PC is <reason>",
dropping the wake lock as on suspend.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'deckit/sleep.py')
| -rw-r--r-- | deckit/sleep.py | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/deckit/sleep.py b/deckit/sleep.py index 69b5a05..06cb481 100644 --- a/deckit/sleep.py +++ b/deckit/sleep.py @@ -14,25 +14,25 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -"""Tells the tablet the PC is about to sleep. +"""Tells the tablet the PC is about to sleep, shut down or reboot. -elogind announces a suspend with PrepareForSleep on the system bus, and -waits for it only while someone holds a delay inhibitor (at most -InhibitDelayMaxSec, 5 s by default). deck-it holds one; on the announcement -the server marks every answer for SHOW seconds, so the tablet's next poll -sees it, then lets the inhibitor go and the PC sleeps. On resume it takes -the inhibitor again. +elogind announces a suspend with PrepareForSleep and a poweroff or reboot +with PrepareForShutdown on the system bus, and waits for either only while +someone holds a delay inhibitor (at most InhibitDelayMaxSec, 5 s by +default). deck-it holds one; on the announcement the server marks every +answer for SHOW seconds, so the tablet's next poll sees it, then lets the +inhibitor go. On resume, or a shutdown called off, it takes it again. """ import ctypes import os import signal import subprocess -import threading import time SHOW = 2.0 # the tablet polls every second (0.5 s on the assistant tab) -sleeping = threading.Event() +WHY = {"PrepareForSleep": "asleep", "PrepareForShutdown": "shutting down"} +why = "" # the server sends it to the tablet while set: "The PC is <why>" def _die_with_parent(): @@ -46,8 +46,8 @@ def _inhibit(): # lock delaying every suspend; its own session, so letting go on purpose # kills the tail too. return subprocess.Popen( - ["elogind-inhibit", "--what=sleep", "--mode=delay", "--who=deck-it", - "--why=Tell the tablet the PC is going to sleep", + ["elogind-inhibit", "--what=sleep:shutdown", "--mode=delay", "--who=deck-it", + "--why=Tell the tablet the PC is going to sleep or off", "tail", f"--pid={os.getpid()}", "-f", "/dev/null"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) @@ -62,21 +62,25 @@ def _release(p): def watch(): """Runs for the server's life; without elogind or dbus-monitor it does nothing.""" + global why + rule = "type='signal',interface='org.freedesktop.login1.Manager',member='{}'" try: mon = subprocess.Popen( - ["dbus-monitor", "--system", - "type='signal',interface='org.freedesktop.login1.Manager',member='PrepareForSleep'"], + ["dbus-monitor", "--system", *(rule.format(m) for m in WHY)], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, preexec_fn=_die_with_parent) lock = _inhibit() except OSError: return + member = "" for line in mon.stdout: line = line.strip() - if line == "boolean true": - sleeping.set() + if line.startswith("signal "): + member = next((m for m in WHY if f"member={m}" in line), "") + elif line == "boolean true" and member: + why = WHY[member] time.sleep(SHOW) _release(lock) - elif line == "boolean false": - sleeping.clear() + elif line == "boolean false" and member: + why = "" lock = _inhibit() |
