Files
ricing/quickshell/default/scripts/mouse.py
nico 4547c27aa7 Move mouse widget standalone, add RGB control widget, align peripherals
The mouse battery pill lived in Bar.qml, duplicated per-screen (it
renders once per monitor) and invisible whenever taskbar mode hid the
stats row entirely. Pulled into its own MouseWidget.qml, same
standalone pattern as the theme widget - single instance, click opens
DPI/poll-rate/idle-time/low-battery-threshold controls. mouse_battery.py
folded into the new mouse.py (adds set-dpi-stage/set-poll-rate/
set-idle-time/set-low-battery-threshold on top of the old status-only
script).

New RGBWidget.qml + scripts/rgb.py: per-device mode/color/speed/
brightness for RAM/GPU/mainboard RGB via OpenRGB's SDK server
(autostarted headless in hyprland.lua), plus a one-click "sync all to
accent". Both RAM sticks are grouped into one card/one set of controls
since they're the same model - rgb.py's commands take a comma-separated
device-id list for exactly this. Found and worked around two real
issues along the way: this server build mishandles openrgb-python's
default protocol version (pinned to 3 instead), and set_color() doesn't
reliably force Direct mode first on its own - a device left on a
cycling effect can keep animating right through a color-set call
despite it reporting success, worst on the GPU's own i2c adapter.
Mitigated with an explicit mode switch + verify-and-retry.

Also: hyprland.lua was missing accel_profile entirely, so libinput's
default adaptive curve was always active - flat now, for the 1:1
response a gaming mouse is normally run with (found while chasing an
unrelated OpenRazer DPI reset making it suddenly noticeable).

Widget layout: all three corner widgets now align on a shared row
height instead of each guessing the previous one's width.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dujsk9jYfxUVLtb5B8eC94
2026-10-04 02:12:09 +02:00

85 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Reads/controls the Razer mouse via openrazer.
mouse.py status JSON: battery + dpi + poll rate + idle time +
low-battery threshold, or {"available": false}
mouse.py set-dpi-stage N apply stored DPI stage N (0-4)
mouse.py set-poll-rate HZ one of status's supported_poll_rates
mouse.py set-idle-time SECONDS wireless sleep timeout, 60-900
mouse.py set-low-battery-threshold PCT
Every command prints the same shape "status" does (the new state, after applying any
change) - deliberately never raises, since a traceback on stdout would break the bar's
JSON.parse of this output. {"available": false} covers openrazer not being installed,
its daemon not being reachable, and no device being connected, alike.
openrazer-daemon only scans for devices at its own startup, not on USB hotplug - once
it's running, switching the mouse between its USB cable and its wireless dongle (each
a different USB product ID) goes completely unnoticed until something restarts it. So
an empty device list gets one retry after doing exactly that, instead of just reporting
unavailable until the next full daemon restart for an unrelated reason.
"""
import json
import subprocess
import sys
import time
def get_device():
from openrazer.client import DeviceManager
def devices():
return DeviceManager().devices
found = devices()
if not found:
subprocess.run(["systemctl", "--user", "restart", "openrazer-daemon"],
check=False, capture_output=True, timeout=5)
time.sleep(1.5)
found = devices()
if not found:
raise RuntimeError("no device")
return found[0]
def status_json(d):
stage_index, stages = d.dpi_stages
return {
"available": True,
"name": d.name,
"pct": d.battery_level,
"charging": d.is_charging,
"dpi": d.dpi[0],
"dpi_stage": stage_index,
"dpi_stages": [s[0] for s in stages],
"poll_rate": d.poll_rate,
"supported_poll_rates": d.supported_poll_rates,
"idle_time": d.get_idle_time(),
"low_battery_threshold": d.get_low_battery_threshold(),
}
def main():
args = sys.argv[1:]
try:
d = get_device()
if args[:1] == ["set-dpi-stage"]:
_, stages = d.dpi_stages
n = int(args[1])
d.dpi = stages[n]
elif args[:1] == ["set-poll-rate"]:
d.poll_rate = int(args[1])
elif args[:1] == ["set-idle-time"]:
d.set_idle_time(int(args[1]))
elif args[:1] == ["set-low-battery-threshold"]:
d.set_low_battery_threshold(int(args[1]))
elif args[:1] != ["status"] and args:
sys.exit(__doc__)
print(json.dumps(status_json(d)))
except Exception:
print(json.dumps({"available": False}))
if __name__ == "__main__":
main()