Added an API to get the joystick instance ID before opening the device: SDL_JoystickGetDeviceInstanceID()
authorSam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2017 16:09:16 -0800
changeset 10934380eae4f5059
parent 10933 a9ce2bbd76e1
child 10935 9a4c02660fcf
Added an API to get the joystick instance ID before opening the device: SDL_JoystickGetDeviceInstanceID()
include/SDL_joystick.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/joystick/SDL_joystick.c
     1.1 --- a/include/SDL_joystick.h	Thu Mar 09 15:12:19 2017 -0800
     1.2 +++ b/include/SDL_joystick.h	Thu Mar 09 16:09:16 2017 -0800
     1.3 @@ -71,6 +71,13 @@
     1.4      Uint8 data[16];
     1.5  } SDL_JoystickGUID;
     1.6  
     1.7 +/**
     1.8 + * This is a unique ID for a joystick for the time it is connected to the system,
     1.9 + * and is never reused for the lifetime of the application. If the joystick is
    1.10 + * disconnected and reconnected, it will get a new ID.
    1.11 + *
    1.12 + * The ID value starts at 0 and increments from there. The value -1 is an invalid ID.
    1.13 + */
    1.14  typedef Sint32 SDL_JoystickID;
    1.15  
    1.16  typedef enum
    1.17 @@ -145,6 +152,13 @@
    1.18  extern DECLSPEC SDL_JoystickType SDLCALL SDL_JoystickGetDeviceType(int device_index);
    1.19  
    1.20  /**
    1.21 + *  Get the instance ID of a joystick.
    1.22 + *  This can be called before any joysticks are opened.
    1.23 + *  If the index is out of range, this function will return -1.
    1.24 + */
    1.25 +extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickGetDeviceInstanceID(int device_index);
    1.26 +
    1.27 +/**
    1.28   *  Open a joystick for use.
    1.29   *  The index passed as an argument refers to the N'th joystick on the system.
    1.30   *  This index is not the value which will identify this joystick in future
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Thu Mar 09 15:12:19 2017 -0800
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Thu Mar 09 16:09:16 2017 -0800
     2.3 @@ -623,3 +623,4 @@
     2.4  #define SDL_JoystickGetType SDL_JoystickGetType_REAL
     2.5  #define SDL_MemoryBarrierReleaseFunction SDL_MemoryBarrierReleaseFunction_REAL
     2.6  #define SDL_MemoryBarrierAcquireFunction SDL_MemoryBarrierAcquireFunction_REAL
     2.7 +#define SDL_JoystickGetDeviceInstanceID SDL_JoystickGetDeviceInstanceID_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Thu Mar 09 15:12:19 2017 -0800
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Thu Mar 09 16:09:16 2017 -0800
     3.3 @@ -655,3 +655,4 @@
     3.4  SDL_DYNAPI_PROC(SDL_JoystickType,SDL_JoystickGetType,(SDL_Joystick *a),(a),return)
     3.5  SDL_DYNAPI_PROC(void,SDL_MemoryBarrierReleaseFunction,(void),(),)
     3.6  SDL_DYNAPI_PROC(void,SDL_MemoryBarrierAcquireFunction,(void),(),)
     3.7 +SDL_DYNAPI_PROC(SDL_JoystickID,SDL_JoystickGetDeviceInstanceID,(int a),(a),return)
     4.1 --- a/src/joystick/SDL_joystick.c	Thu Mar 09 15:12:19 2017 -0800
     4.2 +++ b/src/joystick/SDL_joystick.c	Thu Mar 09 16:09:16 2017 -0800
     4.3 @@ -108,7 +108,7 @@
     4.4  const char *
     4.5  SDL_JoystickNameForIndex(int device_index)
     4.6  {
     4.7 -    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
     4.8 +    if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
     4.9          SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
    4.10          return (NULL);
    4.11      }
    4.12 @@ -170,10 +170,10 @@
    4.13  
    4.14      joysticklist = SDL_joysticks;
    4.15      /* If the joystick is already open, return it
    4.16 -    * it is important that we have a single joystick * for each instance id
    4.17 -    */
    4.18 +     * it is important that we have a single joystick * for each instance id
    4.19 +     */
    4.20      while (joysticklist) {
    4.21 -        if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) {
    4.22 +        if (SDL_JoystickGetDeviceInstanceID(device_index) == joysticklist->instance_id) {
    4.23                  joystick = joysticklist;
    4.24                  ++joystick->ref_count;
    4.25                  SDL_UnlockJoystickList();
    4.26 @@ -1078,7 +1078,7 @@
    4.27  /* return the guid for this index */
    4.28  SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
    4.29  {
    4.30 -    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
    4.31 +    if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
    4.32          SDL_JoystickGUID emptyGUID;
    4.33          SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
    4.34          SDL_zero(emptyGUID);
    4.35 @@ -1128,7 +1128,15 @@
    4.36      return type;
    4.37  }
    4.38  
    4.39 -/* return the guid for this opened device */
    4.40 +SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
    4.41 +{
    4.42 +    if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
    4.43 +        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
    4.44 +        return -1;
    4.45 +    }
    4.46 +    return SDL_SYS_GetInstanceIdOfDeviceIndex(device_index);
    4.47 +}
    4.48 +
    4.49  SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
    4.50  {
    4.51      if (!SDL_PrivateJoystickValid(joystick)) {