From 312c4c5c063e99967674d9f897ee632a67ccb042 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Mon, 20 Dec 2021 16:21:01 -0500 Subject: [PATCH] Handle device removal/addition --- App.xaml.cs | 19 +++++----------- Common | 2 +- Common.Wpf | 2 +- LightController.cs | 14 ++++++++++++ UsbService.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++ WorkIndicator.csproj | 1 + 6 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 UsbService.cs diff --git a/App.xaml.cs b/App.xaml.cs index 34192a9..80e10fe 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -59,22 +59,13 @@ namespace WorkIndicator // Initialize the tray icon TrayIcon.Initialize(); - Task.Factory.StartNew(CheckUpdate).ContinueWith(task => StartApplication(task.Result.Result)); - } + // Set automatic start into the registry + Current.SetStartWithWindows(Settings.Default.StartWithWindows); - private void StartApplication(bool updateRequired) - { - if (updateRequired) - return; + // Initialize the light controller + LightController.Initialize(); - Task.Factory.StartNew(() => - { - // Set automatic start into the registry - Current.SetStartWithWindows(Settings.Default.StartWithWindows); - - // Initialize the light controller - LightController.Initialize(); - }); + Task.Factory.StartNew(CheckUpdate); } private void HandleCommandLine(object sender, InterprocessMessageListener.InterprocessMessageEventArgs e) diff --git a/Common b/Common index 81856b2..4da8f42 160000 --- a/Common +++ b/Common @@ -1 +1 @@ -Subproject commit 81856b27001b1e271184f8dce84e7e061fe4d557 +Subproject commit 4da8f426c0f23f7734aec7c1172a9832afc3a44d diff --git a/Common.Wpf b/Common.Wpf index 8a82786..a71af7d 160000 --- a/Common.Wpf +++ b/Common.Wpf @@ -1 +1 @@ -Subproject commit 8a82786166d49271b22e48aba0ca34cf8d366872 +Subproject commit a71af7de8e59b35c02040735728076b00a6402a3 diff --git a/LightController.cs b/LightController.cs index f49c7d0..8193ae0 100644 --- a/LightController.cs +++ b/LightController.cs @@ -15,11 +15,15 @@ namespace WorkIndicator public static class LightController { private static StoplightIndicator _stoplightIndicator; + private static UsbService _usbService; private static bool _initialized; private static Status _status = Status.Auto; public static void Initialize() { + _usbService = new UsbService(); + _usbService.DevicesChanged += DevicesChanged; + _stoplightIndicator = new StoplightIndicator(); _stoplightIndicator.SetLight(StoplightIndicator.Light.Yellow, StoplightIndicator.LightState.On); @@ -29,6 +33,14 @@ namespace WorkIndicator _initialized = true; } + private static void DevicesChanged() + { + _stoplightIndicator?.Dispose(); + _stoplightIndicator = new StoplightIndicator(); + + UpdateLights(); + } + private static void AudioWatcher_MicrophoneInUseChanged(bool microphoneInUse) { UpdateLights(); @@ -44,6 +56,8 @@ namespace WorkIndicator _stoplightIndicator.SetLights(StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off); _stoplightIndicator.Dispose(); + _usbService.Dispose(); + _initialized = false; } diff --git a/UsbService.cs b/UsbService.cs new file mode 100644 index 0000000..9cd0075 --- /dev/null +++ b/UsbService.cs @@ -0,0 +1,52 @@ +using System; +using System.Windows.Forms; + +namespace WorkIndicator +{ + internal class UsbService : NativeWindow, IDisposable + { + public event Action DevicesChanged; + + private const int WM_DEVICECHANGE = 0x0219; + private const int DBT_DEVICEARRIVAL = 0x8000; + private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; + private const int DBT_DEVNODES_CHANGED = 0x0007; + + private bool _isDisposed; + + internal UsbService() + { + base.CreateHandle(new CreateParams()); + } + + + protected override void WndProc(ref Message msg) + { + base.WndProc(ref msg); + + if (msg.Msg == WM_DEVICECHANGE) + { + switch (msg.WParam.ToInt32()) + { + case DBT_DEVNODES_CHANGED: + case DBT_DEVICEARRIVAL: + case DBT_DEVICEREMOVECOMPLETE: + DevicesChanged?.Invoke(); + break; + } + } + } + + public void Dispose() + { + if (!_isDisposed) + { + base.DestroyHandle(); + + _isDisposed = true; + + GC.SuppressFinalize(this); + } + } + } +} diff --git a/WorkIndicator.csproj b/WorkIndicator.csproj index 607d63e..7e42f81 100644 --- a/WorkIndicator.csproj +++ b/WorkIndicator.csproj @@ -144,6 +144,7 @@ + MSBuild:Compile Designer