Fix SDL controllers not automatically reconnecting. Fixes #563

This commit is contained in:
Lior Halphon 2023-12-15 15:24:09 +02:00
parent d8905f57bf
commit fcf31be735
3 changed files with 13 additions and 2 deletions

View File

@ -1776,9 +1776,9 @@ static void enter_keyboard_menu(unsigned index)
}
static unsigned joypad_index = 0;
static SDL_Joystick *joystick = NULL;
static SDL_GameController *controller = NULL;
SDL_Haptic *haptic = NULL;
SDL_Joystick *joystick = NULL;
static const char *current_joypad_name(unsigned index)
{
@ -1830,7 +1830,8 @@ static void cycle_joypads(unsigned index)
}
if (joystick) {
haptic = SDL_HapticOpenFromJoystick(joystick);
}}
}
}
static void cycle_joypads_backwards(unsigned index)
{

View File

@ -21,6 +21,7 @@ extern SDL_Window *window;
extern SDL_Renderer *renderer;
extern SDL_Texture *texture;
extern SDL_PixelFormat *pixel_format;
extern SDL_Joystick *joystick;
extern SDL_Haptic *haptic;
extern shader_t shader;

View File

@ -249,6 +249,15 @@ static void handle_events(GB_gameboy_t *gb)
break;
}
case SDL_JOYDEVICEREMOVED:
if (joystick && event.jdevice.which == SDL_JoystickInstanceID(joystick)) {
SDL_JoystickClose(joystick);
joystick = NULL;
}
case SDL_JOYDEVICEADDED:
connect_joypad();
break;
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN: {
joypad_button_t button = get_joypad_button(event.jbutton.button);