From 65325e2b34f68be94398df393e6156183a797590 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sat, 4 Apr 2020 19:50:52 +0100 Subject: [PATCH] Add hotplugging support to SDL2 backend --- src/Backends/GLFW3/Misc.cpp | 5 +- src/Backends/SDL2/Controller.cpp | 78 ++++++++++++++++---------------- src/Backends/SDL2/Misc.cpp | 14 ++++-- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/Backends/GLFW3/Misc.cpp b/src/Backends/GLFW3/Misc.cpp index 90ed9099..f3034d8a 100644 --- a/src/Backends/GLFW3/Misc.cpp +++ b/src/Backends/GLFW3/Misc.cpp @@ -1,4 +1,5 @@ #include "../Platform.h" +#include "Platform.h" #include #include @@ -9,11 +10,9 @@ #include -#include "../Rendering.h" - #include "../../WindowsWrapper.h" -#include "Platform.h" +#include "../Rendering.h" #include "../../Main.h" #include "../../Organya.h" #include "../../Profile.h" diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index 288165e0..ace9d913 100644 --- a/src/Backends/SDL2/Controller.cpp +++ b/src/Backends/SDL2/Controller.cpp @@ -1,4 +1,5 @@ #include "../Controller.h" +#include "Controller.h" #include #include @@ -11,23 +12,12 @@ static SDL_Joystick *joystick; static int joystick_neutral_x; static int joystick_neutral_y; +void ControllerBackend_JoystickCallback(int joystick_id, BOOL connected); + BOOL ControllerBackend_Init(void) { SDL_InitSubSystem(SDL_INIT_JOYSTICK); - for (int i = 0; i < SDL_NumJoysticks(); ++i) - { - printf("Joystick #%d connected - %s\n", i, SDL_JoystickNameForIndex(i)); - - if (joystick == NULL) - { - joystick = SDL_JoystickOpen(i); - - if (joystick != NULL) - printf("Joystick #%d selected\n", i); - } - } - return TRUE; } @@ -53,35 +43,20 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) numButtons = 32; // Read whatever buttons actually exist - int i = 0; - for (; i < numButtons; ++i) - { - if (SDL_JoystickGetButton(joystick, i) != 0) - status->bButton[i] = TRUE; - else - status->bButton[i] = FALSE; - } + for (int i = 0; i < numButtons; ++i) + status->bButton[i] = SDL_JoystickGetButton(joystick, i); // Blank the buttons that do not - for (; i < 32; ++i) + for (int i = numButtons; i < 32; ++i) status->bButton[i] = FALSE; - status->bDown = FALSE; - status->bRight = FALSE; - status->bUp = FALSE; - status->bLeft = FALSE; - const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0); - if (joystick_x < joystick_neutral_x - 10000) - status->bLeft = TRUE; - else if (joystick_x > joystick_neutral_x + 10000) - status->bRight = TRUE; - const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1); - if (joystick_y < joystick_neutral_y - 10000) - status->bUp = TRUE; - else if (joystick_y > joystick_neutral_y + 10000) - status->bDown = TRUE; + + status->bLeft = joystick_x < joystick_neutral_x - 10000; + status->bRight = joystick_x > joystick_neutral_x + 10000; + status->bUp = joystick_y < joystick_neutral_y - 10000; + status->bDown = joystick_y > joystick_neutral_y + 10000; return TRUE; } @@ -91,8 +66,35 @@ BOOL ControllerBackend_ResetJoystickStatus(void) if (joystick == NULL) return FALSE; - joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0); - joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1); + // The code that would normally run here has been moved to JoystickCallback, to better-support hotplugging return TRUE; } + +void ControllerBackend_JoystickConnect(Sint32 joystick_id) +{ + printf("Joystick #%d connected - %s\n", joystick_id, SDL_JoystickNameForIndex(joystick_id)); + + if (joystick == NULL) + { + joystick = SDL_JoystickOpen(joystick_id); + + if (joystick != NULL) + { + printf("Joystick #%d selected\n", joystick_id); + + // Reset default stick positions (this is performed in ResetJoystickStatus in vanilla Cave Story + joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0); + joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1); + } + } +} + +void ControllerBackend_JoystickDisconnect(Sint32 joystick_id) +{ + if (joystick_id == SDL_JoystickInstanceID(joystick)) + { + printf("Joystick #%d disconnected\n", joystick_id); + joystick = NULL; + } +} diff --git a/src/Backends/SDL2/Misc.cpp b/src/Backends/SDL2/Misc.cpp index dca9a6c7..827138c7 100644 --- a/src/Backends/SDL2/Misc.cpp +++ b/src/Backends/SDL2/Misc.cpp @@ -1,4 +1,5 @@ #include "../Platform.h" +#include "Platform.h" #include #include @@ -6,11 +7,10 @@ #include "SDL.h" -#include "../Rendering.h" - #include "../../WindowsWrapper.h" -#include "Platform.h" +#include "Controller.h" +#include "../Rendering.h" #include "../../Main.h" #include "../../Organya.h" #include "../../Profile.h" @@ -196,6 +196,14 @@ BOOL PlatformBackend_SystemTask(void) break; + case SDL_JOYDEVICEADDED: + ControllerBackend_JoystickConnect(event.jdevice.which); + break; + + case SDL_JOYDEVICEREMOVED: + ControllerBackend_JoystickDisconnect(event.jdevice.which); + break; + case SDL_DROPFILE: LoadProfile(event.drop.file); SDL_free(event.drop.file);