Commit 0323d983c68ebf0b51cd3d1469380685a56e2de5

Sam Lantinga 2021-11-07T12:52:06

Add the platform to the game controller mapping so it can be read back in without changes Fixes https://github.com/libsdl-org/SDL/issues/4848

diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index d65439b..37f6d18 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -1531,16 +1531,17 @@ SDL_GameControllerMappingForIndex(int mapping_index)
             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 + \0 */
-            needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
+            /* 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", pchGUID, mapping->name, mapping->mapping);
+            SDL_snprintf(pMappingString, needed, "%s,%s,%s%s%s", pchGUID, mapping->name, mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD, platform);
             return pMappingString;
         }
         --mapping_index;
@@ -1559,15 +1560,17 @@ SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
     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 + \0 */
-        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
+        /* 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", pchGUID, mapping->name, mapping->mapping);
+        SDL_snprintf(pMappingString, needed, "%s,%s,%s%s%s", pchGUID, mapping->name, mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD, platform);
     }
     return pMappingString;
 }