Skip to content

Commit 007c36e

Browse files
committed
Added SDL_HINT_HIDAPI_IGNORE_DEVICES to specify devices that should be ignored in SDL_hid_enumerate()
1 parent e6834a1 commit 007c36e

File tree

9 files changed

+84
-21
lines changed

9 files changed

+84
-21
lines changed

src/hidapi/SDL_hidapi.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ static void HIDAPI_ShutdownDiscovery(void)
527527

528528
/* Platform HIDAPI Implementation */
529529

530+
#define HIDAPI_IGNORE_DEVICE(VID, PID) SDL_HIDAPI_ShouldIgnoreDevice(VID, PID)
531+
530532
struct PLATFORM_hid_device_;
531533
typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;
532534

@@ -1029,6 +1031,7 @@ static void CopyHIDDeviceInfo(struct hid_device_info *pSrc, struct SDL_hid_devic
10291031
#undef WCOPY_IF_EXISTS
10301032

10311033
static int SDL_hidapi_refcount = 0;
1034+
static char *SDL_hidapi_ignored_devices = NULL;
10321035

10331036
static void SDL_SetHIDAPIError(const wchar_t *error)
10341037
{
@@ -1041,6 +1044,33 @@ static void SDL_SetHIDAPIError(const wchar_t *error)
10411044
}
10421045
}
10431046

1047+
static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
1048+
{
1049+
if (SDL_hidapi_ignored_devices) {
1050+
SDL_free(SDL_hidapi_ignored_devices);
1051+
}
1052+
if (hint && *hint) {
1053+
SDL_hidapi_ignored_devices = SDL_strdup(hint);
1054+
} else {
1055+
SDL_hidapi_ignored_devices = NULL;
1056+
}
1057+
}
1058+
1059+
SDL_bool SDL_HIDAPI_ShouldIgnoreDevice(Uint16 vendor_id, Uint16 product_id)
1060+
{
1061+
/* See if there are any devices we should skip in enumeration */
1062+
if (SDL_hidapi_ignored_devices) {
1063+
char vendor_match[16], product_match[16];
1064+
SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", vendor_id);
1065+
SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", vendor_id, product_id);
1066+
if (SDL_strcasestr(SDL_hidapi_ignored_devices, vendor_match) ||
1067+
SDL_strcasestr(SDL_hidapi_ignored_devices, product_match)) {
1068+
return SDL_TRUE;
1069+
}
1070+
}
1071+
return SDL_FALSE;
1072+
}
1073+
10441074
int SDL_hid_init(void)
10451075
{
10461076
int attempts = 0, success = 0;
@@ -1050,6 +1080,8 @@ int SDL_hid_init(void)
10501080
return 0;
10511081
}
10521082

1083+
SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
1084+
10531085
#ifdef SDL_USE_LIBUDEV
10541086
if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
10551087
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
@@ -1193,6 +1225,13 @@ int SDL_hid_exit(void)
11931225
}
11941226
#endif /* HAVE_LIBUSB */
11951227

1228+
SDL_DelHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
1229+
1230+
if (SDL_hidapi_ignored_devices) {
1231+
SDL_free(SDL_hidapi_ignored_devices);
1232+
SDL_hidapi_ignored_devices = NULL;
1233+
}
1234+
11961235
return result;
11971236
}
11981237

src/hidapi/SDL_hidapi_c.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
*/
2121
#include "SDL_internal.h"
2222

23-
#ifdef SDL_JOYSTICK_HIDAPI
2423

24+
/* Return true if the HIDAPI should ignore a device during enumeration */
25+
extern SDL_bool SDL_HIDAPI_ShouldIgnoreDevice(Uint16 vendor_id, Uint16 product_id);
26+
27+
#ifdef SDL_JOYSTICK_HIDAPI
2528
#ifdef HAVE_LIBUSB
2629
#define HAVE_ENABLE_GAMECUBE_ADAPTORS
2730
#endif
2831

2932
#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
3033
extern void SDL_EnableGameCubeAdaptors(void);
3134
#endif
32-
3335
#endif /* SDL_JOYSTICK_HIDAPI */

src/hidapi/SDL_hidapi_windows.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
#define free SDL_free
2626
#define malloc SDL_malloc
2727
#define memcmp SDL_memcmp
28-
#define snprintf SDL_snprintf
29-
#define strlen SDL_strlen
30-
#define _strnicmp SDL_strncasecmp
3128
#define swprintf SDL_swprintf
3229
#define towupper SDL_toupper
3330
#define wcscmp SDL_wcscmp

src/hidapi/android/hid.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151

5252
#ifndef SDL_HIDAPI_DISABLED
5353

54+
extern "C" {
55+
#include "../SDL_hidapi_c.h"
56+
}
5457
#include "../../core/android/SDL_android.h"
5558

5659
#define hid_close PLATFORM_hid_close
@@ -1064,21 +1067,15 @@ int hid_init(void)
10641067
struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
10651068
{
10661069
struct hid_device_info *root = NULL;
1067-
const char *hint = SDL_GetHint(SDL_HINT_HIDAPI_IGNORE_DEVICES);
10681070

10691071
hid_mutex_guard l( &g_DevicesMutex );
10701072
for ( hid_device_ref<CHIDDevice> pDevice = g_Devices; pDevice; pDevice = pDevice->next )
10711073
{
10721074
const hid_device_info *info = pDevice->GetDeviceInfo();
10731075

10741076
/* See if there are any devices we should skip in enumeration */
1075-
if (hint) {
1076-
char vendor_match[16], product_match[16];
1077-
SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", info->vendor_id);
1078-
SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", info->vendor_id, info->product_id);
1079-
if (SDL_strcasestr(hint, vendor_match) || SDL_strcasestr(hint, product_match)) {
1080-
continue;
1081-
}
1077+
if (SDL_HIDAPI_ShouldIgnoreDevice(info->vendor_id, info->product_id)) {
1078+
continue;
10821079
}
10831080

10841081
if ( ( vendor_id == 0x0 || info->vendor_id == vendor_id ) &&

src/hidapi/ios/hid.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#ifndef SDL_HIDAPI_DISABLED
2424

25+
#include "../SDL_hidapi_c.h"
2526

2627
#define hid_close PLATFORM_hid_close
2728
#define hid_device PLATFORM_hid_device
@@ -856,16 +857,10 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
856857
struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
857858
{ @autoreleasepool {
858859
struct hid_device_info *root = NULL;
859-
const char *hint = SDL_GetHint(SDL_HINT_HIDAPI_IGNORE_DEVICES);
860860

861861
/* See if there are any devices we should skip in enumeration */
862-
if (hint) {
863-
char vendor_match[16], product_match[16];
864-
SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", VALVE_USB_VID);
865-
SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", VALVE_USB_VID, D0G_BLE2_PID);
866-
if (SDL_strcasestr(hint, vendor_match) || SDL_strcasestr(hint, product_match)) {
867-
return NULL;
868-
}
862+
if (SDL_HIDAPI_ShouldIgnoreDevice(VALVE_USB_VID, D0G_BLE2_PID)) {
863+
return NULL;
869864
}
870865

871866
if ( ( vendor_id == 0 && product_id == 0 ) ||

src/hidapi/libusb/hid.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,13 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
10851085
continue;
10861086
}
10871087

1088+
#ifdef HIDAPI_IGNORE_DEVICE
1089+
/* See if there are any devices we should skip in enumeration */
1090+
if (HIDAPI_IGNORE_DEVICE(dev_vid, dev_pid)) {
1091+
continue;
1092+
}
1093+
#endif
1094+
10881095
res = libusb_get_active_config_descriptor(dev, &conf_desc);
10891096
if (res < 0)
10901097
libusb_get_config_descriptor(dev, 0, &conf_desc);

src/hidapi/linux/hid.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,16 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
960960
continue;
961961
}
962962

963+
#ifdef HIDAPI_IGNORE_DEVICE
964+
/* See if there are any devices we should skip in enumeration */
965+
if (!parse_hid_vid_pid_from_sysfs(sysfs_path, &bus_type, &dev_vid, &dev_pid))
966+
continue;
967+
968+
if (HIDAPI_IGNORE_DEVICE(dev_vid, dev_pid)) {
969+
continue;
970+
}
971+
#endif
972+
963973
raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
964974
if (!raw_dev)
965975
continue;

src/hidapi/mac/hid.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,15 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
833833
continue;
834834
}
835835

836+
#ifdef HIDAPI_IGNORE_DEVICE
837+
/* See if there are any devices we should skip in enumeration */
838+
unsigned short dev_vid = get_vendor_id(dev);
839+
unsigned short dev_pid = get_product_id(dev);
840+
if (HIDAPI_IGNORE_DEVICE(dev_vid, dev_pid)) {
841+
continue;
842+
}
843+
#endif
844+
836845
struct hid_device_info *tmp = create_device_info(dev);
837846
if (tmp == NULL) {
838847
continue;

src/hidapi/windows/hid.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,13 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor
872872
goto cont_close;
873873
}
874874

875+
#ifdef HIDAPI_IGNORE_DEVICE
876+
/* See if there are any devices we should skip in enumeration */
877+
if (HIDAPI_IGNORE_DEVICE(attrib.VendorID, attrib.ProductID)) {
878+
goto cont_close;
879+
}
880+
#endif
881+
875882
/* Check the VID/PID to see if we should add this
876883
device to the enumeration list. */
877884
if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) &&

0 commit comments

Comments
 (0)