diff options
| -rw-r--r-- | AGENTS.md | 3 | ||||
| -rw-r--r-- | deckit/sleep.py | 16 | ||||
| -rw-r--r-- | tests/test_sleep.py | 12 |
3 files changed, 26 insertions, 5 deletions
@@ -83,7 +83,8 @@ Keep the server dependency-free apart from that one package. Before a suspend, poweroff or reboot `deckit/sleep.py` (an elogind delay inhibitor plus `dbus-monitor` on PrepareForSleep/PrepareForShutdown) marks answers with `X-Deckit-Sleep: asleep|shutting down` for 2s, and `app.js` - covers the page with "The PC is <that>" and drops the lock, so the tablet sleeps too; 15s with no answer at all gives the same + covers the page with "The PC is <that>" and drops the lock (a shutdown then + runs `deck-it stop`; login starts it again), so the tablet sleeps too; 15s with no answer at all gives the same overlay as "The PC is not answering". The first plain answer takes both back. - The Windows tab's previews are `grim -T <stableId>` (Hyprland's stableId is the ext-foreign-toplevel identifier), served at `/api/windows/image/<id>` diff --git a/deckit/sleep.py b/deckit/sleep.py index 06cb481..c607cda 100644 --- a/deckit/sleep.py +++ b/deckit/sleep.py @@ -22,6 +22,8 @@ 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. +A shutdown also runs `deck-it stop` first, so the tunnels, wayvnc and the +headless output go cleanly; the next boot's login brings it all back. """ import ctypes @@ -29,10 +31,12 @@ import os import signal import subprocess import time +from pathlib import Path SHOW = 2.0 # the tablet polls every second (0.5 s on the assistant tab) WHY = {"PrepareForSleep": "asleep", "PrepareForShutdown": "shutting down"} why = "" # the server sends it to the tablet while set: "The PC is <why>" +STOP = [str(Path(__file__).resolve().parent.parent / "deck-it"), "stop"] def _die_with_parent(): @@ -60,6 +64,16 @@ def _release(p): p.wait() +def _stop(): + # Its own session: the stop kills this server, and must outlive it. The + # wait is bounded, the inhibitor only holds the shutdown for 5 s. + try: + subprocess.Popen(STOP, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + start_new_session=True).wait(timeout=2.5) + except (OSError, subprocess.TimeoutExpired): + pass + + def watch(): """Runs for the server's life; without elogind or dbus-monitor it does nothing.""" global why @@ -80,6 +94,8 @@ def watch(): elif line == "boolean true" and member: why = WHY[member] time.sleep(SHOW) + if member == "PrepareForShutdown": + _stop() _release(lock) elif line == "boolean false" and member: why = "" diff --git a/tests/test_sleep.py b/tests/test_sleep.py index ced176d..823c69d 100644 --- a/tests/test_sleep.py +++ b/tests/test_sleep.py @@ -60,6 +60,9 @@ class Watch(unittest.TestCase): self.addCleanup(os.environ.__setitem__, "PATH", path) sleep.SHOW = 0.2 sleep.why = "" + stop = sleep.STOP + sleep.STOP = ["sh", "-c", f"echo stop >> {log}"] + self.addCleanup(setattr, sleep, "STOP", stop) taken = [] real = sleep._inhibit sleep._inhibit = lambda: taken.append(real()) or taken[-1] @@ -73,19 +76,20 @@ class Watch(unittest.TestCase): return (f"sleep {at}; echo '{SIG}{member}'; echo ' boolean true'; " f"sleep {back}; echo '{SIG}{member}'; echo ' boolean false'; sleep 1") - def check(self, member, why): + def check(self, member, why, stops=False): taken, log = self.watch(self.announce(member, 0.3, 0.9)) self.assertTrue(until(lambda: sleep.why == why)) self.assertTrue(until(lambda: taken[0].poll() is not None)) # let go: the PC may go self.assertTrue(until(lambda: sleep.why == "")) self.assertTrue(until(lambda: len(taken) == 2 and taken[1].poll() is None)) - self.assertTrue(until(lambda: log.read_text().split() == ["--what=sleep:shutdown"] * 2)) + lock = "--what=sleep:shutdown" + self.assertTrue(until(lambda: log.read_text().split() == [lock, *["stop"] * stops, lock])) def test_suspend_flags_then_releases_and_takes_the_lock_again(self): self.check("PrepareForSleep", "asleep") - def test_shutdown_flags_then_releases(self): - self.check("PrepareForShutdown", "shutting down") + def test_shutdown_flags_stops_deck_it_then_releases(self): + self.check("PrepareForShutdown", "shutting down", stops=True) def test_other_signals_are_ignored(self): taken, _ = self.watch(f"echo '{SIG}SessionNew'; echo ' boolean true'; sleep 1") |
