Fixed bug 4854 - Add a way to extend the values in controller_type.h
authorSam Lantinga <slouken@libsdl.org>
Mon, 02 Mar 2020 10:37:28 -0800
changeset 135729d16201f3a3f
parent 13571 b2b43ee5f1bf
child 13573 31443025238f
Fixed bug 4854 - Add a way to extend the values in controller_type.h

Added a hint SDL_HINT_GAMECONTROLLERTYPE to allow overriding the built-in controller type database
include/SDL_hints.h
src/joystick/controller_type.h
     1.1 --- a/include/SDL_hints.h	Mon Mar 02 09:46:37 2020 -0800
     1.2 +++ b/include/SDL_hints.h	Mon Mar 02 10:37:28 2020 -0800
     1.3 @@ -469,6 +469,24 @@
     1.4  #define SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING "SDL_XINPUT_USE_OLD_JOYSTICK_MAPPING"
     1.5  
     1.6  /**
     1.7 + *  \brief  A variable that overrides the automatic controller type detection
     1.8 + *
     1.9 + *  The variable should be comma separated entries, in the form: VID/PID=type
    1.10 + *
    1.11 + *  The VID and PID should be hexadecimal with exactly 4 digits, e.g. 0x00fd
    1.12 + *
    1.13 + *  The type should be one of:
    1.14 + *      Xbox360
    1.15 + *      XboxOne
    1.16 + *      PS3
    1.17 + *      PS4
    1.18 + *      SwitchPro
    1.19 + *
    1.20 + *  This hint affects what driver is used, and must be set before calling SDL_Init(SDL_INIT_GAMECONTROLLER)
    1.21 + */
    1.22 +#define SDL_HINT_GAMECONTROLLERTYPE "SDL_GAMECONTROLLERTYPE"
    1.23 +
    1.24 +/**
    1.25   *  \brief  A variable that lets you manually hint extra gamecontroller db entries.
    1.26   *
    1.27   *  The variable should be newline delimited rows of gamecontroller config data, see SDL_gamecontroller.h
     2.1 --- a/src/joystick/controller_type.h	Mon Mar 02 09:46:37 2020 -0800
     2.2 +++ b/src/joystick/controller_type.h	Mon Mar 02 10:37:28 2020 -0800
     2.3 @@ -540,6 +540,30 @@
     2.4  	{ MAKE_CONTROLLER_ID( 0x28de, 0x1202 ), k_eControllerType_SteamControllerV2, NULL },	// Valve Bluetooth Steam Controller (HEADCRAB)
     2.5  };
     2.6  
     2.7 +static SDL_INLINE const char *GetControllerTypeOverride( int nVID, int nPID )
     2.8 +{
     2.9 +	const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERTYPE);
    2.10 +	if (hint) {
    2.11 +		char key[32];
    2.12 +		const char *spot = NULL;
    2.13 +
    2.14 +		SDL_snprintf(key, sizeof(key), "0x%.4x/0x%.4x=", nVID, nPID);
    2.15 +		spot = SDL_strstr(hint, key);
    2.16 +		if (!spot) {
    2.17 +			SDL_snprintf(key, sizeof(key), "0x%.4X/0x%.4X=", nVID, nPID);
    2.18 +			spot = SDL_strstr(hint, key);
    2.19 +		}
    2.20 +		if (spot) {
    2.21 +			spot += SDL_strlen(key);
    2.22 +			if (SDL_strncmp(spot, "k_eControllerType_", 18) == 0) {
    2.23 +				spot += 18;
    2.24 +			}
    2.25 +			return spot;
    2.26 +		}
    2.27 +	}
    2.28 +	return NULL;
    2.29 +}
    2.30 +
    2.31  static SDL_INLINE EControllerType GuessControllerType( int nVID, int nPID )
    2.32  {
    2.33  #if 0//def _DEBUG
    2.34 @@ -565,6 +589,37 @@
    2.35  
    2.36  	unsigned int unDeviceID = MAKE_CONTROLLER_ID( nVID, nPID );
    2.37  	int iIndex;
    2.38 +
    2.39 +	const char *pszOverride = GetControllerTypeOverride( nVID, nPID );
    2.40 +	if ( pszOverride )
    2.41 +	{
    2.42 +		if ( SDL_strncasecmp( pszOverride, "Xbox360", 7 ) == 0 )
    2.43 +		{
    2.44 +			return k_eControllerType_XBox360Controller;
    2.45 +		}
    2.46 +		if ( SDL_strncasecmp( pszOverride, "XboxOne", 7 ) == 0 )
    2.47 +		{
    2.48 +			return k_eControllerType_XBoxOneController;
    2.49 +		}
    2.50 +		if ( SDL_strncasecmp( pszOverride, "PS3", 3 ) == 0 )
    2.51 +		{
    2.52 +			return k_eControllerType_PS3Controller;
    2.53 +		}
    2.54 +		if ( SDL_strncasecmp( pszOverride, "PS4", 3 ) == 0 )
    2.55 +		{
    2.56 +			return k_eControllerType_PS4Controller;
    2.57 +		}
    2.58 +		if ( SDL_strncasecmp( pszOverride, "SwitchPro", 9 ) == 0 )
    2.59 +		{
    2.60 +			return k_eControllerType_SwitchProController;
    2.61 +		}
    2.62 +		if ( SDL_strncasecmp( pszOverride, "Steam", 5 ) == 0 )
    2.63 +		{
    2.64 +			return k_eControllerType_SteamController;
    2.65 +		}
    2.66 +		return k_eControllerType_UnknownNonSteamController;
    2.67 +	}
    2.68 +
    2.69  	for ( iIndex = 0; iIndex < sizeof( arrControllers ) / sizeof( arrControllers[0] ); ++iIndex )
    2.70  	{
    2.71  		if ( unDeviceID == arrControllers[ iIndex ].m_unDeviceID )