Skip to content

Commit

Permalink
Generalized the XInput user index into a player index
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 25, 2018
1 parent 545febc commit 1432925
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 18 deletions.
7 changes: 7 additions & 0 deletions include/SDL_gamecontroller.h
Expand Up @@ -204,6 +204,13 @@ extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromInstanceID(SDL
*/
extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);

/**
* Get the player index of an opened game controller, or -1 if it's not available
*
* For XInput controllers this returns the XInput user index.
*/
extern DECLSPEC int SDLCALL SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller);

/**
* Get the USB vendor ID of an opened controller, if available.
* If the vendor ID isn't available this function returns 0.
Expand Down
18 changes: 13 additions & 5 deletions include/SDL_joystick.h
Expand Up @@ -132,6 +132,12 @@ extern DECLSPEC int SDLCALL SDL_NumJoysticks(void);
*/
extern DECLSPEC const char *SDLCALL SDL_JoystickNameForIndex(int device_index);

/**
* Get the player index of a joystick, or -1 if it's not available
* This can be called before any joysticks are opened.
*/
extern DECLSPEC int SDLCALL SDL_JoystickGetDevicePlayerIndex(int device_index);

/**
* Return the GUID for the joystick at this index
* This can be called before any joysticks are opened.
Expand Down Expand Up @@ -194,6 +200,13 @@ extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromInstanceID(SDL_JoystickID
*/
extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick * joystick);

/**
* Get the player index of an opened joystick, or -1 if it's not available
*
* For XInput controllers this returns the XInput user index.
*/
extern DECLSPEC int SDLCALL SDL_JoystickGetPlayerIndex(SDL_Joystick * joystick);

/**
* Return the GUID for this opened joystick
*/
Expand Down Expand Up @@ -384,11 +397,6 @@ 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
}
Expand Down
4 changes: 3 additions & 1 deletion src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -698,4 +698,6 @@
#define SDL_GetDisplayOrientation SDL_GetDisplayOrientation_REAL
#define SDL_HasColorKey SDL_HasColorKey_REAL
#define SDL_CreateThreadWithStackSize SDL_CreateThreadWithStackSize_REAL
#define SDL_JoystickGetXInputUserIndex SDL_JoystickGetXInputUserIndex_REAL
#define SDL_JoystickGetDevicePlayerIndex SDL_JoystickGetDevicePlayerIndex_REAL
#define SDL_JoystickGetPlayerIndex SDL_JoystickGetPlayerIndex_REAL
#define SDL_GameControllerGetPlayerIndex SDL_GameControllerGetPlayerIndex_REAL
4 changes: 3 additions & 1 deletion src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -752,4 +752,6 @@ 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)
SDL_DYNAPI_PROC(int,SDL_JoystickGetDevicePlayerIndex,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_JoystickGetPlayerIndex,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GameControllerGetPlayerIndex,(SDL_GameController *a),(a),return)
6 changes: 6 additions & 0 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -1716,6 +1716,12 @@ SDL_GameControllerName(SDL_GameController * gamecontroller)
}
}

int
SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller)
{
return SDL_JoystickGetPlayerIndex(SDL_GameControllerGetJoystick(gamecontroller));
}

Uint16
SDL_GameControllerGetVendor(SDL_GameController * gamecontroller)
{
Expand Down
34 changes: 25 additions & 9 deletions src/joystick/SDL_joystick.c
Expand Up @@ -227,6 +227,21 @@ SDL_JoystickNameForIndex(int device_index)
return name;
}

int
SDL_JoystickGetDevicePlayerIndex(int device_index)
{
SDL_JoystickDriver *driver;
int player_index = -1;

SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
player_index = driver->GetDevicePlayerIndex(device_index);
}
SDL_UnlockJoysticks();

return player_index;
}

/*
* Return true if this joystick is known to have all axes centered at zero
* This isn't generally needed unless the joystick never generates an initial axis value near zero,
Expand Down Expand Up @@ -307,7 +322,7 @@ SDL_JoystickOpen(int device_index)
joystick->driver = driver;
joystick->instance_id = instance_id;
joystick->attached = SDL_TRUE;
joystick->userid = -1;
joystick->player_index = -1;

if (driver->Open(joystick, device_index) < 0) {
SDL_free(joystick);
Expand Down Expand Up @@ -603,6 +618,15 @@ SDL_JoystickName(SDL_Joystick * joystick)
return SDL_FixupJoystickName(joystick->name);
}

int
SDL_JoystickGetPlayerIndex(SDL_Joystick * joystick)
{
if (!SDL_PrivateJoystickValid(joystick)) {
return -1;
}
return joystick->player_index;
}

int
SDL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
Expand Down Expand Up @@ -1557,12 +1581,4 @@ 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: */
5 changes: 4 additions & 1 deletion src/joystick/SDL_sysjoystick.h
Expand Up @@ -42,8 +42,8 @@ struct _SDL_Joystick
{
SDL_JoystickID instance_id; /* Device instance, monotonically increasing from 0 */
char *name; /* Joystick name - system dependent */
int player_index; /* Joystick player index, or -1 if unavailable */
SDL_JoystickGUID guid; /* Joystick guid */
int userid; /* XInput user index, if any */

int naxes; /* Number of axis controls on the joystick */
SDL_JoystickAxisInfo *axes;
Expand Down Expand Up @@ -106,6 +106,9 @@ typedef struct _SDL_JoystickDriver
/* Function to get the device-dependent name of a joystick */
const char *(*GetDeviceName)(int device_index);

/* Function to get the player index of a joystick */
int (*GetDevicePlayerIndex)(int device_index);

/* Function to return the stable GUID for a plugged in device */
SDL_JoystickGUID (*GetDeviceGUID)(int device_index);

Expand Down
7 changes: 7 additions & 0 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -581,6 +581,12 @@ ANDROID_JoystickGetDeviceName(int device_index)
return JoystickByDevIndex(device_index)->name;
}

static int
ANDROID_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickGUID
ANDROID_JoystickGetDeviceGUID(int device_index)
{
Expand Down Expand Up @@ -689,6 +695,7 @@ SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
ANDROID_JoystickGetCount,
ANDROID_JoystickDetect,
ANDROID_JoystickGetDeviceName,
ANDROID_JoystickGetDevicePlayerIndex,
ANDROID_JoystickGetDeviceGUID,
ANDROID_JoystickGetDeviceInstanceID,
ANDROID_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/bsd/SDL_sysjoystick.c
Expand Up @@ -227,6 +227,12 @@ BSD_JoystickGetDeviceName(int device_index)
return (joynames[device_index]);
}

static int
BSD_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

/* Function to perform the mapping from device index to the instance id for this index */
static SDL_JoystickID
BSD_JoystickGetDeviceInstanceID(int device_index)
Expand Down Expand Up @@ -687,6 +693,7 @@ SDL_JoystickDriver SDL_BSD_JoystickDriver =
BSD_JoystickGetCount,
BSD_JoystickDetect,
BSD_JoystickGetDeviceName,
BSD_JoystickGetDevicePlayerIndex,
BSD_JoystickGetDeviceGUID,
BSD_JoystickGetDeviceInstanceID,
BSD_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -700,6 +700,12 @@ DARWIN_JoystickGetDeviceName(int device_index)
return device ? device->product : "UNKNOWN";
}

static int
DARWIN_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickGUID
DARWIN_JoystickGetDeviceGUID( int device_index )
{
Expand Down Expand Up @@ -998,6 +1004,7 @@ SDL_JoystickDriver SDL_DARWIN_JoystickDriver =
DARWIN_JoystickGetCount,
DARWIN_JoystickDetect,
DARWIN_JoystickGetDeviceName,
DARWIN_JoystickGetDevicePlayerIndex,
DARWIN_JoystickGetDeviceGUID,
DARWIN_JoystickGetDeviceInstanceID,
DARWIN_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/dummy/SDL_sysjoystick.c
Expand Up @@ -52,6 +52,12 @@ DUMMY_JoystickGetDeviceName(int device_index)
return NULL;
}

static int
DUMMY_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickGUID
DUMMY_JoystickGetDeviceGUID(int device_index)
{
Expand Down Expand Up @@ -99,6 +105,7 @@ SDL_JoystickDriver SDL_DUMMY_JoystickDriver =
DUMMY_JoystickGetCount,
DUMMY_JoystickDetect,
DUMMY_JoystickGetDeviceName,
DUMMY_JoystickGetDevicePlayerIndex,
DUMMY_JoystickGetDeviceGUID,
DUMMY_JoystickGetDeviceInstanceID,
DUMMY_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/emscripten/SDL_sysjoystick.c
Expand Up @@ -279,6 +279,12 @@ EMSCRIPTEN_JoystickGetDeviceName(int device_index)
return JoystickByDeviceIndex(device_index)->name;
}

static int
EMSCRIPTEN_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickID
EMSCRIPTEN_JoystickGetDeviceInstanceID(int device_index)
{
Expand Down Expand Up @@ -394,6 +400,7 @@ SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
EMSCRIPTEN_JoystickGetCount,
EMSCRIPTEN_JoystickDetect,
EMSCRIPTEN_JoystickGetDeviceName,
EMSCRIPTEN_JoystickGetDevicePlayerIndex,
EMSCRIPTEN_JoystickGetDeviceGUID,
EMSCRIPTEN_JoystickGetDeviceInstanceID,
EMSCRIPTEN_JoystickOpen,
Expand Down
6 changes: 6 additions & 0 deletions src/joystick/haiku/SDL_haikujoystick.cc
Expand Up @@ -99,6 +99,11 @@ extern "C"
return SDL_joyname[device_index];
}

static int HAIKU_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

/* Function to perform the mapping from device index to the instance id for this index */
static SDL_JoystickID HAIKU_JoystickGetDeviceInstanceID(int device_index)
{
Expand Down Expand Up @@ -256,6 +261,7 @@ extern "C"
HAIKU_JoystickGetCount,
HAIKU_JoystickDetect,
HAIKU_JoystickGetDeviceName,
HAIKU_JoystickGetDevicePlayerIndex,
HAIKU_JoystickGetDeviceGUID,
HAIKU_JoystickGetDeviceInstanceID,
HAIKU_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/hidapi/SDL_hidapijoystick.c
Expand Up @@ -932,6 +932,12 @@ HIDAPI_JoystickGetDeviceName(int device_index)
return HIDAPI_GetJoystickByIndex(device_index)->name;
}

static int
HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickGUID
HIDAPI_JoystickGetDeviceGUID(int device_index)
{
Expand Down Expand Up @@ -1048,6 +1054,7 @@ SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
HIDAPI_JoystickGetCount,
HIDAPI_JoystickDetect,
HIDAPI_JoystickGetDeviceName,
HIDAPI_JoystickGetDevicePlayerIndex,
HIDAPI_JoystickGetDeviceGUID,
HIDAPI_JoystickGetDeviceInstanceID,
HIDAPI_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/iphoneos/SDL_sysjoystick.m
Expand Up @@ -358,6 +358,12 @@
return device ? device->name : "Unknown";
}

static int
IOS_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickGUID
IOS_JoystickGetDeviceGUID( int device_index )
{
Expand Down Expand Up @@ -715,6 +721,7 @@
IOS_JoystickGetCount,
IOS_JoystickDetect,
IOS_JoystickGetDeviceName,
IOS_JoystickGetDevicePlayerIndex,
IOS_JoystickGetDeviceGUID,
IOS_JoystickGetDeviceInstanceID,
IOS_JoystickOpen,
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/linux/SDL_sysjoystick.c
Expand Up @@ -582,6 +582,12 @@ LINUX_JoystickGetDeviceName(int device_index)
return JoystickByDevIndex(device_index)->name;
}

static int
LINUX_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}

static SDL_JoystickGUID
LINUX_JoystickGetDeviceGUID( int device_index )
{
Expand Down Expand Up @@ -1100,6 +1106,7 @@ SDL_JoystickDriver SDL_LINUX_JoystickDriver =
LINUX_JoystickGetCount,
LINUX_JoystickDetect,
LINUX_JoystickGetDeviceName,
LINUX_JoystickGetDevicePlayerIndex,
LINUX_JoystickGetDeviceGUID,
LINUX_JoystickGetDeviceInstanceID,
LINUX_JoystickOpen,
Expand Down
13 changes: 13 additions & 0 deletions src/joystick/windows/SDL_windowsjoystick.c
Expand Up @@ -407,6 +407,18 @@ WINDOWS_JoystickGetDeviceName(int device_index)
return device->joystickname;
}

static int
WINDOWS_JoystickGetDevicePlayerIndex(int device_index)
{
JoyStick_DeviceData *device = SYS_Joystick;
int index;

for (index = device_index; index > 0; index--)
device = device->pNext;

return device->bXInputDevice ? (int)device->XInputUserId : -1;
}

/* return the stable device guid for this device index */
static SDL_JoystickGUID
WINDOWS_JoystickGetDeviceGUID(int device_index)
Expand Down Expand Up @@ -544,6 +556,7 @@ SDL_JoystickDriver SDL_WINDOWS_JoystickDriver =
WINDOWS_JoystickGetCount,
WINDOWS_JoystickDetect,
WINDOWS_JoystickGetDeviceName,
WINDOWS_JoystickGetDevicePlayerIndex,
WINDOWS_JoystickGetDeviceGUID,
WINDOWS_JoystickGetDeviceInstanceID,
WINDOWS_JoystickOpen,
Expand Down
3 changes: 2 additions & 1 deletion src/joystick/windows/SDL_xinputjoystick.c
Expand Up @@ -312,6 +312,8 @@ SDL_XINPUT_JoystickOpen(SDL_Joystick * joystick, JoyStick_DeviceData *joystickde
SDL_assert(XINPUTSETSTATE);
SDL_assert(userId < XUSER_MAX_COUNT);

joystick->player_index = userId;

joystick->hwdata->bXInputDevice = SDL_TRUE;

if (XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities) != ERROR_SUCCESS) {
Expand All @@ -322,7 +324,6 @@ 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()) {
Expand Down

0 comments on commit 1432925

Please sign in to comment.