From 0da346f12989cd2410034da7eaecfd003ff673a3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 3 Aug 2024 11:42:06 -0700
Subject: [PATCH] Added SDL_HINT_HIDAPI_LIBUSB,
SDL_HINT_HIDAPI_LIBUSB_WHITELIST, and SDL_HINT_HIDAPI_UDEV
---
docs/README-migration.md | 2 ++
include/SDL3/SDL_hints.h | 46 ++++++++++++++++++++++++
src/hidapi/SDL_hidapi.c | 18 +++++-----
src/joystick/hidapi/SDL_hidapijoystick.c | 4 +--
4 files changed, 59 insertions(+), 11 deletions(-)
diff --git a/docs/README-migration.md b/docs/README-migration.md
index b0f432b14920c..526f7c844ddfd 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -812,6 +812,8 @@ The following environment variables have been removed:
* SDL_DISKAUDIOFILE - replaced with the hint SDL_HINT_AUDIO_DISK_OUTPUT_FILE
* SDL_DISKAUDIOFILEIN - replaced with the hint SDL_HINT_AUDIO_DISK_INPUT_FILE
* SDL_DUMMYAUDIODELAY - replaced with the hint SDL_HINT_AUDIO_DUMMY_TIMESCALE which allows scaling the audio time rather than specifying an absolute delay.
+* SDL_HIDAPI_DISABLE_LIBUSB - replaced with the hint SDL_HINT_HIDAPI_LIBUSB
+* SDL_HIDAPI_JOYSTICK_DISABLE_UDEV - replaced with the hint SDL_HINT_HIDAPI_UDEV
* VITA_DISABLE_TOUCH_BACK - replaced with the hint SDL_HINT_VITA_ENABLE_BACK_TOUCH
* VITA_DISABLE_TOUCH_FRONT - replaced with the hint SDL_HINT_VITA_ENABLE_FRONT_TOUCH
* VITA_MODULE_PATH - replaced with the hint SDL_HINT_VITA_MODULE_PATH
diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h
index dedc8a9b58bba..0bffad638f689 100644
--- a/include/SDL3/SDL_hints.h
+++ b/include/SDL3/SDL_hints.h
@@ -930,6 +930,52 @@ extern "C" {
*/
#define SDL_HINT_GDK_TEXTINPUT_TITLE "SDL_GDK_TEXTINPUT_TITLE"
+/**
+ * A variable to control whether HIDAPI uses libusb for device access.
+ *
+ * By default libusb will only be used for a few devices that require direct USB access, and this can be controlled with SDL_HINT_HIDAPI_LIBUSB_WHITELIST.
+ *
+ * The variable can be set to the following values:
+ *
+ * - "0": HIDAPI will not use libusb for device access.
+ * - "1": HIDAPI will use libusb for device access if available. (default)
+ *
+ * This hint should be set before SDL is initialized.
+ *
+ * \since This hint is available since SDL 3.0.0.
+ */
+#define SDL_HINT_HIDAPI_LIBUSB "SDL_HIDAPI_LIBUSB"
+
+/**
+ * A variable to control whether HIDAPI uses libusb only for whitelisted devices.
+ *
+ * By default libusb will only be used for a few devices that require direct USB access.
+ *
+ * The variable can be set to the following values:
+ *
+ * - "0": HIDAPI will use libusb for all device access.
+ * - "1": HIDAPI will use libusb only for whitelisted devices. (default)
+ *
+ * This hint should be set before SDL is initialized.
+ *
+ * \since This hint is available since SDL 3.0.0.
+ */
+#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST "SDL_HIDAPI_LIBUSB_WHITELIST"
+
+/**
+ * A variable to control whether HIDAPI uses udev for device detection.
+ *
+ * The variable can be set to the following values:
+ *
+ * - "0": HIDAPI will poll for device changes.
+ * - "1": HIDAPI will use udev for device detection. (default)
+ *
+ * This hint should be set before SDL is initialized.
+ *
+ * \since This hint is available since SDL 3.0.0.
+ */
+#define SDL_HINT_HIDAPI_UDEV "SDL_HIDAPI_UDEV"
+
/**
* A variable to control whether SDL_hid_enumerate() enumerates all HID
* devices or only controllers.
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index 930db26df2097..ee4abeec7510b 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -909,13 +909,13 @@ static SDL_bool IsInWhitelist(Uint16 vendor, Uint16 product)
#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND)
/* We have another way to get HID devices, so use the whitelist to get devices where libusb is preferred */
-#define SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT SDL_TRUE
+#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT SDL_TRUE
#else
/* libusb is the only way to get HID devices, so don't use the whitelist, get them all */
-#define SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT SDL_FALSE
+#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT SDL_FALSE
#endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND */
-static SDL_bool use_libusb_whitelist = SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT;
+static SDL_bool use_libusb_whitelist = SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT;
/* Shared HIDAPI Implementation */
@@ -1140,9 +1140,9 @@ int SDL_hid_init(void)
SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
#ifdef SDL_USE_LIBUDEV
- if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
+ if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_UDEV, SDL_TRUE)) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
- "udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
+ "udev disabled by SDL_HINT_HIDAPI_UDEV");
linux_enumeration_method = ENUMERATION_FALLBACK;
} else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
@@ -1155,12 +1155,12 @@ int SDL_hid_init(void)
}
#endif
- use_libusb_whitelist = SDL_GetHintBoolean("SDL_HIDAPI_LIBUSB_WHITELIST",
- SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT);
+ use_libusb_whitelist = SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB_WHITELIST,
+ SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT);
#ifdef HAVE_LIBUSB
- if (SDL_getenv("SDL_HIDAPI_DISABLE_LIBUSB") != NULL) {
+ if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB, SDL_TRUE)) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
- "libusb disabled by SDL_HIDAPI_DISABLE_LIBUSB");
+ "libusb disabled with SDL_HINT_HIDAPI_LIBUSB");
libusb_ctx.libhandle = NULL;
} else {
++attempts;
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 17ec232e7e0f0..dbf59ddcb9bc2 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -579,9 +579,9 @@ static int HIDAPI_JoystickInit(void)
#ifdef SDL_USE_LIBUDEV
if (linux_enumeration_method == ENUMERATION_UNSET) {
- if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
+ if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_UDEV, SDL_TRUE)) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
- "udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
+ "udev disabled by SDL_HINT_HIDAPI_UDEV");
linux_enumeration_method = ENUMERATION_FALLBACK;
} else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,