mirror of
https://github.com/spice2x/spice2x.github.io.git
synced 2026-08-01 22:30:42 -07:00
rawinput: handle midi scanning asynchronously, fix midi hotplug (#793)
## Link to GitHub Issue or related Pull Request, if one exists Fixes #603 ## Description of change Scanning for midi devices can take a while on Windows 11 with MIDI 2.0 service. Sometimes it'll take a couple seconds. Sometimes, on a PC with zero MIDI devices, it takes 10-11 seconds. This was causing two issues: 1. slow startup time 2. crash on invalid memory access due to a race condition Address both. Also fix a bug: when a device change event fires, we do a MIDI scan, and invalidated all existing MIDI devices in favor of creating new handles. Stop doing this, and instead check for duplicates by matching the ID and keep existing device handles alone. Properly clean up devices on unplug. Also: fix MIDI buttons being stuck on when unplugged while holding a key. ## Testing Tested with Nostroller in MIDI mode. rtpMIDI works too.
This commit is contained in:
@@ -54,7 +54,7 @@ namespace rawinput {
|
||||
switch (hdr->dbch_devicetype) {
|
||||
case DBT_DEVTYP_DEVICEINTERFACE: {
|
||||
auto dev = (const DEV_BROADCAST_DEVICEINTERFACE *) hdr;
|
||||
this->ri_mgr->devices_scan_midi();
|
||||
this->ri_mgr->midi_scan_start();
|
||||
|
||||
// check if class is not HID
|
||||
if (memcmp(&dev->dbcc_classguid, &GUID_HID, sizeof(GUID_HID)) != 0)
|
||||
@@ -112,6 +112,7 @@ namespace rawinput {
|
||||
std::string name(dev->dbcc_name);
|
||||
|
||||
// destruct device
|
||||
log_misc("hotplug", "device interface removal: {}", name);
|
||||
this->ri_mgr->devices_remove(name);
|
||||
}
|
||||
}
|
||||
@@ -120,8 +121,11 @@ namespace rawinput {
|
||||
return TRUE;
|
||||
}
|
||||
case DBT_DEVNODES_CHANGED: {
|
||||
// TODO: this can be a little noisy as it gets called on every device
|
||||
this->ri_mgr->devices_scan_midi();
|
||||
// catch-all device-tree change: MIDI and XInput have no targeted
|
||||
// arrival/removal notification, so this is our only hook to detect them
|
||||
// can be a little noisy as it gets called on every device
|
||||
log_misc("hotplug", "device tree changed, rescanning MIDI + XInput");
|
||||
this->ri_mgr->midi_scan_start();
|
||||
this->ri_mgr->devices_scan_xinput();
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user