99# startup of the graphical desktop
1010
1111import argparse
12+ import json
1213from shutil import which
1314from sys import argv , exit
1415from subprocess import check_output , CalledProcessError
3435global SWITCHSOUND_BINARY
3536SWITCHSOUND_BINARY = None
3637
37- OPTION_CAPABILITIES = '-?'
38- OPTION_BATTERY = '-b'
39- OPTION_SILENT = '-c'
38+ OPTION_OUTPUT = "-oJSON"
4039OPTION_CHATMIX = '-m'
4140OPTION_SIDETONE = '-s'
4241OPTION_LED = '-l'
@@ -81,61 +80,50 @@ def change_icon():
8180 ind .set_icon_full ("monitor" , "Monitor" )
8281 prevSwitch = 5
8382
84- def fetch_capabilities ():
85- try :
86- # ask HeadsetControl for the available capabilities for the current headset
87- output = check_output ([HEADSETCONTROL_BINARY , OPTION_CAPABILITIES , OPTION_SILENT ])
88- if args .verbose :
89- print ('Cap: ' + str (output , 'utf-8' ))
90-
91- return output
92- except CalledProcessError as e :
93- print (e )
94- return "all"
95-
9683
9784def change_label ():
9885 try :
99- output = check_output ([HEADSETCONTROL_BINARY , OPTION_BATTERY , OPTION_SILENT ])
100- if args .verbose :
101- print ('Bat: ' + str (output , 'utf-8' ))
102-
103- # -1 indicates "Battery is charging"
104- if int (output ) == - 1 :
105- text = 'Chg'
106- # -2 indicates "Battery is unavailable"
107- elif int (output ) == - 2 :
108- text = 'Off'
109- elif int (output ) < 100 :
110- text = str (output , 'utf-8' ) + '%'
111- else :
112- text = str (output , 'utf-8' ) + '%'
86+ output = check_output ([HEADSETCONTROL_BINARY , OPTION_OUTPUT ])
11387 except CalledProcessError as e :
11488 print (e )
11589 text = 'N/A'
90+ else :
91+ if args .verbose :
92+ print ('Bat: ' + str (output , 'utf-8' ))
11693
117- ind .set_label (text , '999%' )
118- charge .get_child ().set_text ('Charge: ' + text )
119-
94+ j = json .loads (output )
95+ if len (j ["devices" ]) >= 1 :
96+ dev = j ["devices" ][0 ]
97+ bat = dev ["battery" ]
98+ level = bat .get ("level" )
99+ status = bat .get ("status" )
100+ if level >= 0 :
101+ text = f"{ level } %"
102+ else :
103+ if status == "BATTERY_AVAILABLE" :
104+ text = "Ava"
105+ elif status == "BATTERY_UNAVAILABLE" :
106+ text = "Off"
107+ elif status == "BATTERY_CHARGING" :
108+ text = "Chg"
109+ else :
110+ text = "Unknown"
111+ cm = dev .get ("chatmix" )
112+ if cm :
113+ chatmix .get_child ().set_text (f"ChatMix: { cm } " )
114+ else :
115+ chatmix .get_child ().set_text (f"ChatMix: N/A" )
120116
121- def change_chatmix ():
122- global chatmix
123117
124- try :
125- output = check_output ([HEADSETCONTROL_BINARY , OPTION_CHATMIX , OPTION_SILENT ])
126- if args .verbose :
127- print ("ChatMix: " + str (output , 'utf-8' ))
128- chatmix .get_child ().set_text ('ChatMix: ' + str (output , 'utf-8' ))
129- except CalledProcessError as e :
130- print (e )
131- chatmix .get_child ().set_text ('ChatMix: N/A' )
118+ ind .set_label (text , '999%' )
119+ charge .get_child ().set_text ('Charge: ' + text )
132120
133121
134122def set_sidetone (dummy , level ):
135123 if args .verbose :
136124 print ("Set sidetone to: " + str (level ))
137125 try :
138- output = check_output ([HEADSETCONTROL_BINARY , OPTION_SIDETONE , str (level ), OPTION_SILENT ])
126+ output = check_output ([HEADSETCONTROL_BINARY , OPTION_SIDETONE , str (level ), OPTION_OUTPUT ])
139127 if args .verbose :
140128 print ("Result: " + str (output , 'utf-8' ))
141129 except CalledProcessError as e :
@@ -148,7 +136,7 @@ def set_inactive_time(dummy, level):
148136 if args .verbose :
149137 print ("Set inactive-time to: " + str (level ))
150138 try :
151- output = check_output ([HEADSETCONTROL_BINARY , OPTION_INACTIVE_TIME , str (level ), OPTION_SILENT ])
139+ output = check_output ([HEADSETCONTROL_BINARY , OPTION_INACTIVE_TIME , str (level ), OPTION_OUTPUT ])
152140 if args .verbose :
153141 print ("Result: " + str (output , 'utf-8' ))
154142 except CalledProcessError as e :
@@ -161,7 +149,7 @@ def set_led(dummy, level):
161149 if args .verbose :
162150 print ("Set LED to: " + str (level ))
163151 try :
164- output = check_output ([HEADSETCONTROL_BINARY , OPTION_LED , str (level ), OPTION_SILENT ])
152+ output = check_output ([HEADSETCONTROL_BINARY , OPTION_LED , str (level ), OPTION_OUTPUT ])
165153 if args .verbose :
166154 print ("Result: " + str (output , 'utf-8' ))
167155 except CalledProcessError as e :
@@ -313,13 +301,8 @@ def switch_menu():
313301
314302
315303def refresh (dummy ):
316- cap = fetch_capabilities ()
317-
318304 change_icon ()
319- if "all" == cap or b'b' in cap :
320- change_label ()
321- if "all" == cap or b'm' in cap :
322- change_chatmix ()
305+ change_label ()
323306
324307 # return True to keep the timer running
325308 return True
0 commit comments