Skip to content

Commit

Permalink
Fix SDL controllers not automatically reconnecting. Fixes #563
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Dec 15, 2023
1 parent d8905f5 commit fcf31be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions SDL/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -1830,7 +1830,8 @@ static void cycle_joypads(unsigned index)
}
if (joystick) {
haptic = SDL_HapticOpenFromJoystick(joystick);
}}
}
}

static void cycle_joypads_backwards(unsigned index)
{
Expand Down
1 change: 1 addition & 0 deletions SDL/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions SDL/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fcf31be

Please sign in to comment.