|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import socket |
| 3 | +import json |
| 4 | +import sys |
| 5 | + |
| 6 | +# Path to the public RGB observer socket |
| 7 | +SOCKET_PATH = "/run/contextd/public/contextd-rgb-observer.socket" |
| 8 | + |
| 9 | +def main(): |
| 10 | + try: |
| 11 | + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 12 | + sock.connect(SOCKET_PATH) |
| 13 | + except FileNotFoundError: |
| 14 | + print(f"Error: Could not connect to {SOCKET_PATH}. Is contextd running in RGB mode?") |
| 15 | + sys.exit(1) |
| 16 | + |
| 17 | + # Send a subscription request with "more": True |
| 18 | + request = { |
| 19 | + "method": "com.performativenonsense.contextd.rgb.Observer.SubscribeLightingContext", |
| 20 | + "parameters": {}, |
| 21 | + "more": True |
| 22 | + } |
| 23 | + |
| 24 | + message = json.dumps(request).encode('utf-8') + b'\0' |
| 25 | + sock.sendall(message) |
| 26 | + |
| 27 | + print("Subscribed to RGB lighting updates. Press Ctrl+C to stop.") |
| 28 | + |
| 29 | + # Read the stream of responses |
| 30 | + buffer = bytearray() |
| 31 | + try: |
| 32 | + while True: |
| 33 | + chunk = sock.recv(4096) |
| 34 | + if not chunk: |
| 35 | + print("Connection closed by daemon.") |
| 36 | + break |
| 37 | + |
| 38 | + buffer.extend(chunk) |
| 39 | + |
| 40 | + # Process complete messages (separated by null bytes) |
| 41 | + while b'\0' in buffer: |
| 42 | + msg_bytes, buffer = buffer.split(b'\0', 1) |
| 43 | + |
| 44 | + try: |
| 45 | + response = json.loads(msg_bytes.decode('utf-8')) |
| 46 | + if "error" in response: |
| 47 | + print(f"Error: {response['error']}") |
| 48 | + continue |
| 49 | + |
| 50 | + params = response.get("parameters", {}) |
| 51 | + color = params.get("main_color") |
| 52 | + |
| 53 | + if color: |
| 54 | + print(f"New Vibe Color -> R: {color.get('r'):3} | G: {color.get('g'):3} | B: {color.get('b'):3} | A: {color.get('a'):3}") |
| 55 | + |
| 56 | + except json.JSONDecodeError: |
| 57 | + pass |
| 58 | + |
| 59 | + except KeyboardInterrupt: |
| 60 | + print("\nUnsubscribing...") |
| 61 | + finally: |
| 62 | + sock.close() |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + main() |
0 commit comments