aspect ratio and keep awake
This commit is contained in:
@ -6,10 +6,17 @@ CAPTURING (countdowns, flashes, 3 photos per shoot). Every 4th shoot the
|
||||
12 photos are composed onto a contact sheet and printed. Five minutes of
|
||||
inactivity brings up the fish tank screensaver.
|
||||
|
||||
While running, the booth re-execs itself under systemd-inhibit so the
|
||||
machine will not sleep or suspend (and, when the lock is available, ignores
|
||||
the laptop lid) - it can run headless with the lid closed.
|
||||
|
||||
Run with --windowed for testing without taking over the display.
|
||||
Esc or Q quits.
|
||||
"""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import tomllib
|
||||
@ -26,6 +33,51 @@ ROOT = Path(__file__).resolve().parent
|
||||
|
||||
IDLE, PREVIEW, COUNTDOWN, FLASH, SCREENSAVER = range(5)
|
||||
|
||||
KEEP_AWAKE_ENV = "PHOTOBOOTH_INHIBITED"
|
||||
KEEP_AWAKE_WHY = "Photobooth is running (keep awake, ignore lid)"
|
||||
|
||||
|
||||
def _probe_inhibit_locks():
|
||||
"""Largest lock set systemd-inhibit can hold on this machine.
|
||||
|
||||
Returns a --what value like "sleep:idle:handle-lid-switch", or None when
|
||||
systemd-inhibit is missing or no lock can be taken. handle-lid-switch
|
||||
allows only one block holder, so it is skipped when a desktop
|
||||
environment already holds it. Each probe runs `true` under the lock, so
|
||||
locks are taken and released instantly.
|
||||
"""
|
||||
inhibit = shutil.which("systemd-inhibit")
|
||||
if not inhibit:
|
||||
return None
|
||||
for what in ("sleep:idle:handle-lid-switch", "sleep:idle", "sleep"):
|
||||
probe = subprocess.run([inhibit, "--mode=block", f"--what={what}",
|
||||
"--", "true"], capture_output=True)
|
||||
if probe.returncode == 0:
|
||||
return what
|
||||
return None
|
||||
|
||||
|
||||
def reexec_keep_awake():
|
||||
"""Re-exec this process under systemd-inhibit (once) so the machine
|
||||
stays awake - and ignores the lid when that lock is free - while the
|
||||
booth runs. Locks are released when the program exits. Best-effort:
|
||||
hosts without systemd-inhibit simply run without locks."""
|
||||
if os.environ.get(KEEP_AWAKE_ENV):
|
||||
return
|
||||
what = _probe_inhibit_locks()
|
||||
if not what:
|
||||
return
|
||||
os.environ[KEEP_AWAKE_ENV] = "1"
|
||||
argv = [shutil.which("systemd-inhibit"), "--mode=block",
|
||||
f"--what={what}", f"--why={KEEP_AWAKE_WHY}", "--",
|
||||
sys.executable, str(ROOT / "photobooth.py"), *sys.argv[1:]]
|
||||
# flush: execvp replaces the process without draining stdout's buffer.
|
||||
print(f"Keep-awake: re-running under systemd-inhibit ({what})", flush=True)
|
||||
try:
|
||||
os.execvp(argv[0], argv)
|
||||
except OSError as e:
|
||||
print(f"Keep-awake: re-exec failed ({e}); running without locks")
|
||||
|
||||
|
||||
def load_config():
|
||||
with open(ROOT / "config.toml", "rb") as f:
|
||||
@ -290,6 +342,7 @@ class Booth:
|
||||
|
||||
|
||||
def main():
|
||||
reexec_keep_awake()
|
||||
windowed = "--windowed" in sys.argv
|
||||
booth = Booth(windowed=windowed)
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user