SDL: Look at all available usages when determining whether to ignore a device

From c951bbe3def1712a402cba8e66c0992f130af2ce Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 7 Feb 2024 16:02:12 -0800
Subject: [PATCH] Look at all available usages when determining whether to
 ignore a device

---
 src/hidapi/mac/hid.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/src/hidapi/mac/hid.c b/src/hidapi/mac/hid.c
index ee6666b6963a..db20b35c031c 100644
--- a/src/hidapi/mac/hid.c
+++ b/src/hidapi/mac/hid.c
@@ -594,6 +594,14 @@ static struct hid_device_info *create_device_info_with_usage(IOHIDDeviceRef dev,
 	dev_vid = get_vendor_id(dev);
 	dev_pid = get_product_id(dev);
 
+#ifdef HIDAPI_IGNORE_DEVICE
+	/* See if there are any devices we should skip in enumeration */
+	if (HIDAPI_IGNORE_DEVICE(get_bus_type(dev), dev_vid, dev_pid, usage_page, usage)) {
+		free(cur_dev);
+		return NULL;
+	}
+#endif
+
 	cur_dev->usage_page = usage_page;
 	cur_dev->usage = usage;
 
@@ -691,9 +699,6 @@ static struct hid_device_info *create_device_info(IOHIDDeviceRef device)
 	struct hid_device_info *root = create_device_info_with_usage(device, primary_usage_page, primary_usage);
 	struct hid_device_info *cur = root;
 
-	if (!root)
-		return NULL;
-
 	CFArrayRef usage_pairs = get_usage_pairs(device);
 
 	if (usage_pairs != NULL) {
@@ -719,9 +724,13 @@ static struct hid_device_info *create_device_info(IOHIDDeviceRef device)
 				continue; /* Already added. */
 
 			next = create_device_info_with_usage(device, usage_page, usage);
-			cur->next = next;
-			if (next != NULL) {
-				cur = next;
+			if (cur) {
+				if (next != NULL) {
+					cur->next = next;
+					cur = next;
+				}
+			} else {
+				root = cur = next;
 			}
 		}
 	}
@@ -788,18 +797,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 */
-		hid_bus_type bus_type = get_bus_type(dev);
-		unsigned short dev_vid = get_vendor_id(dev);
-		unsigned short dev_pid = get_product_id(dev);
-		unsigned short usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
-		unsigned short usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));
-		if (HIDAPI_IGNORE_DEVICE(bus_type, dev_vid, dev_pid, usage_page, usage)) {
-			continue;
-		}
-#endif
-
 		struct hid_device_info *tmp = create_device_info(dev);
 		if (tmp == NULL) {
 			continue;