diff options
Diffstat (limited to 'deckit')
| -rw-r--r-- | deckit/modules/obs.py | 18 | ||||
| -rw-r--r-- | deckit/modules/windows.py | 10 | ||||
| -rw-r--r-- | deckit/obsclient.py | 5 | ||||
| -rw-r--r-- | deckit/server.py | 7 |
4 files changed, 30 insertions, 10 deletions
diff --git a/deckit/modules/obs.py b/deckit/modules/obs.py index f4bf71a..96217c4 100644 --- a/deckit/modules/obs.py +++ b/deckit/modules/obs.py @@ -18,6 +18,7 @@ def make(cfg: dict) -> Module: host=o.get("host", "127.0.0.1"), port=o.get("port", 4455), password=o.get("password", "")) lock = threading.Lock() stop = threading.Event() + last_error = {"msg": None} def req(kind, data=None): with lock: @@ -28,13 +29,14 @@ def make(cfg: dict) -> Module: if not getattr(client, "connected", False): try: client.connect() - except Exception: - pass + last_error["msg"] = None + except Exception as e: + last_error["msg"] = str(e) or type(e).__name__ stop.wait(5) def state(): if not getattr(client, "connected", False): - return {"connected": False} + return {"connected": False, "error": last_error["msg"]} try: scenes = req("GetSceneList") rec = req("GetRecordStatus") @@ -80,11 +82,17 @@ def make(cfg: dict) -> Module: raise ModuleError("scene required") req("SetCurrentProgramScene", {"sceneName": name}) + def flag(body): + on = body.get("on") + if not isinstance(on, bool): + raise ModuleError('"on" must be true or false') + return on + def record(body): - req("StartRecord" if body.get("on") else "StopRecord") + req("StartRecord" if flag(body) else "StopRecord") def stream(body): - req("StartStream" if body.get("on") else "StopStream") + req("StartStream" if flag(body) else "StopStream") def mute(body): req("SetInputMute", {"inputName": str(body.get("input")), "inputMuted": bool(body.get("muted"))}) diff --git a/deckit/modules/windows.py b/deckit/modules/windows.py index 022bdd7..d02fc0c 100644 --- a/deckit/modules/windows.py +++ b/deckit/modules/windows.py @@ -45,7 +45,7 @@ def _state(run): try: clients = json.loads(run(["hyprctl", "clients", "-j"]) or "[]") active = json.loads(run(["hyprctl", "activewindow", "-j"]) or "{}").get("address") - monitors = json.loads(run(["hyprctl", "monitors", "-j"]) or "[]") + workspaces = json.loads(run(["hyprctl", "workspaces", "-j"]) or "[]") except json.JSONDecodeError: return {"windows": [], "workspaces": [], "activeClass": None} wins = sorted( @@ -54,7 +54,8 @@ def _state(run): for c in clients if c.get("mapped", True) and c["workspace"]["id"] > 0), key=lambda w: (w["workspace"], w["class"])) active_class = next((w["class"] for w in wins if w["active"]), None) - return {"windows": wins, "workspaces": [m["activeWorkspace"]["id"] for m in monitors], + return {"windows": wins, + "workspaces": sorted(w["id"] for w in workspaces if w.get("id", 0) > 0), "activeClass": active_class} @@ -88,5 +89,8 @@ def make(cfg: dict) -> Module: raise ModuleError("unknown launch key") _popen(["sh", "-c", command] if isinstance(command, str) else command) - return Module(name="windows", title="Windows", state=lambda: _state(run), + def state(): + return {**_state(run), "launches": list(launch)} + + return Module(name="windows", title="Windows", state=state, actions={"workspace": workspace, "focus": focus, "close": close, "launch": launch_key}) diff --git a/deckit/obsclient.py b/deckit/obsclient.py index 2a8fd8c..181225a 100644 --- a/deckit/obsclient.py +++ b/deckit/obsclient.py @@ -49,9 +49,14 @@ class ObsClient: self._thread.start() def _recv_loop(self): + import websocket + while not self._stop.is_set(): try: msg = json.loads(self.ws.recv()) + except websocket.WebSocketTimeoutException: + # An idle OBS is not a closed one; keep reading. + continue except Exception: break op, d = msg.get("op"), msg.get("d", {}) diff --git a/deckit/server.py b/deckit/server.py index bbd92cf..bddbb00 100644 --- a/deckit/server.py +++ b/deckit/server.py @@ -129,14 +129,17 @@ class Handler(BaseHTTPRequestHandler): return self._send(404, str(e).encode()) if fn is None: return self._send(404, b"unknown action") - length = int(self.headers.get("Content-Length", 0) or 0) + try: + length = int(self.headers.get("Content-Length", 0) or 0) + except ValueError: + return self._send(400, b"Invalid Content-Length") if self.headers.get("Content-Type") != "application/json" or not 0 < length <= 4096: return self._send(400, b"Expected a small JSON body") try: result = fn(json.loads(self.rfile.read(length))) except module.ModuleError as e: return self._send(400, str(e).encode()) - except (ValueError, AttributeError): + except (ValueError, AttributeError, TypeError, KeyError): return self._send(400, b"Invalid request") if result is None: return self._send(204) |
