SmartestHome/hosts/kitchen-display/agent/kitchen_display_agent/session.py

48 lines
1.5 KiB
Python

"""Session-environment resolution and process launching.
A trimmed version of hosts/thin-client's/hosts/touch-panel's sway_control.py: this
device has only one Sway workspace and one app (the pantry-kiosk Chromium window), so
there is no workspace-switching or window-focus surface to wrap — just enough to run
`/usr/local/bin/pantry-kiosk [fragment]` with the graphical session's environment.
"""
from __future__ import annotations
import glob
import logging
import os
import subprocess
log = logging.getLogger(__name__)
def runtime_dir() -> str:
return os.environ.get("XDG_RUNTIME_DIR") or f"/run/user/{os.getuid()}"
def session_env() -> dict[str, str]:
env = dict(os.environ)
env["XDG_RUNTIME_DIR"] = runtime_dir()
env.setdefault("DBUS_SESSION_BUS_ADDRESS", f"unix:path={runtime_dir()}/bus")
env.setdefault("WAYLAND_DISPLAY", "wayland-1")
matches = sorted(glob.glob(os.path.join(runtime_dir(), "sway-ipc.*.sock")))
if matches:
env["SWAYSOCK"] = matches[-1]
return env
def launch_pantry_kiosk(fragment: str) -> None:
command = ["/usr/local/bin/pantry-kiosk", fragment] if fragment else ["/usr/local/bin/pantry-kiosk"]
log.info("launching %s", " ".join(command))
try:
subprocess.Popen(
command,
env=session_env(),
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
)
except OSError as exc:
log.error("could not launch pantry-kiosk: %s", exc)