Merge pull request #3560 from birarda/xbox-controller

allow a call to Joysticks.reset to reset SDL while interface is running
This commit is contained in:
Brad Hefta-Gaub 2014-10-08 17:06:12 -07:00
commit 80e82c95ca
4 changed files with 87 additions and 32 deletions

View file

@ -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
@ -38,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<short>::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<short>::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++) {

View file

@ -37,6 +37,12 @@ public:
void update();
void closeJoystick();
#ifdef HAVE_SDL
void setSDLJoystick(SDL_Joystick* sdlJoystick) { _sdlJoystick = sdlJoystick; }
#endif
const QString& getName() const { return _name; }
const QVector<float>& getAxes() const { return _axes; }

View file

@ -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

View file

@ -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<QString, Joystick*> _openJoysticks;
QStringList _availableDeviceNames;
bool _isInitialized;
};
#endif // hifi_JoystickScriptingInterface_h