From a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c Mon Sep 17 00:00:00 2001 From: ewewukek Date: Wed, 10 Jan 2024 15:57:50 +0300 Subject: Hotplug support for joysticks --- src/gui/widgets/tabs/setup_joystick.cpp | 8 ++++---- src/input/joystick.cpp | 33 ++++++++++++++++++++++++--------- src/input/joystick.h | 8 ++++++-- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/gui/widgets/tabs/setup_joystick.cpp b/src/gui/widgets/tabs/setup_joystick.cpp index 8215390de..ab623b901 100644 --- a/src/gui/widgets/tabs/setup_joystick.cpp +++ b/src/gui/widgets/tabs/setup_joystick.cpp @@ -123,12 +123,12 @@ void Setup_Joystick::action(const ActionEvent &event) } else if (source == mDetectButton) { + Joystick::detect(); + Joystick::getNames(mNamesModel->getNames()); if (joystick != nullptr) - { - joystick->reload(); - Joystick::getNames(mNamesModel->getNames()); mNamesDropDown->setSelected(joystick->getNumber()); - } + else + mNamesDropDown->setSelected(0); } else { 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) diff --git a/src/input/joystick.h b/src/input/joystick.h index 35c114d82..a3c129950 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -58,6 +58,11 @@ class Joystick final */ static void init(); + /** + * Detects joysticks and (re)connects. + */ + static void detect(); + /** * Returns the number of available joysticks. */ @@ -136,8 +141,6 @@ class Joystick final void resetRepeat(const int key); - void reload(); - protected: unsigned char mDirection; @@ -166,6 +169,7 @@ class Joystick final * Is joystick support enabled. */ static bool mEnabled; + static bool mInitialized; static int joystickCount; void doCalibration(); -- cgit v1.2.3-60-g2f50