Commit 4535d654919bbf13c05809e5eba9329f2d698ee2

Steven Noonan 2021-04-22T14:35:59

HIDAPI_UpdateDiscovery: only treat "add" and "remove" events as relevant I have a buggy system which reports a udev "change" event for an empty USB-C port every 0.14 seconds, which causes annoying frame hitches because SDL decides that means it needs to do a libusb hid_enumerate, which is slow (~25ms!) because of the get_usb_string() calls in there. We only need to re-enumerate if we've seen a device added or removed, so let's filter out the change event first. Signed-off-by: Steven Noonan <steven@valvesoftware.com> Signed-off-by: Sam Lantinga <slouken@libsdl.org>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 12eb59f..e3f99bc 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -442,10 +442,13 @@ HIDAPI_UpdateDiscovery()
                     break;
                 }
 
-                SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
-
                 pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
                 if (pUdevDevice) {
+                    const char *action = NULL;
+                    action = usyms->udev_device_get_action(pUdevDevice);
+                    if (!action || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) {
+                        SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
+                    }
                     usyms->udev_device_unref(pUdevDevice);
                 }
             }