SDL: HIDAPI_UpdateDiscovery: only treat "add" and "remove" events as relevant

From 4535d654919bbf13c05809e5eba9329f2d698ee2 Mon Sep 17 00:00:00 2001
From: Steven Noonan <[EMAIL REDACTED]>
Date: Thu, 22 Apr 2021 14:35:59 -0700
Subject: [PATCH] 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>
---
 src/joystick/hidapi/SDL_hidapijoystick.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 12eb59f24..e3f99bc98 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);
                 }
             }