Skip to content

Commit

Permalink
[Windows] Improve USB device detection (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Jul 21, 2021
1 parent f9be516 commit ae55274
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
17 changes: 3 additions & 14 deletions windows/QMK Toolbox/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions windows/QMK Toolbox/USB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ae55274

Please sign in to comment.