From fcf31be7357ba15acc21417817d09e7c8d737173 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 15 Dec 2023 15:24:09 +0200 Subject: [PATCH] Fix SDL controllers not automatically reconnecting. Fixes #563 --- SDL/gui.c | 5 +++-- SDL/gui.h | 1 + SDL/main.c | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 87deb2fad..a4928406a 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -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) { diff --git a/SDL/gui.h b/SDL/gui.h index 52091bc45..8f7cf03a3 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -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; diff --git a/SDL/main.c b/SDL/main.c index 08bc17dd3..d4dd9b723 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -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);