mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 12:57:59 +02:00
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:
commit
80e82c95ca
4 changed files with 87 additions and 32 deletions
|
@ -29,6 +29,10 @@ Joystick::Joystick(const QString& name, SDL_Joystick* sdlJoystick) :
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Joystick::~Joystick() {
|
Joystick::~Joystick() {
|
||||||
|
closeJoystick();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Joystick::closeJoystick() {
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
SDL_JoystickClose(_sdlJoystick);
|
SDL_JoystickClose(_sdlJoystick);
|
||||||
#endif
|
#endif
|
||||||
|
@ -38,14 +42,12 @@ void Joystick::update() {
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
// update our current values, emit a signal when there is a change
|
// update our current values, emit a signal when there is a change
|
||||||
for (int j = 0; j < getNumAxes(); j++) {
|
for (int j = 0; j < getNumAxes(); j++) {
|
||||||
float value = glm::round(SDL_JoystickGetAxis(_sdlJoystick, j) + 0.5f) / std::numeric_limits<short>::max();
|
float newValue = 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] != newValue) {
|
||||||
|
|
||||||
if (_axes[j] != cleanValue) {
|
|
||||||
float oldValue = _axes[j];
|
float oldValue = _axes[j];
|
||||||
_axes[j] = cleanValue;
|
_axes[j] = newValue;
|
||||||
emit axisValueChanged(j, cleanValue, oldValue);
|
emit axisValueChanged(j, newValue, oldValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int j = 0; j < getNumButtons(); j++) {
|
for (int j = 0; j < getNumButtons(); j++) {
|
||||||
|
|
|
@ -37,6 +37,12 @@ public:
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
void closeJoystick();
|
||||||
|
|
||||||
|
#ifdef HAVE_SDL
|
||||||
|
void setSDLJoystick(SDL_Joystick* sdlJoystick) { _sdlJoystick = sdlJoystick; }
|
||||||
|
#endif
|
||||||
|
|
||||||
const QString& getName() const { return _name; }
|
const QString& getName() const { return _name; }
|
||||||
|
|
||||||
const QVector<float>& getAxes() const { return _axes; }
|
const QVector<float>& getAxes() const { return _axes; }
|
||||||
|
|
|
@ -27,17 +27,10 @@ JoystickScriptingInterface& JoystickScriptingInterface::getInstance() {
|
||||||
|
|
||||||
JoystickScriptingInterface::JoystickScriptingInterface() :
|
JoystickScriptingInterface::JoystickScriptingInterface() :
|
||||||
_openJoysticks(),
|
_openJoysticks(),
|
||||||
_availableDeviceNames()
|
_availableDeviceNames(),
|
||||||
|
_isInitialized(false)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_SDL
|
reset();
|
||||||
SDL_Init(SDL_INIT_JOYSTICK);
|
|
||||||
|
|
||||||
int joystickCount = SDL_NumJoysticks();
|
|
||||||
|
|
||||||
for (int i = 0; i < joystickCount; i++) {
|
|
||||||
_availableDeviceNames << SDL_JoystickName(i);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JoystickScriptingInterface::~JoystickScriptingInterface() {
|
JoystickScriptingInterface::~JoystickScriptingInterface() {
|
||||||
|
@ -45,18 +38,53 @@ JoystickScriptingInterface::~JoystickScriptingInterface() {
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
SDL_Quit();
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void JoystickScriptingInterface::update() {
|
void JoystickScriptingInterface::update() {
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
PerformanceTimer perfTimer("JoystickScriptingInterface::update");
|
if (_isInitialized) {
|
||||||
SDL_JoystickUpdate();
|
PerformanceTimer perfTimer("JoystickScriptingInterface::update");
|
||||||
|
SDL_JoystickUpdate();
|
||||||
foreach(Joystick* joystick, _openJoysticks) {
|
|
||||||
joystick->update();
|
foreach(Joystick* joystick, _openJoysticks) {
|
||||||
|
joystick->update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,17 +92,13 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) {
|
||||||
Joystick* matchingJoystick = _openJoysticks.value(name);
|
Joystick* matchingJoystick = _openJoysticks.value(name);
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
if (!matchingJoystick) {
|
if (!matchingJoystick) {
|
||||||
// we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
|
SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name);
|
||||||
int joystickCount = SDL_NumJoysticks();
|
|
||||||
|
|
||||||
for (int i = 0; i < joystickCount; i++) {
|
if (openSDLJoystick) {
|
||||||
if (SDL_JoystickName(i) == name) {
|
matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value();
|
||||||
matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value();
|
} else {
|
||||||
break;
|
qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer.";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer.";
|
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
||||||
|
|
|
@ -31,13 +31,19 @@ public:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
Joystick* joystickWithName(const QString& name);
|
Joystick* joystickWithName(const QString& name);
|
||||||
|
void reset();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#ifdef HAVE_SDL
|
||||||
|
SDL_Joystick* openSDLJoystickWithName(const QString& name);
|
||||||
|
#endif
|
||||||
|
|
||||||
JoystickScriptingInterface();
|
JoystickScriptingInterface();
|
||||||
~JoystickScriptingInterface();
|
~JoystickScriptingInterface();
|
||||||
|
|
||||||
QMap<QString, Joystick*> _openJoysticks;
|
QMap<QString, Joystick*> _openJoysticks;
|
||||||
QStringList _availableDeviceNames;
|
QStringList _availableDeviceNames;
|
||||||
|
bool _isInitialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_JoystickScriptingInterface_h
|
#endif // hifi_JoystickScriptingInterface_h
|
||||||
|
|
Loading…
Reference in a new issue