From fd10eec0bf489fc9b91e323e7751c82da88ceae7 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Thu, 26 Jun 2025 13:13:02 -0400 Subject: [PATCH 1/8] hidapi: Add a stub driver for Switch 2. Thanks to kiddkaffeine for the Xcode updates! --- VisualC-GDK/SDL/SDL.vcxproj | 1 + VisualC-GDK/SDL/SDL.vcxproj.filters | 1 + VisualC/SDL/SDL.vcxproj | 1 + VisualC/SDL/SDL.vcxproj.filters | 3 + Xcode/SDL/SDL.xcodeproj/project.pbxproj | 4 + include/SDL3/SDL_hints.h | 17 +++ src/joystick/hidapi/SDL_hidapi_switch2.c | 148 +++++++++++++++++++++ src/joystick/hidapi/SDL_hidapijoystick.c | 3 + src/joystick/hidapi/SDL_hidapijoystick_c.h | 2 + src/joystick/usb_ids.h | 1 + 10 files changed, 181 insertions(+) create mode 100644 src/joystick/hidapi/SDL_hidapi_switch2.c diff --git a/VisualC-GDK/SDL/SDL.vcxproj b/VisualC-GDK/SDL/SDL.vcxproj index 58194e72467cd..266d426fdaa36 100644 --- a/VisualC-GDK/SDL/SDL.vcxproj +++ b/VisualC-GDK/SDL/SDL.vcxproj @@ -728,6 +728,7 @@ + diff --git a/VisualC-GDK/SDL/SDL.vcxproj.filters b/VisualC-GDK/SDL/SDL.vcxproj.filters index 8a988ace966c8..1dcabe37beef4 100644 --- a/VisualC-GDK/SDL/SDL.vcxproj.filters +++ b/VisualC-GDK/SDL/SDL.vcxproj.filters @@ -79,6 +79,7 @@ + diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj index 843f8e61ce613..af4e646ae76a4 100644 --- a/VisualC/SDL/SDL.vcxproj +++ b/VisualC/SDL/SDL.vcxproj @@ -618,6 +618,7 @@ + diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters index 375c175c0e95a..74d32874986b9 100644 --- a/VisualC/SDL/SDL.vcxproj.filters +++ b/VisualC/SDL/SDL.vcxproj.filters @@ -1230,6 +1230,9 @@ joystick\hidapi + + joystick\hidapi + joystick\hidapi diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj index ffe8fa75a60bf..f6a46240cfec1 100644 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj @@ -228,6 +228,7 @@ A7D8B54523E2514300DCD162 /* SDL_hidapijoystick.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C423E2513E00DCD162 /* SDL_hidapijoystick.c */; }; A7D8B54B23E2514300DCD162 /* SDL_hidapi_xboxone.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C523E2513E00DCD162 /* SDL_hidapi_xboxone.c */; }; A7D8B55123E2514300DCD162 /* SDL_hidapi_switch.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C623E2513E00DCD162 /* SDL_hidapi_switch.c */; }; + A7D8B55123E2514300DCD163 /* SDL_hidapi_switch2.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C623E2513E00DCD163 /* SDL_hidapi_switch2.c */; }; A7D8B55723E2514300DCD162 /* SDL_hidapijoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = A7D8A7C723E2513E00DCD162 /* SDL_hidapijoystick_c.h */; }; A7D8B55D23E2514300DCD162 /* SDL_hidapi_xbox360w.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C823E2513E00DCD162 /* SDL_hidapi_xbox360w.c */; }; A7D8B56323E2514300DCD162 /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */; }; @@ -811,6 +812,7 @@ A7D8A7C423E2513E00DCD162 /* SDL_hidapijoystick.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapijoystick.c; sourceTree = ""; }; A7D8A7C523E2513E00DCD162 /* SDL_hidapi_xboxone.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xboxone.c; sourceTree = ""; }; A7D8A7C623E2513E00DCD162 /* SDL_hidapi_switch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch.c; sourceTree = ""; }; + A7D8A7C623E2513E00DCD163 /* SDL_hidapi_switch2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch2.c; sourceTree = ""; }; A7D8A7C723E2513E00DCD162 /* SDL_hidapijoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_hidapijoystick_c.h; sourceTree = ""; }; A7D8A7C823E2513E00DCD162 /* SDL_hidapi_xbox360w.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xbox360w.c; sourceTree = ""; }; A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_gamecube.c; sourceTree = ""; }; @@ -1948,6 +1950,7 @@ F3FD042D2C9B755700824C4C /* SDL_hidapi_steam_hori.c */, A797456F2B2E9D39009D224A /* SDL_hidapi_steamdeck.c */, A7D8A7C623E2513E00DCD162 /* SDL_hidapi_switch.c */, + A7D8A7C623E2513E00DCD163 /* SDL_hidapi_switch2.c */, F3D60A8228C16A1800788A3A /* SDL_hidapi_wii.c */, A7D8A7C223E2513E00DCD162 /* SDL_hidapi_xbox360.c */, A7D8A7C823E2513E00DCD162 /* SDL_hidapi_xbox360w.c */, @@ -3035,6 +3038,7 @@ F3FA5A222B59ACE000FEAD97 /* yuv_rgb_sse.c in Sources */, F3C2CB232C5DDDB2004D7998 /* SDL_categories.c in Sources */, A7D8B55123E2514300DCD162 /* SDL_hidapi_switch.c in Sources */, + A7D8B55123E2514300DCD163 /* SDL_hidapi_switch2.c in Sources */, A7D8B96223E2514400DCD162 /* SDL_strtokr.c in Sources */, A7D8BB7523E2514500DCD162 /* SDL_clipboardevents.c in Sources */, E4F798202AD8D87F00669F54 /* SDL_video_unsupported.c in Sources */, diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h index 30a04c4da416d..344b36f84ac63 100644 --- a/include/SDL3/SDL_hints.h +++ b/include/SDL3/SDL_hints.h @@ -1809,6 +1809,23 @@ extern "C" { */ #define SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED "SDL_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED" +/** + * A variable controlling whether the HIDAPI driver for Nintendo Switch 2 + * controllers should be used. + * + * The variable can be set to the following values: + * + * - "0": HIDAPI driver is not used. + * - "1": HIDAPI driver is used. + * + * The default is the value of SDL_HINT_JOYSTICK_HIDAPI. + * + * This hint should be set before initializing joysticks and gamepads. + * + * \since This hint is available since SDL 3.4.0. + */ +#define SDL_HINT_JOYSTICK_HIDAPI_SWITCH2 "SDL_JOYSTICK_HIDAPI_SWITCH2" + /** * A variable controlling whether Nintendo Switch Joy-Con controllers will be * in vertical mode when using the HIDAPI driver. diff --git a/src/joystick/hidapi/SDL_hidapi_switch2.c b/src/joystick/hidapi/SDL_hidapi_switch2.c new file mode 100644 index 0000000000000..4891ac169e28e --- /dev/null +++ b/src/joystick/hidapi/SDL_hidapi_switch2.c @@ -0,0 +1,148 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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. +*/ +/* This driver supports the Nintendo Switch Pro controller. + Code and logic contributed by Valve Corporation under the SDL zlib license. +*/ +#include "SDL_internal.h" + +#ifdef SDL_JOYSTICK_HIDAPI + +#include "../../SDL_hints_c.h" +#include "../SDL_sysjoystick.h" +#include "SDL_hidapijoystick_c.h" +#include "SDL_hidapi_rumble.h" +#include "SDL_hidapi_nintendo.h" + +#ifdef SDL_JOYSTICK_HIDAPI_SWITCH2 + +static void HIDAPI_DriverSwitch2_RegisterHints(SDL_HintCallback callback, void *userdata) +{ + SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, callback, userdata); +} + +static void HIDAPI_DriverSwitch2_UnregisterHints(SDL_HintCallback callback, void *userdata) +{ + SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, callback, userdata); +} + +static bool HIDAPI_DriverSwitch2_IsEnabled(void) +{ + return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT)); +} + +static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol) +{ + if (vendor_id == USB_VENDOR_NINTENDO) { + if (product_id == USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER) { + return true; + } + } + + return false; +} + +static bool HIDAPI_DriverSwitch2_InitDevice(SDL_HIDAPI_Device *device) +{ + return SDL_Unsupported(); +} + +static int HIDAPI_DriverSwitch2_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id) +{ + return -1; +} + +static void HIDAPI_DriverSwitch2_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index) +{ +} + +static bool HIDAPI_DriverSwitch2_UpdateDevice(SDL_HIDAPI_Device *device) +{ + return SDL_Unsupported(); +} + +static bool HIDAPI_DriverSwitch2_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) +{ + return SDL_Unsupported(); +} + +static bool HIDAPI_DriverSwitch2_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) +{ + return SDL_Unsupported(); +} + +static bool HIDAPI_DriverSwitch2_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble) +{ + return SDL_Unsupported(); +} + +static Uint32 HIDAPI_DriverSwitch2_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) +{ + return 0; +} + +static bool HIDAPI_DriverSwitch2_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) +{ + return SDL_Unsupported(); +} + +static bool HIDAPI_DriverSwitch2_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size) +{ + return SDL_Unsupported(); +} + +static bool HIDAPI_DriverSwitch2_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled) +{ + return SDL_Unsupported(); +} + +static void HIDAPI_DriverSwitch2_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) +{ +} + +static void HIDAPI_DriverSwitch2_FreeDevice(SDL_HIDAPI_Device *device) +{ +} + +SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch2 = { + SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, + true, + HIDAPI_DriverSwitch2_RegisterHints, + HIDAPI_DriverSwitch2_UnregisterHints, + HIDAPI_DriverSwitch2_IsEnabled, + HIDAPI_DriverSwitch2_IsSupportedDevice, + HIDAPI_DriverSwitch2_InitDevice, + HIDAPI_DriverSwitch2_GetDevicePlayerIndex, + HIDAPI_DriverSwitch2_SetDevicePlayerIndex, + HIDAPI_DriverSwitch2_UpdateDevice, + HIDAPI_DriverSwitch2_OpenJoystick, + HIDAPI_DriverSwitch2_RumbleJoystick, + HIDAPI_DriverSwitch2_RumbleJoystickTriggers, + HIDAPI_DriverSwitch2_GetJoystickCapabilities, + HIDAPI_DriverSwitch2_SetJoystickLED, + HIDAPI_DriverSwitch2_SendJoystickEffect, + HIDAPI_DriverSwitch2_SetJoystickSensorsEnabled, + HIDAPI_DriverSwitch2_CloseJoystick, + HIDAPI_DriverSwitch2_FreeDevice, +}; + +#endif // SDL_JOYSTICK_HIDAPI_SWITCH2 + +#endif // SDL_JOYSTICK_HIDAPI diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c index 5d26deafe8e3c..bc50bc1185951 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick.c +++ b/src/joystick/hidapi/SDL_hidapijoystick.c @@ -75,6 +75,9 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = { &SDL_HIDAPI_DriverJoyCons, &SDL_HIDAPI_DriverSwitch, #endif +#ifdef SDL_JOYSTICK_HIDAPI_SWITCH2 + &SDL_HIDAPI_DriverSwitch2, +#endif #ifdef SDL_JOYSTICK_HIDAPI_WII &SDL_HIDAPI_DriverWii, #endif diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h index f6b8ebfae4053..885573cabbd61 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h @@ -35,6 +35,7 @@ #define SDL_JOYSTICK_HIDAPI_STEAM #define SDL_JOYSTICK_HIDAPI_STEAMDECK #define SDL_JOYSTICK_HIDAPI_SWITCH +#define SDL_JOYSTICK_HIDAPI_SWITCH2 #define SDL_JOYSTICK_HIDAPI_WII #define SDL_JOYSTICK_HIDAPI_XBOX360 #define SDL_JOYSTICK_HIDAPI_XBOXONE @@ -157,6 +158,7 @@ extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamDeck; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch; +extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch2; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverWii; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W; diff --git a/src/joystick/usb_ids.h b/src/joystick/usb_ids.h index 33a8a6cb7e4f8..1804dd556702b 100644 --- a/src/joystick/usb_ids.h +++ b/src/joystick/usb_ids.h @@ -101,6 +101,7 @@ #define USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR 0x2008 // Used by joycond #define USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT 0x2007 #define USB_PRODUCT_NINTENDO_SWITCH_PRO 0x2009 +#define USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER 0x2073 #define USB_PRODUCT_NINTENDO_WII_REMOTE 0x0306 #define USB_PRODUCT_NINTENDO_WII_REMOTE2 0x0330 #define USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103 0x7210 From d2353089fc09b87bd65a797fe95f8b7f7aaf94e2 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Sat, 5 Jul 2025 14:52:06 -0400 Subject: [PATCH 2/8] hidapi: Add support for NSO GameCube controller via libusb. Thanks to Nohzockt for the initial libusb init and hidapi polling work! --- src/hidapi/SDL_hidapi.c | 15 ++- src/hidapi/libusb/hid.c | 78 ++++++++++++++ src/joystick/SDL_gamepad.c | 7 ++ src/joystick/SDL_joystick.c | 1 + src/joystick/hidapi/SDL_hidapi_switch2.c | 127 ++++++++++++++++++++++- src/joystick/usb_ids.h | 1 + 6 files changed, 225 insertions(+), 4 deletions(-) diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c index 41e24ec8a0474..3d26abbea5d44 100644 --- a/src/hidapi/SDL_hidapi.c +++ b/src/hidapi/SDL_hidapi.c @@ -743,6 +743,14 @@ static struct int *actual_length, unsigned int timeout ); + int (LIBUSB_CALL *bulk_transfer)( + libusb_device_handle *dev_handle, + unsigned char endpoint, + unsigned char *data, + int length, + int *transferred, + unsigned int timeout + ); int (LIBUSB_CALL *handle_events)(libusb_context *ctx); int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed); const char * (LIBUSB_CALL *error_name)(int errcode); @@ -776,6 +784,7 @@ static struct #define libusb_free_transfer libusb_ctx.free_transfer #define libusb_control_transfer libusb_ctx.control_transfer #define libusb_interrupt_transfer libusb_ctx.interrupt_transfer +#define libusb_bulk_transfer libusb_ctx.bulk_transfer #define libusb_handle_events libusb_ctx.handle_events #define libusb_handle_events_completed libusb_ctx.handle_events_completed #define libusb_error_name libusb_ctx.error_name @@ -843,6 +852,7 @@ typedef struct LIBUSB_hid_device_ LIBUSB_hid_device; #undef libusb_free_transfer #undef libusb_control_transfer #undef libusb_interrupt_transfer +#undef libusb_bulk_transfer #undef libusb_handle_events #undef libusb_handle_events_completed #undef libusb_error_name @@ -889,7 +899,9 @@ static const struct { Uint16 vendor; Uint16 product; } SDL_libusb_whitelist[] = { - { 0x057e, 0x0337 } // Nintendo WUP-028, Wii U/Switch GameCube Adapter + { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER }, + { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH2_PRO }, + { USB_VENDOR_NINTENDO, USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER }, }; static bool IsInWhitelist(Uint16 vendor, Uint16 product) @@ -1212,6 +1224,7 @@ int SDL_hid_init(void) LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer) LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char *, uint16_t, unsigned int), control_transfer) LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), bulk_transfer) LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events) LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed) LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name) diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c index 94cc50dcb41f0..a21637e01ebc7 100644 --- a/src/hidapi/libusb/hid.c +++ b/src/hidapi/libusb/hid.c @@ -1307,6 +1307,79 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV } } +static bool is_ns2(unsigned short idVendor, unsigned short idProduct) +{ + if (idVendor == 0x057e) { + if (idProduct == 0x2069) { + return true; + } + if (idProduct == 0x2073) { + return true; + } + } + return false; +} + +static bool ns2_find_bulk_out_endpoint(libusb_device_handle* handle, uint8_t* endpoint_out) +{ + struct libusb_config_descriptor* config; + if (libusb_get_config_descriptor(libusb_get_device(handle), 0, &config) != 0) { + return false; + } + + for (int i = 0; i < config->bNumInterfaces; i++) { + const struct libusb_interface* iface = &config->interface[i]; + for (int j = 0; j < iface->num_altsetting; j++) { + const struct libusb_interface_descriptor* altsetting = &iface->altsetting[j]; + if (altsetting->bInterfaceNumber == 1) { + for (int k = 0; k < altsetting->bNumEndpoints; k++) { + const struct libusb_endpoint_descriptor* ep = &altsetting->endpoint[k]; + if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK && (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) { + *endpoint_out = ep->bEndpointAddress; + libusb_free_config_descriptor(config); + return true; + } + } + } + } + } + + libusb_free_config_descriptor(config); + return false; +} + +static void init_ns2(libusb_device_handle *device_handle) +{ + const unsigned char DEFAULT_REPORT_DATA[] = { + 0x03, 0x91, 0x00, 0x0d, 0x00, 0x08, + 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF + }; + const unsigned char SET_LED_DATA[] = { + 0x09, 0x91, 0x00, 0x07, 0x00, 0x08, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + uint8_t endpoint_out = 0; + if (!ns2_find_bulk_out_endpoint(device_handle, &endpoint_out)) { + return; + } + + int transferred; + libusb_bulk_transfer(device_handle, + endpoint_out, + (unsigned char*)DEFAULT_REPORT_DATA, + sizeof(DEFAULT_REPORT_DATA), + &transferred, + 1000); + + libusb_bulk_transfer(device_handle, + endpoint_out, + (unsigned char*)SET_LED_DATA, + sizeof(SET_LED_DATA), + &transferred, + 1000); +} + static void calculate_device_quirks(hid_device *dev, unsigned short idVendor, unsigned short idProduct) { static const int VENDOR_SONY = 0x054c; @@ -1368,6 +1441,11 @@ static int hidapi_initialize_device(hid_device *dev, const struct libusb_interfa init_xboxone(dev->device_handle, desc.idVendor, desc.idProduct, conf_desc); } + /* Initialize NSO GameCube controllers */ + if (is_ns2(desc.idVendor, desc.idProduct)) { + init_ns2(dev->device_handle); + } + /* Store off the string descriptor indexes */ dev->manufacturer_index = desc.iManufacturer; dev->product_index = desc.iProduct; diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c index 8a5ecc0ab9279..aaed810a8d37a 100644 --- a/src/joystick/SDL_gamepad.c +++ b/src/joystick/SDL_gamepad.c @@ -715,6 +715,13 @@ static GamepadMapping_t *SDL_CreateMappingForHIDAPIGamepad(SDL_GUID guid) product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER3))) { // GameCube driver has 12 buttons and 6 axes SDL_strlcat(mapping_string, "a:b0,b:b2,dpdown:b6,dpleft:b4,dpright:b5,dpup:b7,lefttrigger:a4,leftx:a0,lefty:a1~,rightshoulder:b9,righttrigger:a5,rightx:a2,righty:a3~,start:b8,x:b1,y:b3,hint:!SDL_GAMECONTROLLER_USE_GAMECUBE_LABELS:=1,", sizeof(mapping_string)); + } else if (vendor == USB_VENDOR_NINTENDO && + (product == USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER)) { + // Switch 2 GameCube has additional buttons for ZL and C + SDL_strlcat(mapping_string, "a:b1,b:b3,dpdown:b8,dpleft:b10,dpright:b9,dpup:b11,guide:b16,leftshoulder:b13,lefttrigger:a4,leftx:a0,lefty:a1~,misc1:b17,misc2:b20,misc3:b4,misc4:b12,rightshoulder:b5,righttrigger:a5,rightx:a2,righty:a3~,start:b6,x:b0,y:b2,hint:!SDL_GAMECONTROLLER_USE_GAMECUBE_LABELS:=1,", sizeof(mapping_string)); + } else if (vendor == USB_VENDOR_NINTENDO && + (product == USB_PRODUCT_NINTENDO_SWITCH2_PRO)) { + SDL_strlcat(mapping_string, "a:b1,b:b0,dpdown:b8,dpleft:b10,dpright:b9,dpup:b11,guide:b16,leftshoulder:b12,lefttrigger:b13,leftx:a0,lefty:a1~,misc1:b17,misc2:b20,rightshoulder:b4,righttrigger:b5,rightx:a2,righty:a3~,start:b6,back:b14,x:b3,y:b2,leftstick:b15,rightstick:b7,paddle1:b18,paddle2:b19,", sizeof(mapping_string)); } else if (vendor == USB_VENDOR_NINTENDO && (guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCLeft || guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCRight || diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index 0e9fcab3b55c4..1cc1c06e3fd60 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -483,6 +483,7 @@ static Uint32 initial_gamecube_devices[] = { MAKE_VIDPID(0x0079, 0x1844), // DragonRise GameCube Controller Adapter MAKE_VIDPID(0x0079, 0x1846), // DragonRise GameCube Controller Adapter MAKE_VIDPID(0x057e, 0x0337), // Nintendo Wii U GameCube Controller Adapter + MAKE_VIDPID(0x057e, 0x2073), // Nintendo Switch 2 NSO GameCube Controller MAKE_VIDPID(0x0926, 0x8888), // Cyber Gadget GameCube Controller MAKE_VIDPID(0x0e6f, 0x0185), // PDP Wired Fight Pad Pro for Nintendo Switch MAKE_VIDPID(0x1a34, 0xf705), // GameCube {HuiJia USB box} diff --git a/src/joystick/hidapi/SDL_hidapi_switch2.c b/src/joystick/hidapi/SDL_hidapi_switch2.c index 4891ac169e28e..641df9f4dca3b 100644 --- a/src/joystick/hidapi/SDL_hidapi_switch2.c +++ b/src/joystick/hidapi/SDL_hidapi_switch2.c @@ -51,6 +51,9 @@ static bool HIDAPI_DriverSwitch2_IsEnabled(void) static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol) { if (vendor_id == USB_VENDOR_NINTENDO) { + if (product_id == USB_PRODUCT_NINTENDO_SWITCH2_PRO) { + return true; + } if (product_id == USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER) { return true; } @@ -61,7 +64,7 @@ static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, co static bool HIDAPI_DriverSwitch2_InitDevice(SDL_HIDAPI_Device *device) { - return SDL_Unsupported(); + return HIDAPI_JoystickConnected(device, NULL); } static int HIDAPI_DriverSwitch2_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id) @@ -75,12 +78,130 @@ static void HIDAPI_DriverSwitch2_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, static bool HIDAPI_DriverSwitch2_UpdateDevice(SDL_HIDAPI_Device *device) { - return SDL_Unsupported(); + const struct { + int byte; + unsigned char mask; + } buttons[] = { + {3, 0x01}, // B + {3, 0x02}, // A + {3, 0x04}, // Y + {3, 0x08}, // X + {3, 0x10}, // R (GameCube R Click) + {3, 0x20}, // ZR (GameCube Z) + {3, 0x40}, // PLUS (GameCube Start) + {3, 0x80}, // RS (not on GameCube) + {4, 0x01}, // DPAD_DOWN + {4, 0x02}, // DPAD_RIGHT + {4, 0x04}, // DPAD_LEFT + {4, 0x08}, // DPAD_UP + {4, 0x10}, // L (GameCube L Click) + {4, 0x20}, // ZL + {4, 0x40}, // MINUS (not on GameCube) + {4, 0x80}, // LS (not on GameCube) + {5, 0x01}, // Home + {5, 0x02}, // Capture + {5, 0x04}, // GR (not on GameCube) + {5, 0x08}, // GL (not on GameCube) + {5, 0x10}, // C + }; + + SDL_Joystick *joystick = NULL; + if (device->num_joysticks > 0) { + joystick = SDL_GetJoystickFromID(device->joysticks[0]); + } + if (joystick == NULL) { + return true; + } + + // Read input packet + + Uint8 packet[USB_PACKET_LENGTH]; + int size; + while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) { + if (size < 15) { + continue; + } + + Uint64 timestamp = SDL_GetTicksNS(); + for (size_t i = 0; i < SDL_arraysize(buttons); ++i) { + SDL_SendJoystickButton( + timestamp, + joystick, + (Uint8) i, + (packet[buttons[i].byte] & buttons[i].mask) != 0 + ); + } + SDL_SendJoystickAxis( + timestamp, + joystick, + SDL_GAMEPAD_AXIS_LEFTX, + (Sint16) HIDAPI_RemapVal( + (float) (packet[6] | ((packet[7] & 0x0F) << 8)), + 0, + 4096, + SDL_MIN_SINT16, + SDL_MAX_SINT16 + ) + ); + SDL_SendJoystickAxis( + timestamp, + joystick, + SDL_GAMEPAD_AXIS_LEFTY, + (Sint16) HIDAPI_RemapVal( + (float) ((packet[7] >> 4) | (packet[8] << 4)), + 0, + 4096, + SDL_MIN_SINT16, + SDL_MAX_SINT16 + ) + ); + SDL_SendJoystickAxis( + timestamp, + joystick, + SDL_GAMEPAD_AXIS_RIGHTX, + (Sint16) HIDAPI_RemapVal( + (float) (packet[9] | ((packet[10] & 0x0F) << 8)), + 0, + 4096, + SDL_MIN_SINT16, + SDL_MAX_SINT16 + ) + ); + SDL_SendJoystickAxis( + timestamp, + joystick, + SDL_GAMEPAD_AXIS_RIGHTY, + (Sint16) HIDAPI_RemapVal( + (float) ((packet[10] >> 4) | (packet[11] << 4)), + 0, + 4096, + SDL_MIN_SINT16, + SDL_MAX_SINT16 + ) + ); + SDL_SendJoystickAxis( + timestamp, + joystick, + SDL_GAMEPAD_AXIS_LEFT_TRIGGER, + (Sint16) HIDAPI_RemapVal(packet[13], 0, 255, SDL_MIN_SINT16, SDL_MAX_SINT16) + ); + SDL_SendJoystickAxis( + timestamp, + joystick, + SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, + (Sint16) HIDAPI_RemapVal(packet[14], 0, 255, SDL_MIN_SINT16, SDL_MAX_SINT16) + ); + } + return true; } static bool HIDAPI_DriverSwitch2_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) { - return SDL_Unsupported(); + // Initialize the joystick capabilities + joystick->nbuttons = 21; + joystick->naxes = SDL_GAMEPAD_AXIS_COUNT; + + return true; } static bool HIDAPI_DriverSwitch2_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) diff --git a/src/joystick/usb_ids.h b/src/joystick/usb_ids.h index 1804dd556702b..23c31bb5a332b 100644 --- a/src/joystick/usb_ids.h +++ b/src/joystick/usb_ids.h @@ -101,6 +101,7 @@ #define USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR 0x2008 // Used by joycond #define USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT 0x2007 #define USB_PRODUCT_NINTENDO_SWITCH_PRO 0x2009 +#define USB_PRODUCT_NINTENDO_SWITCH2_PRO 0x2069 #define USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER 0x2073 #define USB_PRODUCT_NINTENDO_WII_REMOTE 0x0306 #define USB_PRODUCT_NINTENDO_WII_REMOTE2 0x0330 From 8868a2c788b430c9e0e3a13013ae520d7b36b31b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 27 Aug 2025 12:40:05 -0700 Subject: [PATCH 3/8] Claim the interface before doing bulk transfers --- src/hidapi/libusb/hid.c | 107 +++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c index a21637e01ebc7..bd7b57df80cf3 100644 --- a/src/hidapi/libusb/hid.c +++ b/src/hidapi/libusb/hid.c @@ -1320,64 +1320,67 @@ static bool is_ns2(unsigned short idVendor, unsigned short idProduct) return false; } -static bool ns2_find_bulk_out_endpoint(libusb_device_handle* handle, uint8_t* endpoint_out) +static void init_ns2(libusb_device_handle *device_handle, const struct libusb_config_descriptor *conf_desc) { - struct libusb_config_descriptor* config; - if (libusb_get_config_descriptor(libusb_get_device(handle), 0, &config) != 0) { - return false; - } - - for (int i = 0; i < config->bNumInterfaces; i++) { - const struct libusb_interface* iface = &config->interface[i]; - for (int j = 0; j < iface->num_altsetting; j++) { - const struct libusb_interface_descriptor* altsetting = &iface->altsetting[j]; - if (altsetting->bInterfaceNumber == 1) { - for (int k = 0; k < altsetting->bNumEndpoints; k++) { - const struct libusb_endpoint_descriptor* ep = &altsetting->endpoint[k]; + int j, k, l, res; + + for (j = 0; j < conf_desc->bNumInterfaces; j++) { + const struct libusb_interface *intf = &conf_desc->interface[j]; + for (k = 0; k < intf->num_altsetting; k++) { + const struct libusb_interface_descriptor *intf_desc = &intf->altsetting[k]; + if (intf_desc->bInterfaceNumber == 1) { + uint8_t endpoint = 0; + for (l = 0; l < intf_desc->bNumEndpoints; l++) { + const struct libusb_endpoint_descriptor* ep = &intf_desc->endpoint[l]; if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK && (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) { - *endpoint_out = ep->bEndpointAddress; - libusb_free_config_descriptor(config); - return true; + endpoint = ep->bEndpointAddress; + break; } } - } - } - } - libusb_free_config_descriptor(config); - return false; -} + if (endpoint) { + res = libusb_claim_interface(device_handle, intf_desc->bInterfaceNumber); + if (res < 0) { + LOG("can't claim interface %d: %d\n", intf_desc->bInterfaceNumber, res); + continue; + } -static void init_ns2(libusb_device_handle *device_handle) -{ - const unsigned char DEFAULT_REPORT_DATA[] = { - 0x03, 0x91, 0x00, 0x0d, 0x00, 0x08, - 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF - }; - const unsigned char SET_LED_DATA[] = { - 0x09, 0x91, 0x00, 0x07, 0x00, 0x08, - 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; + const unsigned char DEFAULT_REPORT_DATA[] = { + 0x03, 0x91, 0x00, 0x0d, 0x00, 0x08, + 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF + }; + const unsigned char SET_LED_DATA[] = { + 0x09, 0x91, 0x00, 0x07, 0x00, 0x08, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + int transferred; + res = libusb_bulk_transfer(device_handle, + endpoint, + (unsigned char*)DEFAULT_REPORT_DATA, + sizeof(DEFAULT_REPORT_DATA), + &transferred, + 1000); + if (res < 0) { + LOG("can't set report data: %d\n", res); + } - uint8_t endpoint_out = 0; - if (!ns2_find_bulk_out_endpoint(device_handle, &endpoint_out)) { - return; - } + res = libusb_bulk_transfer(device_handle, + endpoint, + (unsigned char*)SET_LED_DATA, + sizeof(SET_LED_DATA), + &transferred, + 1000); + if (res < 0) { + LOG("can't set LED data: %d\n", res); + } - int transferred; - libusb_bulk_transfer(device_handle, - endpoint_out, - (unsigned char*)DEFAULT_REPORT_DATA, - sizeof(DEFAULT_REPORT_DATA), - &transferred, - 1000); - - libusb_bulk_transfer(device_handle, - endpoint_out, - (unsigned char*)SET_LED_DATA, - sizeof(SET_LED_DATA), - &transferred, - 1000); + libusb_release_interface(device_handle, intf_desc->bInterfaceNumber); + return; + } + } + } + } } static void calculate_device_quirks(hid_device *dev, unsigned short idVendor, unsigned short idProduct) @@ -1441,9 +1444,9 @@ static int hidapi_initialize_device(hid_device *dev, const struct libusb_interfa init_xboxone(dev->device_handle, desc.idVendor, desc.idProduct, conf_desc); } - /* Initialize NSO GameCube controllers */ + /* Initialize Nintendo Switch 2 controllers */ if (is_ns2(desc.idVendor, desc.idProduct)) { - init_ns2(dev->device_handle); + init_ns2(dev->device_handle, conf_desc); } /* Store off the string descriptor indexes */ From b7056a33de498d8c85faa05056d564e0979e9fd8 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 27 Aug 2025 12:46:34 -0700 Subject: [PATCH 4/8] Fixed controller name when hotplugging the Nintendo Switch 2 Pro Controller --- src/joystick/hidapi/SDL_hidapi_switch2.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/joystick/hidapi/SDL_hidapi_switch2.c b/src/joystick/hidapi/SDL_hidapi_switch2.c index 641df9f4dca3b..8e0a23a1a0557 100644 --- a/src/joystick/hidapi/SDL_hidapi_switch2.c +++ b/src/joystick/hidapi/SDL_hidapi_switch2.c @@ -51,12 +51,11 @@ static bool HIDAPI_DriverSwitch2_IsEnabled(void) static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol) { if (vendor_id == USB_VENDOR_NINTENDO) { - if (product_id == USB_PRODUCT_NINTENDO_SWITCH2_PRO) { + switch (product_id) { + case USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER: + case USB_PRODUCT_NINTENDO_SWITCH2_PRO: return true; - } - if (product_id == USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER) { - return true; - } + } } return false; @@ -64,6 +63,17 @@ static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, co static bool HIDAPI_DriverSwitch2_InitDevice(SDL_HIDAPI_Device *device) { + // Sometimes the device handle isn't available during enumeration so we don't get the device name, so set it explicitly + switch (device->product_id) { + case USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER: + HIDAPI_SetDeviceName(device, "Nintendo GameCube Controller"); + break; + case USB_PRODUCT_NINTENDO_SWITCH2_PRO: + HIDAPI_SetDeviceName(device, "Nintendo Switch Pro Controller"); + break; + default: + break; + } return HIDAPI_JoystickConnected(device, NULL); } From 45f9326b77ed75ff261d6672afe68c2334f798e4 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 27 Aug 2025 12:50:36 -0700 Subject: [PATCH 5/8] Fixed button mapping for the Nintendo Switch 2 Pro Controller --- src/joystick/SDL_gamepad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c index aaed810a8d37a..4cabd0f752dd7 100644 --- a/src/joystick/SDL_gamepad.c +++ b/src/joystick/SDL_gamepad.c @@ -721,7 +721,7 @@ static GamepadMapping_t *SDL_CreateMappingForHIDAPIGamepad(SDL_GUID guid) SDL_strlcat(mapping_string, "a:b1,b:b3,dpdown:b8,dpleft:b10,dpright:b9,dpup:b11,guide:b16,leftshoulder:b13,lefttrigger:a4,leftx:a0,lefty:a1~,misc1:b17,misc2:b20,misc3:b4,misc4:b12,rightshoulder:b5,righttrigger:a5,rightx:a2,righty:a3~,start:b6,x:b0,y:b2,hint:!SDL_GAMECONTROLLER_USE_GAMECUBE_LABELS:=1,", sizeof(mapping_string)); } else if (vendor == USB_VENDOR_NINTENDO && (product == USB_PRODUCT_NINTENDO_SWITCH2_PRO)) { - SDL_strlcat(mapping_string, "a:b1,b:b0,dpdown:b8,dpleft:b10,dpright:b9,dpup:b11,guide:b16,leftshoulder:b12,lefttrigger:b13,leftx:a0,lefty:a1~,misc1:b17,misc2:b20,rightshoulder:b4,righttrigger:b5,rightx:a2,righty:a3~,start:b6,back:b14,x:b3,y:b2,leftstick:b15,rightstick:b7,paddle1:b18,paddle2:b19,", sizeof(mapping_string)); + SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:b8,dpleft:b10,dpright:b9,dpup:b11,guide:b16,leftshoulder:b12,lefttrigger:b13,leftx:a0,lefty:a1~,misc1:b17,misc2:b20,rightshoulder:b4,righttrigger:b5,rightx:a2,righty:a3~,start:b6,back:b14,x:b2,y:b3,leftstick:b15,rightstick:b7,paddle1:b18,paddle2:b19,hint:!SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1,", sizeof(mapping_string)); } else if (vendor == USB_VENDOR_NINTENDO && (guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCLeft || guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCRight || From 507d31d3b686e723fca1c161d5e0e189a20aa5f2 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 27 Aug 2025 14:35:17 -0700 Subject: [PATCH 6/8] Move libusb support into a shared location --- VisualC-GDK/SDL/SDL.vcxproj | 1 + VisualC-GDK/SDL/SDL.vcxproj.filters | 1 + VisualC/SDL/SDL.vcxproj | 1 + VisualC/SDL/SDL.vcxproj.filters | 3 + Xcode/SDL/SDL.xcodeproj/project.pbxproj | 8 + src/hidapi/SDL_hidapi.c | 245 ++++++------------------ src/misc/SDL_libusb.c | 105 ++++++++++ src/misc/SDL_libusb.h | 97 ++++++++++ 8 files changed, 277 insertions(+), 184 deletions(-) create mode 100644 src/misc/SDL_libusb.c create mode 100644 src/misc/SDL_libusb.h diff --git a/VisualC-GDK/SDL/SDL.vcxproj b/VisualC-GDK/SDL/SDL.vcxproj index 266d426fdaa36..7199fc2acc058 100644 --- a/VisualC-GDK/SDL/SDL.vcxproj +++ b/VisualC-GDK/SDL/SDL.vcxproj @@ -765,6 +765,7 @@ + diff --git a/VisualC-GDK/SDL/SDL.vcxproj.filters b/VisualC-GDK/SDL/SDL.vcxproj.filters index 1dcabe37beef4..c2396422fa0cc 100644 --- a/VisualC-GDK/SDL/SDL.vcxproj.filters +++ b/VisualC-GDK/SDL/SDL.vcxproj.filters @@ -98,6 +98,7 @@ + diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj index af4e646ae76a4..6be47a5eff1ed 100644 --- a/VisualC/SDL/SDL.vcxproj +++ b/VisualC/SDL/SDL.vcxproj @@ -637,6 +637,7 @@ + diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters index 74d32874986b9..cc1be3d535590 100644 --- a/VisualC/SDL/SDL.vcxproj.filters +++ b/VisualC/SDL/SDL.vcxproj.filters @@ -1137,6 +1137,9 @@ loadso\windows + + misc + misc diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj index f6a46240cfec1..77193e11c1ec9 100644 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj @@ -521,6 +521,8 @@ F3D60A8328C16A1900788A3A /* SDL_hidapi_wii.c in Sources */ = {isa = PBXBuildFile; fileRef = F3D60A8228C16A1800788A3A /* SDL_hidapi_wii.c */; }; F3D8BDFC2D6D2C7000B22FA1 /* SDL_eventwatch_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F3D8BDFB2D6D2C7000B22FA1 /* SDL_eventwatch_c.h */; }; F3D8BDFD2D6D2C7000B22FA1 /* SDL_eventwatch.c in Sources */ = {isa = PBXBuildFile; fileRef = F3D8BDFA2D6D2C7000B22FA1 /* SDL_eventwatch.c */; }; + F3DC38C92E5FC60300CD73DE /* SDL_libusb.h in Headers */ = {isa = PBXBuildFile; fileRef = F3DC38C72E5FC60300CD73DE /* SDL_libusb.h */; }; + F3DC38CA2E5FC60300CD73DE /* SDL_libusb.c in Sources */ = {isa = PBXBuildFile; fileRef = F3DC38C82E5FC60300CD73DE /* SDL_libusb.c */; }; F3DDCC562AFD42B600B0842B /* SDL_clipboard_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F3DDCC4D2AFD42B500B0842B /* SDL_clipboard_c.h */; }; F3DDCC5B2AFD42B600B0842B /* SDL_video_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F3DDCC522AFD42B600B0842B /* SDL_video_c.h */; }; F3DDCC5D2AFD42B600B0842B /* SDL_rect_impl.h in Headers */ = {isa = PBXBuildFile; fileRef = F3DDCC542AFD42B600B0842B /* SDL_rect_impl.h */; }; @@ -1099,6 +1101,8 @@ F3D60A8228C16A1800788A3A /* SDL_hidapi_wii.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_wii.c; sourceTree = ""; }; F3D8BDFA2D6D2C7000B22FA1 /* SDL_eventwatch.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_eventwatch.c; sourceTree = ""; }; F3D8BDFB2D6D2C7000B22FA1 /* SDL_eventwatch_c.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDL_eventwatch_c.h; sourceTree = ""; }; + F3DC38C72E5FC60300CD73DE /* SDL_libusb.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDL_libusb.h; sourceTree = ""; }; + F3DC38C82E5FC60300CD73DE /* SDL_libusb.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_libusb.c; sourceTree = ""; }; F3DDCC4D2AFD42B500B0842B /* SDL_clipboard_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_clipboard_c.h; sourceTree = ""; }; F3DDCC522AFD42B600B0842B /* SDL_video_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_video_c.h; sourceTree = ""; }; F3DDCC542AFD42B600B0842B /* SDL_rect_impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_rect_impl.h; sourceTree = ""; }; @@ -1412,6 +1416,8 @@ children = ( F3ADAB8C2576F08500A6B1D9 /* ios */, 5616CA48252BB285005D5928 /* macos */, + F3DC38C72E5FC60300CD73DE /* SDL_libusb.h */, + F3DC38C82E5FC60300CD73DE /* SDL_libusb.c */, 5616CA4A252BB2A6005D5928 /* SDL_sysurl.h */, 5616CA49252BB2A5005D5928 /* SDL_url.c */, ); @@ -2747,6 +2753,7 @@ F3FA5A252B59ACE000FEAD97 /* yuv_rgb_common.h in Headers */, F3FA5A1D2B59ACE000FEAD97 /* yuv_rgb_internal.h in Headers */, F3D8BDFC2D6D2C7000B22FA1 /* SDL_eventwatch_c.h in Headers */, + F3DC38C92E5FC60300CD73DE /* SDL_libusb.h in Headers */, F3FA5A242B59ACE000FEAD97 /* yuv_rgb_lsx.h in Headers */, F3FA5A1E2B59ACE000FEAD97 /* yuv_rgb_lsx_func.h in Headers */, F3FA5A1F2B59ACE000FEAD97 /* yuv_rgb_sse.h in Headers */, @@ -3097,6 +3104,7 @@ 0000481D255AF155B42C0000 /* SDL_sysfsops.c in Sources */, 0000494CC93F3E624D3C0000 /* SDL_systime.c in Sources */, 000095FA1BDE436CF3AF0000 /* SDL_time.c in Sources */, + F3DC38CA2E5FC60300CD73DE /* SDL_libusb.c in Sources */, 0000140640E77F73F1DF0000 /* SDL_dialog_utils.c in Sources */, 0000D5B526B85DE7AB1C0000 /* SDL_cocoapen.m in Sources */, 6312C66D2B42341400A7BB00 /* SDL_murmur3.c in Sources */, diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c index 3d26abbea5d44..5f86a361e2f59 100644 --- a/src/hidapi/SDL_hidapi.c +++ b/src/hidapi/SDL_hidapi.c @@ -689,105 +689,40 @@ typedef struct DRIVER_hid_device_ DRIVER_hid_device; #ifdef HAVE_LIBUSB // libusb HIDAPI Implementation -// Include this now, for our dynamically-loaded libusb context -#include - -static struct -{ - SDL_SharedObject *libhandle; - - /* *INDENT-OFF* */ // clang-format off - int (LIBUSB_CALL *init)(libusb_context **ctx); - void (LIBUSB_CALL *exit)(libusb_context *ctx); - ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list); - void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices); - int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc); - int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config); - int (LIBUSB_CALL *get_config_descriptor)( - libusb_device *dev, - uint8_t config_index, - struct libusb_config_descriptor **config - ); - void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config); - uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev); - int (LIBUSB_CALL *get_port_numbers)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len); - uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev); - int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle); - void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle); - libusb_device *(LIBUSB_CALL *get_device)(libusb_device_handle *dev_handle); - int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number); - int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number); - int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number); - int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number); - int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number); - int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting); - struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets); - int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer); - int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer); - void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer); - int (LIBUSB_CALL *control_transfer)( - libusb_device_handle *dev_handle, - uint8_t request_type, - uint8_t bRequest, - uint16_t wValue, - uint16_t wIndex, - unsigned char *data, - uint16_t wLength, - unsigned int timeout - ); - int (LIBUSB_CALL *interrupt_transfer)( - libusb_device_handle *dev_handle, - unsigned char endpoint, - unsigned char *data, - int length, - int *actual_length, - unsigned int timeout - ); - int (LIBUSB_CALL *bulk_transfer)( - libusb_device_handle *dev_handle, - unsigned char endpoint, - unsigned char *data, - int length, - int *transferred, - unsigned int timeout - ); - int (LIBUSB_CALL *handle_events)(libusb_context *ctx); - int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed); - const char * (LIBUSB_CALL *error_name)(int errcode); -/* *INDENT-ON* */ // clang-format on - -} libusb_ctx; - -#define libusb_init libusb_ctx.init -#define libusb_exit libusb_ctx.exit -#define libusb_get_device_list libusb_ctx.get_device_list -#define libusb_free_device_list libusb_ctx.free_device_list -#define libusb_get_device_descriptor libusb_ctx.get_device_descriptor -#define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor -#define libusb_get_config_descriptor libusb_ctx.get_config_descriptor -#define libusb_free_config_descriptor libusb_ctx.free_config_descriptor -#define libusb_get_bus_number libusb_ctx.get_bus_number -#define libusb_get_port_numbers libusb_ctx.get_port_numbers -#define libusb_get_device_address libusb_ctx.get_device_address -#define libusb_open libusb_ctx.open -#define libusb_close libusb_ctx.close -#define libusb_get_device libusb_ctx.get_device -#define libusb_claim_interface libusb_ctx.claim_interface -#define libusb_release_interface libusb_ctx.release_interface -#define libusb_kernel_driver_active libusb_ctx.kernel_driver_active -#define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver -#define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver -#define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting -#define libusb_alloc_transfer libusb_ctx.alloc_transfer -#define libusb_submit_transfer libusb_ctx.submit_transfer -#define libusb_cancel_transfer libusb_ctx.cancel_transfer -#define libusb_free_transfer libusb_ctx.free_transfer -#define libusb_control_transfer libusb_ctx.control_transfer -#define libusb_interrupt_transfer libusb_ctx.interrupt_transfer -#define libusb_bulk_transfer libusb_ctx.bulk_transfer -#define libusb_handle_events libusb_ctx.handle_events -#define libusb_handle_events_completed libusb_ctx.handle_events_completed -#define libusb_error_name libusb_ctx.error_name +#include "../misc/SDL_libusb.h" + +static SDL_LibUSBContext *libusb_ctx; + +#define libusb_init libusb_ctx->init +#define libusb_exit libusb_ctx->exit +#define libusb_get_device_list libusb_ctx->get_device_list +#define libusb_free_device_list libusb_ctx->free_device_list +#define libusb_get_device_descriptor libusb_ctx->get_device_descriptor +#define libusb_get_active_config_descriptor libusb_ctx->get_active_config_descriptor +#define libusb_get_config_descriptor libusb_ctx->get_config_descriptor +#define libusb_free_config_descriptor libusb_ctx->free_config_descriptor +#define libusb_get_bus_number libusb_ctx->get_bus_number +#define libusb_get_port_numbers libusb_ctx->get_port_numbers +#define libusb_get_device_address libusb_ctx->get_device_address +#define libusb_open libusb_ctx->open +#define libusb_close libusb_ctx->close +#define libusb_get_device libusb_ctx->get_device +#define libusb_claim_interface libusb_ctx->claim_interface +#define libusb_release_interface libusb_ctx->release_interface +#define libusb_kernel_driver_active libusb_ctx->kernel_driver_active +#define libusb_detach_kernel_driver libusb_ctx->detach_kernel_driver +#define libusb_attach_kernel_driver libusb_ctx->attach_kernel_driver +#define libusb_set_interface_alt_setting libusb_ctx->set_interface_alt_setting +#define libusb_alloc_transfer libusb_ctx->alloc_transfer +#define libusb_submit_transfer libusb_ctx->submit_transfer +#define libusb_cancel_transfer libusb_ctx->cancel_transfer +#define libusb_free_transfer libusb_ctx->free_transfer +#define libusb_control_transfer libusb_ctx->control_transfer +#define libusb_interrupt_transfer libusb_ctx->interrupt_transfer +#define libusb_bulk_transfer libusb_ctx->bulk_transfer +#define libusb_handle_events libusb_ctx->handle_events +#define libusb_handle_events_completed libusb_ctx->handle_events_completed +#define libusb_error_name libusb_ctx->error_name struct LIBUSB_hid_device_; typedef struct LIBUSB_hid_device_ LIBUSB_hid_device; @@ -1179,71 +1114,15 @@ int SDL_hid_init(void) if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB, true)) { SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "libusb disabled with SDL_HINT_HIDAPI_LIBUSB"); - libusb_ctx.libhandle = NULL; } else { ++attempts; -#ifdef SDL_LIBUSB_DYNAMIC - libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC); -#else - libusb_ctx.libhandle = (void *)1; -#endif - if (libusb_ctx.libhandle != NULL) { - bool loaded = true; -#ifdef SDL_LIBUSB_DYNAMIC -#define LOAD_LIBUSB_SYMBOL(type, func) \ - if ((libusb_ctx.func = (type)SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func)) == NULL) { \ - loaded = false; \ - } -#else -#define LOAD_LIBUSB_SYMBOL(type, func) \ - libusb_ctx.func = libusb_##func; -#endif - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init) - LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit) - LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list) - LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor) - LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor) - LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers) - LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open) - LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close) - LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting) - LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer) - LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char *, uint16_t, unsigned int), control_transfer) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), bulk_transfer) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events) - LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed) - LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name) -#undef LOAD_LIBUSB_SYMBOL - - if (!loaded) { -#ifdef SDL_LIBUSB_DYNAMIC - SDL_UnloadObject(libusb_ctx.libhandle); -#endif - libusb_ctx.libhandle = NULL; - // SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function"); - } else if (LIBUSB_hid_init() < 0) { -#ifdef SDL_LIBUSB_DYNAMIC - SDL_UnloadObject(libusb_ctx.libhandle); -#endif - libusb_ctx.libhandle = NULL; - } else { - ++success; - } + if (!SDL_InitLibUSB(&libusb_ctx)) { + SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Couldn't load libusb"); + } else if (LIBUSB_hid_init() < 0) { + SDL_QuitLibUSB(); + libusb_ctx = NULL; + } else { + ++success; } } #endif // HAVE_LIBUSB @@ -1297,12 +1176,10 @@ int SDL_hid_exit(void) #endif // HAVE_PLATFORM_BACKEND #ifdef HAVE_LIBUSB - if (libusb_ctx.libhandle) { + if (libusb_ctx) { result |= LIBUSB_hid_exit(); -#ifdef SDL_LIBUSB_DYNAMIC - SDL_UnloadObject(libusb_ctx.libhandle); -#endif - libusb_ctx.libhandle = NULL; + SDL_QuitLibUSB(); + libusb_ctx = NULL; } #endif // HAVE_LIBUSB @@ -1443,7 +1320,7 @@ struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned #endif #ifdef HAVE_LIBUSB - if (libusb_ctx.libhandle) { + if (libusb_ctx) { usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id); if (use_libusb_whitelist) { @@ -1544,7 +1421,7 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id #endif // HAVE_DRIVER_BACKEND #ifdef HAVE_LIBUSB - if (libusb_ctx.libhandle != NULL) { + if (libusb_ctx) { pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number); if (pDevice != NULL) { return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); @@ -1583,7 +1460,7 @@ SDL_hid_device *SDL_hid_open_path(const char *path) #endif // HAVE_DRIVER_BACKEND #ifdef HAVE_LIBUSB - if (libusb_ctx.libhandle != NULL) { + if (libusb_ctx) { pDevice = LIBUSB_hid_open_path(path); if (pDevice != NULL) { return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); @@ -1724,14 +1601,14 @@ void SDL_EnableGameCubeAdaptors(void) ssize_t i, num_devs; int kernel_detached = 0; - if (libusb_ctx.libhandle == NULL) { + if (!libusb_ctx) { return; } - if (libusb_ctx.init(&context) == 0) { - num_devs = libusb_ctx.get_device_list(context, &devs); + if (libusb_ctx->init(&context) == 0) { + num_devs = libusb_ctx->get_device_list(context, &devs); for (i = 0; i < num_devs; ++i) { - if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) { + if (libusb_ctx->get_device_descriptor(devs[i], &desc) != 0) { continue; } @@ -1739,31 +1616,31 @@ void SDL_EnableGameCubeAdaptors(void) continue; } - if (libusb_ctx.open(devs[i], &handle) != 0) { + if (libusb_ctx->open(devs[i], &handle) != 0) { continue; } - if (libusb_ctx.kernel_driver_active(handle, 0)) { - if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) { + if (libusb_ctx->kernel_driver_active(handle, 0)) { + if (libusb_ctx->detach_kernel_driver(handle, 0) == 0) { kernel_detached = 1; } } - if (libusb_ctx.claim_interface(handle, 0) == 0) { - libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000); - libusb_ctx.release_interface(handle, 0); + if (libusb_ctx->claim_interface(handle, 0) == 0) { + libusb_ctx->control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000); + libusb_ctx->release_interface(handle, 0); } if (kernel_detached) { - libusb_ctx.attach_kernel_driver(handle, 0); + libusb_ctx->attach_kernel_driver(handle, 0); } - libusb_ctx.close(handle); + libusb_ctx->close(handle); } - libusb_ctx.free_device_list(devs, 1); + libusb_ctx->free_device_list(devs, 1); - libusb_ctx.exit(context); + libusb_ctx->exit(context); } #endif // HAVE_LIBUSB } diff --git a/src/misc/SDL_libusb.c b/src/misc/SDL_libusb.c new file mode 100644 index 0000000000000..b3ad54c651322 --- /dev/null +++ b/src/misc/SDL_libusb.c @@ -0,0 +1,105 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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. +*/ +#include "SDL_internal.h" + +#include "SDL_libusb.h" + +#ifdef HAVE_LIBUSB + +static SDL_AtomicInt SDL_libusb_refcount; +static bool SDL_libusb_loaded; +static SDL_SharedObject *SDL_libusb_handle; +static SDL_LibUSBContext SDL_libusb_context; + +bool SDL_InitLibUSB(SDL_LibUSBContext **ctx) +{ + if (SDL_AtomicIncRef(&SDL_libusb_refcount) == 0) { +#ifdef SDL_LIBUSB_DYNAMIC + SDL_libusb_handle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC); + if (SDL_libusb_handle) +#endif + { + SDL_libusb_loaded = true; +#ifdef SDL_LIBUSB_DYNAMIC +#define LOAD_LIBUSB_SYMBOL(type, func) \ + if ((SDL_libusb_context.func = (type)SDL_LoadFunction(SDL_libusb_handle, "libusb_" #func)) == NULL) { \ + SDL_libusb_loaded = false; \ + } +#else +#define LOAD_LIBUSB_SYMBOL(type, func) \ + SDL_libusb_context.func = libusb_##func; +#endif + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init) + LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit) + LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list) + LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor) + LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor) + LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers) + LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open) + LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close) + LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting) + LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer) + LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char *, uint16_t, unsigned int), control_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), bulk_transfer) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events) + LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed) + LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name) +#undef LOAD_LIBUSB_SYMBOL + } + } + + if (SDL_libusb_loaded) { + *ctx = &SDL_libusb_context; + return true; + } else { + SDL_QuitLibUSB(); + *ctx = NULL; + return false; + } +} + +void SDL_QuitLibUSB(void) +{ + if (SDL_AtomicDecRef(&SDL_libusb_refcount)) { + if (SDL_libusb_handle) { + SDL_UnloadObject(SDL_libusb_handle); + SDL_libusb_handle = NULL; + } + SDL_libusb_loaded = false; + } +} + +#endif // HAVE_LIBUSB diff --git a/src/misc/SDL_libusb.h b/src/misc/SDL_libusb.h new file mode 100644 index 0000000000000..896916f623591 --- /dev/null +++ b/src/misc/SDL_libusb.h @@ -0,0 +1,97 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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. +*/ + +#include "SDL_internal.h" + +#ifdef HAVE_LIBUSB +// libusb HIDAPI Implementation + +// Include this now, for our dynamically-loaded libusb context +#include + +typedef struct SDL_LibUSBContext +{ +/* *INDENT-OFF* */ // clang-format off + int (LIBUSB_CALL *init)(libusb_context **ctx); + void (LIBUSB_CALL *exit)(libusb_context *ctx); + ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list); + void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices); + int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc); + int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config); + int (LIBUSB_CALL *get_config_descriptor)( + libusb_device *dev, + uint8_t config_index, + struct libusb_config_descriptor **config + ); + void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config); + uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev); + int (LIBUSB_CALL *get_port_numbers)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len); + uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev); + int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle); + void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle); + libusb_device *(LIBUSB_CALL *get_device)(libusb_device_handle *dev_handle); + int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number); + int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number); + int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number); + int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number); + int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number); + int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting); + struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets); + int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer); + int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer); + void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer); + int (LIBUSB_CALL *control_transfer)( + libusb_device_handle *dev_handle, + uint8_t request_type, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned char *data, + uint16_t wLength, + unsigned int timeout + ); + int (LIBUSB_CALL *interrupt_transfer)( + libusb_device_handle *dev_handle, + unsigned char endpoint, + unsigned char *data, + int length, + int *actual_length, + unsigned int timeout + ); + int (LIBUSB_CALL *bulk_transfer)( + libusb_device_handle *dev_handle, + unsigned char endpoint, + unsigned char *data, + int length, + int *transferred, + unsigned int timeout + ); + int (LIBUSB_CALL *handle_events)(libusb_context *ctx); + int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed); + const char * (LIBUSB_CALL *error_name)(int errcode); +/* *INDENT-ON* */ // clang-format on + +} SDL_LibUSBContext; + +extern bool SDL_InitLibUSB(SDL_LibUSBContext **ctx); +extern void SDL_QuitLibUSB(void); + +#endif // HAVE_LIBUSB From 9e9223347843090bc5203c8f2712d48a4e55e444 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 27 Aug 2025 15:11:08 -0700 Subject: [PATCH 7/8] Added SDL_hid_get_properties() --- include/SDL3/SDL_hidapi.h | 17 +++++++++++++++++ src/hidapi/SDL_hidapi.c | 22 +++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/SDL3/SDL_hidapi.h b/include/SDL3/SDL_hidapi.h index 131b03723a5fa..521599af3550d 100644 --- a/include/SDL3/SDL_hidapi.h +++ b/include/SDL3/SDL_hidapi.h @@ -283,6 +283,23 @@ extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open(unsigned short vendor_ */ extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path); +/** + * Get the properties associated with an SDL_hid_device. + * + * The following read-only properties are provided by SDL: + * + * - `SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER`: the libusb_device_handle associated with the device, if it was opened using libusb. + * + * \param dev a device handle returned from SDL_hid_open(). + * \returns a valid property ID on success or 0 on failure; call + * SDL_GetError() for more information. + * + * \since This function is available since SDL 3.4.0. + */ +extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_hid_get_properties(SDL_hid_device *dev); + +#define SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER "SDL.hidapi.libusb.device.handle" + /** * Write an Output report to a HID device. * diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c index 5f86a361e2f59..64b09d5a122ca 100644 --- a/src/hidapi/SDL_hidapi.c +++ b/src/hidapi/SDL_hidapi.c @@ -951,13 +951,14 @@ struct SDL_hid_device void *device; const struct hidapi_backend *backend; SDL_hid_device_info info; + SDL_PropertiesID props; }; #if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB) static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend) { - SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper)); + SDL_hid_device *wrapper = (SDL_hid_device *)SDL_calloc(1, sizeof(*wrapper)); SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, true); wrapper->device = device; wrapper->backend = backend; @@ -1424,7 +1425,9 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id if (libusb_ctx) { pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number); if (pDevice != NULL) { - return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); + SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); + SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle); + return dev; } } #endif // HAVE_LIBUSB @@ -1463,7 +1466,9 @@ SDL_hid_device *SDL_hid_open_path(const char *path) if (libusb_ctx) { pDevice = LIBUSB_hid_open_path(path); if (pDevice != NULL) { - return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); + SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend); + SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle); + return dev; } } #endif // HAVE_LIBUSB @@ -1473,6 +1478,16 @@ SDL_hid_device *SDL_hid_open_path(const char *path) return NULL; } +SDL_PropertiesID SDL_hid_get_properties(SDL_hid_device *device) +{ + CHECK_DEVICE_MAGIC(device, 0); + + if (!device->props) { + device->props = SDL_CreateProperties(); + } + return device->props; +} + int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length) { CHECK_DEVICE_MAGIC(device, -1); @@ -1527,6 +1542,7 @@ int SDL_hid_close(SDL_hid_device *device) CHECK_DEVICE_MAGIC(device, -1); device->backend->hid_close(device->device); + SDL_DestroyProperties(device->props); DeleteHIDDeviceWrapper(device); return 0; } From d583ff8a71f41d41c990513300c63cb793bcce84 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 27 Aug 2025 15:50:54 -0700 Subject: [PATCH 8/8] Moved Nintendo Switch 2 Controller initialization from hid.c to SDL_hidapi_switch2.c --- src/hidapi/libusb/hid.c | 81 ---------- src/joystick/hidapi/SDL_hidapi_switch2.c | 163 ++++++++++++++++++--- src/joystick/hidapi/SDL_hidapijoystick_c.h | 2 + 3 files changed, 148 insertions(+), 98 deletions(-) diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c index bd7b57df80cf3..94cc50dcb41f0 100644 --- a/src/hidapi/libusb/hid.c +++ b/src/hidapi/libusb/hid.c @@ -1307,82 +1307,6 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV } } -static bool is_ns2(unsigned short idVendor, unsigned short idProduct) -{ - if (idVendor == 0x057e) { - if (idProduct == 0x2069) { - return true; - } - if (idProduct == 0x2073) { - return true; - } - } - return false; -} - -static void init_ns2(libusb_device_handle *device_handle, const struct libusb_config_descriptor *conf_desc) -{ - int j, k, l, res; - - for (j = 0; j < conf_desc->bNumInterfaces; j++) { - const struct libusb_interface *intf = &conf_desc->interface[j]; - for (k = 0; k < intf->num_altsetting; k++) { - const struct libusb_interface_descriptor *intf_desc = &intf->altsetting[k]; - if (intf_desc->bInterfaceNumber == 1) { - uint8_t endpoint = 0; - for (l = 0; l < intf_desc->bNumEndpoints; l++) { - const struct libusb_endpoint_descriptor* ep = &intf_desc->endpoint[l]; - if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK && (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) { - endpoint = ep->bEndpointAddress; - break; - } - } - - if (endpoint) { - res = libusb_claim_interface(device_handle, intf_desc->bInterfaceNumber); - if (res < 0) { - LOG("can't claim interface %d: %d\n", intf_desc->bInterfaceNumber, res); - continue; - } - - const unsigned char DEFAULT_REPORT_DATA[] = { - 0x03, 0x91, 0x00, 0x0d, 0x00, 0x08, - 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF - }; - const unsigned char SET_LED_DATA[] = { - 0x09, 0x91, 0x00, 0x07, 0x00, 0x08, - 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; - - int transferred; - res = libusb_bulk_transfer(device_handle, - endpoint, - (unsigned char*)DEFAULT_REPORT_DATA, - sizeof(DEFAULT_REPORT_DATA), - &transferred, - 1000); - if (res < 0) { - LOG("can't set report data: %d\n", res); - } - - res = libusb_bulk_transfer(device_handle, - endpoint, - (unsigned char*)SET_LED_DATA, - sizeof(SET_LED_DATA), - &transferred, - 1000); - if (res < 0) { - LOG("can't set LED data: %d\n", res); - } - - libusb_release_interface(device_handle, intf_desc->bInterfaceNumber); - return; - } - } - } - } -} - static void calculate_device_quirks(hid_device *dev, unsigned short idVendor, unsigned short idProduct) { static const int VENDOR_SONY = 0x054c; @@ -1444,11 +1368,6 @@ static int hidapi_initialize_device(hid_device *dev, const struct libusb_interfa init_xboxone(dev->device_handle, desc.idVendor, desc.idProduct, conf_desc); } - /* Initialize Nintendo Switch 2 controllers */ - if (is_ns2(desc.idVendor, desc.idProduct)) { - init_ns2(dev->device_handle, conf_desc); - } - /* Store off the string descriptor indexes */ dev->manufacturer_index = desc.iManufacturer; dev->product_index = desc.iProduct; diff --git a/src/joystick/hidapi/SDL_hidapi_switch2.c b/src/joystick/hidapi/SDL_hidapi_switch2.c index 8e0a23a1a0557..3655cc02b15bc 100644 --- a/src/joystick/hidapi/SDL_hidapi_switch2.c +++ b/src/joystick/hidapi/SDL_hidapi_switch2.c @@ -26,13 +26,21 @@ #ifdef SDL_JOYSTICK_HIDAPI #include "../../SDL_hints_c.h" +#include "../../misc/SDL_libusb.h" #include "../SDL_sysjoystick.h" #include "SDL_hidapijoystick_c.h" -#include "SDL_hidapi_rumble.h" -#include "SDL_hidapi_nintendo.h" #ifdef SDL_JOYSTICK_HIDAPI_SWITCH2 +typedef struct +{ + SDL_LibUSBContext *libusb; + libusb_device_handle *device_handle; + bool interface_claimed; + Uint8 interface_number; + Uint8 bulk_endpoint; +} SDL_DriverSwitch2_Context; + static void HIDAPI_DriverSwitch2_RegisterHints(SDL_HintCallback callback, void *userdata) { SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, callback, userdata); @@ -51,29 +59,138 @@ static bool HIDAPI_DriverSwitch2_IsEnabled(void) static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol) { if (vendor_id == USB_VENDOR_NINTENDO) { - switch (product_id) { - case USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER: - case USB_PRODUCT_NINTENDO_SWITCH2_PRO: + switch (product_id) { + case USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER: + case USB_PRODUCT_NINTENDO_SWITCH2_PRO: return true; - } + } } return false; } +static bool HIDAPI_DriverSwitch2_InitBluetooth(SDL_HIDAPI_Device *device) +{ + // FIXME: Need to add Bluetooth support + return SDL_SetError("Nintendo Switch2 controllers not supported over Bluetooth"); +} + +static bool FindBulkOutEndpoint(SDL_LibUSBContext *libusb, libusb_device_handle *handle, Uint8 *bInterfaceNumber, Uint8 *bEndpointAddress) +{ + struct libusb_config_descriptor *config; + if (libusb->get_config_descriptor(libusb->get_device(handle), 0, &config) != 0) { + return false; + } + + for (int i = 0; i < config->bNumInterfaces; i++) { + const struct libusb_interface *iface = &config->interface[i]; + for (int j = 0; j < iface->num_altsetting; j++) { + const struct libusb_interface_descriptor *altsetting = &iface->altsetting[j]; + if (altsetting->bInterfaceNumber == 1) { + for (int k = 0; k < altsetting->bNumEndpoints; k++) { + const struct libusb_endpoint_descriptor *ep = &altsetting->endpoint[k]; + if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK && (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) { + + *bInterfaceNumber = altsetting->bInterfaceNumber; + *bEndpointAddress = ep->bEndpointAddress; + libusb->free_config_descriptor(config); + return true; + } + } + } + } + } + libusb->free_config_descriptor(config); + return false; +} + +static bool HIDAPI_DriverSwitch2_InitUSB(SDL_HIDAPI_Device *device) +{ + SDL_DriverSwitch2_Context *ctx = (SDL_DriverSwitch2_Context *)device->context; + + if (!SDL_InitLibUSB(&ctx->libusb)) { + return false; + } + + ctx->device_handle = (libusb_device_handle *)SDL_GetPointerProperty(SDL_hid_get_properties(device->dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, NULL); + if (!ctx->device_handle) { + return SDL_SetError("Couldn't get libusb device handle"); + } + + if (!FindBulkOutEndpoint(ctx->libusb, ctx->device_handle, &ctx->interface_number, &ctx->bulk_endpoint)) { + return SDL_SetError("Couldn't find bulk endpoint"); + } + + int res = ctx->libusb->claim_interface(ctx->device_handle, ctx->interface_number); + if (res < 0) { + return SDL_SetError("Couldn't claim interface %d: %d\n", ctx->interface_number, res); + } + ctx->interface_claimed = true; + + const unsigned char DEFAULT_REPORT_DATA[] = { + 0x03, 0x91, 0x00, 0x0d, 0x00, 0x08, + 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF + }; + const unsigned char SET_LED_DATA[] = { + 0x09, 0x91, 0x00, 0x07, 0x00, 0x08, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + int transferred; + res = ctx->libusb->bulk_transfer(ctx->device_handle, + ctx->bulk_endpoint, + (unsigned char *)DEFAULT_REPORT_DATA, + sizeof(DEFAULT_REPORT_DATA), + &transferred, + 1000); + if (res < 0) { + return SDL_SetError("Couldn't set report data: %d\n", res); + } + + res = ctx->libusb->bulk_transfer(ctx->device_handle, + ctx->bulk_endpoint, + (unsigned char *)SET_LED_DATA, + sizeof(SET_LED_DATA), + &transferred, + 1000); + if (res < 0) { + return SDL_SetError("Couldn't set LED data: %d\n", res); + } + + return true; +} + static bool HIDAPI_DriverSwitch2_InitDevice(SDL_HIDAPI_Device *device) { - // Sometimes the device handle isn't available during enumeration so we don't get the device name, so set it explicitly - switch (device->product_id) { - case USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER: - HIDAPI_SetDeviceName(device, "Nintendo GameCube Controller"); - break; - case USB_PRODUCT_NINTENDO_SWITCH2_PRO: - HIDAPI_SetDeviceName(device, "Nintendo Switch Pro Controller"); - break; - default: - break; - } + SDL_DriverSwitch2_Context *ctx; + + ctx = (SDL_DriverSwitch2_Context *)SDL_calloc(1, sizeof(*ctx)); + if (!ctx) { + return false; + } + device->context = ctx; + + if (device->is_bluetooth) { + if (!HIDAPI_DriverSwitch2_InitBluetooth(device)) { + return false; + } + } else { + if (!HIDAPI_DriverSwitch2_InitUSB(device)) { + return false; + } + } + + // Sometimes the device handle isn't available during enumeration so we don't get the device name, so set it explicitly + switch (device->product_id) { + case USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER: + HIDAPI_SetDeviceName(device, "Nintendo GameCube Controller"); + break; + case USB_PRODUCT_NINTENDO_SWITCH2_PRO: + HIDAPI_SetDeviceName(device, "Nintendo Switch Pro Controller"); + break; + default: + break; + } return HIDAPI_JoystickConnected(device, NULL); } @@ -250,6 +367,18 @@ static void HIDAPI_DriverSwitch2_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Jo static void HIDAPI_DriverSwitch2_FreeDevice(SDL_HIDAPI_Device *device) { + SDL_DriverSwitch2_Context *ctx = (SDL_DriverSwitch2_Context *)device->context; + + if (ctx) { + if (ctx->interface_claimed) { + ctx->libusb->release_interface(ctx->device_handle, ctx->interface_number); + ctx->interface_claimed = false; + } + if (ctx->libusb) { + SDL_QuitLibUSB(); + ctx->libusb = NULL; + } + } } SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch2 = { diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h index 885573cabbd61..6f80bc52fd81b 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h @@ -35,7 +35,9 @@ #define SDL_JOYSTICK_HIDAPI_STEAM #define SDL_JOYSTICK_HIDAPI_STEAMDECK #define SDL_JOYSTICK_HIDAPI_SWITCH +#ifdef HAVE_LIBUSB #define SDL_JOYSTICK_HIDAPI_SWITCH2 +#endif #define SDL_JOYSTICK_HIDAPI_WII #define SDL_JOYSTICK_HIDAPI_XBOX360 #define SDL_JOYSTICK_HIDAPI_XBOXONE