Added SDL_GameControllerMappingForDeviceIndex() to get the mapping for a controller before it's opened

This commit is contained in:
Sam Lantinga 2018-03-07 13:30:40 -08:00
parent f0be85b8b3
commit b81da78518
7 changed files with 58 additions and 22 deletions

View file

@ -425,6 +425,12 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickG
}
pSupportedController = pSupportedController->next;
}
#if SDL_JOYSTICK_XINPUT
if (guid->data[14] == 'x') {
/* This is an XInput device */
return s_pXInputMapping;
}
#endif
return NULL;
}
@ -1033,11 +1039,6 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
name = SDL_JoystickNameForIndex(device_index);
guid = SDL_JoystickGetDeviceGUID(device_index);
mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
#if SDL_JOYSTICK_XINPUT
if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
mapping = s_pXInputMapping;
}
#endif
SDL_UnlockJoysticks();
return mapping;
}
@ -1374,6 +1375,40 @@ SDL_GameControllerNameForIndex(int device_index)
}
/**
* Get the mapping of a game controller.
* This can be called before any controllers are opened.
* If no mapping can be found, this function returns NULL.
*/
char *
SDL_GameControllerMappingForDeviceIndex(int joystick_index)
{
char *pMappingString = NULL;
ControllerMapping_t *mapping;
SDL_LockJoysticks();
mapping = SDL_PrivateGetControllerMapping(joystick_index);
if (mapping) {
SDL_JoystickGUID guid;
char pchGUID[33];
size_t needed;
guid = SDL_JoystickGetDeviceGUID(joystick_index);
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;
pMappingString = SDL_malloc(needed);
if (!pMappingString) {
SDL_OutOfMemory();
SDL_UnlockJoysticks();
return NULL;
}
SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
}
SDL_UnlockJoysticks();
return pMappingString;
}
/*
* Return 1 if the joystick with this name and GUID is a supported controller
*/