SDLControllerInterface: Fix broken input when player IDs clash

This commit is contained in:
Connor McLaughlin 2020-04-11 19:36:53 +10:00
parent 4fd5d6ca4f
commit e843fcd9f0
2 changed files with 24 additions and 5 deletions

View file

@ -109,6 +109,23 @@ SDLControllerInterface::ControllerDataVector::iterator SDLControllerInterface::G
[id](const ControllerData& cd) { return cd.player_id == id; });
}
int SDLControllerInterface::GetFreePlayerId() const
{
for (int player_id = 0;; player_id++)
{
size_t i;
for (i = 0; i < m_controllers.size(); i++)
{
if (m_controllers[i].player_id == player_id)
break;
}
if (i == m_controllers.size())
return player_id;
}
return 0;
}
bool SDLControllerInterface::OpenGameController(int index)
{
SDL_GameController* gcontroller = SDL_GameControllerOpen(index);
@ -128,12 +145,13 @@ bool SDLControllerInterface::OpenGameController(int index)
#else
int player_id = -1;
#endif
if (player_id < 0)
if (player_id < 0 || GetControllerDataForPlayerId(player_id) != m_controllers.end())
{
Log_WarningPrintf("Controller %d (joystick %d) returned player ID %d. Setting to zero, but this may cause issues "
"if you try to use multiple controllers.",
index, joystick_id, player_id);
player_id = 0;
const int free_player_id = GetFreePlayerId();
Log_WarningPrintf(
"Controller %d (joystick %d) returned player ID %d, which is invalid or in use. Using ID %d instead.", index,
joystick_id, player_id, free_player_id);
player_id = free_player_id;
}
Log_InfoPrintf("Opened controller %d (instance id %d, player id %d): %s", index, joystick_id, player_id,

View file

@ -50,6 +50,7 @@ private:
ControllerDataVector::iterator GetControllerDataForController(void* controller);
ControllerDataVector::iterator GetControllerDataForJoystickId(int id);
ControllerDataVector::iterator GetControllerDataForPlayerId(int id);
int GetFreePlayerId() const;
bool OpenGameController(int index);
bool CloseGameController(int joystick_index, bool notify);