From 17b9f4a37d23797990dc6f8e1e7421da22ee1dab Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 8 Oct 2014 11:34:45 -0700 Subject: [PATCH 1/3] allow a call to Joysticks.reset to reset SDL while interface is running --- interface/src/devices/Joystick.cpp | 4 + interface/src/devices/Joystick.h | 3 + .../scripting/JoystickScriptingInterface.cpp | 91 ++++++++++++++----- .../scripting/JoystickScriptingInterface.h | 6 ++ 4 files changed, 79 insertions(+), 25 deletions(-) diff --git a/interface/src/devices/Joystick.cpp b/interface/src/devices/Joystick.cpp index d220a827f1..f636e47a42 100644 --- a/interface/src/devices/Joystick.cpp +++ b/interface/src/devices/Joystick.cpp @@ -29,6 +29,10 @@ Joystick::Joystick(const QString& name, SDL_Joystick* sdlJoystick) : #endif Joystick::~Joystick() { + closeJoystick(); +} + +void Joystick::closeJoystick() { #ifdef HAVE_SDL SDL_JoystickClose(_sdlJoystick); #endif diff --git a/interface/src/devices/Joystick.h b/interface/src/devices/Joystick.h index 228e993204..0335bf6af3 100644 --- a/interface/src/devices/Joystick.h +++ b/interface/src/devices/Joystick.h @@ -37,6 +37,9 @@ public: void update(); + void closeJoystick(); + void setSDLJoystick(SDL_Joystick* sdlJoystick) { _sdlJoystick = sdlJoystick; } + const QString& getName() const { return _name; } const QVector& getAxes() const { return _axes; } diff --git a/interface/src/scripting/JoystickScriptingInterface.cpp b/interface/src/scripting/JoystickScriptingInterface.cpp index 1e35c11f61..87f841c494 100644 --- a/interface/src/scripting/JoystickScriptingInterface.cpp +++ b/interface/src/scripting/JoystickScriptingInterface.cpp @@ -27,17 +27,10 @@ JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { JoystickScriptingInterface::JoystickScriptingInterface() : _openJoysticks(), - _availableDeviceNames() + _availableDeviceNames(), + _isInitialized(false) { -#ifdef HAVE_SDL - SDL_Init(SDL_INIT_JOYSTICK); - - int joystickCount = SDL_NumJoysticks(); - - for (int i = 0; i < joystickCount; i++) { - _availableDeviceNames << SDL_JoystickName(i); - } -#endif + reset(); } JoystickScriptingInterface::~JoystickScriptingInterface() { @@ -45,18 +38,53 @@ JoystickScriptingInterface::~JoystickScriptingInterface() { #ifdef HAVE_SDL SDL_Quit(); + _isInitialized = false; +#endif +} + +void JoystickScriptingInterface::reset() { +#ifdef HAVE_SDL + + if (_isInitialized) { + _isInitialized = false; + + // close all the open joysticks before we quit + foreach(Joystick* openJoystick, _openJoysticks) { + openJoystick->closeJoystick(); + } + + SDL_Quit(); + } + + bool initSuccess = (SDL_Init(SDL_INIT_JOYSTICK) == 0); + + if (initSuccess) { + + int joystickCount = SDL_NumJoysticks(); + + for (int i = 0; i < joystickCount; i++) { + _availableDeviceNames << SDL_JoystickName(i); + } + + foreach(const QString& joystickName, _openJoysticks.keys()) { + _openJoysticks[joystickName]->setSDLJoystick(openSDLJoystickWithName(joystickName)); + } + + _isInitialized = true; + } #endif } void JoystickScriptingInterface::update() { #ifdef HAVE_SDL - PerformanceTimer perfTimer("JoystickScriptingInterface::update"); - SDL_JoystickUpdate(); - - foreach(Joystick* joystick, _openJoysticks) { - joystick->update(); + if (_isInitialized) { + PerformanceTimer perfTimer("JoystickScriptingInterface::update"); + SDL_JoystickUpdate(); + + foreach(Joystick* joystick, _openJoysticks) { + joystick->update(); + } } - #endif } @@ -64,17 +92,13 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { Joystick* matchingJoystick = _openJoysticks.value(name); #ifdef HAVE_SDL if (!matchingJoystick) { - // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists - int joystickCount = SDL_NumJoysticks(); + SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name); - for (int i = 0; i < joystickCount; i++) { - if (SDL_JoystickName(i) == name) { - matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value(); - break; - } + if (openSDLJoystick) { + matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value(); + } else { + qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; } - - qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; } #endif @@ -82,3 +106,20 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { } +#ifdef HAVE_SDL + +SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString &name) { + // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists + int joystickCount = SDL_NumJoysticks(); + + for (int i = 0; i < joystickCount; i++) { + if (SDL_JoystickName(i) == name) { + return SDL_JoystickOpen(i); + break; + } + } + + return NULL; +} + +#endif diff --git a/interface/src/scripting/JoystickScriptingInterface.h b/interface/src/scripting/JoystickScriptingInterface.h index 98e38f5698..02624c70d5 100644 --- a/interface/src/scripting/JoystickScriptingInterface.h +++ b/interface/src/scripting/JoystickScriptingInterface.h @@ -31,13 +31,19 @@ public: public slots: Joystick* joystickWithName(const QString& name); + void reset(); private: +#ifdef HAVE_SDL + SDL_Joystick* openSDLJoystickWithName(const QString& name); +#endif + JoystickScriptingInterface(); ~JoystickScriptingInterface(); QMap _openJoysticks; QStringList _availableDeviceNames; + bool _isInitialized; }; #endif // hifi_JoystickScriptingInterface_h From 8e2d41f37fe79a02382ee858a3979090f8a28349 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 8 Oct 2014 11:41:07 -0700 Subject: [PATCH 2/3] remove deadzone handling from Joystick class --- interface/src/devices/Joystick.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/interface/src/devices/Joystick.cpp b/interface/src/devices/Joystick.cpp index f636e47a42..fe67eaceaa 100644 --- a/interface/src/devices/Joystick.cpp +++ b/interface/src/devices/Joystick.cpp @@ -42,14 +42,12 @@ void Joystick::update() { #ifdef HAVE_SDL // update our current values, emit a signal when there is a change for (int j = 0; j < getNumAxes(); j++) { - float value = glm::round(SDL_JoystickGetAxis(_sdlJoystick, j) + 0.5f) / std::numeric_limits::max(); - const float DEAD_ZONE = 0.1f; - float cleanValue = glm::abs(value) < DEAD_ZONE ? 0.0f : value; - - if (_axes[j] != cleanValue) { + float newValue = glm::round(SDL_JoystickGetAxis(_sdlJoystick, j) + 0.5f) / std::numeric_limits::max(); + + if (_axes[j] != newValue) { float oldValue = _axes[j]; - _axes[j] = cleanValue; - emit axisValueChanged(j, cleanValue, oldValue); + _axes[j] = newValue; + emit axisValueChanged(j, newValue, oldValue); } } for (int j = 0; j < getNumButtons(); j++) { From ce505c1b0cae9c75c76d39c34169987f100f6b48 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 8 Oct 2014 12:09:40 -0700 Subject: [PATCH 3/3] add a missing HAVE_SDL wrap on setter in Joystick --- interface/src/devices/Joystick.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/interface/src/devices/Joystick.h b/interface/src/devices/Joystick.h index 0335bf6af3..8343c20a04 100644 --- a/interface/src/devices/Joystick.h +++ b/interface/src/devices/Joystick.h @@ -38,7 +38,10 @@ public: void update(); void closeJoystick(); + +#ifdef HAVE_SDL void setSDLJoystick(SDL_Joystick* sdlJoystick) { _sdlJoystick = sdlJoystick; } +#endif const QString& getName() const { return _name; }