Navigation Menu

Skip to content

Commit

Permalink
Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstance…
Browse files Browse the repository at this point in the history
…ID().
  • Loading branch information
icculus committed Nov 14, 2015
1 parent 998a54f commit e6ad29a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions WhatsNew.txt
Expand Up @@ -34,6 +34,8 @@ General:
* Added a hint SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN to prevent window interaction while cursor is hidden
* Added SDL_GetDisplayDPI() to get the DPI information for a display
* Added SDL_JoystickCurrentPowerLevel() to get the battery level of a joystick
* Added SDL_JoystickFromInstanceID(), as a helper function, to get the SDL_Joystick* that an event is referring to.
* Added SDL_GameControllerFromInstanceID(), as a helper function, to get the SDL_GameController* that an event is referring to.

Windows:
* Added support for Windows Phone 8.1
Expand Down
5 changes: 5 additions & 0 deletions include/SDL_gamecontroller.h
Expand Up @@ -171,6 +171,11 @@ extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_
*/
extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);

/**
* Return the SDL_GameController associated with an instance id.
*/
extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromInstanceID(SDL_JoystickID joyid);

/**
* Return the name for this currently opened controller
*/
Expand Down
5 changes: 5 additions & 0 deletions include/SDL_joystick.h
Expand Up @@ -106,6 +106,11 @@ extern DECLSPEC const char *SDLCALL SDL_JoystickNameForIndex(int device_index);
*/
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);

/**
* Return the SDL_Joystick associated with an instance id.
*/
extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickFromInstanceID(SDL_JoystickID joyid);

/**
* Return the name for this currently opened joystick.
* If no name can be found, this function returns NULL.
Expand Down
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -595,3 +595,5 @@
#define SDL_GetGrabbedWindow SDL_GetGrabbedWindow_REAL
#define SDL_SetWindowsMessageHook SDL_SetWindowsMessageHook_REAL
#define SDL_JoystickCurrentPowerLevel SDL_JoystickCurrentPowerLevel_REAL
#define SDL_GameControllerFromInstanceID SDL_GameControllerFromInstanceID_REAL
#define SDL_JoystickFromInstanceID SDL_JoystickFromInstanceID_REAL
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -629,3 +629,5 @@ SDL_DYNAPI_PROC(void,SDL_SetWindowsMessageHook,(SDL_WindowsMessageHook a, void *
#endif
SDL_DYNAPI_PROC(int,SDL_GetDisplayDPI,(int a, float *b, float *c, float *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(SDL_JoystickPowerLevel,SDL_JoystickCurrentPowerLevel,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_GameController*,SDL_GameControllerFromInstanceID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_Joystick*,SDL_JoystickFromInstanceID,(SDL_JoystickID a),(a),return)
19 changes: 19 additions & 0 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -1046,6 +1046,25 @@ SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
return gamecontroller->joystick;
}


/*
* Find the SDL_GameController that owns this instance id
*/
SDL_GameController *
SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
{
SDL_GameController *gamecontroller = SDL_gamecontrollers;
while (gamecontroller) {
if (gamecontroller->joystick->instance_id == joyid) {
return gamecontroller;
}
gamecontroller = gamecontroller->next;
}

return NULL;
}


/**
* Get the SDL joystick layer binding for this controller axis mapping
*/
Expand Down
17 changes: 17 additions & 0 deletions src/joystick/SDL_joystick.c
Expand Up @@ -373,6 +373,23 @@ SDL_JoystickInstanceID(SDL_Joystick * joystick)
return (joystick->instance_id);
}

/*
* Find the SDL_Joystick that owns this instance id
*/
SDL_Joystick *
SDL_JoystickFromInstanceID(SDL_JoystickID joyid)
{
SDL_Joystick *joystick = SDL_joysticks;
while (joystick) {
if (joystick->instance_id == joyid) {
return joystick;
}
joystick = joystick->next;
}

return NULL;
}

/*
* Get the friendly name of this joystick
*/
Expand Down
8 changes: 8 additions & 0 deletions test/testgamecontroller.c
Expand Up @@ -291,6 +291,11 @@ main(int argc, char *argv[])
guid, sizeof (guid));
SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
gamecontroller = SDL_GameControllerOpen(device);

if (gamecontroller != NULL) {
SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
}

while (keepGoing) {
if (gamecontroller == NULL) {
if (!reportederror) {
Expand All @@ -316,6 +321,9 @@ main(int argc, char *argv[])
keepGoing = SDL_FALSE;
} else if (event.type == SDL_CONTROLLERDEVICEADDED) {
gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
if (gamecontroller != NULL) {
SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
}
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/testjoystick.c
Expand Up @@ -265,6 +265,7 @@ main(int argc, char *argv[])
SDL_GetError());
} else {
char guid[64];
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
guid, sizeof (guid));
SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
Expand Down Expand Up @@ -292,6 +293,9 @@ main(int argc, char *argv[])
device = atoi(argv[1]);
#endif
joystick = SDL_JoystickOpen(device);
if (joystick != NULL) {
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
}

while ( keepGoing ) {
if (joystick == NULL) {
Expand All @@ -317,6 +321,9 @@ main(int argc, char *argv[])
keepGoing = SDL_FALSE;
} else if (event.type == SDL_JOYDEVICEADDED) {
joystick = SDL_JoystickOpen(device);
if (joystick != NULL) {
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
}
break;
}
}
Expand Down

0 comments on commit e6ad29a

Please sign in to comment.