Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
authorRyan C. Gordon <icculus@icculus.org>
Sat, 14 Nov 2015 12:35:45 -0500
changeset 99169429bc102632
parent 9915 77cf2d1b7215
child 9917 450a2f6e2bf2
Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
WhatsNew.txt
include/SDL_gamecontroller.h
include/SDL_joystick.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/joystick/SDL_gamecontroller.c
src/joystick/SDL_joystick.c
test/testgamecontroller.c
test/testjoystick.c
     1.1 --- a/WhatsNew.txt	Sat Nov 14 04:24:39 2015 -0400
     1.2 +++ b/WhatsNew.txt	Sat Nov 14 12:35:45 2015 -0500
     1.3 @@ -34,6 +34,8 @@
     1.4  * Added a hint SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN to prevent window interaction while cursor is hidden
     1.5  * Added SDL_GetDisplayDPI() to get the DPI information for a display
     1.6  * Added SDL_JoystickCurrentPowerLevel() to get the battery level of a joystick
     1.7 +* Added SDL_JoystickFromInstanceID(), as a helper function, to get the SDL_Joystick* that an event is referring to.
     1.8 +* Added SDL_GameControllerFromInstanceID(), as a helper function, to get the SDL_GameController* that an event is referring to.
     1.9  
    1.10  Windows:
    1.11  * Added support for Windows Phone 8.1
     2.1 --- a/include/SDL_gamecontroller.h	Sat Nov 14 04:24:39 2015 -0400
     2.2 +++ b/include/SDL_gamecontroller.h	Sat Nov 14 12:35:45 2015 -0500
     2.3 @@ -172,6 +172,11 @@
     2.4  extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
     2.5  
     2.6  /**
     2.7 + * Return the SDL_GameController associated with an instance id.
     2.8 + */
     2.9 +extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromInstanceID(SDL_JoystickID joyid);
    2.10 +
    2.11 +/**
    2.12   *  Return the name for this currently opened controller
    2.13   */
    2.14  extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
     3.1 --- a/include/SDL_joystick.h	Sat Nov 14 04:24:39 2015 -0400
     3.2 +++ b/include/SDL_joystick.h	Sat Nov 14 12:35:45 2015 -0500
     3.3 @@ -107,6 +107,11 @@
     3.4  extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);
     3.5  
     3.6  /**
     3.7 + * Return the SDL_Joystick associated with an instance id.
     3.8 + */
     3.9 +extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromInstanceID(SDL_JoystickID joyid);
    3.10 +
    3.11 +/**
    3.12   *  Return the name for this currently opened joystick.
    3.13   *  If no name can be found, this function returns NULL.
    3.14   */
     4.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Sat Nov 14 04:24:39 2015 -0400
     4.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Sat Nov 14 12:35:45 2015 -0500
     4.3 @@ -595,3 +595,5 @@
     4.4  #define SDL_GetGrabbedWindow SDL_GetGrabbedWindow_REAL
     4.5  #define SDL_SetWindowsMessageHook SDL_SetWindowsMessageHook_REAL
     4.6  #define SDL_JoystickCurrentPowerLevel SDL_JoystickCurrentPowerLevel_REAL
     4.7 +#define SDL_GameControllerFromInstanceID SDL_GameControllerFromInstanceID_REAL
     4.8 +#define SDL_JoystickFromInstanceID SDL_JoystickFromInstanceID_REAL
     5.1 --- a/src/dynapi/SDL_dynapi_procs.h	Sat Nov 14 04:24:39 2015 -0400
     5.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Sat Nov 14 12:35:45 2015 -0500
     5.3 @@ -629,3 +629,5 @@
     5.4  #endif
     5.5  SDL_DYNAPI_PROC(int,SDL_GetDisplayDPI,(int a, float *b, float *c, float *d),(a,b,c,d),return)
     5.6  SDL_DYNAPI_PROC(SDL_JoystickPowerLevel,SDL_JoystickCurrentPowerLevel,(SDL_Joystick *a),(a),return)
     5.7 +SDL_DYNAPI_PROC(SDL_GameController*,SDL_GameControllerFromInstanceID,(SDL_JoystickID a),(a),return)
     5.8 +SDL_DYNAPI_PROC(SDL_Joystick*,SDL_JoystickFromInstanceID,(SDL_JoystickID a),(a),return)
     6.1 --- a/src/joystick/SDL_gamecontroller.c	Sat Nov 14 04:24:39 2015 -0400
     6.2 +++ b/src/joystick/SDL_gamecontroller.c	Sat Nov 14 12:35:45 2015 -0500
     6.3 @@ -1046,6 +1046,25 @@
     6.4      return gamecontroller->joystick;
     6.5  }
     6.6  
     6.7 +
     6.8 +/*
     6.9 + * Find the SDL_GameController that owns this instance id
    6.10 + */
    6.11 +SDL_GameController *
    6.12 +SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
    6.13 +{
    6.14 +    SDL_GameController *gamecontroller = SDL_gamecontrollers;
    6.15 +    while (gamecontroller) {
    6.16 +        if (gamecontroller->joystick->instance_id == joyid) {
    6.17 +            return gamecontroller;
    6.18 +        }
    6.19 +        gamecontroller = gamecontroller->next;
    6.20 +    }
    6.21 +
    6.22 +    return NULL;
    6.23 +}
    6.24 +
    6.25 +
    6.26  /**
    6.27   * Get the SDL joystick layer binding for this controller axis mapping
    6.28   */
     7.1 --- a/src/joystick/SDL_joystick.c	Sat Nov 14 04:24:39 2015 -0400
     7.2 +++ b/src/joystick/SDL_joystick.c	Sat Nov 14 12:35:45 2015 -0500
     7.3 @@ -374,6 +374,23 @@
     7.4  }
     7.5  
     7.6  /*
     7.7 + * Find the SDL_Joystick that owns this instance id
     7.8 + */
     7.9 +SDL_Joystick *
    7.10 +SDL_JoystickFromInstanceID(SDL_JoystickID joyid)
    7.11 +{
    7.12 +    SDL_Joystick *joystick = SDL_joysticks;
    7.13 +    while (joystick) {
    7.14 +        if (joystick->instance_id == joyid) {
    7.15 +            return joystick;
    7.16 +        }
    7.17 +        joystick = joystick->next;
    7.18 +    }
    7.19 +
    7.20 +    return NULL;
    7.21 +}
    7.22 +
    7.23 +/*
    7.24   * Get the friendly name of this joystick
    7.25   */
    7.26  const char *
     8.1 --- a/test/testgamecontroller.c	Sat Nov 14 04:24:39 2015 -0400
     8.2 +++ b/test/testgamecontroller.c	Sat Nov 14 12:35:45 2015 -0500
     8.3 @@ -291,6 +291,11 @@
     8.4                                        guid, sizeof (guid));
     8.5              SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
     8.6              gamecontroller = SDL_GameControllerOpen(device);
     8.7 +
     8.8 +            if (gamecontroller != NULL) {
     8.9 +                SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
    8.10 +            }
    8.11 +
    8.12              while (keepGoing) {
    8.13                  if (gamecontroller == NULL) {
    8.14                      if (!reportederror) {
    8.15 @@ -316,6 +321,9 @@
    8.16                          keepGoing = SDL_FALSE;
    8.17                      } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
    8.18                          gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
    8.19 +                        if (gamecontroller != NULL) {
    8.20 +                            SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
    8.21 +                        }
    8.22                          break;
    8.23                      }
    8.24                  }
     9.1 --- a/test/testjoystick.c	Sat Nov 14 04:24:39 2015 -0400
     9.2 +++ b/test/testjoystick.c	Sat Nov 14 12:35:45 2015 -0500
     9.3 @@ -265,6 +265,7 @@
     9.4                      SDL_GetError());
     9.5          } else {
     9.6              char guid[64];
     9.7 +            SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
     9.8              SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
     9.9                                        guid, sizeof (guid));
    9.10              SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
    9.11 @@ -292,6 +293,9 @@
    9.12          device = atoi(argv[1]);
    9.13  #endif
    9.14          joystick = SDL_JoystickOpen(device);
    9.15 +        if (joystick != NULL) {
    9.16 +            SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
    9.17 +        }
    9.18  
    9.19          while ( keepGoing ) {
    9.20              if (joystick == NULL) {
    9.21 @@ -317,6 +321,9 @@
    9.22                      keepGoing = SDL_FALSE;
    9.23                  } else if (event.type == SDL_JOYDEVICEADDED) {
    9.24                      joystick = SDL_JoystickOpen(device);
    9.25 +                    if (joystick != NULL) {
    9.26 +                        SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
    9.27 +                    }
    9.28                      break;
    9.29                  }
    9.30              }