From 9987ca69f3da9e958a622c8c2ef35e4e3871d96b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 25 Oct 2018 12:54:42 -0700 Subject: [PATCH] Added SDL_JoystickGetXInputUserIndex() --- include/SDL_joystick.h | 5 +++++ src/dynapi/SDL_dynapi_overrides.h | 1 + src/dynapi/SDL_dynapi_procs.h | 1 + src/joystick/SDL_joystick.c | 9 +++++++++ src/joystick/SDL_sysjoystick.h | 1 + src/joystick/windows/SDL_xinputjoystick.c | 1 + 6 files changed, 18 insertions(+) diff --git a/include/SDL_joystick.h b/include/SDL_joystick.h index 8303c5e86..20c314ac8 100644 --- a/include/SDL_joystick.h +++ b/include/SDL_joystick.h @@ -384,6 +384,11 @@ extern DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick * joystick); */ extern DECLSPEC SDL_JoystickPowerLevel SDLCALL SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick); +/** + * Return the XInput user index for this joystick, or -1 if it's not available + */ +extern DECLSPEC int SDLCALL SDL_JoystickGetXInputUserIndex(SDL_Joystick * joystick); + /* Ends C function definitions when using C++ */ #ifdef __cplusplus } diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 22e4d63d9..e2a627d51 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -698,3 +698,4 @@ #define SDL_GetDisplayOrientation SDL_GetDisplayOrientation_REAL #define SDL_HasColorKey SDL_HasColorKey_REAL #define SDL_CreateThreadWithStackSize SDL_CreateThreadWithStackSize_REAL +#define SDL_JoystickGetXInputUserIndex SDL_JoystickGetXInputUserIndex_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index cd50c02e9..7c228beae 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -752,3 +752,4 @@ SDL_DYNAPI_PROC(SDL_Thread*,SDL_CreateThreadWithStackSize,(SDL_ThreadFunction a, SDL_DYNAPI_PROC(SDL_Thread*,SDL_CreateThreadWithStackSize,(SDL_ThreadFunction a, const char *b, const size_t c, void *d),(a,b,c,d),return) #endif +SDL_DYNAPI_PROC(int,SDL_JoystickGetXInputUserIndex,(SDL_Joystick *a),(a),return) diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index f7d862b3c..c68dbfd6d 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -322,6 +322,7 @@ SDL_JoystickOpen(int device_index) } joystick->guid = driver->GetDeviceGUID(device_index); + joystick->userid = -1; if (joystick->naxes > 0) { joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo)); @@ -1556,4 +1557,12 @@ SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick) return joystick->epowerlevel; } +int SDL_JoystickGetXInputUserIndex(SDL_Joystick * joystick) +{ + if (!SDL_PrivateJoystickValid(joystick)) { + return -1; + } + return joystick->userid; +} + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h index 2b609dbc7..6fb0fecb0 100644 --- a/src/joystick/SDL_sysjoystick.h +++ b/src/joystick/SDL_sysjoystick.h @@ -43,6 +43,7 @@ struct _SDL_Joystick SDL_JoystickID instance_id; /* Device instance, monotonically increasing from 0 */ char *name; /* Joystick name - system dependent */ SDL_JoystickGUID guid; /* Joystick guid */ + int userid; /* XInput user index, if any */ int naxes; /* Number of axis controls on the joystick */ SDL_JoystickAxisInfo *axes; diff --git a/src/joystick/windows/SDL_xinputjoystick.c b/src/joystick/windows/SDL_xinputjoystick.c index d81436feb..a13ca9f0f 100644 --- a/src/joystick/windows/SDL_xinputjoystick.c +++ b/src/joystick/windows/SDL_xinputjoystick.c @@ -322,6 +322,7 @@ SDL_XINPUT_JoystickOpen(SDL_Joystick * joystick, JoyStick_DeviceData *joystickde SDL_zero(state); joystick->hwdata->bXInputHaptic = (XINPUTSETSTATE(userId, &state) == ERROR_SUCCESS); joystick->hwdata->userid = userId; + joystick->userid = userId; /* The XInput API has a hard coded button/axis mapping, so we just match it */ if (SDL_XInputUseOldJoystickMapping()) {