Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added an API to get the joystick instance ID before opening the devic…
…e: SDL_JoystickGetDeviceInstanceID()
  • Loading branch information
slouken committed Mar 10, 2017
1 parent 6bdc0e7 commit 763e138
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
14 changes: 14 additions & 0 deletions include/SDL_joystick.h
Expand Up @@ -71,6 +71,13 @@ typedef struct {
Uint8 data[16];
} SDL_JoystickGUID;

/**
* This is a unique ID for a joystick for the time it is connected to the system,
* and is never reused for the lifetime of the application. If the joystick is
* disconnected and reconnected, it will get a new ID.
*
* The ID value starts at 0 and increments from there. The value -1 is an invalid ID.
*/
typedef Sint32 SDL_JoystickID;

typedef enum
Expand Down Expand Up @@ -144,6 +151,13 @@ extern DECLSPEC Uint16 SDLCALL SDL_JoystickGetDeviceProductVersion(int device_in
*/
extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetDeviceType(int device_index);

/**
* Get the instance ID of a joystick.
* This can be called before any joysticks are opened.
* If the index is out of range, this function will return -1.
*/
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickGetDeviceInstanceID(int device_index);

/**
* Open a joystick for use.
* The index passed as an argument refers to the N'th joystick on the system.
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -623,3 +623,4 @@
#define SDL_JoystickGetType SDL_JoystickGetType_REAL
#define SDL_MemoryBarrierReleaseFunction SDL_MemoryBarrierReleaseFunction_REAL
#define SDL_MemoryBarrierAcquireFunction SDL_MemoryBarrierAcquireFunction_REAL
#define SDL_JoystickGetDeviceInstanceID SDL_JoystickGetDeviceInstanceID_REAL
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -655,3 +655,4 @@ SDL_DYNAPI_PROC(SDL_JoystickType,SDL_JoystickGetDeviceType,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickType,SDL_JoystickGetType,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_MemoryBarrierReleaseFunction,(void),(),)
SDL_DYNAPI_PROC(void,SDL_MemoryBarrierAcquireFunction,(void),(),)
SDL_DYNAPI_PROC(SDL_JoystickID,SDL_JoystickGetDeviceInstanceID,(int a),(a),return)
20 changes: 14 additions & 6 deletions src/joystick/SDL_joystick.c
Expand Up @@ -108,7 +108,7 @@ SDL_NumJoysticks(void)
const char *
SDL_JoystickNameForIndex(int device_index)
{
if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
return (NULL);
}
Expand Down Expand Up @@ -170,10 +170,10 @@ SDL_JoystickOpen(int device_index)

joysticklist = SDL_joysticks;
/* If the joystick is already open, return it
* it is important that we have a single joystick * for each instance id
*/
* it is important that we have a single joystick * for each instance id
*/
while (joysticklist) {
if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) {
if (SDL_JoystickGetDeviceInstanceID(device_index) == joysticklist->instance_id) {
joystick = joysticklist;
++joystick->ref_count;
SDL_UnlockJoystickList();
Expand Down Expand Up @@ -1078,7 +1078,7 @@ static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
/* return the guid for this index */
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
{
if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
SDL_JoystickGUID emptyGUID;
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
SDL_zero(emptyGUID);
Expand Down Expand Up @@ -1128,7 +1128,15 @@ SDL_JoystickType SDL_JoystickGetDeviceType(int device_index)
return type;
}

/* return the guid for this opened device */
SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
{
if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
return -1;
}
return SDL_SYS_GetInstanceIdOfDeviceIndex(device_index);
}

SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
{
if (!SDL_PrivateJoystickValid(joystick)) {
Expand Down

0 comments on commit 763e138

Please sign in to comment.