# deck-it - actions module # Copyright (C) 2026 Danilo M. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """The tablet following the PC's lock. A watcher checks for hyprlock every POLL seconds and sends the tablet KEYCODE_SLEEP when it appears and KEYCODE_WAKEUP when it goes, so every lock (SUPER+L, hypridle) takes the tablet with it. """ import subprocess import threading from ..module import Module from .tablet import _adb, _find_tablet POLL = 2 def _locked(): try: return subprocess.run(["pidof", "hyprlock"], capture_output=True).returncode == 0 except OSError: return False # no pidof: never locked, and the watcher keeps running def _key(code): serial = _find_tablet() if serial: _adb("-s", serial, "shell", "input", "keyevent", code, timeout=5) def follow(locked, key, was): """One watcher step: a change of lock state sends the tablet to sleep or wakes it. Returns the state seen; an adb failure waits for the next change.""" now = locked() if now != was: try: key("KEYCODE_SLEEP" if now else "KEYCODE_WAKEUP") except (subprocess.SubprocessError, OSError): pass return now def make(cfg: dict) -> Module: locked = cfg.get("locked", _locked) key = cfg.get("key", _key) stop = threading.Event() def watch(): was = locked() # a server started while locked does not sleep the tablet while not stop.wait(POLL): was = follow(locked, key, was) return Module( name="actions", title="Actions", state=dict, start=lambda: threading.Thread(target=watch, daemon=True).start(), stop=stop.set, )