overte-HifiExperiments/interface/src/scripting/JoystickScriptingInterface.cpp
2014-10-10 17:03:10 -07:00

125 lines
3.7 KiB
C++

//
// JoystickScriptingInterface.cpp
// interface/src/devices
//
// Created by Andrzej Kapolka on 5/15/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QtDebug>
#include <QScriptValue>
#ifdef HAVE_SDL2
#include <SDL.h>
#undef main
#endif
#include <PerfStat.h>
#include "JoystickScriptingInterface.h"
#ifdef HAVE_SDL2
SDL_JoystickID getInstanceId(SDL_GameController* controller) {
SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller);
return SDL_JoystickInstanceID(joystick);
}
#endif
JoystickScriptingInterface& JoystickScriptingInterface::getInstance() {
static JoystickScriptingInterface sharedInstance;
return sharedInstance;
}
JoystickScriptingInterface::JoystickScriptingInterface() :
#ifdef HAVE_SDL2
_openJoysticks(),
#endif
_isInitialized(false)
{
#ifdef HAVE_SDL2
bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER) == 0);
if (initSuccess) {
int joystickCount = SDL_NumJoysticks();
for (int i = 0; i < joystickCount; i++) {
SDL_GameController* controller = SDL_GameControllerOpen(i);
SDL_JoystickID id = getInstanceId(controller);
Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
_openJoysticks[id] = joystick;
}
_isInitialized = true;
} else {
qDebug() << "Error initializing SDL";
}
#endif
}
JoystickScriptingInterface::~JoystickScriptingInterface() {
#ifdef HAVE_SDL2
qDeleteAll(_openJoysticks);
SDL_Quit();
#endif
}
const QObjectList JoystickScriptingInterface::getAllJoysticks() const {
QObjectList objectList;
#ifdef HAVE_SDL2
const QList<Joystick*> joystickList = _openJoysticks.values();
for (int i = 0; i < joystickList.length(); i++) {
objectList << joystickList[i];
}
#endif
return objectList;
}
void JoystickScriptingInterface::update() {
#ifdef HAVE_SDL2
if (_isInitialized) {
PerformanceTimer perfTimer("JoystickScriptingInterface::update");
SDL_GameControllerUpdate();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_CONTROLLERAXISMOTION) {
Joystick* joystick = _openJoysticks[event.caxis.which];
if (joystick) {
joystick->handleAxisEvent(event.caxis);
}
} else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) {
Joystick* joystick = _openJoysticks[event.cbutton.which];
if (joystick) {
joystick->handleButtonEvent(event.cbutton);
}
} else if (event.type == SDL_CONTROLLERDEVICEADDED) {
SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which);
SDL_JoystickID id = getInstanceId(controller);
Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
_openJoysticks[id] = joystick;
emit joystickAdded(joystick);
} else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
Joystick* joystick = _openJoysticks[event.cdevice.which];
_openJoysticks.remove(event.cdevice.which);
emit joystickRemoved(joystick);
}
}
}
#endif
}
#ifdef HAVE_SDL2
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();
return NULL;
}
#endif