From 22f22472a0c40830cf8ee6faa163cf814704fa0c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 25 May 2023 07:51:16 -0700
Subject: [PATCH] Create wrapper headers for platform specific hidapi modules
---
src/hidapi/SDL_hidapi.c | 79 +++----------------------------
src/hidapi/SDL_hidapi_android.h | 26 ++++++++++
src/hidapi/SDL_hidapi_ios.h | 26 ++++++++++
src/hidapi/SDL_hidapi_libusb.h | 36 ++++++++++++++
src/hidapi/SDL_hidapi_linux.h | 46 ++++++++++++++++++
src/hidapi/SDL_hidapi_mac.h | 25 ++++++++++
src/hidapi/SDL_hidapi_steamxbox.h | 23 +++++++++
src/hidapi/SDL_hidapi_windows.h | 39 +++++++++++++++
8 files changed, 228 insertions(+), 72 deletions(-)
create mode 100644 src/hidapi/SDL_hidapi_android.h
create mode 100644 src/hidapi/SDL_hidapi_ios.h
create mode 100644 src/hidapi/SDL_hidapi_libusb.h
create mode 100644 src/hidapi/SDL_hidapi_linux.h
create mode 100644 src/hidapi/SDL_hidapi_mac.h
create mode 100644 src/hidapi/SDL_hidapi_steamxbox.h
create mode 100644 src/hidapi/SDL_hidapi_windows.h
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index a15526b8f115..a2603337d7f3 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -564,66 +564,16 @@ typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;
#define read_thread PLATFORM_read_thread
#define return_data PLATFORM_return_data
-/* Allow hidapi.h to be included in the platform implementation */
-#undef HIDAPI_H__
-
#ifdef __LINUX__
-
-#ifdef SDL_USE_LIBUDEV
-static const SDL_UDEV_Symbols *udev_ctx = NULL;
-
-#define udev_device_get_devnode udev_ctx->udev_device_get_devnode
-#define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
-#define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
-#define udev_device_get_syspath udev_ctx->udev_device_get_syspath
-#define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
-#define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
-#define udev_device_unref udev_ctx->udev_device_unref
-#define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
-#define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
-#define udev_enumerate_new udev_ctx->udev_enumerate_new
-#define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
-#define udev_enumerate_unref udev_ctx->udev_enumerate_unref
-#define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
-#define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
-#define udev_new udev_ctx->udev_new
-#define udev_unref udev_ctx->udev_unref
-
-#include "linux/hid.c"
-#define HAVE_PLATFORM_BACKEND 1
-#endif /* SDL_USE_LIBUDEV */
-
+#include "SDL_hidapi_linux.h"
#elif defined(__MACOS__)
-#include "mac/hid.c"
-#define HAVE_PLATFORM_BACKEND 1
-#define udev_ctx 1
+#include "SDL_hidapi_mac.h"
#elif defined(__WINDOWS__) || defined(__WINGDK__)
-/* Define standard library functions in terms of SDL */
-#define calloc SDL_calloc
-#define free SDL_free
-#define malloc SDL_malloc
-#define memcmp SDL_memcmp
-#define swprintf SDL_swprintf
-#define towupper SDL_toupper
-#define wcscmp SDL_wcscmp
-#define _wcsdup SDL_wcsdup
-#define wcslen SDL_wcslen
-#define wcsncpy SDL_wcslcpy
-#define wcsstr SDL_wcsstr
-#define wcstol SDL_wcstol
-#include "windows/hid.c"
-#define HAVE_PLATFORM_BACKEND 1
-#define udev_ctx 1
+#include "SDL_hidapi_windows.h"
#elif defined(__ANDROID__)
-/* The implementation for Android is in a separate .cpp file */
-#include "hidapi/hidapi.h"
-#define HAVE_PLATFORM_BACKEND 1
-#define udev_ctx 1
+#include "SDL_hidapi_android.h"
#elif defined(__IOS__) || defined(__TVOS__)
-/* The implementation for iOS and tvOS is in a separate .m file */
-#include "hidapi/hidapi.h"
-#define HAVE_PLATFORM_BACKEND 1
-#define udev_ctx 1
+#include "SDL_hidapi_ios.h"
#endif
#undef api_version
@@ -696,8 +646,7 @@ typedef struct DRIVER_hid_device_ DRIVER_hid_device;
#define hid_write DRIVER_hid_write
#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
-#undef HIDAPI_H__
-#include "steamxbox/hid.c"
+#include "SDL_hidapi_steamxbox.h"
#else
#error Need a driver hid.c for this platform!
#endif
@@ -857,21 +806,7 @@ typedef struct LIBUSB_hid_device_ LIBUSB_hid_device;
#define read_thread LIBUSB_read_thread
#define return_data LIBUSB_return_data
-#ifndef __FreeBSD__
-/* this is awkwardly inlined, so we need to re-implement it here
- * so we can override the libusb_control_transfer call */
-static int SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
- uint8_t descriptor_index, uint16_t lang_id,
- unsigned char *data, int length)
-{
- return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | 0x0, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | descriptor_index, lang_id,
- data, (uint16_t)length, 1000); /* Endpoint 0 IN */
-}
-#define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
-#endif /* __FreeBSD__ */
-
-#undef HIDAPI_H__
-#include "libusb/hid.c"
+#include "SDL_hidapi_libusb.h"
#undef libusb_init
#undef libusb_exit
diff --git a/src/hidapi/SDL_hidapi_android.h b/src/hidapi/SDL_hidapi_android.h
new file mode 100644
index 000000000000..4fe554c7e57b
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_android.h
@@ -0,0 +1,26 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+/* The implementation for Android is in a separate .cpp file */
+#undef HIDAPI_H__
+#include "hidapi/hidapi.h"
+#define HAVE_PLATFORM_BACKEND 1
+#define udev_ctx 1
diff --git a/src/hidapi/SDL_hidapi_ios.h b/src/hidapi/SDL_hidapi_ios.h
new file mode 100644
index 000000000000..c921fde332b1
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_ios.h
@@ -0,0 +1,26 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+/* The implementation for iOS and tvOS is in a separate .m file */
+#undef HIDAPI_H__
+#include "hidapi/hidapi.h"
+#define HAVE_PLATFORM_BACKEND 1
+#define udev_ctx 1
diff --git a/src/hidapi/SDL_hidapi_libusb.h b/src/hidapi/SDL_hidapi_libusb.h
new file mode 100644
index 000000000000..ead421ba06cc
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_libusb.h
@@ -0,0 +1,36 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef __FreeBSD__
+/* this is awkwardly inlined, so we need to re-implement it here
+ * so we can override the libusb_control_transfer call */
+static int SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
+ uint8_t descriptor_index, uint16_t lang_id,
+ unsigned char *data, int length)
+{
+ return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | 0x0, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | descriptor_index, lang_id,
+ data, (uint16_t)length, 1000); /* Endpoint 0 IN */
+}
+#define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
+#endif /* __FreeBSD__ */
+
+#undef HIDAPI_H__
+#include "libusb/hid.c"
diff --git a/src/hidapi/SDL_hidapi_linux.h b/src/hidapi/SDL_hidapi_linux.h
new file mode 100644
index 000000000000..f06627608092
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_linux.h
@@ -0,0 +1,46 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifdef SDL_USE_LIBUDEV
+static const SDL_UDEV_Symbols *udev_ctx = NULL;
+
+#define udev_device_get_devnode udev_ctx->udev_device_get_devnode
+#define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
+#define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
+#define udev_device_get_syspath udev_ctx->udev_device_get_syspath
+#define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
+#define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
+#define udev_device_unref udev_ctx->udev_device_unref
+#define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
+#define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
+#define udev_enumerate_new udev_ctx->udev_enumerate_new
+#define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
+#define udev_enumerate_unref udev_ctx->udev_enumerate_unref
+#define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
+#define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
+#define udev_new udev_ctx->udev_new
+#define udev_unref udev_ctx->udev_unref
+
+#undef HIDAPI_H__
+#include "linux/hid.c"
+#define HAVE_PLATFORM_BACKEND 1
+
+#endif /* SDL_USE_LIBUDEV */
diff --git a/src/hidapi/SDL_hidapi_mac.h b/src/hidapi/SDL_hidapi_mac.h
new file mode 100644
index 000000000000..d7c0f50044a4
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_mac.h
@@ -0,0 +1,25 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#undef HIDAPI_H__
+#include "mac/hid.c"
+#define HAVE_PLATFORM_BACKEND 1
+#define udev_ctx 1
diff --git a/src/hidapi/SDL_hidapi_steamxbox.h b/src/hidapi/SDL_hidapi_steamxbox.h
new file mode 100644
index 000000000000..0570acb38d87
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_steamxbox.h
@@ -0,0 +1,23 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#undef HIDAPI_H__
+#include "steamxbox/hid.c"
diff --git a/src/hidapi/SDL_hidapi_windows.h b/src/hidapi/SDL_hidapi_windows.h
new file mode 100644
index 000000000000..c368863d863b
--- /dev/null
+++ b/src/hidapi/SDL_hidapi_windows.h
@@ -0,0 +1,39 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+/* Define standard library functions in terms of SDL */
+#define calloc SDL_calloc
+#define free SDL_free
+#define malloc SDL_malloc
+#define memcmp SDL_memcmp
+#define swprintf SDL_swprintf
+#define towupper SDL_toupper
+#define wcscmp SDL_wcscmp
+#define _wcsdup SDL_wcsdup
+#define wcslen SDL_wcslen
+#define wcsncpy SDL_wcslcpy
+#define wcsstr SDL_wcsstr
+#define wcstol SDL_wcstol
+
+#undef HIDAPI_H__
+#include "windows/hid.c"
+#define HAVE_PLATFORM_BACKEND 1
+#define udev_ctx 1