From be6cda9f95463cd3bc44ad7d51b724e9b4058e8c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 19 Jun 2019 15:54:21 -0700 Subject: [PATCH] Rolling back GameCube HIDAPI support It causes the HIDAPI devices to always be opened on enumeration, which causes crashes in the Windows drivers when multiple applications are reading and writing at the same time. We can revisit this after 2.0.10 release. --- VisualC/SDL/SDL.vcxproj | 1 - VisualC/SDL/SDL.vcxproj.filters | 1 - Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj | 10 - Xcode/SDL/SDL.xcodeproj/project.pbxproj | 8 - include/SDL_hints.h | 11 - src/joystick/SDL_joystick.c | 11 - src/joystick/SDL_joystick_c.h | 3 - src/joystick/SDL_sysjoystick.h | 3 - src/joystick/controller_type.h | 3 - src/joystick/hidapi/SDL_hidapi_gamecube.c | 380 -------------------- src/joystick/hidapi/SDL_hidapi_ps4.c | 95 ++--- src/joystick/hidapi/SDL_hidapi_switch.c | 75 +--- src/joystick/hidapi/SDL_hidapi_xbox360.c | 87 ++--- src/joystick/hidapi/SDL_hidapi_xboxone.c | 89 ++--- src/joystick/hidapi/SDL_hidapijoystick.c | 193 +++++----- src/joystick/hidapi/SDL_hidapijoystick_c.h | 32 +- 16 files changed, 176 insertions(+), 826 deletions(-) delete mode 100644 src/joystick/hidapi/SDL_hidapi_gamecube.c diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj index 082070cc2..a010a7435 100644 --- a/VisualC/SDL/SDL.vcxproj +++ b/VisualC/SDL/SDL.vcxproj @@ -421,7 +421,6 @@ - diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters index b911d1bf5..75a9e4fb9 100644 --- a/VisualC/SDL/SDL.vcxproj.filters +++ b/VisualC/SDL/SDL.vcxproj.filters @@ -360,7 +360,6 @@ - diff --git a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj index a87c80cf7..ef8539b1a 100644 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj @@ -468,9 +468,6 @@ F3BDD79B20F51CB8004ECBF3 /* SDL_hidapijoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F3BDD79020F51CB8004ECBF3 /* SDL_hidapijoystick_c.h */; }; F3BDD79C20F51CB8004ECBF3 /* SDL_hidapijoystick.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD79120F51CB8004ECBF3 /* SDL_hidapijoystick.c */; }; F3BDD79D20F51CB8004ECBF3 /* SDL_hidapijoystick.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD79120F51CB8004ECBF3 /* SDL_hidapijoystick.c */; }; - F3E3C55D223DEC6C007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C55C223DEC6C007D243C /* SDL_hidapi_gamecube.c */; }; - F3E3C55E223DEC6C007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C55C223DEC6C007D243C /* SDL_hidapi_gamecube.c */; }; - F3E3C55F224065AE007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C55C223DEC6C007D243C /* SDL_hidapi_gamecube.c */; }; F3E3C658224069CE007D243C /* SDL_uikit_main.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C657224069CE007D243C /* SDL_uikit_main.c */; }; F3E3C65B2241389A007D243C /* SDL_blit.h in Headers */ = {isa = PBXBuildFile; fileRef = FDA683010DF2374E00F98A1A /* SDL_blit.h */; }; F3E3C65C2241389A007D243C /* SDL_uikitmetalview.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D7517191EE1D32200820EEA /* SDL_uikitmetalview.h */; }; @@ -657,7 +654,6 @@ F3E3C7122241389A007D243C /* SDL_uikitevents.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F0D0E26E5D900F90B21 /* SDL_uikitevents.m */; }; F3E3C7132241389A007D243C /* yuv_rgb.c in Sources */ = {isa = PBXBuildFile; fileRef = AA13B3561FB8B46300D9FEE6 /* yuv_rgb.c */; }; F3E3C7142241389A007D243C /* SDL_uikitopengles.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F0F0E26E5D900F90B21 /* SDL_uikitopengles.m */; }; - F3E3C7152241389A007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C55C223DEC6C007D243C /* SDL_hidapi_gamecube.c */; }; F3E3C7162241389A007D243C /* SDL_uikitvideo.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F110E26E5D900F90B21 /* SDL_uikitvideo.m */; }; F3E3C7172241389A007D243C /* SDL_uikitview.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F130E26E5D900F90B21 /* SDL_uikitview.m */; }; F3E3C7182241389A007D243C /* SDL_displayevents.c in Sources */ = {isa = PBXBuildFile; fileRef = A7C19D28212E552B00DF2152 /* SDL_displayevents.c */; }; @@ -1062,7 +1058,6 @@ F3BDD78E20F51CB8004ECBF3 /* SDL_hidapi_ps4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_ps4.c; sourceTree = ""; }; F3BDD79020F51CB8004ECBF3 /* SDL_hidapijoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_hidapijoystick_c.h; sourceTree = ""; }; F3BDD79120F51CB8004ECBF3 /* SDL_hidapijoystick.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapijoystick.c; sourceTree = ""; }; - F3E3C55C223DEC6C007D243C /* SDL_hidapi_gamecube.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_gamecube.c; sourceTree = ""; }; F3E3C65222406928007D243C /* libSDLmain.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDLmain.a; sourceTree = BUILT_PRODUCTS_DIR; }; F3E3C657224069CE007D243C /* SDL_uikit_main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_uikit_main.c; sourceTree = ""; }; F3E3C7572241389A007D243C /* libSDL2.dylib */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDL2.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1463,7 +1458,6 @@ F3BDD78A20F51C8D004ECBF3 /* hidapi */ = { isa = PBXGroup; children = ( - F3E3C55C223DEC6C007D243C /* SDL_hidapi_gamecube.c */, F3BDD78E20F51CB8004ECBF3 /* SDL_hidapi_ps4.c */, F3BDD78C20F51CB8004ECBF3 /* SDL_hidapi_switch.c */, F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */, @@ -2518,7 +2512,6 @@ 52ED1E24222889500061FCE0 /* SDL_uikitevents.m in Sources */, 52ED1E25222889500061FCE0 /* yuv_rgb.c in Sources */, 52ED1E26222889500061FCE0 /* SDL_uikitopengles.m in Sources */, - F3E3C55F224065AE007D243C /* SDL_hidapi_gamecube.c in Sources */, 52ED1E27222889500061FCE0 /* SDL_uikitvideo.m in Sources */, 52ED1E28222889500061FCE0 /* SDL_uikitview.m in Sources */, 52ED1E29222889500061FCE0 /* SDL_displayevents.c in Sources */, @@ -2647,7 +2640,6 @@ F3E3C7122241389A007D243C /* SDL_uikitevents.m in Sources */, F3E3C7132241389A007D243C /* yuv_rgb.c in Sources */, F3E3C7142241389A007D243C /* SDL_uikitopengles.m in Sources */, - F3E3C7152241389A007D243C /* SDL_hidapi_gamecube.c in Sources */, F3E3C7162241389A007D243C /* SDL_uikitvideo.m in Sources */, F3E3C7172241389A007D243C /* SDL_uikitview.m in Sources */, F3E3C7182241389A007D243C /* SDL_displayevents.c in Sources */, @@ -2824,7 +2816,6 @@ FAB598BD1BB5C31600BE72C5 /* SDL_hints.c in Sources */, FAB598BE1BB5C31600BE72C5 /* SDL_log.c in Sources */, FAB598BF1BB5C31600BE72C5 /* SDL.c in Sources */, - F3E3C55E223DEC6C007D243C /* SDL_hidapi_gamecube.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2895,7 +2886,6 @@ FD689F1D0E26E5D900F90B21 /* SDL_uikitevents.m in Sources */, AA13B35A1FB8B46400D9FEE6 /* yuv_rgb.c in Sources */, FD689F1F0E26E5D900F90B21 /* SDL_uikitopengles.m in Sources */, - F3E3C55D223DEC6C007D243C /* SDL_hidapi_gamecube.c in Sources */, FD689F210E26E5D900F90B21 /* SDL_uikitvideo.m in Sources */, FD689F230E26E5D900F90B21 /* SDL_uikitview.m in Sources */, A7C19D2A212E552C00DF2152 /* SDL_displayevents.c in Sources */, diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj index cdb879046..80520458e 100755 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj @@ -919,9 +919,6 @@ F3950CD8212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; }; F3950CD9212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; }; F3950CDA212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; }; - F3E3C559223DEBC7007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C558223DEBC7007D243C /* SDL_hidapi_gamecube.c */; }; - F3E3C55A223DEBC7007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C558223DEBC7007D243C /* SDL_hidapi_gamecube.c */; }; - F3E3C55B223DEBC7007D243C /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C558223DEBC7007D243C /* SDL_hidapi_gamecube.c */; }; FA73671D19A540EF004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; }; FA73671E19A54140004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; }; FA73671F19A54144004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; }; @@ -1252,7 +1249,6 @@ F30D9CCB212EB4810047DF2E /* SDL_displayevents_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_displayevents_c.h; sourceTree = ""; }; F30D9CCC212EB4810047DF2E /* SDL_displayevents.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_displayevents.c; sourceTree = ""; }; F3950CD7212BC88D00F51292 /* SDL_sensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor.h; sourceTree = ""; }; - F3E3C558223DEBC7007D243C /* SDL_hidapi_gamecube.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_gamecube.c; sourceTree = ""; }; F59C710300D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = ""; }; F59C710600D5CB5801000001 /* SDL.info */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = SDL.info; sourceTree = ""; }; F5A2EF3900C6A39A01000001 /* BUGS.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; name = BUGS.txt; path = ../../BUGS.txt; sourceTree = SOURCE_ROOT; }; @@ -1932,7 +1928,6 @@ A704170C20F09AA600A82227 /* hidapi */ = { isa = PBXGroup; children = ( - F3E3C558223DEBC7007D243C /* SDL_hidapi_gamecube.c */, A704171120F09AC900A82227 /* SDL_hidapi_ps4.c */, A704170F20F09AC800A82227 /* SDL_hidapi_switch.c */, A704171320F09AC900A82227 /* SDL_hidapi_xbox360.c */, @@ -2707,7 +2702,6 @@ 04BD005B12E6671800899322 /* SDL_syshaptic.c in Sources */, 04BD005F12E6671800899322 /* SDL_haptic.c in Sources */, 4D1664551EDD60AD003DE88E /* SDL_cocoavulkan.m in Sources */, - F3E3C559223DEBC7007D243C /* SDL_hidapi_gamecube.c in Sources */, 04BD006612E6671800899322 /* SDL_sysjoystick.c in Sources */, 04BD007012E6671800899322 /* SDL_joystick.c in Sources */, 04BD008812E6671800899322 /* SDL_sysloadso.c in Sources */, @@ -2846,7 +2840,6 @@ 04BD026D12E6671800899322 /* SDL_quit.c in Sources */, 04BD026F12E6671800899322 /* SDL_touch.c in Sources */, 04BD027112E6671800899322 /* SDL_windowevents.c in Sources */, - F3E3C55A223DEBC7007D243C /* SDL_hidapi_gamecube.c in Sources */, 04BD027412E6671800899322 /* SDL_rwopsbundlesupport.m in Sources */, 04BD027512E6671800899322 /* SDL_rwops.c in Sources */, 04BD027612E6671800899322 /* SDL_syshaptic.c in Sources */, @@ -2985,7 +2978,6 @@ DB31401017554B71006C0E22 /* SDL_quit.c in Sources */, DB31401117554B71006C0E22 /* SDL_touch.c in Sources */, DB31401217554B71006C0E22 /* SDL_windowevents.c in Sources */, - F3E3C55B223DEBC7007D243C /* SDL_hidapi_gamecube.c in Sources */, DB31401317554B71006C0E22 /* SDL_rwopsbundlesupport.m in Sources */, DB31401417554B71006C0E22 /* SDL_rwops.c in Sources */, DB31401517554B71006C0E22 /* SDL_syshaptic.c in Sources */, diff --git a/include/SDL_hints.h b/include/SDL_hints.h index b5d9d8d1b..8fa0dd660 100644 --- a/include/SDL_hints.h +++ b/include/SDL_hints.h @@ -565,17 +565,6 @@ extern "C" { */ #define SDL_HINT_JOYSTICK_HIDAPI_XBOX "SDL_JOYSTICK_HIDAPI_XBOX" -/** - * \brief A variable controlling whether the HIDAPI driver for Nintendo GameCube controllers should be used. - * - * This 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 - */ -#define SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE "SDL_JOYSTICK_HIDAPI_GAMECUBE" - /** * \brief A variable that controls whether Steam Controllers should be exposed using the SDL joystick and game controller APIs * diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index bd89a7c5a..12d5ea137 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -1037,11 +1037,6 @@ SDL_JoystickUpdate(void) /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */ SDL_UnlockJoysticks(); - /* Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks */ -#ifdef SDL_JOYSTICK_HIDAPI - SDL_HIDAPI_UpdateDevices(); -#endif /* SDL_JOYSTICK_HIDAPI */ - for (joystick = SDL_joysticks; joystick; joystick = joystick->next) { if (joystick->attached) { /* This should always be true, but seeing a crash in the wild...? */ @@ -1199,12 +1194,6 @@ SDL_IsJoystickXboxOne(Uint16 vendor, Uint16 product) return (GuessControllerType(vendor, product) == k_eControllerType_XBoxOneController); } -SDL_bool -SDL_IsJoystickGameCube(Uint16 vendor, Uint16 product) -{ - return (GuessControllerType(vendor, product) == k_eControllerType_GameCube); -} - SDL_bool SDL_IsJoystickXInput(SDL_JoystickGUID guid) { diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h index f02d30b66..165c3700a 100644 --- a/src/joystick/SDL_joystick_c.h +++ b/src/joystick/SDL_joystick_c.h @@ -66,9 +66,6 @@ extern SDL_bool SDL_IsJoystickXbox360(Uint16 vendor_id, Uint16 product_id); /* Function to return whether a joystick is an Xbox One controller */ extern SDL_bool SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id); -/* Function to return whether a joystick is a GameCube controller */ -extern SDL_bool SDL_IsJoystickGameCube(Uint16 vendor_id, Uint16 product_id); - /* Function to return whether a joystick guid comes from the XInput driver */ extern SDL_bool SDL_IsJoystickXInput(SDL_JoystickGUID guid); diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h index ef7f082e4..8f5752326 100644 --- a/src/joystick/SDL_sysjoystick.h +++ b/src/joystick/SDL_sysjoystick.h @@ -152,9 +152,6 @@ extern SDL_JoystickDriver SDL_IOS_JoystickDriver; extern SDL_JoystickDriver SDL_LINUX_JoystickDriver; extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver; -/* Special function to update HIDAPI devices */ -extern void SDL_HIDAPI_UpdateDevices(void); - #endif /* SDL_sysjoystick_h_ */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/controller_type.h b/src/joystick/controller_type.h index 22dec71c1..51ac20b4e 100644 --- a/src/joystick/controller_type.h +++ b/src/joystick/controller_type.h @@ -57,7 +57,6 @@ typedef enum k_eControllerType_SwitchJoyConPair = 41, k_eControllerType_SwitchInputOnlyController = 42, k_eControllerType_MobileTouch = 43, - k_eControllerType_GameCube = 44, k_eControllerType_LastController, // Don't add game controllers below this enumeration - this enumeration can change value // Keyboards and Mice @@ -388,8 +387,6 @@ static const ControllerDescription_t arrControllers[] = { { MAKE_CONTROLLER_ID( 0x20d6, 0xa711 ), k_eControllerType_SwitchInputOnlyController }, // PowerA Wired Controller Plus { MAKE_CONTROLLER_ID( 0x0f0d, 0x0092 ), k_eControllerType_SwitchInputOnlyController }, // HORI Pokken Tournament DX Pro Pad - { MAKE_CONTROLLER_ID( 0x057e, 0x0337 ), k_eControllerType_GameCube }, // Nintendo Wii U/Switch GameCube USB Adapter - // Valve products - don't add to public list { MAKE_CONTROLLER_ID( 0x0000, 0x11fb ), k_eControllerType_MobileTouch }, // Streaming mobile touch virtual controls diff --git a/src/joystick/hidapi/SDL_hidapi_gamecube.c b/src/joystick/hidapi/SDL_hidapi_gamecube.c deleted file mode 100644 index d44c55d40..000000000 --- a/src/joystick/hidapi/SDL_hidapi_gamecube.c +++ /dev/null @@ -1,380 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2019 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 SDL_JOYSTICK_HIDAPI - -#include "SDL_hints.h" -#include "SDL_log.h" -#include "SDL_events.h" -#include "SDL_timer.h" -#include "SDL_haptic.h" -#include "SDL_joystick.h" -#include "SDL_gamecontroller.h" -#include "../SDL_sysjoystick.h" -#include "SDL_hidapijoystick_c.h" - - -#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE - -typedef struct { - SDL_JoystickID joysticks[4]; - Uint8 wireless[4]; - Uint8 rumbleAllowed[4]; - Uint8 rumble[5]; - Uint32 rumbleExpiration[4]; - /* Without this variable, hid_write starts to lag a TON */ - Uint8 rumbleUpdate; -} SDL_DriverGameCube_Context; - -static SDL_bool -HIDAPI_DriverGameCube_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number) -{ - return SDL_IsJoystickGameCube(vendor_id, product_id); -} - -static const char * -HIDAPI_DriverGameCube_GetDeviceName(Uint16 vendor_id, Uint16 product_id) -{ - /* Give a user friendly name for this controller */ - if (SDL_IsJoystickGameCube(vendor_id, product_id)) { - return "Nintendo GameCube Controller"; - } - return NULL; -} - -static SDL_bool -HIDAPI_DriverGameCube_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks) -{ - SDL_DriverGameCube_Context *ctx; - Uint8 packet[37]; - Uint8 *curSlot; - Uint8 i; - int size; - Uint8 initMagic = 0x13; - Uint8 rumbleMagic = 0x11; - - ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx)); - if (!ctx) { - SDL_OutOfMemory(); - return SDL_FALSE; - } - ctx->joysticks[0] = -1; - ctx->joysticks[1] = -1; - ctx->joysticks[2] = -1; - ctx->joysticks[3] = -1; - ctx->rumble[0] = rumbleMagic; - - context->context = ctx; - - /* This is all that's needed to initialize the device. Really! */ - if (hid_write(context->device, &initMagic, sizeof(initMagic)) <= 0) { - SDL_SetError("Couldn't initialize WUP-028"); - SDL_free(ctx); - return SDL_FALSE; - } - - /* Add all the applicable joysticks */ - while ((size = hid_read_timeout(context->device, packet, sizeof(packet), 0)) > 0) { - if (size < 37 || packet[0] != 0x21) { - continue; /* Nothing to do yet...? */ - } - - /* Go through all 4 slots */ - curSlot = packet + 1; - for (i = 0; i < 4; i += 1, curSlot += 9) { - ctx->wireless[i] = (curSlot[0] & 0x20) != 0; - - /* Only allow rumble if the adapter's second USB cable is connected */ - ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i]; - - if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */ - if (ctx->joysticks[i] == -1) { - ctx->joysticks[i] = SDL_GetNextJoystickInstanceID(); - - *num_joysticks += 1; - - SDL_PrivateJoystickAdded(ctx->joysticks[i]); - } - } else { - if (ctx->joysticks[i] != -1) { - SDL_PrivateJoystickRemoved(ctx->joysticks[i]); - - *num_joysticks -= 1; - - ctx->joysticks[i] = -1; - } - continue; - } - } - } - - return SDL_TRUE; -} - -static void -HIDAPI_DriverGameCube_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - Uint8 i; - - /* Stop all rumble activity */ - for (i = 1; i < 5; i += 1) { - ctx->rumble[i] = 0; - } - hid_write(context->device, ctx->rumble, sizeof(ctx->rumble)); - - /* Remove all joysticks! */ - for (i = 0; i < 4; i += 1) { - if (ctx->joysticks[i] != -1) { - *num_joysticks -= 1; - if (send_event) { - SDL_PrivateJoystickRemoved(ctx->joysticks[i]); - } - } - } - - SDL_free(context->context); -} - -static SDL_bool -HIDAPI_DriverGameCube_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - SDL_Joystick *joystick; - Uint8 packet[37]; - Uint8 *curSlot; - Uint32 now; - Uint8 i; - int size; - - /* Read input packet */ - while ((size = hid_read_timeout(context->device, packet, sizeof(packet), 0)) > 0) { - if (size < 37 || packet[0] != 0x21) { - continue; /* Nothing to do right now...? */ - } - - /* Go through all 4 slots */ - curSlot = packet + 1; - for (i = 0; i < 4; i += 1, curSlot += 9) { - ctx->wireless[i] = (curSlot[0] & 0x20) != 0; - - /* Only allow rumble if the adapter's second USB cable is connected */ - ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i]; - - if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */ - if (ctx->joysticks[i] == -1) { - ctx->joysticks[i] = SDL_GetNextJoystickInstanceID(); - - *num_joysticks += 1; - - SDL_PrivateJoystickAdded(ctx->joysticks[i]); - } - joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]); - - /* Hasn't been opened yet, skip */ - if (joystick == NULL) { - continue; - } - } else { - if (ctx->joysticks[i] != -1) { - SDL_PrivateJoystickRemoved(ctx->joysticks[i]); - - *num_joysticks -= 1; - - ctx->joysticks[i] = -1; - } - continue; - } - - #define READ_BUTTON(off, flag, button) \ - SDL_PrivateJoystickButton( \ - joystick, \ - button, \ - (curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \ - ); - READ_BUTTON(1, 0x01, 0) /* A */ - READ_BUTTON(1, 0x02, 1) /* B */ - READ_BUTTON(1, 0x04, 2) /* X */ - READ_BUTTON(1, 0x08, 3) /* Y */ - READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */ - READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */ - READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */ - READ_BUTTON(1, 0x80, 7) /* DPAD_UP */ - READ_BUTTON(2, 0x01, 8) /* START */ - READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */ - /* These two buttons are for the bottoms of the analog triggers. - * More than likely, you're going to want to read the axes instead! - * -flibit - */ - READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */ - READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */ - #undef READ_BUTTON - - /* Axis math taken from SDL_xinputjoystick.c */ - #define READ_AXIS(off, axis) \ - SDL_PrivateJoystickAxis( \ - joystick, \ - axis, \ - (Sint16)(((int)curSlot[off] * 257) - 32768) \ - ); - READ_AXIS(3, 0) /* LEFTX */ - READ_AXIS(4, 1) /* LEFTY */ - READ_AXIS(5, 2) /* RIGHTX */ - READ_AXIS(6, 3) /* RIGHTY */ - READ_AXIS(7, 4) /* TRIGGERLEFT */ - READ_AXIS(8, 5) /* TRIGGERRIGHT */ - #undef READ_AXIS - } - } - - /* Write rumble packet */ - now = SDL_GetTicks(); - for (i = 0; i < 4; i += 1) { - if (ctx->rumbleExpiration[i] > 0) { - if (SDL_TICKS_PASSED(now, ctx->rumbleExpiration[i]) || !ctx->rumbleAllowed[i]) { - ctx->rumble[i + 1] = 0; - ctx->rumbleExpiration[i] = 0; - ctx->rumbleUpdate = 1; - } - } - } - if (ctx->rumbleUpdate) { - hid_write(context->device, ctx->rumble, sizeof(ctx->rumble)); - ctx->rumbleUpdate = 0; - } - - /* If we got here, nothing bad happened! */ - return SDL_TRUE; -} - -static int -HIDAPI_DriverGameCube_NumJoysticks(SDL_HIDAPI_DriverData *context) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - int i, joysticks = 0; - for (i = 0; i < 4; i += 1) { - if (ctx->joysticks[i] != -1) { - joysticks += 1; - } - } - return joysticks; -} - -static int -HIDAPI_DriverGameCube_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - Uint8 i; - for (i = 0; i < 4; i += 1) { - if (ctx->joysticks[i] != -1) { - if (index == 0) { - return i; - } - index -= 1; - } - } - return -1; /* Should never get here! */ -} - -static SDL_JoystickID -HIDAPI_DriverGameCube_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - Uint8 i; - for (i = 0; i < 4; i += 1) { - if (ctx->joysticks[i] != -1) { - if (index == 0) { - return ctx->joysticks[i]; - } - index -= 1; - } - } - return -1; /* Should never get here! */ -} - -static SDL_bool -HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - SDL_JoystickID instance = SDL_JoystickInstanceID(joystick); - Uint8 i; - for (i = 0; i < 4; i += 1) { - if (instance == ctx->joysticks[i]) { - joystick->nbuttons = 12; - joystick->naxes = 6; - joystick->epowerlevel = ctx->wireless[i] ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED; - joystick->player_index = i; - return SDL_TRUE; - } - } - return SDL_FALSE; /* Should never get here! */ -} - -static int -HIDAPI_DriverGameCube_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) -{ - SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context; - SDL_JoystickID instance = SDL_JoystickInstanceID(joystick); - Uint8 i, val; - for (i = 0; i < 4; i += 1) { - if (instance == ctx->joysticks[i]) { - if (ctx->wireless[i]) { - return SDL_SetError("Ninteno GameCube WaveBird controllers do not support rumble"); - } - if (!ctx->rumbleAllowed[i]) { - return SDL_SetError("Second USB cable for WUP-028 not connected"); - } - val = (low_frequency_rumble > 0 || high_frequency_rumble > 0); - if (val != ctx->rumble[i + 1]) { - ctx->rumble[i + 1] = val; - ctx->rumbleUpdate = 1; - } - if (val && duration_ms < SDL_HAPTIC_INFINITY) { - ctx->rumbleExpiration[i] = SDL_GetTicks() + duration_ms; - } else { - ctx->rumbleExpiration[i] = 0; - } - return 0; - } - } - return -1; /* Should never get here! */ -} - -SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube = -{ - SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, - SDL_TRUE, - HIDAPI_DriverGameCube_IsSupportedDevice, - HIDAPI_DriverGameCube_GetDeviceName, - HIDAPI_DriverGameCube_InitDriver, - HIDAPI_DriverGameCube_QuitDriver, - HIDAPI_DriverGameCube_UpdateDriver, - HIDAPI_DriverGameCube_NumJoysticks, - HIDAPI_DriverGameCube_PlayerIndexForIndex, - HIDAPI_DriverGameCube_InstanceIDForIndex, - HIDAPI_DriverGameCube_OpenJoystick, - HIDAPI_DriverGameCube_Rumble -}; - -#endif /* SDL_JOYSTICK_HIDAPI_GAMECUBE */ - -#endif /* SDL_JOYSTICK_HIDAPI */ diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c index 499f94959..8a21ab7c1 100644 --- a/src/joystick/hidapi/SDL_hidapi_ps4.c +++ b/src/joystick/hidapi/SDL_hidapi_ps4.c @@ -108,7 +108,6 @@ typedef struct } DS4EffectsState_t; typedef struct { - SDL_JoystickID joystickID; SDL_bool is_dongle; SDL_bool is_bluetooth; SDL_bool audio_supported; @@ -273,8 +272,10 @@ static SDL_bool HIDAPI_DriverPS4_CanRumble(Uint16 vendor_id, Uint16 product_id) return SDL_TRUE; } +static int HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms); + static SDL_bool -HIDAPI_DriverPS4_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks) +HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context) { SDL_DriverPS4_Context *ctx; @@ -283,14 +284,14 @@ HIDAPI_DriverPS4_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Ui SDL_OutOfMemory(); return SDL_FALSE; } - context->context = ctx; + *context = ctx; /* Check for type of connection */ ctx->is_dongle = (vendor_id == SONY_USB_VID && product_id == SONY_DS4_DONGLE_PID); if (ctx->is_dongle) { ctx->is_bluetooth = SDL_FALSE; } else if (vendor_id == SONY_USB_VID) { - ctx->is_bluetooth = !CheckUSBConnected(context->device); + ctx->is_bluetooth = !CheckUSBConnected(dev); } else { /* Third party controllers appear to all be wired */ ctx->is_bluetooth = SDL_FALSE; @@ -313,51 +314,8 @@ HIDAPI_DriverPS4_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Ui } } - ctx->joystickID = SDL_GetNextJoystickInstanceID(); - *num_joysticks += 1; - SDL_PrivateJoystickAdded(ctx->joystickID); - - return SDL_TRUE; -} - -static void -HIDAPI_DriverPS4_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks) -{ - SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context->context; - - *num_joysticks -= 1; - if (send_event) { - SDL_PrivateJoystickRemoved(ctx->joystickID); - } - SDL_free(context->context); -} - -static int -HIDAPI_DriverPS4_NumJoysticks(SDL_HIDAPI_DriverData *context) -{ - return 1; -} - -static int -HIDAPI_DriverPS4_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - return -1; -} - -static SDL_JoystickID -HIDAPI_DriverPS4_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context->context; - return ctx->joystickID; -} - -static int HIDAPI_DriverPS4_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms); - -static SDL_bool -HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick) -{ /* Initialize LED and effect state */ - HIDAPI_DriverPS4_Rumble(context, joystick, 0, 0, 0); + HIDAPI_DriverPS4_Rumble(joystick, dev, ctx, 0, 0, 0); /* Initialize the joystick capabilities */ joystick->nbuttons = 16; @@ -368,9 +326,9 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joys } static int -HIDAPI_DriverPS4_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) +HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) { - SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context->context; + SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context; DS4EffectsState_t *effects; Uint8 data[78]; int report_size, offset; @@ -428,7 +386,7 @@ HIDAPI_DriverPS4_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC)); } - if (hid_write(context->device, data, report_size) != report_size) { + if (hid_write(dev, data, report_size) != report_size) { return SDL_SetError("Couldn't send rumble packet"); } @@ -551,25 +509,20 @@ HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_ } static SDL_bool -HIDAPI_DriverPS4_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks) +HIDAPI_DriverPS4_Update(SDL_Joystick *joystick, hid_device *dev, void *context) { - SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context->context; - SDL_Joystick *joystick = SDL_JoystickFromInstanceID(ctx->joystickID); + SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context; Uint8 data[USB_PACKET_LENGTH]; int size; - if (joystick == NULL) { - return SDL_TRUE; /* Nothing to do right now! */ - } - - while ((size = hid_read_timeout(context->device, data, sizeof(data), 0)) > 0) { + while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) { switch (data[0]) { case k_EPS4ReportIdUsbState: - HIDAPI_DriverPS4_HandleStatePacket(joystick, context->device, ctx, (PS4StatePacket_t *)&data[1]); + HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[1]); break; case k_EPS4ReportIdBluetoothState: /* Bluetooth state packets have two additional bytes at the beginning */ - HIDAPI_DriverPS4_HandleStatePacket(joystick, context->device, ctx, (PS4StatePacket_t *)&data[3]); + HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[3]); break; default: #ifdef DEBUG_JOYSTICK @@ -582,27 +535,29 @@ HIDAPI_DriverPS4_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks if (ctx->rumble_expiration) { Uint32 now = SDL_GetTicks(); if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) { - HIDAPI_DriverPS4_Rumble(context, joystick, 0, 0, 0); + HIDAPI_DriverPS4_Rumble(joystick, dev, context, 0, 0, 0); } } return (size >= 0); } +static void +HIDAPI_DriverPS4_Quit(SDL_Joystick *joystick, hid_device *dev, void *context) +{ + SDL_free(context); +} + SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 = { SDL_HINT_JOYSTICK_HIDAPI_PS4, SDL_TRUE, HIDAPI_DriverPS4_IsSupportedDevice, HIDAPI_DriverPS4_GetDeviceName, - HIDAPI_DriverPS4_InitDriver, - HIDAPI_DriverPS4_QuitDriver, - HIDAPI_DriverPS4_UpdateDriver, - HIDAPI_DriverPS4_NumJoysticks, - HIDAPI_DriverPS4_PlayerIndexForIndex, - HIDAPI_DriverPS4_InstanceIDForIndex, - HIDAPI_DriverPS4_OpenJoystick, - HIDAPI_DriverPS4_Rumble + HIDAPI_DriverPS4_Init, + HIDAPI_DriverPS4_Rumble, + HIDAPI_DriverPS4_Update, + HIDAPI_DriverPS4_Quit }; #endif /* SDL_JOYSTICK_HIDAPI_PS4 */ diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c index d2f246b01..27c988c9d 100644 --- a/src/joystick/hidapi/SDL_hidapi_switch.c +++ b/src/joystick/hidapi/SDL_hidapi_switch.c @@ -183,7 +183,6 @@ typedef struct #pragma pack() typedef struct { - SDL_JoystickID joystickID; hid_device *dev; SDL_bool m_bIsUsingBluetooth; Uint8 m_nCommandNumber; @@ -571,7 +570,7 @@ static Sint16 ApplyStickCalibration(SDL_DriverSwitch_Context *ctx, int nStick, i } static SDL_bool -HIDAPI_DriverSwitch_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks) +HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context) { SDL_DriverSwitch_Context *ctx; Uint8 input_mode; @@ -581,9 +580,9 @@ HIDAPI_DriverSwitch_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, SDL_OutOfMemory(); return SDL_FALSE; } - ctx->dev = context->device; + ctx->dev = dev; - context->context = ctx; + *context = ctx; /* Initialize rumble data */ SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]); @@ -628,18 +627,6 @@ HIDAPI_DriverSwitch_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, } } - ctx->joystickID = SDL_GetNextJoystickInstanceID(); - *num_joysticks += 1; - SDL_PrivateJoystickAdded(ctx->joystickID); - - return SDL_TRUE; -} - -static SDL_bool -HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick) -{ - SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context; - /* Set the LED state */ SetHomeLED(ctx, 100); SetSlotLED(ctx, (joystick->instance_id % 4)); @@ -653,9 +640,9 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *j } static int -HIDAPI_DriverSwitch_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) +HIDAPI_DriverSwitch_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) { - SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context; + SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context; /* Experimentally determined rumble values. These will only matter on some controllers as tested ones * seem to disregard these and just use any non-zero rumble values as a binary flag for constant rumble @@ -860,16 +847,11 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C } static SDL_bool -HIDAPI_DriverSwitch_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks) +HIDAPI_DriverSwitch_Update(SDL_Joystick *joystick, hid_device *dev, void *context) { - SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context; - SDL_Joystick *joystick = SDL_JoystickFromInstanceID(ctx->joystickID); + SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context; int size; - if (joystick == NULL) { - return SDL_TRUE; /* Nothing to do right now! */ - } - while ((size = ReadInput(ctx)) > 0) { switch (ctx->m_rgucReadBuffer[0]) { case k_eSwitchInputReportIDs_SimpleControllerState: @@ -886,7 +868,7 @@ HIDAPI_DriverSwitch_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysti if (ctx->m_nRumbleExpiration) { Uint32 now = SDL_GetTicks(); if (SDL_TICKS_PASSED(now, ctx->m_nRumbleExpiration)) { - HIDAPI_DriverSwitch_Rumble(context, joystick, 0, 0, 0); + HIDAPI_DriverSwitch_Rumble(joystick, dev, context, 0, 0, 0); } } @@ -894,37 +876,14 @@ HIDAPI_DriverSwitch_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysti } static void -HIDAPI_DriverSwitch_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks) +HIDAPI_DriverSwitch_Quit(SDL_Joystick *joystick, hid_device *dev, void *context) { - SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context; + SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context; /* Restore simple input mode for other applications */ SetInputMode(ctx, k_eSwitchInputReportIDs_SimpleControllerState); - *num_joysticks -= 1; - if (send_event) { - SDL_PrivateJoystickRemoved(ctx->joystickID); - } - SDL_free(context->context); -} - -static int -HIDAPI_DriverSwitch_NumJoysticks(SDL_HIDAPI_DriverData *context) -{ - return 1; -} - -static int -HIDAPI_DriverSwitch_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - return -1; -} - -static SDL_JoystickID -HIDAPI_DriverSwitch_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context->context; - return ctx->joystickID; + SDL_free(context); } SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch = @@ -933,14 +892,10 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch = SDL_TRUE, HIDAPI_DriverSwitch_IsSupportedDevice, HIDAPI_DriverSwitch_GetDeviceName, - HIDAPI_DriverSwitch_InitDriver, - HIDAPI_DriverSwitch_QuitDriver, - HIDAPI_DriverSwitch_UpdateDriver, - HIDAPI_DriverSwitch_NumJoysticks, - HIDAPI_DriverSwitch_PlayerIndexForIndex, - HIDAPI_DriverSwitch_InstanceIDForIndex, - HIDAPI_DriverSwitch_OpenJoystick, - HIDAPI_DriverSwitch_Rumble + HIDAPI_DriverSwitch_Init, + HIDAPI_DriverSwitch_Rumble, + HIDAPI_DriverSwitch_Update, + HIDAPI_DriverSwitch_Quit }; #endif /* SDL_JOYSTICK_HIDAPI_SWITCH */ diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360.c b/src/joystick/hidapi/SDL_hidapi_xbox360.c index eb67089c0..535e53f61 100644 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c @@ -54,7 +54,6 @@ typedef struct { - SDL_JoystickID joystickID; Uint8 last_state[USB_PACKET_LENGTH]; Uint32 rumble_expiration; #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT @@ -282,7 +281,7 @@ static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot) } static SDL_bool -HIDAPI_DriverXbox360_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks) +HIDAPI_DriverXbox360_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context) { SDL_DriverXbox360_Context *ctx; @@ -301,20 +300,10 @@ HIDAPI_DriverXbox360_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx); #endif - context->context = ctx; + *context = ctx; - ctx->joystickID = SDL_GetNextJoystickInstanceID(); - *num_joysticks += 1; - SDL_PrivateJoystickAdded(ctx->joystickID); - - return SDL_TRUE; -} - -static SDL_bool -HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick) -{ /* Set the controller LED */ - SetSlotLED(context->device, (joystick->instance_id % 4)); + SetSlotLED(dev, (joystick->instance_id % 4)); /* Initialize the joystick capabilities */ joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX; @@ -325,28 +314,9 @@ HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick * } static int -HIDAPI_DriverXbox360_NumJoysticks(SDL_HIDAPI_DriverData *context) +HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) { - return 1; -} - -static int -HIDAPI_DriverXbox360_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - return -1; -} - -static SDL_JoystickID -HIDAPI_DriverXbox360_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context; - return ctx->joystickID; -} - -static int -HIDAPI_DriverXbox360_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) -{ - SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context; + SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context; #ifdef __WIN32__ SDL_bool rumbled = SDL_FALSE; @@ -399,7 +369,7 @@ HIDAPI_DriverXbox360_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joysti rumble_packet[4] = (high_frequency_rumble >> 8); #endif - if (hid_write(context->device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) { + if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) { return SDL_SetError("Couldn't send rumble packet"); } #endif /* __WIN32__ */ @@ -739,31 +709,26 @@ HIDAPI_DriverXboxOneS_HandleGuidePacket(SDL_Joystick *joystick, hid_device *dev, #endif /* __MACOSX__ */ static SDL_bool -HIDAPI_DriverXbox360_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks) +HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *context) { - SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context; - SDL_Joystick *joystick = SDL_JoystickFromInstanceID(ctx->joystickID); + SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context; Uint8 data[USB_PACKET_LENGTH]; int size; - if (joystick == NULL) { - return SDL_TRUE; /* Nothing to do right now! */ - } - - while ((size = hid_read_timeout(context->device, data, sizeof(data), 0)) > 0) { + while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) { #ifdef __WIN32__ - HIDAPI_DriverXbox360_HandleStatePacket(joystick, context->device, ctx, data, size); + HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size); #else switch (data[0]) { case 0x00: - HIDAPI_DriverXbox360_HandleStatePacket(joystick, context->device, ctx, data, size); + HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size); break; #ifdef __MACOSX__ case 0x01: - HIDAPI_DriverXboxOneS_HandleStatePacket(joystick, context->device, ctx, data, size); + HIDAPI_DriverXboxOneS_HandleStatePacket(joystick, dev, ctx, data, size); break; case 0x02: - HIDAPI_DriverXboxOneS_HandleGuidePacket(joystick, context->device, ctx, data, size); + HIDAPI_DriverXboxOneS_HandleGuidePacket(joystick, dev, ctx, data, size); break; #endif default: @@ -781,7 +746,7 @@ HIDAPI_DriverXbox360_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joyst if (ctx->rumble_expiration) { Uint32 now = SDL_GetTicks(); if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) { - HIDAPI_DriverXbox360_Rumble(context, joystick, 0, 0, 0); + HIDAPI_DriverXbox360_Rumble(joystick, dev, context, 0, 0, 0); } } @@ -789,9 +754,11 @@ HIDAPI_DriverXbox360_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joyst } static void -HIDAPI_DriverXbox360_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks) +HIDAPI_DriverXbox360_Quit(SDL_Joystick *joystick, hid_device *dev, void *context) { - SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context->context; +#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT) + SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context; +#endif #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT if (ctx->xinput_enabled) { @@ -802,11 +769,7 @@ HIDAPI_DriverXbox360_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_ev #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx); #endif - *num_joysticks -= 1; - if (send_event) { - SDL_PrivateJoystickRemoved(ctx->joystickID); - } - SDL_free(context->context); + SDL_free(context); } SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 = @@ -815,14 +778,10 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 = SDL_TRUE, HIDAPI_DriverXbox360_IsSupportedDevice, HIDAPI_DriverXbox360_GetDeviceName, - HIDAPI_DriverXbox360_InitDriver, - HIDAPI_DriverXbox360_QuitDriver, - HIDAPI_DriverXbox360_UpdateDriver, - HIDAPI_DriverXbox360_NumJoysticks, - HIDAPI_DriverXbox360_PlayerIndexForIndex, - HIDAPI_DriverXbox360_InstanceIDForIndex, - HIDAPI_DriverXbox360_OpenJoystick, - HIDAPI_DriverXbox360_Rumble + HIDAPI_DriverXbox360_Init, + HIDAPI_DriverXbox360_Rumble, + HIDAPI_DriverXbox360_Update, + HIDAPI_DriverXbox360_Quit }; #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */ diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c index 734c76ed7..fde74bb23 100644 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c @@ -124,7 +124,6 @@ static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = { }; typedef struct { - SDL_JoystickID joystickID; Uint8 sequence; Uint8 last_state[USB_PACKET_LENGTH]; Uint32 rumble_expiration; @@ -144,7 +143,7 @@ HIDAPI_DriverXboxOne_GetDeviceName(Uint16 vendor_id, Uint16 product_id) } static SDL_bool -HIDAPI_DriverXboxOne_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks) +HIDAPI_DriverXboxOne_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context) { SDL_DriverXboxOne_Context *ctx; int i; @@ -155,7 +154,7 @@ HIDAPI_DriverXboxOne_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id SDL_OutOfMemory(); return SDL_FALSE; } - context->context = ctx; + *context = ctx; /* Send the controller init data */ for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) { @@ -163,7 +162,7 @@ HIDAPI_DriverXboxOne_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id if (!packet->vendor_id || (vendor_id == packet->vendor_id && product_id == packet->product_id)) { SDL_memcpy(init_packet, packet->data, packet->size); init_packet[2] = ctx->sequence++; - if (hid_write(context->device, init_packet, packet->size) != packet->size) { + if (hid_write(dev, init_packet, packet->size) != packet->size) { SDL_SetError("Couldn't write Xbox One initialization packet"); SDL_free(ctx); return SDL_FALSE; @@ -171,16 +170,6 @@ HIDAPI_DriverXboxOne_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id } } - ctx->joystickID = SDL_GetNextJoystickInstanceID(); - *num_joysticks += 1; - SDL_PrivateJoystickAdded(ctx->joystickID); - - return SDL_TRUE; -} - -static SDL_bool -HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick) -{ /* Initialize the joystick capabilities */ joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX; joystick->naxes = SDL_CONTROLLER_AXIS_MAX; @@ -189,41 +178,10 @@ HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick * return SDL_TRUE; } -static void -HIDAPI_DriverXboxOne_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks) -{ - SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context->context; - - *num_joysticks -= 1; - if (send_event) { - SDL_PrivateJoystickRemoved(ctx->joystickID); - } - SDL_free(context->context); -} - static int -HIDAPI_DriverXboxOne_NumJoysticks(SDL_HIDAPI_DriverData *context) +HIDAPI_DriverXboxOne_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) { - return 1; -} - -static int -HIDAPI_DriverXboxOne_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - return -1; -} - -static SDL_JoystickID -HIDAPI_DriverXboxOne_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index) -{ - SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context->context; - return ctx->joystickID; -} - -static int -HIDAPI_DriverXboxOne_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) -{ - SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context->context; + SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context; Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF }; /* The Rock Candy Xbox One Controller limits the range of @@ -236,7 +194,7 @@ HIDAPI_DriverXboxOne_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joysti rumble_packet[8] = (low_frequency_rumble >> 9); rumble_packet[9] = (high_frequency_rumble >> 9); - if (hid_write(context->device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) { + if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) { return SDL_SetError("Couldn't send rumble packet"); } @@ -309,24 +267,19 @@ HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, S } static SDL_bool -HIDAPI_DriverXboxOne_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks) +HIDAPI_DriverXboxOne_Update(SDL_Joystick *joystick, hid_device *dev, void *context) { - SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context->context; - SDL_Joystick *joystick = SDL_JoystickFromInstanceID(ctx->joystickID); + SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context; Uint8 data[USB_PACKET_LENGTH]; int size; - if (joystick == NULL) { - return SDL_TRUE; /* Nothing to do right now! */ - } - - while ((size = hid_read_timeout(context->device, data, sizeof(data), 0)) > 0) { + while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) { switch (data[0]) { case 0x20: - HIDAPI_DriverXboxOne_HandleStatePacket(joystick, context->device, ctx, data, size); + HIDAPI_DriverXboxOne_HandleStatePacket(joystick, dev, ctx, data, size); break; case 0x07: - HIDAPI_DriverXboxOne_HandleModePacket(joystick, context->device, ctx, data, size); + HIDAPI_DriverXboxOne_HandleModePacket(joystick, dev, ctx, data, size); break; default: #ifdef DEBUG_JOYSTICK @@ -339,27 +292,29 @@ HIDAPI_DriverXboxOne_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joyst if (ctx->rumble_expiration) { Uint32 now = SDL_GetTicks(); if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) { - HIDAPI_DriverXboxOne_Rumble(context, joystick, 0, 0, 0); + HIDAPI_DriverXboxOne_Rumble(joystick, dev, context, 0, 0, 0); } } return (size >= 0); } +static void +HIDAPI_DriverXboxOne_Quit(SDL_Joystick *joystick, hid_device *dev, void *context) +{ + SDL_free(context); +} + SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne = { SDL_HINT_JOYSTICK_HIDAPI_XBOX, SDL_TRUE, HIDAPI_DriverXboxOne_IsSupportedDevice, HIDAPI_DriverXboxOne_GetDeviceName, - HIDAPI_DriverXboxOne_InitDriver, - HIDAPI_DriverXboxOne_QuitDriver, - HIDAPI_DriverXboxOne_UpdateDriver, - HIDAPI_DriverXboxOne_NumJoysticks, - HIDAPI_DriverXboxOne_PlayerIndexForIndex, - HIDAPI_DriverXboxOne_InstanceIDForIndex, - HIDAPI_DriverXboxOne_OpenJoystick, - HIDAPI_DriverXboxOne_Rumble + HIDAPI_DriverXboxOne_Init, + HIDAPI_DriverXboxOne_Rumble, + HIDAPI_DriverXboxOne_Update, + HIDAPI_DriverXboxOne_Quit }; #endif /* SDL_JOYSTICK_HIDAPI_XBOXONE */ diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c index 319cf3ac0..e4203544c 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick.c +++ b/src/joystick/hidapi/SDL_hidapijoystick.c @@ -50,10 +50,18 @@ #endif #endif +struct joystick_hwdata +{ + SDL_HIDAPI_DeviceDriver *driver; + void *context; + + SDL_mutex *mutex; + hid_device *dev; +}; + typedef struct _SDL_HIDAPI_Device { - SDL_HIDAPI_DriverData devdata; - SDL_mutex *mutex; + SDL_JoystickID instance_id; char *name; char *path; Uint16 vendor_id; @@ -87,9 +95,6 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = { #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE &SDL_HIDAPI_DriverXboxOne, #endif -#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE - &SDL_HIDAPI_DriverGameCube, -#endif }; static SDL_HIDAPI_Device *SDL_HIDAPI_devices; static int SDL_HIDAPI_numjoysticks = 0; @@ -388,36 +393,6 @@ HIDAPI_ShutdownDiscovery() #endif } -static void -HIDAPI_InitDriver(SDL_HIDAPI_Device *device) -{ - device->devdata.device = hid_open_path(device->path, 0); - if (!device->devdata.device) { - SDL_SetError("Couldn't open HID device %s", device->path); - device->driver = NULL; - } else { - device->driver->InitDriver( - &device->devdata, - device->vendor_id, - device->product_id, - &SDL_HIDAPI_numjoysticks - ); - device->mutex = SDL_CreateMutex(); - } -} - -static void -HIDAPI_QuitDriver(SDL_HIDAPI_Device *device, SDL_bool send_event) -{ - device->driver->QuitDriver( - &device->devdata, - send_event, - &SDL_HIDAPI_numjoysticks - ); - hid_close(device->devdata.device); - SDL_DestroyMutex(device->mutex); - device->driver = NULL; -} const char * HIDAPI_XboxControllerName(Uint16 vendor_id, Uint16 product_id) @@ -630,17 +605,15 @@ HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device) } static SDL_HIDAPI_Device * -HIDAPI_GetDeviceByIndex(int device_index) +HIDAPI_GetJoystickByIndex(int device_index) { SDL_HIDAPI_Device *device = SDL_HIDAPI_devices; - int joysticks; while (device) { if (device->driver) { - joysticks = device->driver->NumJoysticks(&device->devdata); - if (device_index < joysticks) { + if (device_index == 0) { break; } - device_index -= joysticks; + --device_index; } device = device->next; } @@ -687,12 +660,20 @@ SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldVal while (device) { if (device->driver) { if (!device->driver->enabled) { - HIDAPI_QuitDriver(device, SDL_TRUE); + device->driver = NULL; + + --SDL_HIDAPI_numjoysticks; + + SDL_PrivateJoystickRemoved(device->instance_id); } } else { device->driver = HIDAPI_GetDeviceDriver(device); if (device->driver) { - HIDAPI_InitDriver(device); + device->instance_id = SDL_GetNextJoystickInstanceID(); + + ++SDL_HIDAPI_numjoysticks; + + SDL_PrivateJoystickAdded(device->instance_id); } } device = device->next; @@ -742,6 +723,7 @@ HIDAPI_AddDevice(struct hid_device_info *info) if (!device) { return; } + device->instance_id = -1; device->seen = SDL_TRUE; device->vendor_id = info->vendor_id; device->product_id = info->product_id; @@ -836,8 +818,12 @@ HIDAPI_AddDevice(struct hid_device_info *info) } if (device->driver) { - /* It's a joystick device! */ - HIDAPI_InitDriver(device); + /* It's a joystick! */ + device->instance_id = SDL_GetNextJoystickInstanceID(); + + ++SDL_HIDAPI_numjoysticks; + + SDL_PrivateJoystickAdded(device->instance_id); } } @@ -854,8 +840,11 @@ HIDAPI_DelDevice(SDL_HIDAPI_Device *device, SDL_bool send_event) SDL_HIDAPI_devices = curr->next; } - if (device->driver) { - HIDAPI_QuitDriver(device, send_event); + if (device->driver && send_event) { + /* Need to decrement the joystick count before we post the event */ + --SDL_HIDAPI_numjoysticks; + + SDL_PrivateJoystickRemoved(device->instance_id); } SDL_free(device->name); @@ -942,85 +931,101 @@ HIDAPI_JoystickDetect(void) static const char * HIDAPI_JoystickGetDeviceName(int device_index) { - return HIDAPI_GetDeviceByIndex(device_index)->name; + return HIDAPI_GetJoystickByIndex(device_index)->name; } static int HIDAPI_JoystickGetDevicePlayerIndex(int device_index) { - SDL_HIDAPI_Device *device = SDL_HIDAPI_devices; - int joysticks; - while (device) { - if (device->driver) { - joysticks = device->driver->NumJoysticks(&device->devdata); - if (device_index < joysticks) { - break; - } - device_index -= joysticks; - } - device = device->next; - } - return device->driver->PlayerIndexForIndex(&device->devdata, device_index); + return -1; } static SDL_JoystickGUID HIDAPI_JoystickGetDeviceGUID(int device_index) { - return HIDAPI_GetDeviceByIndex(device_index)->guid; + return HIDAPI_GetJoystickByIndex(device_index)->guid; } static SDL_JoystickID HIDAPI_JoystickGetDeviceInstanceID(int device_index) { - SDL_HIDAPI_Device *device = SDL_HIDAPI_devices; - int joysticks; - while (device) { - if (device->driver) { - joysticks = device->driver->NumJoysticks(&device->devdata); - if (device_index < joysticks) { - break; - } - device_index -= joysticks; - } - device = device->next; - } - return device->driver->InstanceIDForIndex(&device->devdata, device_index); + return HIDAPI_GetJoystickByIndex(device_index)->instance_id; } static int HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index) { - SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index); + SDL_HIDAPI_Device *device = HIDAPI_GetJoystickByIndex(device_index); + struct joystick_hwdata *hwdata; - if (!device->driver->OpenJoystick(&device->devdata, joystick)) { + hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata)); + if (!hwdata) { + return SDL_OutOfMemory(); + } + + hwdata->driver = device->driver; + hwdata->dev = hid_open_path(device->path, 0); + if (!hwdata->dev) { + SDL_free(hwdata); + return SDL_SetError("Couldn't open HID device %s", device->path); + } + hwdata->mutex = SDL_CreateMutex(); + + if (!device->driver->Init(joystick, hwdata->dev, device->vendor_id, device->product_id, &hwdata->context)) { + hid_close(hwdata->dev); + SDL_free(hwdata); return -1; } - joystick->hwdata = (struct joystick_hwdata *)device; + joystick->hwdata = hwdata; return 0; } static int HIDAPI_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) { - SDL_HIDAPI_Device *device = (SDL_HIDAPI_Device *)joystick->hwdata; + struct joystick_hwdata *hwdata = joystick->hwdata; + SDL_HIDAPI_DeviceDriver *driver = hwdata->driver; int result; - SDL_LockMutex(device->mutex); - result = device->driver->Rumble(&device->devdata, joystick, low_frequency_rumble, high_frequency_rumble, duration_ms); - SDL_UnlockMutex(device->mutex); + SDL_LockMutex(hwdata->mutex); + result = driver->Rumble(joystick, hwdata->dev, hwdata->context, low_frequency_rumble, high_frequency_rumble, duration_ms); + SDL_UnlockMutex(hwdata->mutex); return result; } static void HIDAPI_JoystickUpdate(SDL_Joystick * joystick) { - /* No-op, all updates are done in SDL_HIDAPI_UpdateDevices */ + struct joystick_hwdata *hwdata = joystick->hwdata; + SDL_HIDAPI_DeviceDriver *driver = hwdata->driver; + SDL_bool succeeded; + + SDL_LockMutex(hwdata->mutex); + succeeded = driver->Update(joystick, hwdata->dev, hwdata->context); + SDL_UnlockMutex(hwdata->mutex); + + if (!succeeded) { + SDL_HIDAPI_Device *device; + for (device = SDL_HIDAPI_devices; device; device = device->next) { + if (device->instance_id == joystick->instance_id) { + HIDAPI_DelDevice(device, SDL_TRUE); + break; + } + } + } } static void HIDAPI_JoystickClose(SDL_Joystick * joystick) { + struct joystick_hwdata *hwdata = joystick->hwdata; + SDL_HIDAPI_DeviceDriver *driver = hwdata->driver; + driver->Quit(joystick, hwdata->dev, hwdata->context); + + hid_close(hwdata->dev); + SDL_DestroyMutex(hwdata->mutex); + SDL_free(hwdata); joystick->hwdata = NULL; } @@ -1045,30 +1050,6 @@ HIDAPI_JoystickQuit(void) hid_exit(); } -void -SDL_HIDAPI_UpdateDevices(void) -{ - SDL_HIDAPI_Device *next, *device = SDL_HIDAPI_devices; - SDL_bool succeeded; - - while (device) { - if (device->driver) { - SDL_LockMutex(device->mutex); - succeeded = device->driver->UpdateDriver(&device->devdata, &SDL_HIDAPI_numjoysticks); - SDL_UnlockMutex(device->mutex); - if (!succeeded) { - next = device->next; - HIDAPI_DelDevice(device, SDL_TRUE); - device = next; - } else { - device = device->next; - } - } else { - device = device->next; - } - } -} - SDL_JoystickDriver SDL_HIDAPI_JoystickDriver = { HIDAPI_JoystickInit, diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h index 5b2f73619..a8e707393 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h @@ -30,7 +30,6 @@ #define SDL_JOYSTICK_HIDAPI_SWITCH #define SDL_JOYSTICK_HIDAPI_XBOX360 #define SDL_JOYSTICK_HIDAPI_XBOXONE -#define SDL_JOYSTICK_HIDAPI_GAMECUBE #ifdef __WINDOWS__ /* On Windows, Xbox One controllers are handled by the Xbox 360 driver */ @@ -44,38 +43,16 @@ #undef SDL_JOYSTICK_HIDAPI_XBOXONE #endif -typedef struct _SDL_HIDAPI_DriverData -{ - hid_device *device; - void *context; -} SDL_HIDAPI_DriverData; - typedef struct _SDL_HIDAPI_DeviceDriver { const char *hint; SDL_bool enabled; SDL_bool (*IsSupportedDevice)(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number); const char *(*GetDeviceName)(Uint16 vendor_id, Uint16 product_id); - - SDL_bool (*InitDriver)(SDL_HIDAPI_DriverData *context, - Uint16 vendor_id, Uint16 product_id, int *num_joysticks); - void (*QuitDriver)(SDL_HIDAPI_DriverData *context, - SDL_bool send_event, - int *num_joysticks); - SDL_bool (*UpdateDriver)(SDL_HIDAPI_DriverData *context, - int *num_joysticks); - int (*NumJoysticks)(SDL_HIDAPI_DriverData *context); - int (*PlayerIndexForIndex)(SDL_HIDAPI_DriverData *context, - int index); - SDL_JoystickID (*InstanceIDForIndex)(SDL_HIDAPI_DriverData *context, - int index); - SDL_bool (*OpenJoystick)(SDL_HIDAPI_DriverData *context, - SDL_Joystick *joystick); - int (*Rumble)(SDL_HIDAPI_DriverData *context, - SDL_Joystick *joystick, - Uint16 low_frequency_rumble, - Uint16 high_frequency_rumble, - Uint32 duration_ms); + SDL_bool (*Init)(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context); + int (*Rumble)(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms); + SDL_bool (*Update)(SDL_Joystick *joystick, hid_device *dev, void *context); + void (*Quit)(SDL_Joystick *joystick, hid_device *dev, void *context); } SDL_HIDAPI_DeviceDriver; @@ -85,7 +62,6 @@ extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360; extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne; -extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube; /* Return true if a HID device is present and supported as a joystick */ extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version);