summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorewewukek <ewewukek@gmail.com>2024-01-10 15:57:50 +0300
committerJesusalva Jesusalva <jesusalva@themanaworld.org>2024-02-13 02:22:12 +0000
commita38a1ed6fab0cb16e8c7e340a2230cf58db51d3c (patch)
treec73e08ba0a94e14dad1eba18d6f8fab57ee27ebd
parent1c3f0b8e325883ae3ccacdb7bf453daf6c133001 (diff)
downloadplus-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.tar.gz
plus-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.tar.bz2
plus-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.tar.xz
plus-a38a1ed6fab0cb16e8c7e340a2230cf58db51d3c.zip
Hotplug support for joysticks
-rw-r--r--src/gui/widgets/tabs/setup_joystick.cpp8
-rw-r--r--src/input/joystick.cpp33
-rw-r--r--src/input/joystick.h8
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
@@ -59,6 +59,11 @@ class Joystick final
static void init();
/**
+ * Detects joysticks and (re)connects.
+ */
+ static void detect();
+
+ /**
* Returns the number of available joysticks.
*/
static int getNumberOfJoysticks() A_WARN_UNUSED
@@ -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();