SDL: hidapi: check each usage on a device before ignoring it completely

From 6fab61b69d3c8b5f6d8776e2d5e0338a30fc2a71 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 25 Nov 2024 10:49:05 -0800
Subject: [PATCH] hidapi: check each usage on a device before ignoring it
 completely

---
 src/hidapi/linux/hid.c | 64 +++++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 38 deletions(-)

diff --git a/src/hidapi/linux/hid.c b/src/hidapi/linux/hid.c
index dbaa9437738f6..dbbb3277854d8 100644
--- a/src/hidapi/linux/hid.c
+++ b/src/hidapi/linux/hid.c
@@ -900,6 +900,32 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
 		}
 	}
 
+#ifdef HIDAPI_IGNORE_DEVICE
+	{
+		struct hid_device_info *prev_dev = NULL;
+
+		cur_dev = root;
+		while (cur_dev) {
+			if (HIDAPI_IGNORE_DEVICE(cur_dev->bus_type, cur_dev->vendor_id, cur_dev->product_id, cur_dev->usage_page, cur_dev->usage)) {
+				struct hid_device_info *tmp = cur_dev;
+
+				cur_dev = tmp->next;
+				if (prev_dev) {
+					prev_dev->next = cur_dev;
+				} else {
+					root = cur_dev;
+				}
+				tmp->next = NULL;
+			
+				hid_free_enumeration(tmp);
+			} else {
+				prev_dev = cur_dev;
+				cur_dev = cur_dev->next;
+			}
+		}
+	}
+#endif
+
 end:
 	free(serial_number_utf8);
 	free(product_name_utf8);
@@ -1030,44 +1056,6 @@ struct hid_device_info  HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
 				continue;
 		}
 
-#ifdef HIDAPI_IGNORE_DEVICE
-		/* See if there are any devices we should skip in enumeration */
-		if (!parse_hid_vid_pid_from_sysfs(sysfs_path, &bus_type, &dev_vid, &dev_pid))
-			continue;
-
-		struct hidraw_report_descriptor report_desc;
-		unsigned short page = 0, usage = 0;
-		if (get_hid_report_descriptor_from_sysfs(sysfs_path, &report_desc) >= 0) {
-			struct hid_usage_iterator usage_iterator;
-			memset(&usage_iterator, 0, sizeof(usage_iterator));
-			get_next_hid_usage(report_desc.value, report_desc.size, &usage_iterator, &page, &usage);
-		}
-
-		/* Convert from Linux bus types to standard HIDAPI ones */
-		hid_bus_type hidapi_bus_type;
-		switch (bus_type) {
-			case BUS_USB:
-				hidapi_bus_type = HID_API_BUS_USB;
-				break;
-			case BUS_BLUETOOTH:
-				hidapi_bus_type = HID_API_BUS_BLUETOOTH;
-				break;
-			case BUS_I2C:
-				hidapi_bus_type = HID_API_BUS_I2C;
-				break;
-			case BUS_SPI:
-				hidapi_bus_type = HID_API_BUS_SPI;
-				break;
-			default:
-				hidapi_bus_type = HID_API_BUS_UNKNOWN;
-				break;
-		}
-
-		if (HIDAPI_IGNORE_DEVICE(hidapi_bus_type, dev_vid, dev_pid, page, usage)) {
-			continue;
-		}
-#endif
-
 		raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
 		if (!raw_dev)
 			continue;