skeledance/plugin/contents/ui/cava-server.py
ltadeu6 39e7c07309
Replace QML CAVA polling with SSE server for smooth bars
DataSource fork+exec was limited to ~1fps. Now cava pipes into a Python
SSE server (port 5555) and the HTML connects directly via EventSource,
getting updates at up to 60fps without QML overhead.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-07 09:37:58 -03:00

45 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""
Reads CAVA ASCII output from stdin and streams it to the wallpaper
via Server-Sent Events on http://127.0.0.1:5555.
Run as: cava -p cava.conf | python3 cava-server.py
"""
import sys
import threading
import time
from http.server import HTTPServer, BaseHTTPRequestHandler
latest = ""
lock = threading.Lock()
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/event-stream")
self.send_header("Cache-Control", "no-cache")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
last = None
try:
while True:
with lock:
cur = latest
if cur and cur != last:
self.wfile.write(f"data: {cur}\n\n".encode())
self.wfile.flush()
last = cur
time.sleep(0.016) # cap at ~60fps
except Exception:
pass
def log_message(self, *args):
pass
def serve():
HTTPServer(("127.0.0.1", 5555), Handler).serve_forever()
threading.Thread(target=serve, daemon=True).start()
for line in sys.stdin:
with lock:
latest = line.strip()