-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogibar-hidpp-monitor
More file actions
executable file
·386 lines (340 loc) · 14.3 KB
/
Copy pathlogibar-hidpp-monitor
File metadata and controls
executable file
·386 lines (340 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python3
"""
HID++ Battery Monitor for Logitech Lightspeed devices
- Event-driven: blocking read with timeout, no polling
"""
import hid
import time
import signal
import os
import subprocess
import threading
import tempfile
VID = 0x046d
# Use XDG_RUNTIME_DIR for secure, atomic state files
STATE_DIR = os.path.join(os.environ.get('XDG_RUNTIME_DIR', f'/run/user/{os.getuid()}'), 'logibar')
# HID++ 1.0
RECEIVER_IDX = 0xff
REG_NOTIFICATIONS = 0x00
FLAG_WIRELESS = 0x000100
FLAG_SOFTWARE_PRESENT = 0x000800
# Device configurations
# (wireless_pid, wired_pid, name, waybar_signal)
DEVICES = [
(0xc547, 0xc357, "keyboard", 9), # G915 X TKL
(0xc54d, 0xc09b, "mouse", 10), # PRO X Superlight 2
]
running = True
hid_lock = threading.Lock()
def signal_handler(sig, frame):
global running
running = False
def write_state(name, battery, connected, charging, waybar_signal, state, state_lock):
"""Atomic write to state file with deduplication."""
os.makedirs(STATE_DIR, exist_ok=True, mode=0o700)
state_file = f"{STATE_DIR}/{name}"
new_state = f"{battery}\n{1 if connected else 0}\n{1 if charging else 0}"
try:
# Check if state changed (with lock for thread safety)
with state_lock:
last_written = state.get('_last_written_state')
if last_written == new_state:
return
state['_last_written_state'] = new_state
# Atomic write: write to temp file, then rename
fd, tmp_path = tempfile.mkstemp(dir=STATE_DIR)
try:
os.write(fd, new_state.encode())
os.close(fd)
os.rename(tmp_path, state_file)
except:
os.close(fd)
os.unlink(tmp_path)
raise
subprocess.run(["pkill", f"-RTMIN+{waybar_signal}", "waybar"], stderr=subprocess.DEVNULL)
except:
pass
def safe_enumerate(vid, pid):
with hid_lock:
return [d for d in hid.enumerate(vid, pid) if d['usage_page'] == 0xff00]
def enable_notifications(dev):
flags = FLAG_WIRELESS | FLAG_SOFTWARE_PRESENT
cmd = [0x10, RECEIVER_IDX, 0x80, REG_NOTIFICATIONS, 0x00, (flags >> 8) & 0xff, flags & 0xff]
try:
dev.write(cmd)
time.sleep(0.05)
for _ in range(10):
r = dev.read(64, timeout_ms=50)
if not r:
break
except:
pass
def flush_buffer(dev):
try:
while True:
r = dev.read(64, timeout_ms=5)
if not r:
break
except:
pass
def get_battery_feature_index(dev, device_idx, retries=3):
"""Get battery feature index with retries."""
for attempt in range(retries):
try:
flush_buffer(dev)
cmd = [0x10, device_idx, 0x00, 0x0d, 0x10, 0x04, 0x00]
dev.write(cmd)
for _ in range(20):
r = dev.read(64, timeout_ms=50)
if r and len(r) > 4 and r[0] in [0x10, 0x11] and r[1] == device_idx and r[2] == 0x00:
return r[4]
except:
pass
if attempt < retries - 1:
time.sleep(0.2 * (attempt + 1)) # Backoff
return None
def query_battery(dev, device_idx, feat_idx):
try:
flush_buffer(dev)
cmd = [0x11, device_idx, feat_idx, 0x10] + [0] * 16
dev.write(cmd)
for _ in range(20):
r = dev.read(64, timeout_ms=50)
if r and len(r) > 6 and r[0] == 0x11 and r[1] == device_idx and r[2] == feat_idx:
soc = r[4]
status = r[6]
if 0 < soc <= 100:
return soc, status in [1, 2, 3]
except:
pass
return None, None
def monitor_wireless(wireless_pid, name, waybar_signal, state, state_lock):
device_idx = 0x01
dev = None
while running:
with state_lock:
wired_connected = state.get('wired_connected')
if wired_connected:
if dev:
try:
dev.close()
except:
pass
dev = None
time.sleep(0.5)
continue
try:
devs = safe_enumerate(VID, wireless_pid)
if not devs:
if dev:
try:
dev.close()
except:
pass
dev = None
with state_lock:
was_connected = state.get('wireless_connected')
state['wireless_connected'] = False
wired = state.get('wired_connected')
if was_connected and not wired:
write_state(name, 0, False, False, waybar_signal, state, state_lock)
time.sleep(0.5)
continue
current_path = devs[0]['path']
if dev is None:
dev = hid.device()
dev.open_path(current_path)
# NO set_nonblocking - we want blocking read with timeout
time.sleep(0.1)
enable_notifications(dev)
# Try to query battery with retries
feat_idx = get_battery_feature_index(dev, device_idx)
if feat_idx:
soc, charging = query_battery(dev, device_idx, feat_idx)
if soc:
with state_lock:
state['wireless_connected'] = True
state['battery'] = soc
state['charging'] = charging
state['feat_idx'] = feat_idx
wired = state.get('wired_connected')
if not wired:
write_state(name, soc, True, charging, waybar_signal, state, state_lock)
with state_lock:
wired_just_disconnected = state.get('wired_just_disconnected')
if wired_just_disconnected:
state['wired_just_disconnected'] = False
if wired_just_disconnected:
enable_notifications(dev)
feat_idx = get_battery_feature_index(dev, device_idx)
if feat_idx:
soc, charging = query_battery(dev, device_idx, feat_idx)
if soc:
with state_lock:
state['wireless_connected'] = True
state['battery'] = soc
state['charging'] = charging
state['feat_idx'] = feat_idx
write_state(name, soc, True, charging, waybar_signal, state, state_lock)
# Blocking read with timeout - true event-driven
r = dev.read(64, timeout_ms=1000)
if r and len(r) > 4:
# Connection event (0x41)
if r[2] == 0x41 and r[1] == device_idx:
link_off = r[4] & 0x40
if link_off:
# Device disconnected/off - hide widget
with state_lock:
was_connected = state.get('wireless_connected')
state['wireless_connected'] = False
wired = state.get('wired_connected')
if was_connected and not wired:
write_state(name, 0, False, False, waybar_signal, state, state_lock)
else:
# Device woke up - query battery
time.sleep(0.2)
feat_idx = get_battery_feature_index(dev, device_idx)
if feat_idx:
soc, charging = query_battery(dev, device_idx, feat_idx)
if soc:
with state_lock:
state['wireless_connected'] = True
state['battery'] = soc
state['charging'] = charging
state['feat_idx'] = feat_idx
wired = state.get('wired_connected')
if not wired:
write_state(name, soc, True, charging, waybar_signal, state, state_lock)
# Battery broadcast event (long HID++ 0x11)
else:
with state_lock:
feat_idx = state.get('feat_idx')
if feat_idx and r[0] == 0x11 and r[1] == device_idx and r[2] == feat_idx:
if len(r) > 6:
soc = r[4]
status = r[6]
if 0 < soc <= 100:
charging = status in [1, 2, 3]
with state_lock:
old_battery = state.get('battery')
old_charging = state.get('charging')
state['battery'] = soc
state['charging'] = charging
state['wireless_connected'] = True
wired = state.get('wired_connected')
if (soc != old_battery or charging != old_charging) and not wired:
write_state(name, soc, True, charging, waybar_signal, state, state_lock)
except Exception:
if dev:
try:
dev.close()
except:
pass
dev = None
with state_lock:
was_connected = state.get('wireless_connected')
state['wireless_connected'] = False
wired = state.get('wired_connected')
if was_connected and not wired:
write_state(name, 0, False, False, waybar_signal, state, state_lock)
time.sleep(0.5)
def monitor_wired(wired_pid, name, waybar_signal, state, state_lock):
device_idx = 0xff
dev = None
while running:
try:
devs = safe_enumerate(VID, wired_pid)
if not devs:
if dev:
try:
dev.close()
except:
pass
dev = None
with state_lock:
was_connected = state.get('wired_connected')
state['wired_connected'] = False
state['wired_just_disconnected'] = True
wireless = state.get('wireless_connected')
if was_connected and not wireless:
write_state(name, 0, False, False, waybar_signal, state, state_lock)
time.sleep(0.5)
continue
if dev is None:
dev = hid.device()
dev.open_path(devs[0]['path'])
# NO set_nonblocking - we want blocking read with timeout
feat_idx = get_battery_feature_index(dev, device_idx)
if feat_idx:
soc, charging = query_battery(dev, device_idx, feat_idx)
if soc:
with state_lock:
state['wired_connected'] = True
state['battery'] = soc
state['charging'] = charging
state['wired_feat_idx'] = feat_idx
write_state(name, soc, True, charging, waybar_signal, state, state_lock)
# Blocking read with timeout - true event-driven
with state_lock:
wired_feat_idx = state.get('wired_feat_idx')
if dev and wired_feat_idx:
r = dev.read(64, timeout_ms=1000)
# Verify: long HID++ (0x11), correct device index, correct feature index
if r and len(r) > 6 and r[0] == 0x11 and r[1] == device_idx and r[2] == wired_feat_idx:
soc = r[4]
status = r[6]
if 0 < soc <= 100:
charging = status in [1, 2, 3] # 1=charging, 2=slow charge, 3=full
with state_lock:
old_battery = state.get('battery')
old_charging = state.get('charging')
state['battery'] = soc
state['charging'] = charging
if soc != old_battery or charging != old_charging:
write_state(name, soc, True, charging, waybar_signal, state, state_lock)
else:
time.sleep(1)
except Exception:
if dev:
try:
dev.close()
except:
pass
dev = None
with state_lock:
was_connected = state.get('wired_connected')
state['wired_connected'] = False
state['wired_just_disconnected'] = True
wireless = state.get('wireless_connected')
if was_connected and not wireless:
write_state(name, 0, False, False, waybar_signal, state, state_lock)
time.sleep(0.5)
def monitor_device(wireless_pid, wired_pid, name, waybar_signal):
state = {
'wireless_connected': False,
'wired_connected': False,
'wired_just_disconnected': False,
'battery': None,
'charging': None,
'feat_idx': None,
'wired_feat_idx': None,
'_last_written_state': None,
}
state_lock = threading.Lock()
t_wireless = threading.Thread(target=monitor_wireless, args=(wireless_pid, name, waybar_signal, state, state_lock), daemon=True)
t_wired = threading.Thread(target=monitor_wired, args=(wired_pid, name, waybar_signal, state, state_lock), daemon=True)
t_wireless.start()
t_wired.start()
return [t_wireless, t_wired]
def main():
global running
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
os.makedirs(STATE_DIR, exist_ok=True, mode=0o700)
threads = []
for wireless_pid, wired_pid, name, waybar_signal in DEVICES:
threads.extend(monitor_device(wireless_pid, wired_pid, name, waybar_signal))
while running:
time.sleep(1)
if __name__ == "__main__":
main()