Improved handling of multiple connected joysticks, prefer SDL_GameControllers.

This commit is contained in:
David Salvisberg 2015-02-27 01:48:55 +01:00
parent e1ca17f1f0
commit 733a7fadda
3 changed files with 49 additions and 6 deletions

View File

@ -286,11 +286,14 @@ void EventThread::process(RGSSThreadData &rtData)
break;
case SDL_JOYDEVICEADDED :
if (event.jdevice.which > 0)
if (SDL_JoystickGetAttached(js))
break;
if (SDL_IsGameController(0))
rtData.gamecontroller = SDL_GameControllerOpen(0);
if (SDL_IsGameController(event.jdevice.which))
{
rtData.gamecontroller = SDL_GameControllerOpen(event.jdevice.which);
rtData.gamecontrollerIndex = event.jdevice.which;
}
if (rtData.gamecontroller != NULL)
{
js = SDL_GameControllerGetJoystick(rtData.gamecontroller);
@ -298,14 +301,44 @@ void EventThread::process(RGSSThreadData &rtData)
* the user hasn't set a custom set of keybinds yet */
rtData.bindingUpdateMsg.post(loadBindings(rtData.config, rtData.gamecontroller));
}
else
else if (event.jdevice.which == 0)
{
js = SDL_JoystickOpen(0);
rtData.gamecontrollerIndex = 0;
}
break;
case SDL_JOYDEVICEREMOVED :
if (SDL_JoystickInstanceID(js) != event.jdevice.which)
break;
/* clean up the connected controller and joy device if it was the one we were tracking */
if (rtData.gamecontroller != NULL && SDL_GameControllerGetAttached(rtData.gamecontroller))
SDL_GameControllerClose(rtData.gamecontroller);
else if (SDL_JoystickGetAttached(js))
SDL_JoystickClose(js);
resetInputStates();
/* If there are still joysticks/gamepads around make sure to connect them */
js = NULL;
rtData.gamecontroller = NULL;
rtData.gamecontrollerIndex = 0;
for (int i = 0; i < SDL_NumJoysticks(); i++)
{
if (SDL_IsGameController(i))
{
rtData.gamecontroller = SDL_GameControllerOpen(i);
rtData.gamecontrollerIndex = i;
break;
}
}
if (rtData.gamecontroller != NULL)
{
js = SDL_GameControllerGetJoystick(rtData.gamecontroller);
rtData.bindingUpdateMsg.post(loadBindings(rtData.config, rtData.gamecontroller));
}
else if (SDL_NumJoysticks() > 0)
js = SDL_JoystickOpen(0);
break;
case SDL_MOUSEBUTTONDOWN :

View File

@ -233,6 +233,7 @@ struct RGSSThreadData
Config config;
SDL_GameController *gamecontroller;
int gamecontrollerIndex;
std::string rgssErrorMsg;

View File

@ -284,8 +284,17 @@ int main(int argc, char *argv[])
SDL_GameControllerAddMappingsFromRW(controllerDB, 1);
rtData.gamecontroller = NULL;
if (SDL_NumJoysticks() > 0 && SDL_IsGameController(0))
rtData.gamecontroller = SDL_GameControllerOpen(0);
rtData.gamecontrollerIndex = 0;
/* Add the first index which is a valid GameController if there is one */
for (int i = 0; i < SDL_NumJoysticks(); i++)
{
if (SDL_IsGameController(i))
{
rtData.gamecontroller = SDL_GameControllerOpen(i);
rtData.gamecontrollerIndex = i;
break;
}
}
/* Load and post key bindings */
rtData.bindingUpdateMsg.post(loadBindings(conf, rtData.gamecontroller));