From ae5527471cf8c07cce6f4b609fbcdd2e2c903500 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 21 Jul 2021 17:30:42 +1000 Subject: [PATCH] [Windows] Improve USB device detection (#282) --- windows/QMK Toolbox/MainWindow.cs | 17 +++----------- windows/QMK Toolbox/USB.cs | 37 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/windows/QMK Toolbox/MainWindow.cs b/windows/QMK Toolbox/MainWindow.cs index 9216e87192..4d6a020cf9 100644 --- a/windows/QMK Toolbox/MainWindow.cs +++ b/windows/QMK Toolbox/MainWindow.cs @@ -120,7 +120,7 @@ public MainWindow(string path) : this() _usb = new Usb(_flasher, _printer); _flasher.Usb = _usb; - StartListeningForDeviceEvents(); + _usb.StartListeningForDeviceEvents(DeviceEvent); } private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) @@ -129,6 +129,8 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) Settings.Default.hexFileCollection = arraylist; Settings.Default.targetSetting = mcuBox.GetItemText(mcuBox.SelectedItem); Settings.Default.Save(); + + _usb.StopListeningForDeviceEvents(); } private void MainWindow_Load(object sender, EventArgs e) @@ -565,19 +567,6 @@ private void DeviceEvent(object sender, EventArrivedEventArgs e) } } - private void StartListeningForDeviceEvents() - { - StartManagementEventWatcher("__InstanceCreationEvent"); - StartManagementEventWatcher("__InstanceDeletionEvent"); - } - - private void StartManagementEventWatcher(string eventType) - { - var watcher = new ManagementEventWatcher($"SELECT * FROM {eventType} WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity' AND TargetInstance.DeviceID LIKE 'USB%'"); - watcher.EventArrived += DeviceEvent; - watcher.Start(); - } - private void autoflashCheckbox_CheckedChanged(object sender, EventArgs e) { if (autoflashCheckbox.Checked) diff --git a/windows/QMK Toolbox/USB.cs b/windows/QMK Toolbox/USB.cs index 5acbe32cbf..ad9b97be68 100644 --- a/windows/QMK Toolbox/USB.cs +++ b/windows/QMK Toolbox/USB.cs @@ -16,12 +16,49 @@ public class Usb private readonly Printing _printer; private readonly Regex DeviceIdRegex = new Regex(@"USB\\VID_([0-9A-F]+).*PID_([0-9A-F]+).*REV_([0-9A-F]+).*"); + private ManagementEventWatcher deviceConnectedWatcher; + private ManagementEventWatcher deviceDisconnectedWatcher; + public Usb(Flashing flasher, Printing printer) { _flasher = flasher; _printer = printer; } + public void StartListeningForDeviceEvents(EventArrivedEventHandler handler) + { + if (deviceConnectedWatcher == null) + { + deviceConnectedWatcher = StartManagementEventWatcher("__InstanceCreationEvent", handler); + } + + if (deviceDisconnectedWatcher == null) + { + deviceDisconnectedWatcher = StartManagementEventWatcher("__InstanceDeletionEvent", handler); + } + } + + public void StopListeningForDeviceEvents() + { + if (deviceConnectedWatcher != null) + { + deviceConnectedWatcher.Stop(); + } + + if (deviceDisconnectedWatcher != null) + { + deviceDisconnectedWatcher.Stop(); + } + } + + private ManagementEventWatcher StartManagementEventWatcher(string eventType, EventArrivedEventHandler handler) + { + var watcher = new ManagementEventWatcher($"SELECT * FROM {eventType} WITHIN 2 WHERE TargetInstance ISA 'Win32_PnPEntity' AND TargetInstance.DeviceID LIKE 'USB%'"); + watcher.EventArrived += handler; + watcher.Start(); + return watcher; + } + public bool DetectBootloaderFromCollection(ManagementObjectCollection collection, bool connected = true) { var found = false;