@@ -16,7 +16,7 @@ const APPNAME = "MuteToggler";
16
16
const UUID = APPNAME + "@jebeaudet.com" ;
17
17
const HOME_DIR = GLib . get_home_dir ( ) ;
18
18
19
- var VERBOSE = true ; // VERBOSE value will be changed according to this applet settings.
19
+ let VERBOSE = true ; // VERBOSE value will be changed according to this applet settings.
20
20
21
21
const POTENTIAL_INPUTS = [ "'Capture'" , "'Mic'" ] ;
22
22
@@ -37,23 +37,34 @@ function logError(error) {
37
37
bindtextdomain ( UUID , HOME_DIR + "/.local/share/locale" ) ;
38
38
function _ ( str ) {
39
39
let customTranslation = dgettext ( UUID , str ) ;
40
- if ( customTranslation != str ) {
40
+ if ( customTranslation !== str ) {
41
41
return customTranslation ;
42
42
}
43
43
return gettext ( str ) ;
44
44
}
45
45
46
46
function findFirstMatch ( searchStrings , checkString ) {
47
- for ( let i = 0 ; i < searchStrings . length ; i ++ ) {
48
- if ( checkString . includes ( searchStrings [ i ] ) ) {
49
- return searchStrings [ i ] ;
50
- }
51
- }
52
- return null ; // Return null if no match is found
47
+ for ( let i = 0 ; i < searchStrings . length ; i ++ ) {
48
+ if ( checkString . includes ( searchStrings [ i ] ) ) {
49
+ return searchStrings [ i ] ;
50
+ }
51
+ }
52
+ return null ; // Return null if no match is found
53
53
}
54
54
55
55
MyApplet . prototype = {
56
56
__proto__ : Applet . IconApplet . prototype ,
57
+ settings : null ,
58
+ use_pulseaudio : false ,
59
+ soundcard : null ,
60
+ keybinding : null ,
61
+ use_symbolic_icon : false ,
62
+ tint_symbolic_icon : false ,
63
+ unmuted_color : null ,
64
+ muted_color : null ,
65
+ verbose : true ,
66
+
67
+ applet_is_running : false ,
57
68
58
69
_init : function ( metadata , orientation , panel_height , instance_id ) {
59
70
try {
@@ -66,6 +77,11 @@ MyApplet.prototype = {
66
77
this . set_applet_tooltip ( _ ( "Click to mute/unmute microphone" ) ) ;
67
78
68
79
try {
80
+ this . settings . bindProperty ( Settings . BindingDirection . IN ,
81
+ "use_pulseaudio" ,
82
+ "use_pulseaudio" ,
83
+ this . on_settings_changed ,
84
+ null ) ;
69
85
this . settings . bindProperty ( Settings . BindingDirection . IN ,
70
86
"soundcard" ,
71
87
"soundcard" ,
@@ -105,9 +121,6 @@ MyApplet.prototype = {
105
121
} catch ( e ) {
106
122
logError ( e ) ;
107
123
log ( "Error initializing from Settings, continuing." , true ) ;
108
- this . settings = null ;
109
- this . keybinding = null ;
110
- this . verbose = true
111
124
}
112
125
113
126
if ( this . settings ) {
@@ -135,25 +148,30 @@ MyApplet.prototype = {
135
148
}
136
149
137
150
VERBOSE = this . verbose ;
151
+ // noinspection JSIgnoredPromiseFromCall
138
152
this . evaluate_cmd_line ( ) ;
139
153
} ,
140
154
141
155
refresh_loop : function ( ) {
142
156
this . is_audio_muted ( ) ;
143
- if ( this . applet_is_running )
157
+ if ( this . applet_is_running ) {
144
158
Mainloop . timeout_add ( 1000 , Lang . bind ( this , this . refresh_loop ) ) ;
159
+ }
145
160
} ,
146
161
147
162
is_audio_muted : function ( ) {
148
163
try {
149
- let cmd = [
164
+ const cmd = [
150
165
"sh" ,
151
166
"-c" ,
152
- this . amixer + " sget " + this . input
153
- ] ;
167
+ this . use_pulseaudio
168
+ ? "pactl get-source-mute `pactl get-default-source`"
169
+ : this . amixer + " sget " + this . input
170
+ ] ;
171
+ const match_string = this . use_pulseaudio ? " no" : "] [on]" ;
154
172
Util . spawn_async ( cmd , ( stdout ) => {
155
173
try {
156
- if ( stdout . indexOf ( "] [on]" ) != - 1 ) {
174
+ if ( stdout . indexOf ( match_string ) != = - 1 ) {
157
175
this . set_not_muted_icon ( ) ;
158
176
} else {
159
177
this . set_muted_icon ( ) ;
@@ -168,7 +186,6 @@ MyApplet.prototype = {
168
186
} ,
169
187
170
188
set_not_muted_icon : function ( ) {
171
- this . current_icon = "not_muted" ;
172
189
if ( this . use_symbolic_icon ) {
173
190
this . set_applet_icon_symbolic_name ( "microphone-sensitivity-high-symbolic" ) ;
174
191
if ( this . tint_symbolic_icon ) {
@@ -182,7 +199,6 @@ MyApplet.prototype = {
182
199
} ,
183
200
184
201
set_muted_icon : function ( ) {
185
- this . current_icon = "muted" ;
186
202
if ( this . use_symbolic_icon ) {
187
203
this . set_applet_icon_symbolic_name ( "microphone-disabled-symbolic" ) ;
188
204
if ( this . tint_symbolic_icon ) {
@@ -198,24 +214,25 @@ MyApplet.prototype = {
198
214
evaluate_soundcard : function ( ) {
199
215
return new Promise ( ( resolve , reject ) => {
200
216
// only use specific soundcard if searchstring is not empty
201
- if ( this . soundcard . trim ( ) . length > 0 ) {
202
- // per default use first soundcard
203
- this . soundcard_id = "0" ;
204
-
217
+ if ( this . soundcard ?. trim ( ) . length > 0 ) {
205
218
Util . spawn_async ( [ "sh" , "-c" , "cat /proc/asound/cards" ] , ( stdout ) => {
206
219
try {
207
- // Split the result into lines
220
+ // per default use first soundcard
221
+ let soundcard_id = "0" ;
222
+
223
+ // Split the result into lines
208
224
let lines = stdout . split ( '\n' ) ;
209
225
210
226
// Filter lines that contain the soundcard search string
211
227
let filteredLines = lines . filter ( line => line . includes ( this . soundcard ) ) ;
212
228
213
- // Extract the field after splitting by space and getting the second field (index 1)
229
+ // Extract the field after splitting by space and getting the second field (index 1)
214
230
if ( filteredLines . length > 0 ) {
215
- this . soundcard_id = filteredLines [ 0 ] . split ( ' ' ) [ 1 ] ;
231
+ soundcard_id = filteredLines [ 0 ] . split ( ' ' ) [ 1 ] ;
216
232
}
217
- log ( "Use specific soundcard id: '" + this . soundcard_id + "'" ) ;
218
- this . amixer = this . amixer + " -c " + this . soundcard_id ;
233
+
234
+ log ( "Use specific soundcard id: '" + soundcard_id + "'" ) ;
235
+ this . amixer = this . amixer + " -c " + soundcard_id ;
219
236
resolve ( ) ;
220
237
} catch ( e ) {
221
238
reject ( e ) ;
@@ -245,9 +262,13 @@ MyApplet.prototype = {
245
262
} ) ;
246
263
} ,
247
264
248
- evaluate_cmd_line : async function ( ) {
265
+ evaluate_cmd_line : async function ( ) {
266
+ if ( this . use_pulseaudio ) {
267
+ return ;
268
+ }
269
+
249
270
this . amixer = "amixer" ;
250
- await this . evaluate_soundcard ( ) ;
271
+ await this . evaluate_soundcard ( ) ;
251
272
await this . evaluate_input ( ) ;
252
273
253
274
const parameters = [ "" , " -D pulse" ] ;
@@ -256,22 +277,24 @@ MyApplet.prototype = {
256
277
log ( "Test mixer command '" + cmd + "'" ) ;
257
278
// TODO: Make it async.
258
279
let [ res , stdout ] = GLib . spawn_command_line_sync ( cmd ) ;
259
- if ( res && to_string ( stdout ) . indexOf ( this . input ) != - 1 ) {
280
+ if ( res && to_string ( stdout ) . indexOf ( this . input ) !== - 1 ) {
260
281
this . amixer += param ;
261
282
log ( "Use mixer command '" + this . amixer + "'" ) ;
262
283
break ;
263
284
}
264
285
}
265
286
} ,
266
287
267
- on_applet_clicked : function ( event ) {
288
+ on_applet_clicked : function ( _event ) {
268
289
try {
269
290
let cmd = [
270
291
"sh" ,
271
292
"-c" ,
272
- this . amixer + " set " + this . input + " toggle"
293
+ this . use_pulseaudio
294
+ ? "pactl set-source-mute `pactl get-default-source` toggle"
295
+ : this . amixer + " set " + this . input + " toggle"
273
296
] ;
274
- Util . spawn_async ( cmd , ( stdout ) => {
297
+ Util . spawn_async ( cmd , ( _stdout ) => {
275
298
this . is_audio_muted ( ) ;
276
299
} ) ;
277
300
} catch ( e ) {
@@ -289,7 +312,7 @@ function MyApplet(metadata, orientation, panel_height, instance_id) {
289
312
this . _init ( metadata , orientation , panel_height , instance_id ) ;
290
313
}
291
314
315
+ // noinspection JSUnusedLocalSymbols
292
316
function main ( metadata , orientation , panel_height , instance_id ) {
293
- let myApplet = new MyApplet ( metadata , orientation , panel_height , instance_id ) ;
294
- return myApplet ;
317
+ return new MyApplet ( metadata , orientation , panel_height , instance_id ) ;
295
318
}
0 commit comments