-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathairpods.py
More file actions
executable file
·425 lines (356 loc) · 12.4 KB
/
Copy pathairpods.py
File metadata and controls
executable file
·425 lines (356 loc) · 12.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python3
"""
AirPods battery monitor for Waybar using Apple's AAP protocol.
Registers a BlueZ Profile1 to receive the L2CAP socket, then reads
battery notifications and outputs JSON for Waybar's custom module.
Click-to-connect/disconnect is handled via SIGUSR1.
Based on protocol from:
https://github.com/maniacx/Bluetooth-Battery-Meter
"""
import argparse
import json
import os
import signal
import socket
import subprocess
import sys
import threading
import time
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import gi
gi.require_version("Gio", "2.0")
gi.require_version("GLib", "2.0")
from gi.repository import Gio, GLib
# Apple Accessory Protocol (AAP) constants
AIRPODS_UUID = "74ec2172-0bad-4d01-8f77-997b2be0722a"
PROFILE_PATH = "/com/airpods/waybar/profile"
HANDSHAKE = bytes([
0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
])
REQUEST_NOTIFICATIONS = bytes([
0x04, 0x00, 0x04, 0x00, 0x0f, 0x00, 0xff, 0xff, 0xff, 0xff,
])
BATTERY_PREFIX = bytes([0x04, 0x00, 0x04, 0x00, 0x04, 0x00])
BATT_SINGLE = 0x01
BATT_RIGHT = 0x02
BATT_LEFT = 0x04
BATT_CASE = 0x08
CONNECT_TIMEOUT = 15 # seconds
PROFILE_XML = """
<node>
<interface name="org.bluez.Profile1">
<method name="Release"/>
<method name="NewConnection">
<arg type="o" name="device" direction="in"/>
<arg type="h" name="fd" direction="in"/>
<arg type="a{sv}" name="fd_properties" direction="in"/>
</method>
<method name="RequestDisconnection">
<arg type="o" name="device" direction="in"/>
</method>
</interface>
</node>
"""
# --- Global state ---
mac = None
device_path = None
state = {
"left": None, "right": None, "case": None,
"left_charging": False, "right_charging": False, "case_charging": False,
"connected": False,
"connecting": False,
}
_bus = None
_connecting_timer = None
# --- Waybar output ---
def output():
if state["connecting"]:
data = {
"text": "",
"tooltip": "AirPods connecting\u2026",
"class": "connecting",
"alt": "connecting",
}
elif not state["connected"]:
data = {
"text": "",
"tooltip": "AirPods disconnected",
"class": "disconnected",
"alt": "disconnected",
}
else:
levels = []
tooltip_parts = []
left, right, case = state["left"], state["right"], state["case"]
if left is not None and right is not None:
levels.append((left + right) // 2)
lc = " \u26a1" if state["left_charging"] else ""
rc = " \u26a1" if state["right_charging"] else ""
tooltip_parts.append(f"L: {left}%{lc} R: {right}%{rc}")
elif left is not None:
levels.append(left)
c = " \u26a1" if state["left_charging"] else ""
tooltip_parts.append(f"L: {left}%{c}")
elif right is not None:
levels.append(right)
c = " \u26a1" if state["right_charging"] else ""
tooltip_parts.append(f"R: {right}%{c}")
if case is not None:
c = " \u26a1" if state["case_charging"] else ""
tooltip_parts.append(f"Case: {case}%{c}")
if levels:
avg = levels[0]
cls = "critical" if avg <= 20 else "warning" if avg <= 40 else "good"
data = {
"text": f"{avg}%",
"tooltip": "AirPods\n" + "\n".join(tooltip_parts),
"class": cls,
"alt": "connected",
}
else:
data = {
"text": "",
"tooltip": "AirPods connected (waiting for battery)",
"class": "good",
"alt": "connected",
}
print(json.dumps(data), flush=True)
# --- AAP battery parsing ---
def parse_battery(data):
if len(data) < 12 or data[:6] != BATTERY_PREFIX:
return
count = data[6]
if count < 1 or count > 3:
return
pos = 7
for _ in range(count):
if pos + 4 >= len(data):
break
btype = data[pos]
raw_level = data[pos + 2]
status = data[pos + 3]
charging = (status & 0x01) != 0
disconnected = (status & 0x04) != 0
level = None if raw_level > 100 or disconnected else raw_level
if btype in (BATT_SINGLE, BATT_LEFT) and level is not None:
state["left"] = level
state["left_charging"] = charging
elif btype == BATT_RIGHT and level is not None:
state["right"] = level
state["right_charging"] = charging
elif btype == BATT_CASE and level is not None:
state["case"] = level
state["case_charging"] = charging
pos += 5
output()
def socket_reader(fd):
owned_fd = os.dup(fd)
sock = socket.socket(fileno=owned_fd)
sock.setblocking(True)
sock.settimeout(30)
try:
sock.sendall(HANDSHAKE)
time.sleep(0.3)
sock.sendall(REQUEST_NOTIFICATIONS)
while True:
try:
data = sock.recv(1024)
if not data:
break
parse_battery(data)
except socket.timeout:
try:
sock.sendall(REQUEST_NOTIFICATIONS)
except Exception:
break
except Exception as e:
sys.stderr.write(f"airpods: socket error: {e}\n")
finally:
try:
sock.shutdown(socket.SHUT_RDWR)
except Exception:
pass
sock.close()
state.update(connected=False, left=None, right=None, case=None)
output()
# --- BlueZ Profile1 server ---
class ProfileServer:
def __init__(self, bus):
self._bus = bus
self._reg_id = 0
self._iface = Gio.DBusNodeInfo.new_for_xml(PROFILE_XML).interfaces[0]
def register(self):
self._reg_id = self._bus.register_object(
PROFILE_PATH, self._iface, self._on_method_call, None, None,
)
proxy = Gio.DBusProxy.new_sync(
self._bus, Gio.DBusProxyFlags.NONE, None,
"org.bluez", "/org/bluez", "org.bluez.ProfileManager1", None,
)
proxy.call_sync(
"RegisterProfile",
GLib.Variant.new_tuple(
GLib.Variant("o", PROFILE_PATH),
GLib.Variant("s", AIRPODS_UUID),
GLib.Variant("a{sv}", {
"Name": GLib.Variant("s", "AirPods Waybar"),
"Role": GLib.Variant("s", "client"),
"AutoConnect": GLib.Variant("b", True),
}),
),
Gio.DBusCallFlags.NONE, -1, None,
)
def unregister(self):
if self._reg_id:
self._bus.unregister_object(self._reg_id)
self._reg_id = 0
try:
proxy = Gio.DBusProxy.new_sync(
self._bus, Gio.DBusProxyFlags.NONE, None,
"org.bluez", "/org/bluez", "org.bluez.ProfileManager1", None,
)
proxy.call_sync(
"UnregisterProfile",
GLib.Variant.new_tuple(GLib.Variant("o", PROFILE_PATH)),
Gio.DBusCallFlags.NONE, -1, None,
)
except Exception:
pass
def _on_method_call(self, conn, sender, path, iface, method, params, invocation):
if method == "NewConnection":
_, fd_index, _ = params.unpack()
fd = invocation.get_message().get_unix_fd_list().get(fd_index)
state["connected"] = True
output()
threading.Thread(target=socket_reader, args=(fd,), daemon=True).start()
elif method == "RequestDisconnection":
state.update(connected=False, left=None, right=None, case=None)
output()
invocation.return_value(None)
# --- BlueZ helpers ---
def check_connected(bus):
try:
proxy = Gio.DBusProxy.new_sync(
bus, Gio.DBusProxyFlags.NONE, None,
"org.bluez", device_path, "org.bluez.Device1", None,
)
val = proxy.get_cached_property("Connected")
return val.get_boolean() if val else False
except Exception:
return False
def connect_profile(bus):
proxy = Gio.DBusProxy.new_sync(
bus, Gio.DBusProxyFlags.NONE, None,
"org.bluez", device_path, "org.bluez.Device1", None,
)
for attempt in range(4):
try:
proxy.call_sync(
"ConnectProfile",
GLib.Variant.new_tuple(GLib.Variant("s", AIRPODS_UUID)),
Gio.DBusCallFlags.NONE, 5000, None,
)
return
except Exception as e:
if "InProgress" in str(e) or "busy" in str(e):
time.sleep(1 + attempt)
continue
sys.stderr.write(f"airpods: ConnectProfile: {e}\n")
return
# --- Click-to-toggle via SIGUSR1 ---
def _on_toggle():
global _connecting_timer
if state["connecting"]:
return True
if state["connected"]:
threading.Thread(target=_do_disconnect, daemon=True).start()
else:
state["connecting"] = True
output()
_connecting_timer = threading.Timer(CONNECT_TIMEOUT, _on_connect_timeout)
_connecting_timer.daemon = True
_connecting_timer.start()
threading.Thread(target=_do_connect, daemon=True).start()
return True # keep the signal handler registered
def _do_connect():
try:
subprocess.run(["rfkill", "unblock", "bluetooth"], stderr=subprocess.DEVNULL)
subprocess.run(
["bluetoothctl", "connect", mac],
timeout=CONNECT_TIMEOUT, capture_output=True,
)
except Exception as e:
sys.stderr.write(f"airpods: connect error: {e}\n")
def _do_disconnect():
try:
subprocess.run(
["bluetoothctl", "disconnect", mac],
timeout=10, capture_output=True,
)
except Exception as e:
sys.stderr.write(f"airpods: disconnect error: {e}\n")
def _on_connect_timeout():
if state["connecting"] and not state["connected"]:
state["connecting"] = False
output()
sys.stderr.write("airpods: connection timed out\n")
# --- Main ---
def main():
global mac, device_path, _bus, _connecting_timer
parser = argparse.ArgumentParser(description="AirPods battery monitor for Waybar")
parser.add_argument("mac", nargs="?", default=os.environ.get("AIRPODS_MAC"),
help="Bluetooth MAC address (or set AIRPODS_MAC env var)")
args = parser.parse_args()
if not args.mac:
print("Usage: airpods.py <MAC> or set AIRPODS_MAC", file=sys.stderr)
sys.exit(1)
mac = args.mac
device_path = "/org/bluez/hci0/dev_" + mac.replace(":", "_")
_bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
server = ProfileServer(_bus)
try:
server.register()
except Exception as e:
print(json.dumps({
"text": "err", "tooltip": f"Profile registration failed: {e}",
"class": "critical",
}), flush=True)
sys.exit(1)
if check_connected(_bus):
state["connected"] = True
output()
threading.Thread(target=connect_profile, args=(_bus,), daemon=True).start()
def on_props_changed(conn, sender, path, iface, signal_name, params):
global _connecting_timer
if path != device_path:
return
iface_name, changed, _ = params.unpack()
if iface_name != "org.bluez.Device1" or "Connected" not in changed:
return
connected = bool(changed["Connected"])
state["connected"] = connected
if connected:
state["connecting"] = False
if _connecting_timer:
_connecting_timer.cancel()
threading.Thread(target=connect_profile, args=(_bus,), daemon=True).start()
else:
state.update(left=None, right=None, case=None)
output()
_bus.signal_subscribe(
"org.bluez", "org.freedesktop.DBus.Properties", "PropertiesChanged",
device_path, None, Gio.DBusSignalFlags.NONE, on_props_changed,
)
output()
loop = GLib.MainLoop()
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGUSR1, _on_toggle)
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM, loop.quit)
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, loop.quit)
try:
loop.run()
finally:
server.unregister()
if __name__ == "__main__":
main()