forked from centic9/headset-charge-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadset-charge-indicator.py
executable file
·425 lines (331 loc) · 12 KB
/
headset-charge-indicator.py
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
#
# Simple AppIndicator which uses the HeadsetControl application from
# https://github.com/Sapd/HeadsetControl/ for retrieving charge information
# for wireless headsets and displays it as app-indicator
#
#
# Simple start this application as background process, i.e. during
# startup of the graphical desktop
import argparse
from shutil import which
from sys import argv, exit
from subprocess import check_output, CalledProcessError
from gi import require_version
require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
try:
require_version('AyatanaAppIndicator3', '0.1')
from gi.repository import AyatanaAppIndicator3 as appindicator
print("Using AyatanaAppIndicator")
except ValueError:
require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as appindicator
print("Using AppIndicator")
APPINDICATOR_ID = 'headset-charge-indicator'
global HEADSETCONTROL_BINARY
HEADSETCONTROL_BINARY = None
global SWITCHSOUND_BINARY
SWITCHSOUND_BINARY = None
OPTION_CAPABILITIES = '-?'
OPTION_BATTERY = '-b'
OPTION_SILENT = '-c'
OPTION_CHATMIX = '-m'
OPTION_SIDETONE = '-s'
OPTION_LED = '-l'
OPTION_INACTIVE_TIME = '-i'
global ind
ind = None
global chatmix
chatmix = None
global charge
charge = None
global prevSwitch
prevSwitch = 0
def change_icon():
global prevSwitch
try:
if SWITCHSOUND_BINARY is not None:
check_output([SWITCHSOUND_BINARY, "-1"])
if prevSwitch == 0:
# exit 0 means we could not find out, so set some other icon
ind.set_icon_full("audio-card", "Audio Card")
else:
ind.set_icon_full("audio-headset-symbolic", "Headset")
# ind.set_attention_icon_full("audio-headset-symbolic", "Headset")
except CalledProcessError as e:
print("Response: " + str(e.returncode) + ": " + str(e))
if e.returncode == 1:
ind.set_icon_full("audio-speakers", "Audio Card")
prevSwitch = 1
elif e.returncode == 2:
# ind.set_icon_full("audio-headset", "Headset")
ind.set_icon_full("audio-headset-symbolic", "Headset")
# ind.set_attention_icon_full("audio-headset-symbolic", "Headset")
prevSwitch = 2
elif e.returncode == 3:
ind.set_icon_full("audio-headphones", "USB")
prevSwitch = 3
else:
ind.set_icon_full("audio-input-microphone", "Speakerphone")
prevSwitch = 4
def fetch_capabilities():
try:
# ask HeadsetControl for the available capabilities for the current headset
output = check_output([HEADSETCONTROL_BINARY, OPTION_CAPABILITIES, OPTION_SILENT])
if args.verbose:
print('Cap: ' + str(output, 'utf-8'))
return output
except CalledProcessError as e:
print(e)
return "all"
def change_label():
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_BATTERY, OPTION_SILENT])
if args.verbose:
print('Bat: ' + str(output, 'utf-8'))
# -1 indicates "Battery is charging"
if int(output) == -1:
text = 'Chg'
# -2 indicates "Battery is unavailable"
elif int(output) == -2:
text = 'Off'
elif int(output) < 100:
text = str(output, 'utf-8') + '%'
else:
text = str(output, 'utf-8') + '%'
except CalledProcessError as e:
print(e)
text = 'N/A'
ind.set_label(text, '999%')
charge.get_child().set_text('Charge: ' + text)
def change_chatmix():
global chatmix
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_CHATMIX, OPTION_SILENT])
if args.verbose:
print("ChatMix: " + str(output, 'utf-8'))
chatmix.get_child().set_text('ChatMix: ' + str(output, 'utf-8'))
except CalledProcessError as e:
print(e)
chatmix.get_child().set_text('ChatMix: N/A')
def set_sidetone(dummy, level):
if args.verbose:
print("Set sidetone to: " + str(level))
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_SIDETONE, str(level), OPTION_SILENT])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
print(e)
return True
def set_inactive_time(dummy, level):
if args.verbose:
print("Set inactive-time to: " + str(level))
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_INACTIVE_TIME, str(level), OPTION_SILENT])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
print(e)
return True
def set_led(dummy, level):
if args.verbose:
print("Set LED to: " + str(level))
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_LED, str(level), OPTION_SILENT])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
print(e)
return True
def switch_sound(dummy, level):
if args.verbose:
print("Switch sound to: " + str(level))
try:
output = check_output([SWITCHSOUND_BINARY, str(level)])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
print("Result: " + str(e.output, 'utf-8'))
print(e)
# refresh UI after switching
refresh(None)
return True
def sidetone_menu():
# we map 5 levels to the range of [0-128]
# The Steelseries Arctis internally supports 0-0x12, i.e. 0-18
# OFF -> 0
# LOW -> 32
# MEDIUM -> 64
# HIGH -> 96
# MAX -> 128
sidemenu = Gtk.Menu()
off = Gtk.MenuItem(label="off")
off.connect("activate", set_sidetone, 0)
sidemenu.append(off)
off.show_all()
low = Gtk.MenuItem(label="low")
low.connect("activate", set_sidetone, 32)
sidemenu.append(low)
low.show_all()
medium = Gtk.MenuItem(label="medium")
medium.connect("activate", set_sidetone, 64)
sidemenu.append(medium)
medium.show_all()
high = Gtk.MenuItem(label="high")
high.connect("activate", set_sidetone, 96)
sidemenu.append(high)
high.show_all()
maximum = Gtk.MenuItem(label="max")
maximum.connect("activate", set_sidetone, 128)
sidemenu.append(maximum)
maximum.show_all()
return sidemenu
def inactive_time_menu():
# the option allows to set an inactive time between 0 and 90 minutes
# therefore we map a few different time-spans to the range of [0-90]
inactive_time_menu = Gtk.Menu()
off = Gtk.MenuItem(label="off")
off.connect("activate", set_inactive_time, 0)
inactive_time_menu.append(off)
off.show_all()
five = Gtk.MenuItem(label="5 min")
five.connect("activate", set_inactive_time, 5)
inactive_time_menu.append(five)
five.show_all()
fifteen = Gtk.MenuItem(label="15 min")
fifteen.connect("activate", set_inactive_time, 15)
inactive_time_menu.append(fifteen)
fifteen.show_all()
thirty = Gtk.MenuItem(label="30 min")
thirty.connect("activate", set_inactive_time, 30)
inactive_time_menu.append(thirty)
thirty.show_all()
sixty = Gtk.MenuItem(label="60 min")
sixty.connect("activate", set_inactive_time, 60)
inactive_time_menu.append(sixty)
sixty.show_all()
ninety = Gtk.MenuItem(label="90 min")
ninety.connect("activate", set_inactive_time, 90)
inactive_time_menu.append(ninety)
ninety.show_all()
return inactive_time_menu
def led_menu():
ledmenu = Gtk.Menu()
off = Gtk.MenuItem(label="off")
off.connect("activate", set_led, 0)
ledmenu.append(off)
off.show_all()
on = Gtk.MenuItem(label="on")
on.connect("activate", set_led, 1)
ledmenu.append(on)
on.show_all()
return ledmenu
def switch_menu():
switchmenu = Gtk.Menu()
laptop = Gtk.MenuItem(label="Soundcard")
laptop.connect("activate", switch_sound, 1)
switchmenu.append(laptop)
laptop.show_all()
headset = Gtk.MenuItem(label="Headset")
headset.connect("activate", switch_sound, 2)
switchmenu.append(headset)
headset.show_all()
usb = Gtk.MenuItem(label="USB Headset")
usb.connect("activate", switch_sound, 3)
switchmenu.append(usb)
usb.show_all()
usb = Gtk.MenuItem(label="Chat Device")
usb.connect("activate", switch_sound, 4)
switchmenu.append(usb)
usb.show_all()
return switchmenu
def refresh(dummy):
cap = fetch_capabilities()
change_icon()
if "all" == cap or b'b' in cap:
change_label()
if "all" == cap or b'm' in cap:
change_chatmix()
# return True to keep the timer running
return True
def quit_app(source):
Gtk.main_quit()
def locate_headsetcontrol_binary(binary_location):
location = which(binary_location)
if location is None:
print(f"Unable to locate headsetcontrol binary at: {binary_location}")
elif args.verbose:
print(f"Found headsetcontrol binary at: {location}")
return location
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="""
Simple AppIndicator which uses the HeadsetControl application from
https://github.com/Sapd/HeadsetControl/ for retrieving charge information
for wireless headsets and displays it as app-indicator
The application has two optional commandline arguments, one for the location of the
HeadsetControl binary and one for a command to switch between Laptop, Headset
and other devices.
""", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--headsetcontrol-binary', metavar='<path to headsetcontrol binary>', type=str,
help='Optional path to headsetcontrol binary', required=False, default='headsetcontrol',
dest='headsetcontrolbinary')
parser.add_argument('--switch-command', metavar='<device switch command>', type=str,
help='Optional command to switch between Laptop, Headset and other devices', required=False, default=None,
dest='switch_command')
parser.add_argument("--verbose", help="Increase output verbosity", action="store_true")
args = parser.parse_args()
HEADSETCONTROL_BINARY = locate_headsetcontrol_binary(args.headsetcontrolbinary)
if not HEADSETCONTROL_BINARY:
parser.print_usage()
exit(2);
if args.switch_command is not None:
SWITCHSOUND_BINARY = args.switch_command
ind = appindicator.Indicator.new(
APPINDICATOR_ID,
"audio-headset",
appindicator.IndicatorCategory.HARDWARE)
ind.set_status(appindicator.IndicatorStatus.ACTIVE)
ind.set_label("-1%", '999%')
# create a menu with an Exit-item
menu = Gtk.Menu()
menu_items = Gtk.MenuItem(label="Refresh")
menu.append(menu_items)
menu_items.connect("activate", refresh)
menu_items.show_all()
menu_items = Gtk.MenuItem(label="Charge: -1")
menu.append(menu_items)
menu_items.show_all()
charge = menu_items
menu_items = Gtk.MenuItem(label="Chat: -1")
menu.append(menu_items)
menu_items.show_all()
chatmix = menu_items
menu_items = Gtk.MenuItem(label="Sidetone")
menu.append(menu_items)
menu_items.show_all()
menu_items.set_submenu(sidetone_menu())
menu_items = Gtk.MenuItem(label="LED")
menu.append(menu_items)
menu_items.show_all()
menu_items.set_submenu(led_menu())
menu_items = Gtk.MenuItem(label="Inactive time")
menu.append(menu_items)
menu_items.show_all()
menu_items.set_submenu(inactive_time_menu())
if args.switch_command is not None:
menu_items = Gtk.MenuItem(label="Switch")
menu.append(menu_items)
menu_items.show_all()
menu_items.set_submenu(switch_menu())
menu_items = Gtk.MenuItem(label="Exit")
menu.append(menu_items)
menu_items.connect("activate", quit_app)
menu_items.show_all()
ind.set_menu(menu)
# update information every 60 seconds
GLib.timeout_add(60000, refresh, None)
# refresh values right away
refresh(None)
Gtk.main()