Added SDL_GameControllerMappingForDeviceIndex() to get the mapping for a controller before it's opened
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
diff --git a/WhatsNew.txt b/WhatsNew.txt
index c574946..0cd283e 100644
--- a/WhatsNew.txt
+++ b/WhatsNew.txt
@@ -2,6 +2,14 @@
This is a list of major changes in SDL's version history.
---------------------------------------------------------------------------
+2.0.9:
+---------------------------------------------------------------------------
+
+General:
+* Added SDL_GameControllerMappingForDeviceIndex() to get the mapping for a controller before it's opened
+
+
+---------------------------------------------------------------------------
2.0.8:
---------------------------------------------------------------------------
diff --git a/include/SDL_gamecontroller.h b/include/SDL_gamecontroller.h
index 2e024be..f7e03cd 100644
--- a/include/SDL_gamecontroller.h
+++ b/include/SDL_gamecontroller.h
@@ -176,6 +176,14 @@ extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
/**
+ * Get the mapping of a game controller.
+ * This can be called before any controllers are opened.
+ *
+ * \return the mapping string. Must be freed with SDL_free(). Returns NULL if no mapping is available
+ */
+extern DECLSPEC char *SDLCALL SDL_GameControllerMappingForDeviceIndex(int joystick_index);
+
+/**
* Open a game controller for use.
* The index passed as an argument refers to the N'th game controller on the system.
* This index is not the value which will identify this controller in future
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 1ec2eaf..77a73e5 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -669,3 +669,4 @@
#define SDL_WinRTGetDeviceFamily SDL_WinRTGetDeviceFamily_REAL
#define SDL_log10 SDL_log10_REAL
#define SDL_log10f SDL_log10f_REAL
+#define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index b715d33..1b57a2a 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -707,3 +707,4 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_IsAndroidTV,(void),(),return)
#endif
SDL_DYNAPI_PROC(double,SDL_log10,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
+SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForDeviceIndex,(int a),(a),return)
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 68ac762..3326c46 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -425,6 +425,12 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickG
}
pSupportedController = pSupportedController->next;
}
+#if SDL_JOYSTICK_XINPUT
+ if (guid->data[14] == 'x') {
+ /* This is an XInput device */
+ return s_pXInputMapping;
+ }
+#endif
return NULL;
}
@@ -1033,11 +1039,6 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
name = SDL_JoystickNameForIndex(device_index);
guid = SDL_JoystickGetDeviceGUID(device_index);
mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
-#if SDL_JOYSTICK_XINPUT
- if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
- mapping = s_pXInputMapping;
- }
-#endif
SDL_UnlockJoysticks();
return mapping;
}
@@ -1374,6 +1375,40 @@ SDL_GameControllerNameForIndex(int device_index)
}
+/**
+ * Get the mapping of a game controller.
+ * This can be called before any controllers are opened.
+ * If no mapping can be found, this function returns NULL.
+ */
+char *
+SDL_GameControllerMappingForDeviceIndex(int joystick_index)
+{
+ char *pMappingString = NULL;
+ ControllerMapping_t *mapping;
+
+ SDL_LockJoysticks();
+ mapping = SDL_PrivateGetControllerMapping(joystick_index);
+ if (mapping) {
+ SDL_JoystickGUID guid;
+ char pchGUID[33];
+ size_t needed;
+ guid = SDL_JoystickGetDeviceGUID(joystick_index);
+ SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
+ /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
+ needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
+ pMappingString = SDL_malloc(needed);
+ if (!pMappingString) {
+ SDL_OutOfMemory();
+ SDL_UnlockJoysticks();
+ return NULL;
+ }
+ SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
+ }
+ SDL_UnlockJoysticks();
+ return pMappingString;
+}
+
+
/*
* Return 1 if the joystick with this name and GUID is a supported controller
*/
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 6f7ed3a..0ce3115 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -121,11 +121,6 @@ extern SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index);
/* Function to return the stable GUID for a opened joystick */
extern SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick);
-#if SDL_JOYSTICK_XINPUT
-/* Function returns SDL_TRUE if this device is an XInput gamepad */
-extern SDL_bool SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index);
-#endif
-
#endif /* SDL_sysjoystick_h_ */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/joystick/windows/SDL_xinputjoystick.c b/src/joystick/windows/SDL_xinputjoystick.c
index 823e767..977a36b 100644
--- a/src/joystick/windows/SDL_xinputjoystick.c
+++ b/src/joystick/windows/SDL_xinputjoystick.c
@@ -464,18 +464,6 @@ SDL_XINPUT_JoystickQuit(void)
}
}
-SDL_bool
-SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index)
-{
- JoyStick_DeviceData *device = SYS_Joystick;
- int index;
-
- for (index = device_index; index > 0; index--)
- device = device->pNext;
-
- return device->bXInputDevice;
-}
-
#else /* !SDL_JOYSTICK_XINPUT */
typedef struct JoyStick_DeviceData JoyStick_DeviceData;