77
88namespace SPDIFKA {
99 public partial class SPDIFKAGUI : Form {
10- private const string name = "SPDIF-KA" ;
11- private const string stoppedMessage = "stopped" ;
12- private const string startMessage = "running" ;
13- private static string toolStripStartText = "Start " + name ;
14- private static string toolStripStopText = "Stop " + name ;
10+ private const string AppName = "SPDIF-KA" ;
11+ private const string StoppedMessage = "stopped" ;
12+ private const string RunningMessage = "running" ;
13+ private static string ToolStripStartText = "Start " + AppName ;
14+ private static string ToolStripStopText = "Stop " + AppName ;
1515 private const string UNPLUGGED_SUFFIX = " (*Unplugged*)" ;
16- private bool isAppVisible = true ;
16+ private bool IsAppVisible = true ;
17+ private bool IsAnyTargetedDevicesUnplugged ;
1718
18- private static readonly string version = System . Reflection . Assembly . GetExecutingAssembly ( )
19+ private static readonly string Version = System . Reflection . Assembly . GetExecutingAssembly ( )
1920 . GetName ( )
2021 . Version
2122 . ToString ( ) ;
@@ -43,15 +44,15 @@ public SPDIFKAGUI() {
4344 this . MaximizeBox = false ;
4445
4546 this . spdifka . BalloonTipIcon = ToolTipIcon . Info ;
46- this . spdifka . BalloonTipText = name + " - " + stoppedMessage ;
47- this . spdifka . BalloonTipTitle = name ;
48- this . spdifka . Text = name + " - " + stoppedMessage ;
47+ this . spdifka . BalloonTipText = AppName + " - " + StoppedMessage ;
48+ this . spdifka . BalloonTipTitle = AppName ;
49+ this . spdifka . Text = AppName + " - " + StoppedMessage ;
4950 this . spdifka . Icon = Properties . Resources . bar_chart_64_red ;
5051
51- this . toolStripStart . Text = toolStripStartText ;
52+ this . toolStripStart . Text = ToolStripStartText ;
5253 this . spdifka . ContextMenuStrip = this . RightClickMenuStrip ;
5354 this . Resize += this . Form1_Resize ;
54- this . runningLabel . Text = stoppedMessage ;
55+ this . runningLabel . Text = StoppedMessage ;
5556 this . FormBorderStyle = FormBorderStyle . FixedSingle ;
5657 this . LoadState ( ) ;
5758 this . IsInitializing = false ;
@@ -63,7 +64,7 @@ public SPDIFKAGUI() {
6364 /// </summary>
6465 /// <param name="isVisible"></param>
6566 protected override void SetVisibleCore ( bool isVisible ) {
66- if ( ! this . isAppVisible ) {
67+ if ( ! this . IsAppVisible ) {
6768 isVisible = false ;
6869 if ( ! this . IsHandleCreated ) this . CreateHandle ( ) ;
6970 }
@@ -98,6 +99,15 @@ private void OnPowerChange(object s, PowerModeChangedEventArgs e) {
9899 case PowerModes . Resume :
99100 this . ReloadDevices ( ) ;
100101 this . RestartAudioControlIfRunning ( ) ;
102+ if ( this . IsAnyTargetedDevicesUnplugged ) {
103+ var timer = new Timer { Interval = 5000 } ;
104+ timer . Tick += ( s2 , e2 ) => {
105+ this . ReloadDevices ( ) ;
106+ this . RestartAudioControlIfRunning ( ) ;
107+ timer . Dispose ( ) ;
108+ } ;
109+ timer . Start ( ) ;
110+ }
101111 break ;
102112 case PowerModes . Suspend :
103113 break ;
@@ -106,7 +116,7 @@ private void OnPowerChange(object s, PowerModeChangedEventArgs e) {
106116
107117 private bool IsRegisteredToUsbDeviceNotification = false ;
108118 private void RegisterOrUnregisterUsbDeviceNotificationBasedOnEnabledDevices ( ) {
109- foreach ( var deviceName in UserPrefs . EnabledDeviceNames ) {
119+ foreach ( var deviceName in UserPrefs . TargetedDeviceNames ) {
110120 if ( deviceName != UserPreferences . DEFAULT_AUDIO_DEVICE ) {
111121 if ( ! this . IsRegisteredToUsbDeviceNotification ) {
112122 this . IsRegisteredToUsbDeviceNotification = true ;
@@ -138,22 +148,25 @@ protected override void WndProc(ref Message m) {
138148 }
139149
140150 private void ReloadDevices ( ) {
141- if ( WaveOut . DeviceCount <= 0 ) return ;
142151 this . comboBoxWaveOutDevice . Items . Clear ( ) ;
143152 this . comboBoxWaveOutDevice . Items . Add ( UserPreferences . DEFAULT_AUDIO_DEVICE ) ;
153+ this . IsAnyTargetedDevicesUnplugged = false ;
144154 for ( var deviceId = - 1 ; deviceId < WaveOut . DeviceCount ; deviceId ++ ) {
145155 var capabilities = WaveOut . GetCapabilities ( deviceId ) ;
146156 this . comboBoxWaveOutDevice . Items . Add ( capabilities . ProductName ) ;
147157 }
148- foreach ( var deviceName in UserPrefs . EnabledDeviceNames ) {
149- var found = false ;
150- for ( int i = 0 ; i < this . comboBoxWaveOutDevice . Items . Count ; i ++ ) {
158+ foreach ( var deviceName in UserPrefs . TargetedDeviceNames ) {
159+ var plugged = false ;
160+ for ( var i = 0 ; i < this . comboBoxWaveOutDevice . Items . Count ; i ++ ) {
151161 if ( this . comboBoxWaveOutDevice . Items [ i ] . ToString ( ) . Replace ( UNPLUGGED_SUFFIX , "" ) == deviceName ) {
152162 this . comboBoxWaveOutDevice . SetItemChecked ( i , true ) ;
153- found = true ;
163+ plugged = true ;
154164 }
155165 }
156- if ( ! found ) this . comboBoxWaveOutDevice . Items . Add ( deviceName + UNPLUGGED_SUFFIX ) ;
166+ if ( ! plugged ) {
167+ this . comboBoxWaveOutDevice . Items . Add ( deviceName + UNPLUGGED_SUFFIX ) ;
168+ this . IsAnyTargetedDevicesUnplugged = true ;
169+ }
157170 }
158171 }
159172
@@ -196,12 +209,12 @@ private void Minimize() {
196209
197210 private void minimized_to_notificaton_area ( ) {
198211 if ( UserPrefs . IsMinimizeToNotificationArea ) {
199- this . isAppVisible = false ;
212+ this . IsAppVisible = false ;
200213 this . ShowInTaskbar = false ;
201214 this . Hide ( ) ;
202215 }
203216 else {
204- this . isAppVisible = true ;
217+ this . IsAppVisible = true ;
205218 this . WindowState = FormWindowState . Minimized ;
206219 this . ShowInTaskbar = true ;
207220 }
@@ -211,7 +224,7 @@ private void minimized_to_notificaton_area() {
211224 /// Restore the window to normal user operation mode.
212225 /// </summary>
213226 private void Restore ( ) {
214- this . isAppVisible = true ;
227+ this . IsAppVisible = true ;
215228 this . WindowState = FormWindowState . Normal ;
216229 this . ShowInTaskbar = true ;
217230 this . Show ( ) ;
@@ -271,33 +284,33 @@ private void Exit_application() {
271284 /// <param name="sender"></param>
272285 /// <param name="e"></param>
273286 private void toolStripAbout_Click ( object sender , EventArgs e ) {
274- MessageBox . Show ( "Copyright 2017 handruin.com - Version " + version , "About" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
287+ MessageBox . Show ( "Copyright 2017 handruin.com - Version " + Version , "About" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
275288 }
276289
277290 /// <summary>
278291 /// Generic method to handle the starting and stopping of the SPDIF keep alive audio clip playback.
279292 /// </summary>
280293 private void ToggleStartStop ( ) {
281294 if ( ! AudioControl . Instance . Value . IsRunning ) {
282- this . spdifka . Text = name + " - " + startMessage ;
283- this . toolStripStart . Text = toolStripStopText ;
284- this . runningLabel . Text = startMessage ;
285- this . spdifka . BalloonTipText = name + " - " + startMessage ;
295+ this . spdifka . Text = AppName + " - " + RunningMessage ;
296+ this . toolStripStart . Text = ToolStripStopText ;
297+ this . runningLabel . Text = RunningMessage ;
298+ this . spdifka . BalloonTipText = AppName + " - " + RunningMessage ;
286299 this . startStopButton . Text = "Stop" ;
287300 AudioControl . Instance . Value . Start ( ) ;
288301 this . UpdateTrayIconWhenRunning ( isRunning : true ) ;
289302 }
290303 else {
291- this . spdifka . Text = name + " - " + stoppedMessage ;
304+ this . spdifka . Text = AppName + " - " + StoppedMessage ;
292305 this . startStopButton . Text = "Start" ;
293- this . toolStripStart . Text = toolStripStartText ;
294- this . runningLabel . Text = stoppedMessage ;
295- this . spdifka . BalloonTipText = name + " - " + stoppedMessage ;
306+ this . toolStripStart . Text = ToolStripStartText ;
307+ this . runningLabel . Text = StoppedMessage ;
308+ this . spdifka . BalloonTipText = AppName + " - " + StoppedMessage ;
296309 AudioControl . Instance . Value . Stop ( ) ;
297310 this . UpdateTrayIconWhenRunning ( isRunning : false ) ;
298311 }
299- }
300-
312+ }
313+
301314 /// <summary>
302315 /// Restart the audio control only if the audio was already running.
303316 /// </summary>
@@ -400,25 +413,30 @@ private void silent_sound_CheckedChanged(object sender, EventArgs e) {
400413
401414 private void comboBoxWaveOutDevice_SelectedIndexChanged ( object sender , EventArgs e ) {
402415 if ( this . IsInitializing ) return ;
403- if ( UserPrefs . EnabledDeviceNames == null ) {
404- UserPrefs . EnabledDeviceNames = Properties . Settings . Default . EnabledDeviceNames ;
416+ if ( UserPrefs . TargetedDeviceNames == null ) {
417+ UserPrefs . TargetedDeviceNames = Properties . Settings . Default . TargetedDeviceNames ;
405418 UserPrefs . Save ( ) ;
406419 }
407- UserPrefs . EnabledDeviceNames . Clear ( ) ;
420+ UserPrefs . TargetedDeviceNames . Clear ( ) ;
408421 for ( int i = 0 ; i < this . comboBoxWaveOutDevice . Items . Count ; i ++ ) {
409422 var deviceName = this . comboBoxWaveOutDevice . Items [ i ] . ToString ( ) . Replace ( UNPLUGGED_SUFFIX , "" ) ;
410423 if ( this . comboBoxWaveOutDevice . GetItemChecked ( i ) ) {
411- if ( ! UserPrefs . EnabledDeviceNames . Contains ( deviceName ) ) {
412- UserPrefs . EnabledDeviceNames . Add ( deviceName ) ;
424+ if ( ! UserPrefs . TargetedDeviceNames . Contains ( deviceName ) ) {
425+ UserPrefs . TargetedDeviceNames . Add ( deviceName ) ;
413426 }
414427 }
415428 else {
416- UserPrefs . EnabledDeviceNames . Remove ( deviceName ) ;
429+ UserPrefs . TargetedDeviceNames . Remove ( deviceName ) ;
417430 }
418431 }
419432 UserPrefs . Save ( ) ;
420433 this . RegisterOrUnregisterUsbDeviceNotificationBasedOnEnabledDevices ( ) ;
421434 this . RestartAudioControlIfRunning ( ) ;
422435 }
436+
437+ private void TabsMenu1_Click ( object sender , EventArgs e ) {
438+ this . ReloadDevices ( ) ;
439+ this . RestartAudioControlIfRunning ( ) ;
440+ }
423441 }
424442}
0 commit comments