SDLControllerInterface: Fix crash on shutdown

This commit is contained in:
Connor McLaughlin 2020-03-22 12:37:11 +10:00
parent 8190efa5dc
commit 46f921fb95
2 changed files with 8 additions and 14 deletions

View File

@ -36,16 +36,16 @@ bool SDLControllerInterface::Initialize(CommonHostInterface* host_interface)
void SDLControllerInterface::Shutdown() void SDLControllerInterface::Shutdown()
{ {
ControllerInterface::Shutdown();
while (!m_controllers.empty()) while (!m_controllers.empty())
CloseGameController(m_controllers.begin()->joystick_id); CloseGameController(m_controllers.begin()->joystick_id, false);
if (m_sdl_subsystem_initialized) if (m_sdl_subsystem_initialized)
{ {
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC); SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);
m_sdl_subsystem_initialized = false; m_sdl_subsystem_initialized = false;
} }
ControllerInterface::Shutdown();
} }
void SDLControllerInterface::PollEvents() void SDLControllerInterface::PollEvents()
@ -74,7 +74,7 @@ bool SDLControllerInterface::ProcessSDLEvent(const SDL_Event* event)
case SDL_CONTROLLERDEVICEREMOVED: case SDL_CONTROLLERDEVICEREMOVED:
{ {
Log_InfoPrintf("Controller %d removed", event->cdevice.which); Log_InfoPrintf("Controller %d removed", event->cdevice.which);
CloseGameController(event->cdevice.which); CloseGameController(event->cdevice.which, true);
return true; return true;
} }
@ -153,13 +153,7 @@ bool SDLControllerInterface::OpenGameController(int index)
return true; return true;
} }
void SDLControllerInterface::CloseGameControllers() bool SDLControllerInterface::CloseGameController(int joystick_index, bool notify)
{
while (!m_controllers.empty())
CloseGameController(m_controllers.begin()->player_id);
}
bool SDLControllerInterface::CloseGameController(int joystick_index)
{ {
auto it = GetControllerDataForJoystickId(joystick_index); auto it = GetControllerDataForJoystickId(joystick_index);
if (it == m_controllers.end()) if (it == m_controllers.end())
@ -173,7 +167,8 @@ bool SDLControllerInterface::CloseGameController(int joystick_index)
SDL_GameControllerClose(static_cast<SDL_GameController*>(it->controller)); SDL_GameControllerClose(static_cast<SDL_GameController*>(it->controller));
m_controllers.erase(it); m_controllers.erase(it);
OnControllerDisconnected(player_index); if (notify)
OnControllerDisconnected(player_index);
return true; return true;
} }

View File

@ -52,8 +52,7 @@ private:
ControllerDataVector::iterator GetControllerDataForPlayerId(int id); ControllerDataVector::iterator GetControllerDataForPlayerId(int id);
bool OpenGameController(int index); bool OpenGameController(int index);
bool CloseGameController(int joystick_index); bool CloseGameController(int joystick_index, bool notify);
void CloseGameControllers();
bool HandleControllerAxisEvent(const SDL_Event* event); bool HandleControllerAxisEvent(const SDL_Event* event);
bool HandleControllerButtonEvent(const SDL_Event* event); bool HandleControllerButtonEvent(const SDL_Event* event);