diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c index 2c47f2372..bd30029b9 100644 --- a/src/joystick/SDL_gamecontroller.c +++ b/src/joystick/SDL_gamecontroller.c @@ -125,6 +125,7 @@ struct _SDL_GameController int ref_count; const char *name; + ControllerMapping_t *mapping; int num_bindings; SDL_ExtendedGameControllerBind *bindings; SDL_ExtendedGameControllerBind **last_match_axis; @@ -740,6 +741,10 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) { SDL_JoystickGUID mapping_guid; + if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) { + continue; + } + SDL_memcpy(&mapping_guid, &mapping->guid, sizeof(mapping_guid)); if (!match_version) { SDL_SetJoystickGUIDVersion(&mapping_guid, 0); @@ -1070,19 +1075,20 @@ SDL_PrivateGameControllerParseControllerConfigString(SDL_GameController *gamecon /* * Make a new button mapping struct */ -static void SDL_PrivateLoadButtonMapping(SDL_GameController *gamecontroller, const char *pchName, const char *pchMapping) +static void SDL_PrivateLoadButtonMapping(SDL_GameController *gamecontroller, ControllerMapping_t *pControllerMapping) { int i; CHECK_GAMECONTROLLER_MAGIC(gamecontroller, ); - gamecontroller->name = pchName; + gamecontroller->name = pControllerMapping->name; gamecontroller->num_bindings = 0; + gamecontroller->mapping = pControllerMapping; if (gamecontroller->joystick->naxes) { SDL_memset(gamecontroller->last_match_axis, 0, gamecontroller->joystick->naxes * sizeof(*gamecontroller->last_match_axis)); } - SDL_PrivateGameControllerParseControllerConfigString(gamecontroller, pchMapping); + SDL_PrivateGameControllerParseControllerConfigString(gamecontroller, pControllerMapping->mapping); /* Set the zero point for triggers */ for (i = 0; i < gamecontroller->num_bindings; ++i) { @@ -1191,9 +1197,9 @@ static void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pContro { SDL_GameController *gamecontrollerlist = SDL_gamecontrollers; while (gamecontrollerlist) { - if (!SDL_memcmp(&gamecontrollerlist->joystick->guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) { + if (gamecontrollerlist->mapping == pControllerMapping) { /* Not really threadsafe. Should this lock access within SDL_GameControllerEventWatcher? */ - SDL_PrivateLoadButtonMapping(gamecontrollerlist, pControllerMapping->name, pControllerMapping->mapping); + SDL_PrivateLoadButtonMapping(gamecontrollerlist, pControllerMapping); { SDL_Event event; @@ -1755,7 +1761,7 @@ SDL_GameControllerMapping(SDL_GameController *gamecontroller) { CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL); - return SDL_GameControllerMappingForGUID(gamecontroller->joystick->guid); + return CreateMappingString(gamecontroller->mapping, gamecontroller->joystick->guid); } static void @@ -2126,7 +2132,7 @@ SDL_GameControllerOpen(int device_index) } } - SDL_PrivateLoadButtonMapping(gamecontroller, pSupportedController->name, pSupportedController->mapping); + SDL_PrivateLoadButtonMapping(gamecontroller, pSupportedController); /* Add the controller to list */ ++gamecontroller->ref_count;