Trim any extra platform strings from mappings when they're returned to the application
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
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 59c510f..305139f 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -1516,6 +1516,57 @@ SDL_GameControllerNumMappings(void)
}
/*
+ * Create a mapping string for a mapping
+ */
+static char *
+CreateMappingString(ControllerMapping_t *mapping, SDL_JoystickGUID guid)
+{
+ char *pMappingString, *pPlatformString;
+ char pchGUID[33];
+ size_t needed;
+ const char *platform = SDL_GetPlatform();
+
+ 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;
+
+ if (!SDL_strstr(mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD)) {
+ /* add memory for ',' + platform:PLATFORM */
+ if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
+ needed += 1;
+ }
+ needed += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD) + SDL_strlen(platform);
+ }
+
+ pMappingString = SDL_malloc(needed);
+ if (!pMappingString) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
+
+ SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
+
+ if (!SDL_strstr(mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD)) {
+ if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
+ SDL_strlcat(pMappingString, ",", needed);
+ }
+ SDL_strlcat(pMappingString, SDL_CONTROLLER_PLATFORM_FIELD, needed);
+ SDL_strlcat(pMappingString, platform, needed);
+ }
+
+ /* Make sure multiple platform strings haven't made their way into the mapping */
+ pPlatformString = SDL_strstr(pMappingString, SDL_CONTROLLER_PLATFORM_FIELD);
+ if (pPlatformString) {
+ pPlatformString = SDL_strstr(pPlatformString + 1, SDL_CONTROLLER_PLATFORM_FIELD);
+ if (pPlatformString) {
+ *pPlatformString = '\0';
+ }
+ }
+ return pMappingString;
+}
+
+/*
* Get the mapping at a particular index.
*/
char *
@@ -1528,21 +1579,7 @@ SDL_GameControllerMappingForIndex(int mapping_index)
continue;
}
if (mapping_index == 0) {
- char *pMappingString;
- char pchGUID[33];
- size_t needed;
- const char *platform = SDL_GetPlatform();
-
- SDL_JoystickGetGUIDString(mapping->guid, pchGUID, sizeof(pchGUID));
- /* allocate enough memory for GUID + ',' + name + ',' + mapping + platform:PLATFORM + \0 */
- needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD) + SDL_strlen(platform) + 1;
- pMappingString = SDL_malloc(needed);
- if (!pMappingString) {
- SDL_OutOfMemory();
- return NULL;
- }
- SDL_snprintf(pMappingString, needed, "%s,%s,%s%s%s", pchGUID, mapping->name, mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD, platform);
- return pMappingString;
+ return CreateMappingString(mapping, mapping->guid);
}
--mapping_index;
}
@@ -1555,24 +1592,11 @@ SDL_GameControllerMappingForIndex(int mapping_index)
char *
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
{
- char *pMappingString = NULL;
ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_FALSE);
if (mapping) {
- char pchGUID[33];
- size_t needed;
- const char *platform = SDL_GetPlatform();
-
- SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
- /* allocate enough memory for GUID + ',' + name + ',' + mapping + platform:PLATFORM + \0 */
- needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD) + SDL_strlen(platform) + 1;
- pMappingString = SDL_malloc(needed);
- if (!pMappingString) {
- SDL_OutOfMemory();
- return NULL;
- }
- SDL_snprintf(pMappingString, needed, "%s,%s,%s%s%s", pchGUID, mapping->name, mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD, platform);
+ return CreateMappingString(mapping, guid);
}
- return pMappingString;
+ return NULL;
}
/*