aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-09 18:13:59 +0100
committerDanilo M. <danix@danix.xyz>2026-11-09 18:13:59 +0100
commit1cd822b825145d921d678c27b0600c78e576826b (patch)
tree30ca0c9079eb497f3c94ddbd79f7d0cda07411a9
parent4601ce03a1b41158f94e1e79edacf8540a89d15f (diff)
downloaddeck-it-1cd822b825145d921d678c27b0600c78e576826b.tar.gz
deck-it-1cd822b825145d921d678c27b0600c78e576826b.zip
feat: virtual monitor setup
-rw-r--r--deckit/display.py61
-rw-r--r--tests/test_display.py25
2 files changed, 86 insertions, 0 deletions
diff --git a/deckit/display.py b/deckit/display.py
new file mode 100644
index 0000000..f4d85f4
--- /dev/null
+++ b/deckit/display.py
@@ -0,0 +1,61 @@
+"""The virtual monitor half: a headless Hyprland output sized to the tablet."""
+
+import json
+import re
+import shutil
+import subprocess
+import sys
+import time
+from pathlib import Path
+
+
+def run(cmd, timeout=2):
+ try:
+ return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout).stdout.strip()
+ except (OSError, subprocess.TimeoutExpired):
+ return ""
+
+
+def tablet_resolution(cfg: dict, adb=None) -> str:
+ res = cfg.get("display", {}).get("resolution", "auto")
+ if res != "auto":
+ return res
+ read = adb or (lambda: run(["adb", "shell", "wm", "size"]))
+ m = re.search(r"(\d+)x(\d+)", read())
+ if not m:
+ sys.exit("Could not read the tablet resolution; set display.resolution in the config")
+ w, h = sorted(map(int, m.groups()), reverse=True)
+ return f"{w}x{h}"
+
+
+def hypr_lua(cfg: dict) -> bool:
+ mode = cfg.get("hyprland", "auto")
+ if mode == "auto":
+ return (Path.home() / ".config/hypr/hyprland.lua").is_file()
+ return mode == "lua"
+
+
+def setup_display(cfg: dict, run_cmd=None) -> None:
+ run_cmd = run_cmd or run
+ d = cfg.get("display", {})
+ mon = d.get("name", "TAB")
+ monitors = json.loads(run_cmd(["hyprctl", "monitors", "-j"]) or "[]")
+ if not any(m["name"] == mon for m in monitors):
+ run_cmd(["hyprctl", "output", "create", "headless", mon])
+ time.sleep(1)
+ mode = f"{tablet_resolution(cfg)}@{d.get('refresh', 60)}"
+ pos = d.get("position", "auto-right")
+ scale = d.get("scale", 1)
+ if hypr_lua(cfg):
+ run_cmd(["hyprctl", "eval",
+ f'hl.monitor({{output="{mon}", mode="{mode}", position="{pos}", scale={scale}}})'])
+ else:
+ run_cmd(["hyprctl", "keyword", "monitor", f"{mon},{mode},{pos},{scale}"])
+ tool = shutil.which("awww") or shutil.which("swww")
+ if d.get("wallpaper", True) and tool:
+ others = [m["name"] for m in monitors if m["name"] != mon]
+ for line in run_cmd([tool, "query"]).splitlines():
+ if others and others[0] in line and "image: " in line:
+ run_cmd([tool, "img", "-o", mon, line.split("image: ", 1)[1]], timeout=10)
+ break
+ print(f"{mon}: {mode} at {pos}")
diff --git a/tests/test_display.py b/tests/test_display.py
new file mode 100644
index 0000000..b5d5b11
--- /dev/null
+++ b/tests/test_display.py
@@ -0,0 +1,25 @@
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
+
+import unittest
+
+import deckit.display as display
+
+
+class Resolution(unittest.TestCase):
+ def test_reads_adb(self):
+ self.assertEqual(
+ display.tablet_resolution({"display": {}}, adb=lambda: "Physical size: 1920x1200"),
+ "1920x1200")
+
+ def test_config_override(self):
+ self.assertEqual(
+ display.tablet_resolution({"display": {"resolution": "2560x1600"}},
+ adb=lambda: "1920x1200"),
+ "2560x1600")
+
+
+if __name__ == "__main__":
+ unittest.main()