sdl: handle joystick and gamepad api errors to avoid crashes

Fixes MINIDUMP-7K
This commit is contained in:
Flyinghead 2023-02-06 15:20:21 +01:00
parent 98531e07ea
commit a045c52146
1 changed files with 77 additions and 59 deletions

View File

@ -166,7 +166,16 @@ public:
SDLGamepad(int maple_port, int joystick_idx, SDL_Joystick* sdl_joystick)
: GamepadDevice(maple_port, "SDL"), sdl_joystick(sdl_joystick)
{
_name = SDL_JoystickName(sdl_joystick);
const char *joyName = SDL_JoystickName(sdl_joystick);
if (joyName == nullptr)
{
WARN_LOG(INPUT, "Can't get joystick %d name: %s", joystick_idx, SDL_GetError());
_name = "Joystick " + std::to_string(joystick_idx);
}
else
{
_name = joyName;
}
sdl_joystick_instance = SDL_JoystickInstanceID(sdl_joystick);
_unique_id = "sdl_joystick_" + std::to_string(sdl_joystick_instance);
INFO_LOG(INPUT, "SDL: Opened joystick %d on port %d: '%s' unique_id=%s", sdl_joystick_instance, maple_port, _name.c_str(), _unique_id.c_str());
@ -174,6 +183,12 @@ public:
if (SDL_IsGameController(joystick_idx))
{
sdl_controller = SDL_GameControllerOpen(joystick_idx);
if (sdl_controller == nullptr)
{
WARN_LOG(INPUT, "Can't open game controller %d: %s", joystick_idx, SDL_GetError());
}
else
{
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForAxis(sdl_controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
if (bind.bindType == SDL_CONTROLLER_BINDTYPE_AXIS)
leftTrigger = bind.value.axis;
@ -181,6 +196,7 @@ public:
if (bind.bindType == SDL_CONTROLLER_BINDTYPE_AXIS)
rightTrigger = bind.value.axis;
}
}
if (!find_mapping())
input_mapper = std::make_shared<DefaultInputMapping<>>(sdl_controller);
@ -273,6 +289,7 @@ public:
{ "paddle4", "Paddle 4" },
{ "touchpad", "Touchpad" },
};
if (sdl_controller != nullptr)
for (SDL_GameControllerButton button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button = (SDL_GameControllerButton)(button + 1))
{
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForButton(sdl_controller, button);
@ -336,6 +353,7 @@ public:
{ "righttrigger", "R2" },
};
if (sdl_controller != nullptr)
for (SDL_GameControllerAxis axis = SDL_CONTROLLER_AXIS_LEFTX; axis < SDL_CONTROLLER_AXIS_MAX; axis = (SDL_GameControllerAxis)(axis + 1))
{
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForAxis(sdl_controller, axis);