diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c index 1cf8c2e417..410c985d46 100644 --- a/src/joystick/SDL_gamecontroller.c +++ b/src/joystick/SDL_gamecontroller.c @@ -688,16 +688,14 @@ static ControllerMapping_t *SDL_CreateMappingForWGIController(SDL_JoystickGUID g /* * Helper function to scan the mappings database for a controller with the specified GUID */ -static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool match_crc, SDL_bool match_version) +static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool match_version) { - ControllerMapping_t *mapping; + ControllerMapping_t *mapping, *best_match = NULL; Uint16 crc = 0; SDL_AssertJoysticksLocked(); - if (match_crc) { - SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc); - } + SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc); /* Clear the CRC from the GUID for matching, the mappings never include it in the GUID */ SDL_SetJoystickGUIDCRC(&guid, 0); @@ -719,20 +717,26 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic } if (SDL_memcmp(&guid, &mapping_guid, sizeof(guid)) == 0) { - Uint16 mapping_crc = 0; + const char *crc_string = SDL_strstr(mapping->mapping, SDL_CONTROLLER_CRC_FIELD); + if (crc_string) { + Uint16 mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16); - if (match_crc) { - const char *crc_string = SDL_strstr(mapping->mapping, SDL_CONTROLLER_CRC_FIELD); - if (crc_string) { - mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16); + if (mapping_crc != crc) { + /* This mapping specified a CRC and they don't match */ + continue; } - } - if (crc == mapping_crc) { + + /* An exact match, including CRC */ return mapping; } + + + if (!best_match) { + best_match = mapping; + } } } - return NULL; + return best_match; } /* @@ -741,19 +745,8 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool adding_mapping) { ControllerMapping_t *mapping; - Uint16 vendor, product, crc; - SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, &crc); - if (crc) { - /* First check for exact CRC matching */ - mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, SDL_TRUE); - if (mapping) { - return mapping; - } - } - - /* Now check for a mapping without CRC */ - mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_TRUE); + mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE); if (mapping) { return mapping; } @@ -767,14 +760,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickG if (SDL_JoystickGUIDUsesVersion(guid)) { /* Try again, ignoring the version */ - if (crc) { - mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, SDL_FALSE); - if (mapping) { - return mapping; - } - } - - mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_FALSE); + mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE); if (mapping) { return mapping; }