Added SDL_HINT_HIDAPI_IGNORE_DEVICES to specify devices that should be ignored in SDL_hid_enumerate()

main
Sam Lantinga 2023-05-24 22:37:38 -07:00
parent e6834a1535
commit 007c36e513
9 changed files with 84 additions and 21 deletions

View File

@ -527,6 +527,8 @@ static void HIDAPI_ShutdownDiscovery(void)
/* Platform HIDAPI Implementation */ /* Platform HIDAPI Implementation */
#define HIDAPI_IGNORE_DEVICE(VID, PID) SDL_HIDAPI_ShouldIgnoreDevice(VID, PID)
struct PLATFORM_hid_device_; struct PLATFORM_hid_device_;
typedef struct PLATFORM_hid_device_ PLATFORM_hid_device; typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;
@ -1029,6 +1031,7 @@ static void CopyHIDDeviceInfo(struct hid_device_info *pSrc, struct SDL_hid_devic
#undef WCOPY_IF_EXISTS #undef WCOPY_IF_EXISTS
static int SDL_hidapi_refcount = 0; static int SDL_hidapi_refcount = 0;
static char *SDL_hidapi_ignored_devices = NULL;
static void SDL_SetHIDAPIError(const wchar_t *error) static void SDL_SetHIDAPIError(const wchar_t *error)
{ {
@ -1041,6 +1044,33 @@ static void SDL_SetHIDAPIError(const wchar_t *error)
} }
} }
static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
if (SDL_hidapi_ignored_devices) {
SDL_free(SDL_hidapi_ignored_devices);
}
if (hint && *hint) {
SDL_hidapi_ignored_devices = SDL_strdup(hint);
} else {
SDL_hidapi_ignored_devices = NULL;
}
}
SDL_bool SDL_HIDAPI_ShouldIgnoreDevice(Uint16 vendor_id, Uint16 product_id)
{
/* See if there are any devices we should skip in enumeration */
if (SDL_hidapi_ignored_devices) {
char vendor_match[16], product_match[16];
SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", vendor_id);
SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", vendor_id, product_id);
if (SDL_strcasestr(SDL_hidapi_ignored_devices, vendor_match) ||
SDL_strcasestr(SDL_hidapi_ignored_devices, product_match)) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
int SDL_hid_init(void) int SDL_hid_init(void)
{ {
int attempts = 0, success = 0; int attempts = 0, success = 0;
@ -1050,6 +1080,8 @@ int SDL_hid_init(void)
return 0; return 0;
} }
SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
#ifdef SDL_USE_LIBUDEV #ifdef SDL_USE_LIBUDEV
if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) { if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
@ -1193,6 +1225,13 @@ int SDL_hid_exit(void)
} }
#endif /* HAVE_LIBUSB */ #endif /* HAVE_LIBUSB */
SDL_DelHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
if (SDL_hidapi_ignored_devices) {
SDL_free(SDL_hidapi_ignored_devices);
SDL_hidapi_ignored_devices = NULL;
}
return result; return result;
} }

View File

@ -20,8 +20,11 @@
*/ */
#include "SDL_internal.h" #include "SDL_internal.h"
#ifdef SDL_JOYSTICK_HIDAPI
/* Return true if the HIDAPI should ignore a device during enumeration */
extern SDL_bool SDL_HIDAPI_ShouldIgnoreDevice(Uint16 vendor_id, Uint16 product_id);
#ifdef SDL_JOYSTICK_HIDAPI
#ifdef HAVE_LIBUSB #ifdef HAVE_LIBUSB
#define HAVE_ENABLE_GAMECUBE_ADAPTORS #define HAVE_ENABLE_GAMECUBE_ADAPTORS
#endif #endif
@ -29,5 +32,4 @@
#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS #ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
extern void SDL_EnableGameCubeAdaptors(void); extern void SDL_EnableGameCubeAdaptors(void);
#endif #endif
#endif /* SDL_JOYSTICK_HIDAPI */ #endif /* SDL_JOYSTICK_HIDAPI */

View File

@ -25,9 +25,6 @@
#define free SDL_free #define free SDL_free
#define malloc SDL_malloc #define malloc SDL_malloc
#define memcmp SDL_memcmp #define memcmp SDL_memcmp
#define snprintf SDL_snprintf
#define strlen SDL_strlen
#define _strnicmp SDL_strncasecmp
#define swprintf SDL_swprintf #define swprintf SDL_swprintf
#define towupper SDL_toupper #define towupper SDL_toupper
#define wcscmp SDL_wcscmp #define wcscmp SDL_wcscmp

View File

@ -51,6 +51,9 @@
#ifndef SDL_HIDAPI_DISABLED #ifndef SDL_HIDAPI_DISABLED
extern "C" {
#include "../SDL_hidapi_c.h"
}
#include "../../core/android/SDL_android.h" #include "../../core/android/SDL_android.h"
#define hid_close PLATFORM_hid_close #define hid_close PLATFORM_hid_close
@ -1064,7 +1067,6 @@ int hid_init(void)
struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id) struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{ {
struct hid_device_info *root = NULL; struct hid_device_info *root = NULL;
const char *hint = SDL_GetHint(SDL_HINT_HIDAPI_IGNORE_DEVICES);
hid_mutex_guard l( &g_DevicesMutex ); hid_mutex_guard l( &g_DevicesMutex );
for ( hid_device_ref<CHIDDevice> pDevice = g_Devices; pDevice; pDevice = pDevice->next ) for ( hid_device_ref<CHIDDevice> pDevice = g_Devices; pDevice; pDevice = pDevice->next )
@ -1072,13 +1074,8 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor
const hid_device_info *info = pDevice->GetDeviceInfo(); const hid_device_info *info = pDevice->GetDeviceInfo();
/* See if there are any devices we should skip in enumeration */ /* See if there are any devices we should skip in enumeration */
if (hint) { if (SDL_HIDAPI_ShouldIgnoreDevice(info->vendor_id, info->product_id)) {
char vendor_match[16], product_match[16]; continue;
SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", info->vendor_id);
SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", info->vendor_id, info->product_id);
if (SDL_strcasestr(hint, vendor_match) || SDL_strcasestr(hint, product_match)) {
continue;
}
} }
if ( ( vendor_id == 0x0 || info->vendor_id == vendor_id ) && if ( ( vendor_id == 0x0 || info->vendor_id == vendor_id ) &&

View File

@ -22,6 +22,7 @@
#ifndef SDL_HIDAPI_DISABLED #ifndef SDL_HIDAPI_DISABLED
#include "../SDL_hidapi_c.h"
#define hid_close PLATFORM_hid_close #define hid_close PLATFORM_hid_close
#define hid_device PLATFORM_hid_device #define hid_device PLATFORM_hid_device
@ -856,16 +857,10 @@ static struct hid_device_info *create_device_info_for_hid_device(HIDBLEDevice *d
struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id) struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{ @autoreleasepool { { @autoreleasepool {
struct hid_device_info *root = NULL; struct hid_device_info *root = NULL;
const char *hint = SDL_GetHint(SDL_HINT_HIDAPI_IGNORE_DEVICES);
/* See if there are any devices we should skip in enumeration */ /* See if there are any devices we should skip in enumeration */
if (hint) { if (SDL_HIDAPI_ShouldIgnoreDevice(VALVE_USB_VID, D0G_BLE2_PID)) {
char vendor_match[16], product_match[16]; return NULL;
SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", VALVE_USB_VID);
SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", VALVE_USB_VID, D0G_BLE2_PID);
if (SDL_strcasestr(hint, vendor_match) || SDL_strcasestr(hint, product_match)) {
return NULL;
}
} }
if ( ( vendor_id == 0 && product_id == 0 ) || if ( ( vendor_id == 0 && product_id == 0 ) ||

View File

@ -1085,6 +1085,13 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
continue; continue;
} }
#ifdef HIDAPI_IGNORE_DEVICE
/* See if there are any devices we should skip in enumeration */
if (HIDAPI_IGNORE_DEVICE(dev_vid, dev_pid)) {
continue;
}
#endif
res = libusb_get_active_config_descriptor(dev, &conf_desc); res = libusb_get_active_config_descriptor(dev, &conf_desc);
if (res < 0) if (res < 0)
libusb_get_config_descriptor(dev, 0, &conf_desc); libusb_get_config_descriptor(dev, 0, &conf_desc);

View File

@ -960,6 +960,16 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
continue; 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;
if (HIDAPI_IGNORE_DEVICE(dev_vid, dev_pid)) {
continue;
}
#endif
raw_dev = udev_device_new_from_syspath(udev, sysfs_path); raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
if (!raw_dev) if (!raw_dev)
continue; continue;

View File

@ -833,6 +833,15 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
continue; continue;
} }
#ifdef HIDAPI_IGNORE_DEVICE
/* See if there are any devices we should skip in enumeration */
unsigned short dev_vid = get_vendor_id(dev);
unsigned short dev_pid = get_product_id(dev);
if (HIDAPI_IGNORE_DEVICE(dev_vid, dev_pid)) {
continue;
}
#endif
struct hid_device_info *tmp = create_device_info(dev); struct hid_device_info *tmp = create_device_info(dev);
if (tmp == NULL) { if (tmp == NULL) {
continue; continue;

View File

@ -872,6 +872,13 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor
goto cont_close; goto cont_close;
} }
#ifdef HIDAPI_IGNORE_DEVICE
/* See if there are any devices we should skip in enumeration */
if (HIDAPI_IGNORE_DEVICE(attrib.VendorID, attrib.ProductID)) {
goto cont_close;
}
#endif
/* Check the VID/PID to see if we should add this /* Check the VID/PID to see if we should add this
device to the enumeration list. */ device to the enumeration list. */
if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) && if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) &&