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 6976177d1dc3dba6
parent 6975 36145df5a4dc
child 6977 b73d51026c68
Load the game controller mapping hint when opening a game controller.
src/joystick/SDL_gamecontroller.c
     1.1 --- a/src/joystick/SDL_gamecontroller.c	Thu Mar 07 15:17:06 2013 -0800
     1.2 +++ b/src/joystick/SDL_gamecontroller.c	Thu Mar 07 15:37:08 2013 -0800
     1.3 @@ -762,6 +762,33 @@
     1.4  	return SDL_GameControllerMappingForGUID( gamecontroller->mapping.guid );
     1.5  }
     1.6  
     1.7 +static void
     1.8 +SDL_GameControllerLoadHints()
     1.9 +{
    1.10 +    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
    1.11 +    if ( hint && hint[0] ) {
    1.12 +        int nchHints = SDL_strlen( hint );
    1.13 +        char *pUserMappings = SDL_malloc( nchHints + 1 );
    1.14 +        char *pTempMappings = pUserMappings;
    1.15 +        SDL_memcpy( pUserMappings, hint, nchHints );
    1.16 +        while ( pUserMappings ) {
    1.17 +            char *pchNewLine = NULL;
    1.18 +
    1.19 +            pchNewLine = SDL_strchr( pUserMappings, '\n' );
    1.20 +            if ( pchNewLine )
    1.21 +                *pchNewLine = '\0';
    1.22 +
    1.23 +            SDL_GameControllerAddMapping( pUserMappings );
    1.24 +
    1.25 +            if ( pchNewLine )
    1.26 +                pUserMappings = pchNewLine + 1;
    1.27 +            else
    1.28 +                pUserMappings = NULL;
    1.29 +        }
    1.30 +        SDL_free(pTempMappings);
    1.31 +    }
    1.32 +}
    1.33 +
    1.34  /*
    1.35   * Initialize the game controller system, mostly load our DB of controller config mappings
    1.36   */
    1.37 @@ -781,35 +808,11 @@
    1.38  	}
    1.39  
    1.40  	// load in any user supplied config
    1.41 -	{
    1.42 -		const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
    1.43 -		if ( hint && hint[0] )
    1.44 -		{
    1.45 -			int nchHints = SDL_strlen( hint );
    1.46 -			char *pUserMappings = SDL_malloc( nchHints + 1 );
    1.47 -			char *pTempMappings = pUserMappings;
    1.48 -			SDL_memcpy( pUserMappings, hint, nchHints );
    1.49 -			while ( pUserMappings )
    1.50 -			{
    1.51 -				char *pchNewLine = NULL;
    1.52 -
    1.53 -				pchNewLine = SDL_strchr( pUserMappings, '\n' );
    1.54 -				if ( pchNewLine )
    1.55 -					*pchNewLine = '\0';
    1.56 -
    1.57 -				SDL_GameControllerAddMapping( pUserMappings );
    1.58 -
    1.59 -				if ( pchNewLine )
    1.60 -					pUserMappings = pchNewLine + 1;
    1.61 -				else
    1.62 -					pUserMappings = NULL;
    1.63 -			}
    1.64 -			SDL_free(pTempMappings);
    1.65 -		}
    1.66 -	}
    1.67 +    SDL_GameControllerLoadHints();
    1.68  
    1.69  	/* watch for joy events and fire controller ones if needed */
    1.70  	SDL_AddEventWatch( SDL_GameControllerEventWatcher, NULL );
    1.71 +
    1.72  	return (0);
    1.73  }
    1.74  
    1.75 @@ -875,10 +878,11 @@
    1.76  		gamecontrollerlist = gamecontrollerlist->next;
    1.77      }
    1.78  
    1.79 -	// Find a Controller Mapping 
    1.80 +	// Find a controller mapping 
    1.81 +    // Check for hints in case someone has changed them recently
    1.82 +    SDL_GameControllerLoadHints();
    1.83  	pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
    1.84 -	if ( !pSupportedController )
    1.85 -	{
    1.86 +	if ( !pSupportedController ) {
    1.87  		SDL_SetError("Couldn't find mapping for device (%d)", device_index );
    1.88  		return (NULL);
    1.89  	}