1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-22 17:49:43 +01:00

Controller layout detection

This commit is contained in:
Ethan Lee 2023-08-22 09:52:53 -04:00
parent 8e3e29a14c
commit 7b40a052ed
3 changed files with 63 additions and 6 deletions

View file

@ -167,6 +167,44 @@ void BUTTONGLYPHS_keyboard_set_active(bool active)
keyboard_is_active = active;
}
void BUTTONGLYPHS_update_layout(Uint16 vendor, Uint16 product)
{
if (vendor == 0x054c)
{
layout = LAYOUT_PLAYSTATION;
}
else if (vendor == 0x28de)
{
layout = LAYOUT_DECK;
}
else if (vendor == 0x057e)
{
if (product == 0x2006)
{
layout = LAYOUT_NINTENDO_SWITCH_JOYCON_L;
}
else if (product == 0x2007)
{
layout = LAYOUT_NINTENDO_SWITCH_JOYCON_R;
}
else
{
layout = LAYOUT_NINTENDO_SWITCH_PRO;
}
}
else if (vendor == 0x2dc8) /* 8BitDo */
{
layout = LAYOUT_NINTENDO_SWITCH_PRO;
}
else
{
/* For now we assume Xbox (0x045e), Generic will be used when
* migrating to SDL_ActionSet
*/
layout = LAYOUT_XBOX;
}
}
const char* BUTTONGLYPHS_get_wasd_text(void)
{
/* Returns the string to use in Welcome Aboard */

View file

@ -17,6 +17,8 @@ bool BUTTONGLYPHS_keyboard_is_available(void);
bool BUTTONGLYPHS_keyboard_is_active(void);
void BUTTONGLYPHS_keyboard_set_active(bool active);
void BUTTONGLYPHS_update_layout(Uint16 vendor, Uint16 product);
const char* BUTTONGLYPHS_get_wasd_text(void);
const char* BUTTONGLYPHS_get_button(ActionSet actionset, Action action, int binding);

View file

@ -144,6 +144,7 @@ void KeyPoll::Poll(void)
bool hidemouse = false;
bool altpressed = false;
bool fullscreenkeybind = false;
SDL_GameController *controller = NULL;
SDL_Event evt;
while (SDL_PollEvent(&evt))
{
@ -276,6 +277,12 @@ void KeyPoll::Poll(void)
case SDL_CONTROLLERBUTTONDOWN:
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true;
BUTTONGLYPHS_keyboard_set_active(false);
controller = controllers[evt.cbutton.which];
BUTTONGLYPHS_update_layout(
SDL_GameControllerGetVendor(controller),
SDL_GameControllerGetProduct(controller)
);
break;
case SDL_CONTROLLERBUTTONUP:
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false;
@ -309,26 +316,36 @@ void KeyPoll::Poll(void)
break;
}
BUTTONGLYPHS_keyboard_set_active(false);
controller = controllers[evt.caxis.which];
BUTTONGLYPHS_update_layout(
SDL_GameControllerGetVendor(controller),
SDL_GameControllerGetProduct(controller)
);
break;
}
case SDL_CONTROLLERDEVICEADDED:
{
SDL_GameController *toOpen = SDL_GameControllerOpen(evt.cdevice.which);
controller = SDL_GameControllerOpen(evt.cdevice.which);
vlog_info(
"Opened SDL_GameController ID #%i, %s",
evt.cdevice.which,
SDL_GameControllerName(toOpen)
SDL_GameControllerName(controller)
);
controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen;
controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller))] = controller;
BUTTONGLYPHS_keyboard_set_active(false);
BUTTONGLYPHS_update_layout(
SDL_GameControllerGetVendor(controller),
SDL_GameControllerGetProduct(controller)
);
break;
}
case SDL_CONTROLLERDEVICEREMOVED:
{
SDL_GameController *toClose = controllers[evt.cdevice.which];
controller = controllers[evt.cdevice.which];
controllers.erase(evt.cdevice.which);
vlog_info("Closing %s", SDL_GameControllerName(toClose));
SDL_GameControllerClose(toClose);
vlog_info("Closing %s", SDL_GameControllerName(controller));
SDL_GameControllerClose(controller);
if (controllers.empty())
{
BUTTONGLYPHS_keyboard_set_active(true);