summaryrefslogtreecommitdiff
path: root/src/joystick.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/joystick.cpp')
-rw-r--r--src/joystick.cpp51
1 files changed, 32 insertions, 19 deletions
diff --git a/src/joystick.cpp b/src/joystick.cpp
index 1d3d5a147..3b88e5668 100644
--- a/src/joystick.cpp
+++ b/src/joystick.cpp
@@ -32,25 +32,6 @@
int Joystick::joystickCount = 0;
bool Joystick::mEnabled = false;
-void Joystick::init()
-{
- SDL_InitSubSystem(SDL_INIT_JOYSTICK);
- SDL_JoystickEventState(SDL_ENABLE);
- joystickCount = SDL_NumJoysticks();
- logger->log("%i joysticks/gamepads found", joystickCount);
- for (int i = 0; i < joystickCount; i++)
- logger->log("- %s", SDL_JoystickName(i));
-
- mEnabled = config.getBoolValue("joystickEnabled");
-
- if (Joystick::getNumberOfJoysticks() > 0)
- {
- joystick = new Joystick(config.getIntValue("selectedJoystick"));
- if (mEnabled)
- joystick->open();
- }
-}
-
Joystick::Joystick(const int no):
mDirection(0),
mJoystick(nullptr),
@@ -74,8 +55,34 @@ Joystick::~Joystick()
close();
}
+void Joystick::init()
+{
+ SDL_InitSubSystem(SDL_INIT_JOYSTICK);
+ SDL_JoystickEventState(SDL_ENABLE);
+ joystickCount = SDL_NumJoysticks();
+ logger->log("%i joysticks/gamepads found", joystickCount);
+ for (int i = 0; i < joystickCount; i++)
+ logger->log("- %s", SDL_JoystickName(i));
+
+ mEnabled = config.getBoolValue("joystickEnabled");
+
+ if (joystickCount > 0)
+ {
+ joystick = new Joystick(config.getIntValue("selectedJoystick"));
+ if (mEnabled)
+ joystick->open();
+ }
+}
+
bool Joystick::open()
{
+ if (mNumber >= joystickCount)
+ mNumber = joystickCount - 1;
+ if (mNumber < 0)
+ {
+ logger->log("error: incorrect joystick selection");
+ return false;
+ }
logger->log("open joystick %d", mNumber);
mJoystick = SDL_JoystickOpen(mNumber);
@@ -120,6 +127,12 @@ void Joystick::close()
}
}
+void Joystick::reload()
+{
+ joystickCount = SDL_NumJoysticks();
+ setNumber(mNumber);
+}
+
void Joystick::setNumber(const int n)
{
if (mJoystick)