Load the game controller mapping hint when opening a game controller.
authorSam Lantinga <slouken@libsdl.org>
Thu, 07 Mar 2013 15:37:08 -0800
changeset 6976 177d1dc3dba6
parent 6975 36145df5a4dc
child 6977 b73d51026c68
Load the game controller mapping hint when opening a game controller.
src/joystick/SDL_gamecontroller.c
--- a/src/joystick/SDL_gamecontroller.c	Thu Mar 07 15:17:06 2013 -0800
+++ b/src/joystick/SDL_gamecontroller.c	Thu Mar 07 15:37:08 2013 -0800
@@ -762,6 +762,33 @@
 	return SDL_GameControllerMappingForGUID( gamecontroller->mapping.guid );
 }
 
+static void
+SDL_GameControllerLoadHints()
+{
+    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
+    if ( hint && hint[0] ) {
+        int nchHints = SDL_strlen( hint );
+        char *pUserMappings = SDL_malloc( nchHints + 1 );
+        char *pTempMappings = pUserMappings;
+        SDL_memcpy( pUserMappings, hint, nchHints );
+        while ( pUserMappings ) {
+            char *pchNewLine = NULL;
+
+            pchNewLine = SDL_strchr( pUserMappings, '\n' );
+            if ( pchNewLine )
+                *pchNewLine = '\0';
+
+            SDL_GameControllerAddMapping( pUserMappings );
+
+            if ( pchNewLine )
+                pUserMappings = pchNewLine + 1;
+            else
+                pUserMappings = NULL;
+        }
+        SDL_free(pTempMappings);
+    }
+}
+
 /*
  * Initialize the game controller system, mostly load our DB of controller config mappings
  */
@@ -781,35 +808,11 @@
 	}
 
 	// load in any user supplied config
-	{
-		const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
-		if ( hint && hint[0] )
-		{
-			int nchHints = SDL_strlen( hint );
-			char *pUserMappings = SDL_malloc( nchHints + 1 );
-			char *pTempMappings = pUserMappings;
-			SDL_memcpy( pUserMappings, hint, nchHints );
-			while ( pUserMappings )
-			{
-				char *pchNewLine = NULL;
-
-				pchNewLine = SDL_strchr( pUserMappings, '\n' );
-				if ( pchNewLine )
-					*pchNewLine = '\0';
-
-				SDL_GameControllerAddMapping( pUserMappings );
-
-				if ( pchNewLine )
-					pUserMappings = pchNewLine + 1;
-				else
-					pUserMappings = NULL;
-			}
-			SDL_free(pTempMappings);
-		}
-	}
+    SDL_GameControllerLoadHints();
 
 	/* watch for joy events and fire controller ones if needed */
 	SDL_AddEventWatch( SDL_GameControllerEventWatcher, NULL );
+
 	return (0);
 }
 
@@ -875,10 +878,11 @@
 		gamecontrollerlist = gamecontrollerlist->next;
     }
 
-	// Find a Controller Mapping 
+	// Find a controller mapping 
+    // Check for hints in case someone has changed them recently
+    SDL_GameControllerLoadHints();
 	pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
-	if ( !pSupportedController )
-	{
+	if ( !pSupportedController ) {
 		SDL_SetError("Couldn't find mapping for device (%d)", device_index );
 		return (NULL);
 	}