-
Notifications
You must be signed in to change notification settings - Fork 14
/
soundswitch.au3
652 lines (557 loc) · 18 KB
/
soundswitch.au3
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#NoTrayIcon
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.4.0.0
Author: Dustin Wyatt 3.3.0.0, Michael Linck 3.4.0.0
#ce ----------------------------------------------------------------------------
;~ AutoItSetOption("TrayIconDebug", 1)
#include <Color.au3>
#include <Array.au3>
#include <Constants.au3>
;~ --------------------------------------------------------------------------------------------------------
;~ Initialization
;~ --------------------------------------------------------------------------------------------------------
$hotkeys = IniReadSection("sound_switch.ini", "HotKeys")
If @error Then
$err = "sound_switch.ini cannot be located in "
MsgBox(16, "SoundSwitch Error", StringFormat("%s%s", $err, @ScriptDir))
Terminate()
EndIf
Global $config_source1_tray = ""
Global $config_source2_tray = ""
Global $output_tray = ""
Global $global_soundstate = ""
Global $global_os = ""
GetOS()
Global $ctrl = "SysListView321"
Global $ContextClass = StringFormat("[CLASS:#%s]", 32768)
Global $Source1 = IniRead("sound_switch.ini", "Sound Devices", "Source1", "error")
Global $Source2 = IniRead("sound_switch.ini", "Sound Devices", "Source2", "error")
Global $Set1 = IniRead("sound_switch.ini", "Speakers", "Set1", "error")
Global $Set2 = IniRead("sound_switch.ini", "Speakers", "Set2", "error")
Global $icon_hide = Int(IniRead("sound_switch.ini", "Options", "HideIcon", 0))
Global $display_tray_tip = Int(IniRead("sound_switch.ini", "Options", "DisplayTrayTip", 1))
Global $title = IniRead("sound_switch.ini", "Strings", "WindowsTitle", "error")
Global $text = IniRead("sound_switch.ini", "Strings", "WindowsTab", "error")
Global $speakerCountTitle = IniRead("sound_switch.ini", "Strings", "SpeakerCountTitle", "error")
Global $speakerCountTextBegin = IniRead("sound_switch.ini", "Strings", "SpeakerCountTextBegin", "error")
Global $default_dev = IniRead("sound_switch.ini", "Strings", "DefaultDevice", "error")
Global $default_dev_alt = IniRead("sound_switch.ini", "Strings", "DefaultDeviceAltLetter", "error")
Global $default_comm = IniRead("sound_switch.ini", "Strings", "DefaultCommDevice", "error")
Global $default_comm_alt = IniRead("sound_switch.ini", "Strings", "DefaultCommDeviceAltLetter", "error")
Global $ready = IniRead("sound_switch.ini", "Strings", "Ready", "error")
Global $default = IniRead("sound_switch.ini", "Strings", "Default", "error")
For $key = 1 To $hotkeys[0][0]
$msg = StringFormat("Setting %s to %s", $hotkeys[$key][1], $hotkeys[$key][0])
$err = HotKeySet($hotkeys[$key][1], $hotkeys[$key][0])
If $err == 0 Then
$errmsg_fmt = "Error setting %s to %s."
;~ MsgBox(16, "SoundSwitch Error", StringFormat($errmsg_fmt, $hotkeys[$key][0], $hotkeys[$key][1]))
TrayTip("SoundSwitch", StringFormat($errmsg_fmt, $hotkeys[$key][0], $hotkeys[$key][1]), True, 3)
EndIf
Next
If Not $icon_hide Then
Opt("TrayIconHide", 0)
Opt("TrayMenuMode",3)
EndIf
Opt("TrayOnEventMode", 0)
If Not $global_soundstate Then
ItemStates()
EndIf
TraySetToolTip(InfoMessage($global_soundstate))
TraySetClick(8)
;Build source config menus
Global $config_menu = TrayCreateMenu("Config Sources")
Global $config_source1_menu = TrayCreateMenu("Source 1", $config_menu)
Global $config_source2_menu = TrayCreateMenu("Source 2", $config_menu)
;Build source selection menu
Global $output_menu = TrayCreateMenu("Output")
TrayCreateItem("")
Menus()
$refresh_item = TrayCreateItem("Refresh available devices")
$exit_item = TrayCreateItem("Exit")
ReadCommandLine()
While 1
$msg = TrayGetMsg()
If $msg = 0 Then ContinueLoop
If $msg = $TRAY_EVENT_PRIMARYDOUBLE Then SwitchDefault()
For $i = 0 to UBound($global_soundstate)-1
If $msg = $config_source1_tray[$i][0] Then
$Source1 = $config_source1_tray[$i][1]
IniWrite("sound_switch.ini", "Sound Devices", "Source1", $Source1)
ElseIf $msg = $config_source2_tray[$i][0] Then
$Source2 = $config_source2_tray[$i][1]
IniWrite("sound_switch.ini", "Sound Devices", "Source2", $Source2)
ElseIf $msg = $output_tray[$i][0] Then
$ret = SetAsDefault($i)
If $ret = -1 Then
Menus(False)
EndIf
EndIf
Next
If $msg = $refresh_item Then Menus()
If $msg = $exit_item Then Terminate()
WEnd
Func Menus($refetch_states = False)
; If $refetch_states is True then we reopen Sound applet and fetch states
DeleteSubMenus($refetch_states = False)
Dim $config_source1_tray[UBound($global_soundstate)][3]
Dim $config_source2_tray[UBound($global_soundstate)][3]
Dim $output_tray[UBound($global_soundstate)][3]
For $i = 0 To UBound($global_soundstate)-1
$config_source1_tray[$i][0] = TrayCreateItem($global_soundstate[$i][5], $config_source1_menu, -1, 1)
If $global_soundstate[$i][6] = "Source1" Then
TrayItemSetState(-1, $TRAY_CHECKED)
$config_source1_tray[$i][2] = True
Else
$config_source1_tray[$i][2] = False
EndIf
$config_source1_tray[$i][1] = $global_soundstate[$i][5]
$config_source2_tray[$i][0] = TrayCreateItem($global_soundstate[$i][5], $config_source2_menu, -1, 1)
If $global_soundstate[$i][6] = "Source2" Then
TrayItemSetState(-1, $TRAY_CHECKED)
$config_source2_tray[$i][2] = True
Else
$config_source2_tray[$i][2] = False
EndIf
$config_source2_tray[$i][1] = $global_soundstate[$i][5]
$output_tray[$i][0] = TrayCreateItem($global_soundstate[$i][5], $output_menu, -1, 1)
$curr_default = GetDefault($global_soundstate)
If $curr_default = $i Then
TrayItemSetState(-1, $TRAY_CHECKED)
$output_tray[$i][2] = True
Else
$output_tray[$i][2] = False
EndIf
$output_tray[$i][1] = $global_soundstate[$i][5]
Next
EndFunc
Func DeleteSubMenus($refetch_states = False)
; If $refetch_states is True then we reopen Sound applet and fetch states
If IsArray($config_source1_tray) Then
For $i = 0 to UBound($config_source1_tray)-1
TrayItemDelete($config_source1_tray[$i][0])
Next
If $refetch_states Then ItemStates()
$fetched_states = True
EndIf
If IsArray($config_source2_tray) Then
For $i = 0 to UBound($config_source2_tray)-1
TrayItemDelete($config_source2_tray[$i][0])
Next
If not $fetched_states Then ItemStates()
EndIf
If IsArray($output_tray) Then
For $i = 0 to UBound($output_tray)-1
TrayItemDelete($output_tray[$i][0])
Next
If not $fetched_states Then ItemStates()
EndIf
EndFunc
#region Action Functions
Func SwitchSpeakerCount()
OpenSound()
$states = ItemStates()
For $item = 0 To UBound($states) - 1
If $states[$item][0] Then
$device = $item
EndIf
Next
ControlListView($title, $text, $ctrl, "Select", $device)
If ControlCommand($title, $text, "Button1", "IsEnabled") Then
ControlClick($title, $text, "Button1")
WinWait($speakerCountTitle, $speakerCountTextBegin)
$sel = ControlCommand($speakerCountTitle, $speakerCountTextBegin, "ListBox1", "GetCurrentSelection")
If $sel == $Set1 Then
$select_this = $Set2
Else
$select_this = $Set1
EndIf
ControlCommand($speakerCountTitle, $speakerCountTextBegin, "ListBox1", "SelectString", $select_this)
While WinExists($speakerCountTitle, "")
ControlClick($speakerCountTitle, "", "Button1")
Sleep(50)
WEnd
EndIf
If $display_tray_tip Then TrayTip("SoundSwitch", $speakerCountTitle & ": " & $select_this, 1)
CloseSound()
EndFunc ;==>SwitchSpeakerCount
Func SwitchDefault()
OpenSound()
$states = ItemStates()
$source_indexes = SourceIndexes($states)
$curr_def = GetDefault($states)
Select
Case $curr_def = -1
;If no current default then just use Source1
SetAsDefault($source_indexes[0])
Case ($curr_def <> $source_indexes[0]) And ($curr_def <> $source_indexes[1])
;If current default not Source1 or Source2 we'll just use Source1
SetAsDefault($source_indexes[0])
Case $curr_def = $source_indexes[0]
;If current default is Source1 make it Source2...
SetAsDefault($source_indexes[1])
Case $curr_def = $source_indexes[1]
;...or vice-versa
SetAsDefault($source_indexes[0])
EndSelect
CloseSound()
EndFunc ;==>SwitchDefault
Func SwitchDevice()
OpenSound()
$states = ItemStates()
$source_indexes = SourceIndexes($states)
$curr_def = GetDefaultDevice($states)
Select
Case $curr_def = -1
;~ ;If no current default device then use first Ready device
;THIS SHOULDNT HAPPEN
SetAsDefaultDevice(GetReady($states))
Case ($curr_def <> $source_indexes[0]) And ($curr_def <> $source_indexes[1])
;If current default device isn't either of our Sources, just use Source1
SetAsDefaultDevice($source_indexes[0])
Case $curr_def = $source_indexes[0]
;If current default is Source1 make it Source2...
SetAsDefaultDevice($source_indexes[1])
Case $curr_def = $source_indexes[1]
;...or vice-versa
SetAsDefaultDevice($source_indexes[0])
EndSelect
CloseSound()
EndFunc ;==>SwitchDevice
Func SwitchComm()
OpenSound()
$states = ItemStates()
$source_indexes = SourceIndexes($states)
$curr_def = GetDefaultCommDevice($states)
;~ MsgBox(0, "curr def", $curr_def)
;~ _ArrayDisplay($source_indexes)
;~ _ArrayDisplay($states)
Select
Case $curr_def = -1
;~ ;If no current default device then use first Ready device
;THIS SHOULDNT HAPPEN
SetAsDefaultComm(GetReady($states))
Case ($curr_def <> $source_indexes[0]) And ($curr_def <> $source_indexes[1])
;If current default device isn't either of our Sources, just use Source1
SetAsDefaultComm($source_indexes[0])
Case $curr_def = $source_indexes[0]
;If current default is Source1 make it Source2...
SetAsDefaultComm($source_indexes[1])
Case $curr_def = $source_indexes[1]
;...or vice-versa
SetAsDefaultComm($source_indexes[0])
EndSelect
CloseSound()
EndFunc ;==>SwitchComm
Func ScrollDefault()
OpenSound()
$states = ItemStates()
;Find current default
For $i = 0 To UBound($states) - 1
If $states[$i][4] = $default_dev Then
$curr_def = $i
ExitLoop
EndIf
Next
$next = $curr_def
For $i = 0 To UBound($states) - 1
$next = Scroll($next, UBound($states) - 1)
If IsReady($next, $states) Then
SetAsDefault($next)
CloseSound()
Return
EndIf
Next
CloseSound()
EndFunc ;==>ScrollDefault
Func ScrollDevice()
OpenSound()
$states = ItemStates()
;Find current default
For $i = 0 To UBound($states) - 1
If $states[$i][4] = $default_dev Then
$curr_def = $i
ExitLoop
EndIf
Next
$next = $curr_def
For $i = 0 To UBound($states) - 1
$next = Scroll($next, UBound($states) - 1)
If IsReady($next, $states) Then
SetAsDefaultDevice($next)
CloseSound()
Return
EndIf
Next
CloseSound()
EndFunc ;==>ScrollDevice
Func ScrollComm()
OpenSound()
$states = ItemStates()
;Find current default
$curr_def = False
For $i = 0 To UBound($states) - 1
If $states[$i][4] = $default_comm Then
$curr_def = $i
ExitLoop
EndIf
Next
If Not $curr_def Then
For $i = 0 To UBound($states) - 1
If $states[$i][4] = $default_dev Then
$curr_def = $i
ExitLoop
EndIf
Next
EndIf
$next = $curr_def
For $i = 0 To UBound($states) - 1
$next = Scroll($next, UBound($states) - 1)
If IsReady($next, $states) Then
SetAsDefaultComm($next)
CloseSound()
Return
EndIf
Next
CloseSound()
EndFunc ;==>ScrollComm
Func OpenSound()
If Not WinExists($title, $text) Then
Run("control.exe /name Microsoft.AudioDevicesAndSoundThemes")
WinWait($title, $text)
WinMove($title, $text, -500, -500)
Return 0
Else
Return -1
EndIf
EndFunc ;==>OpenSound
Func CloseSound()
If WinExists($title, $text) Then
ControlSend($title, $text, "", "{ESC}")
EndIf
EndFunc ;==>CloseSound
Func InfoMessage($states)
$defdev = "not found"
$defcomm = "not found"
For $i = 0 to UBound($states)-1
If $states[$i][0] = True Then
$defdev = $states[$i][5]
EndIf
If $states[$i][1] = True Then
$defcomm = $states[$i][5]
EndIf
Next
Return $default_dev & ": " & $defdev & @CRLF & $default_comm & ": " & $defcomm
EndFunc
Func TipTray($states)
$msg = InfoMessage($states)
TrayTip("SoundSwitch", $msg, 1)
EndFunc
Func Notify($states, $force_tip=False)
TraySetToolTip(InfoMessage($states))
If $display_tray_tip Or $force_tip Then TipTray($states)
EndFunc
Func SetAsDefault($item)
$opened = OpenSound()
If IsReady($item, $global_soundstate) Then
ControlListView($title, $text, $ctrl, "Select", $item)
ControlClick($title, $text, "Button2", "primary")
Else
MsgBox(0, "SoundSwitch", "Device not in 'Ready' state")
If $opened = 0 Then
CloseSound()
EndIf
Return -1
EndIf
$states = ItemStates()
Notify($states)
If $opened = 0 Then
CloseSound()
EndIf
Return 1
EndFunc ;==>SetAsDefault
Func SetAsDefaultComm($item)
If IsReady($item) Then
ControlListView($title, $text, $ctrl, "Select", $item)
If GetOS() = "7" Then
ControlSend($title, $text, "Button2", "{DOWN}" & $default_comm_alt)
ElseIf GetOS() = "Vista" Then
SetAsDefault($item)
EndIf
Else
MsgBox(0, "SoundSwitch", "Device not in 'Ready' state")
EndIf
$states = ItemStates()
Notify($states)
EndFunc ;==>SetAsDefaultComm
Func SetAsDefaultDevice($item)
If IsReady($item) Then
ControlListView($title, $text, $ctrl, "Select", $item)
If GetOS() = "7" Then
ControlSend($title, $text, "Button2", "{DOWN}" & $default_dev_alt)
ElseIf GetOS() = "Vista" Then
SetAsDefault($item)
EndIf
Else
MsgBox(0, "SoundSwitch", "Device not in 'Ready' state")
EndIf
$states = ItemStates()
Notify($states)
EndFunc ;==>SetAsDefaultDevice
#endregion Action Functions
#region Info functions
Func Scroll($curr, $limit)
$curr += 1
if $curr > $limit Then
$curr = 0
EndIf
Return $curr
EndFunc
Func GetReady($items)
;~ Pick first device with Ready status from $items
For $i = 0 To UBound($items) - 1
If $items[$i][4] = $ready Then Return $i
Next
Return -1
EndFunc ;==>GetReady
Func GetOS()
If $global_os Then Return $global_os
$a = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName")
$OS = "XP"
If StringInStr($a, "Windows 7") Then
$OS = "7"
ElseIf StringInStr($a, "Windows Vista") Then
$OS = "Vista"
EndIf
$global_os = $OS
Return $OS
EndFunc ;==>GetOS
Func IsReady($item, $states = False)
If Not $states Then $states = ItemStates()
If $states[$item][4] = $ready Then Return True
If StringInStr($states[$item][4], $default) Then Return True
Return False
EndFunc ;==>IsReady
Func ItemStates()
;--------------------------------------------------------------
; Name: ItemStates
; Description: Opens Sound applet and gets states of all devices
; Returns: Array where each row corresponds to a sound device and the columns are:
; Default Comm, Default Dev, Name, Description, State, Name + Description, Source designation
;--------------------------------------------------------------
If Not WinExists($title, $text) Then
OpenSound()
$close_Sound = True
Else
$close_Sound = False
EndIf
$item_count = ControlListView($title, $text, $ctrl, "GetItemCount")
Dim $item_states[$item_count][7]
$found_comm = False
For $i = 0 To $item_count - 1
$device_type = ControlListView($title, $text, $ctrl, "GetText", $i, 0)
$device_name = ControlListView($title, $text, $ctrl, "GetText", $i, 1)
$device_status = ControlListView($title, $text, $ctrl, "GetText", $i, 2)
$device_matcher = $device_type & " " & $device_name
$item_states[$i][2] = $device_type
$item_states[$i][3] = $device_name
$item_states[$i][4] = $device_status
$item_states[$i][5] = $device_matcher
If StringInStr($device_matcher, $Source1) Then
$item_states[$i][6] = "Source1"
EndIf
If StringInStr($device_matcher, $Source2) Then
$item_states[$i][6] = "Source2"
EndIf
If $device_status = $default_dev Then
$item_states[$i][0] = True
$found_def = $i
Else
$item_states[$i][0] = False
EndIf
If $device_status = $default_comm Then
$item_states[$i][1] = True
$found_comm = True
Else
$item_states[$i][1] = False
EndIf
Next
If Not $found_comm Then
$item_states[$found_def][1] = True
EndIf
If $close_Sound Then
CloseSound()
EndIf
;~ _ArrayDisplay($item_states)
$global_soundstate = $item_states
Return $item_states
EndFunc ;==>ItemStates
Func SourceIndexes($items)
Dim $indexes[2]
For $i = 0 To UBound($items) - 1
If $items[$i][6] = "Source1" Then
$indexes[0] = $i
ElseIf $items[$i][6] = "Source2" Then
$indexes[1] = $i
EndIf
Next
Return $indexes
EndFunc ;==>SourceIndexes
Func GetDefault($items)
For $i = 0 To UBound($items) - 1
If $items[$i][0] And $items[$i][1] Then Return $i
Next
Return -1
EndFunc ;==>GetDefault
Func GetDefaultDevice($items)
For $i = 0 To UBound($items) - 1
If $items[$i][0] Then Return $i
Next
Return -1
EndFunc ;==>GetDefaultDevice
Func GetDefaultCommDevice($items)
For $i = 0 To UBound($items) - 1
If $items[$i][1] Then Return $i
Next
Return -1
EndFunc ;==>GetDefaultCommDevice
Func SetDefaultSource1()
OpenSound()
$states = ItemStates()
$source_indexes = SourceIndexes($states)
SetAsDefault($source_indexes[0])
CloseSound()
EndFunc
Func SetDefaultSource2()
OpenSound()
$states = ItemStates()
$source_indexes = SourceIndexes($states)
SetAsDefault($source_indexes[1])
CloseSound()
EndFunc
Func DisplayDeviceInfo()
$states = ItemStates()
Notify($states, True)
EndFunc
#endregion Info functions
#region Command Line Parameters functions
Func ReadCommandLine()
;only read one/first parameter
If $CmdLine[0] > 0 Then
If $CmdLine[1] = "Set1" Then
SetDefaultSource1()
ElseIf $CmdLine[1] = "Set2" Then
SetDefaultSource2()
EndIf
EndIf
EndFunc
#endregion Command Line Parameters functions
#region Helper functions
Func out($msg)
ConsoleWrite(String($msg))
ConsoleWrite(@LF)
EndFunc ;==>out
Func Terminate()
Exit
EndFunc ;==>Terminate
#endregion Helper functions