diff options
author | ewewukek <ewewukek@gmail.com> | 2024-01-10 15:57:50 +0300 |
---|---|---|
committer | Jesusalva Jesusalva <jesusalva@themanaworld.org> | 2024-02-13 02:22:12 +0000 |
commit | a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c (patch) | |
tree | c73e08ba0a94e14dad1eba18d6f8fab57ee27ebd /src/input/joystick.cpp | |
parent | 1c3f0b8e325883ae3ccacdb7bf453daf6c133001 (diff) | |
download | mv-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.tar.gz mv-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.tar.bz2 mv-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.tar.xz mv-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.zip |
Hotplug support for joysticks
Diffstat (limited to 'src/input/joystick.cpp')
-rw-r--r-- | src/input/joystick.cpp | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 9c45b1e63..52d9a7525 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -43,6 +43,7 @@ PRAGMA48(GCC diagnostic pop) Joystick *joystick = nullptr; int Joystick::joystickCount = 0; bool Joystick::mEnabled = false; +bool Joystick::mInitialized = false; Joystick::Joystick(const int no) : mDirection(0), @@ -72,21 +73,41 @@ Joystick::~Joystick() void Joystick::init() { - SDL_InitSubSystem(SDL_INIT_JOYSTICK); // +++ possible to use SDL_EventState with different joystick features. SDL_JoystickEventState(SDL_ENABLE); + mEnabled = config.getBoolValue("joystickEnabled"); + detect(); +} + +void Joystick::detect() +{ + // close current joystick device + if (joystick != nullptr) + { + delete joystick; + joystick = nullptr; + } + + // need to completely reinitialize joystick subsystem + // because SDL does not support hotplugging + if (mInitialized) + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + mInitialized = true; + joystickCount = SDL_NumJoysticks(); logger->log("%i joysticks/gamepads found", joystickCount); for (int i = 0; i < joystickCount; i++) logger->log("- %s", SDL_JoystickNameForIndex(i)); - mEnabled = config.getBoolValue("joystickEnabled"); - if (joystickCount > 0) { joystick = new Joystick(config.getIntValue("selectedJoystick")); if (mEnabled) + { joystick->open(); + joystick->update(); + } } } @@ -207,12 +228,6 @@ void Joystick::close() } } -void Joystick::reload() -{ - joystickCount = SDL_NumJoysticks(); - setNumber(mNumber); -} - void Joystick::setNumber(const int n) { if (mJoystick != nullptr) |