diff options
Diffstat (limited to 'deckit')
| -rw-r--r-- | deckit/module.py | 3 | ||||
| -rw-r--r-- | deckit/modules/windows.py | 38 | ||||
| -rw-r--r-- | deckit/server.py | 13 |
3 files changed, 50 insertions, 4 deletions
diff --git a/deckit/module.py b/deckit/module.py index 9680c58..dec2e04 100644 --- a/deckit/module.py +++ b/deckit/module.py @@ -38,6 +38,9 @@ class Module: # action -> (content type, max bytes): actions that take a raw body, # such as a voice clip, instead of a small JSON object. uploads: dict[str, tuple[str, int]] = field(default_factory=dict) + # key -> (bytes, content type) or None: images served at + # /api/<module>/image/<key>, such as a window's preview. + image: Callable[[str], tuple[bytes, str] | None] | None = None start: Callable[[], None] | None = None stop: Callable[[], None] | None = None diff --git a/deckit/modules/windows.py b/deckit/modules/windows.py index 1907b59..a15f44c 100644 --- a/deckit/modules/windows.py +++ b/deckit/modules/windows.py @@ -24,6 +24,7 @@ from pathlib import Path from ..module import Module, ModuleError ADDR = re.compile(r"0x[0-9a-f]{1,16}") +STABLE = re.compile(r"[0-9a-f]{1,16}") # Hyprland's stableId: grim -T's toplevel identifier # The user's own entries first, so they win over the system's, as in a launcher. DESKTOP_BASES = (Path.home() / ".local/share/applications", Path("/usr/share/applications")) @@ -39,6 +40,16 @@ def _popen(cmd): subprocess.Popen(cmd, start_new_session=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) +def _shot(stable_id): + """A window's preview, captured even on a hidden workspace (60 ms, ~20 KB).""" + try: + out = subprocess.run(["grim", "-T", stable_id, "-s", "0.3", "-t", "jpeg", "-q", "70", "-"], + capture_output=True, timeout=3).stdout + except (OSError, subprocess.TimeoutExpired): + return None + return (out, "image/jpeg") if out else None + + def _dispatch(cmd): """Run a hyprctl dispatch and wait: the two-step move depends on the order.""" try: @@ -91,6 +102,17 @@ def _desktop_icons(entries: dict) -> dict: return mapping +def workspace_icons(path) -> dict: + """Workspace number -> icon name, from keystone's Workspaces.qml map.""" + # ponytail: a regex over the QML's `1: "web-browser",` lines, read once at + # start; a parser if that file ever stops being a plain map. + try: + text = Path(path).expanduser().read_text() + except OSError: + return {} + return {int(n): name for n, name in re.findall(r'^\s*(\d+):\s*"([A-Za-z0-9._-]+)"', text, re.M)} + + def hypr_lua(cfg: dict) -> bool: mode = cfg.get("hyprland", "auto") if mode == "auto": @@ -119,7 +141,7 @@ def _state(run): except json.JSONDecodeError: return {"windows": [], "workspaces": [], "activeClass": None} wins = sorted( - ({"address": c["address"], "class": c["class"], "title": c["title"], + ({"address": c["address"], "id": c.get("stableId", ""), "class": c["class"], "title": c["title"], "workspace": c["workspace"]["id"], "active": c["address"] == active} for c in clients if c.get("mapped", True) and c["workspace"]["id"] > 0), key=lambda w: (w["workspace"], w["class"])) @@ -134,8 +156,11 @@ def make(cfg: dict) -> Module: lua = hypr_lua(cfg) apps = [str(a) for a in cfg.get("launch", {}).get("apps", [])] workspaces = cfg.get("windows", {}).get("workspaces", list(range(1, 11))) + ws_icons = workspace_icons(cfg.get("windows", {}).get("workspace_icons", "")) entries = _desktop_entries() icons = _desktop_icons(entries) + shot = cfg.get("shot", _shot) + shown = set() # stableIds of the windows the last state listed def workspace(body): n = body.get("n") @@ -176,13 +201,20 @@ def make(cfg: dict) -> Module: def state(): st = _state(run) + shown.clear() + shown.update(w["id"] for w in st["windows"]) for win in st["windows"]: win["icons"] = icons.get(win["class"].lower(), [win["class"].lower()]) known = sorted(set(int(n) for n in workspaces) | set(st["workspaces"])) launches = [{"key": a, "icon": entries[a]["icon"], "label": entries[a]["name"] or a} for a in apps if a in entries] - return {**st, "workspaces": known, "launches": launches} + return {**st, "workspaces": known, "launches": launches, + "wsIcons": {str(n): ws_icons[n] for n in known if n in ws_icons}} + + def image(key): + # Only a window the tablet was shown; never an arbitrary identifier. + return shot(key) if STABLE.fullmatch(key) and key in shown else None - return Module(name="windows", title="Windows", state=state, + return Module(name="windows", title="Windows", state=state, image=image, actions={"workspace": workspace, "focus": focus, "close": close, "move": move, "launch": launch_key}) diff --git a/deckit/server.py b/deckit/server.py index 19edbef..9dd5229 100644 --- a/deckit/server.py +++ b/deckit/server.py @@ -89,7 +89,11 @@ def icon_path(name: str, theme: str | None = None, bases=ICON_BASES): for th in (theme or icon_theme(), "hicolor"): for base in bases: # Themes lay out category/size (candy-icons) or size/category (hicolor). - for sub in ("apps/scalable", "apps/64", "apps/48", "apps/32", "scalable/apps"): + # Apps first; places and preferences hold the workspace icons + # keystone's bar uses (network-server, applications-graphics). + for sub in ("apps/scalable", "apps/64", "apps/48", "apps/32", "scalable/apps", + "places/scalable", "places/48", "scalable/places", + "preferences/scalable", "categories/scalable", "scalable/categories"): path = Path(base) / th / sub / f"{name}.svg" if path.is_file(): return path @@ -207,6 +211,13 @@ class Handler(BaseHTTPRequestHandler): if len(parts) == 2 and parts[1] == "bg": image = background() return self._send(200, image[0], image[1], cache=True) if image else self._send(404) + if len(parts) == 4 and parts[2] == "image": + try: + fn = REGISTRY.get(parts[1]).image + except module.ModuleError: + fn = None + image = fn(parts[3]) if fn else None + return self._send(200, image[0], image[1], cache=True) if image else self._send(404) if len(parts) == 2 and parts[1] == "modules": return self._json(REGISTRY.names()) if len(parts) == 2: |
