Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
authorRyan C. Gordon <icculus@icculus.org>
Sat, 14 Nov 2015 12:35:45 -0500
changeset 9916 9429bc102632
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
--- a/WhatsNew.txt	Sat Nov 14 04:24:39 2015 -0400
+++ b/WhatsNew.txt	Sat Nov 14 12:35:45 2015 -0500
@@ -34,6 +34,8 @@
 * 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
--- a/include/SDL_gamecontroller.h	Sat Nov 14 04:24:39 2015 -0400
+++ b/include/SDL_gamecontroller.h	Sat Nov 14 12:35:45 2015 -0500
@@ -172,6 +172,11 @@
 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
  */
 extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
--- a/include/SDL_joystick.h	Sat Nov 14 04:24:39 2015 -0400
+++ b/include/SDL_joystick.h	Sat Nov 14 12:35:45 2015 -0500
@@ -107,6 +107,11 @@
 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.
  */
--- a/src/dynapi/SDL_dynapi_overrides.h	Sat Nov 14 04:24:39 2015 -0400
+++ b/src/dynapi/SDL_dynapi_overrides.h	Sat Nov 14 12:35:45 2015 -0500
@@ -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
--- a/src/dynapi/SDL_dynapi_procs.h	Sat Nov 14 04:24:39 2015 -0400
+++ b/src/dynapi/SDL_dynapi_procs.h	Sat Nov 14 12:35:45 2015 -0500
@@ -629,3 +629,5 @@
 #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)
--- a/src/joystick/SDL_gamecontroller.c	Sat Nov 14 04:24:39 2015 -0400
+++ b/src/joystick/SDL_gamecontroller.c	Sat Nov 14 12:35:45 2015 -0500
@@ -1046,6 +1046,25 @@
     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
  */
--- a/src/joystick/SDL_joystick.c	Sat Nov 14 04:24:39 2015 -0400
+++ b/src/joystick/SDL_joystick.c	Sat Nov 14 12:35:45 2015 -0500
@@ -374,6 +374,23 @@
 }
 
 /*
+ * 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
  */
 const char *
--- a/test/testgamecontroller.c	Sat Nov 14 04:24:39 2015 -0400
+++ b/test/testgamecontroller.c	Sat Nov 14 12:35:45 2015 -0500
@@ -291,6 +291,11 @@
                                       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) {
@@ -316,6 +321,9 @@
                         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;
                     }
                 }
--- a/test/testjoystick.c	Sat Nov 14 04:24:39 2015 -0400
+++ b/test/testjoystick.c	Sat Nov 14 12:35:45 2015 -0500
@@ -265,6 +265,7 @@
                     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));
@@ -292,6 +293,9 @@
         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) {
@@ -317,6 +321,9 @@
                     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;
                 }
             }