aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--deckit/modules/actions.py91
-rw-r--r--tests/test_actions.py81
2 files changed, 172 insertions, 0 deletions
diff --git a/deckit/modules/actions.py b/deckit/modules/actions.py
new file mode 100644
index 0000000..276c753
--- /dev/null
+++ b/deckit/modules/actions.py
@@ -0,0 +1,91 @@
+# deck-it - actions module
+# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+#
+# 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 Dashboard's action icons, and the tablet following the PC's lock.
+
+Each action id runs one fixed command; the client sends the id. 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 (the button,
+SUPER+L, hypridle) takes the tablet with it.
+"""
+
+import subprocess
+import threading
+
+from ..module import Module, ModuleError
+from .tablet import _adb, _find_tablet
+
+ACTIONS = {
+ "screenshot": {"label": "Screenshot", "icon": "", "cmd": ["qar-scrotmenu.sh"]},
+ "lock": {"label": "Lock", "icon": "", "cmd": ["lock.sh"]},
+}
+POLL = 2
+
+
+def _popen(cmd):
+ subprocess.Popen(cmd, start_new_session=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+
+
+def _locked():
+ return subprocess.run(["pidof", "hyprlock"], capture_output=True).returncode == 0
+
+
+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:
+ ids = [i for i in cfg.get("dashboard", {}).get("actions", list(ACTIONS)) if i in ACTIONS]
+ popen = cfg.get("popen", _popen)
+ locked = cfg.get("locked", _locked)
+ key = cfg.get("key", _key)
+ stop = threading.Event()
+
+ def run(cmd):
+ def action(body):
+ try:
+ popen(cmd)
+ except OSError as e:
+ raise ModuleError(f"{cmd[0]}: {e.strerror or e}") from e
+ return action
+
+ 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=lambda: {"actions": [{"id": i, "label": ACTIONS[i]["label"], "icon": ACTIONS[i]["icon"]}
+ for i in ids]},
+ actions={i: run(ACTIONS[i]["cmd"]) for i in ids},
+ start=lambda: threading.Thread(target=watch, daemon=True).start(),
+ stop=stop.set,
+ )
diff --git a/tests/test_actions.py b/tests/test_actions.py
new file mode 100644
index 0000000..f6537fa
--- /dev/null
+++ b/tests/test_actions.py
@@ -0,0 +1,81 @@
+# deck-it - tests for actions
+# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+#
+# 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.
+
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
+
+import subprocess
+import unittest
+
+import deckit.config as config
+from deckit.module import ModuleError
+from deckit.modules import actions
+
+
+class Actions(unittest.TestCase):
+ def test_ids_match_the_config(self):
+ self.assertEqual(tuple(actions.ACTIONS), config.ACTION_IDS)
+
+ def test_state_lists_the_configured_actions_in_order(self):
+ m = actions.make({"dashboard": {"actions": ["lock", "screenshot"]}})
+ self.assertEqual([a["id"] for a in m.state()["actions"]], ["lock", "screenshot"])
+ self.assertEqual(set(m.state()["actions"][0]), {"id", "label", "icon"})
+
+ def test_only_configured_actions_run(self):
+ calls = []
+ m = actions.make({"dashboard": {"actions": ["lock"]}, "popen": calls.append})
+ m.actions["lock"]({})
+ self.assertEqual(calls, [["lock.sh"]])
+ self.assertNotIn("screenshot", m.actions)
+
+ def test_screenshot_opens_the_menu(self):
+ calls = []
+ m = actions.make({"dashboard": {"actions": ["screenshot"]}, "popen": calls.append})
+ m.actions["screenshot"]({})
+ self.assertEqual(calls, [["qar-scrotmenu.sh"]])
+
+ def test_missing_command_is_a_module_error(self):
+ def missing(cmd):
+ raise FileNotFoundError(cmd[0])
+ m = actions.make({"dashboard": {"actions": ["lock"]}, "popen": missing})
+ with self.assertRaises(ModuleError):
+ m.actions["lock"]({})
+
+
+class Follow(unittest.TestCase):
+ def test_sleeps_on_lock_and_wakes_on_unlock(self):
+ keys = []
+ was = actions.follow(lambda: True, keys.append, False)
+ was = actions.follow(lambda: True, keys.append, was)
+ was = actions.follow(lambda: False, keys.append, was)
+ self.assertEqual(keys, ["KEYCODE_SLEEP", "KEYCODE_WAKEUP"])
+ self.assertFalse(was)
+
+ def test_nothing_while_unchanged(self):
+ keys = []
+ actions.follow(lambda: False, keys.append, False)
+ self.assertEqual(keys, [])
+
+ def test_adb_failure_is_ignored(self):
+ def boom(code):
+ raise subprocess.CalledProcessError(1, "adb")
+ self.assertTrue(actions.follow(lambda: True, boom, False))
+
+
+if __name__ == "__main__":
+ unittest.main()