#!/usr/bin/env python3 """Subscribe to the compositor's event socket and write what arrives. This is ``vm-ipc``'s witness, and it runs the **shipped** client (:class:`hhstreamd.ipc.HyprlandIPC`) rather than a second implementation of the same parsing. That is the opposite choice from ``tests/assert_stream.py``, which is deliberately independent of ``hhstream.client`` so a shared framing bug still fails -- and the reason for the difference is what each check is for. ``vm-basic`` asserts a wire format two programs must agree on; this one asserts that *this* client, against *this* compositor, receives events at all. That is the defect it is pointed at. The compositor's event socket is ``/hypr//.socket2.sock``, and at the old 29-character runtime directory the path did not fit in ``sun_path``: ERR ]: Socket2 path is too long. (2) IPC will not work. The *request* socket kept answering ``hyprctl`` -- both ends agreed on the same truncated path -- so everything looked healthy while no event ever arrived. Only a subscriber that waits for a named event can tell the difference, which is why this program exists and why it reports what it saw rather than just its exit status. Every line it writes is flushed, so a caller can tail the file while the compositor is still running. """ from __future__ import annotations import argparse import sys import threading import time from pathlib import Path from hhstreamd.ipc import CompositorUnavailable, HyprlandIPC def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="hhs-ipc-watch", description="Record Hyprland events from the real event socket.", ) parser.add_argument( "--runtime-dir", type=Path, default=Path("/run/hypr-stream"), help="the compositor's runtime directory (default: %(default)s)", ) parser.add_argument( "--out", type=Path, required=True, help="append every event here, one per line" ) parser.add_argument( "--await-event", action="append", default=[], metavar="NAME", help="exit 0 once this event has been seen; repeatable, all must arrive", ) parser.add_argument( "--timeout", type=float, default=120.0, help="give up after this long and exit 1 (default: %(default)ss)", ) args = parser.parse_args(argv) try: ipc = HyprlandIPC.discover(args.runtime_dir) except CompositorUnavailable as exc: print(f"hhs-ipc-watch: {exc}", file=sys.stderr) return 1 out = args.out.open("a", encoding="utf-8", buffering=1) print(f"watching {args.runtime_dir} for {args.await_event or 'everything'}", file=out) wanted = set(args.await_event) seen: set[str] = set() deadline = time.monotonic() + args.timeout # `events()` blocks in `recv`, so the deadline cannot be enforced from the # same thread. Closing the socket from a timer is what unblocks it, and it # is also what a real caller would have to do -- the contract's `events()` # is explicitly a blocking iterator. timer = threading.Timer(args.timeout, ipc.close) timer.daemon = True timer.start() try: for event in ipc.events(): print(f"{event.name}\t{event.data.get('raw', '')}", file=out) if event.name in wanted: seen.add(event.name) if seen >= wanted: print(f"saw {sorted(seen)}", file=out) return 0 if time.monotonic() > deadline: break except CompositorUnavailable as exc: print(f"event stream ended: {exc}", file=out) finally: timer.cancel() ipc.close() out.flush() missing = sorted(wanted - seen) print(f"TIMED OUT after {args.timeout}s; never saw {missing}", file=out) print(f"hhs-ipc-watch: never saw {missing}", file=sys.stderr) out.close() return 1 if __name__ == "__main__": sys.exit(main())