Showing the changes to interface/src/scripting/JoystickScriptingInterface.cpp (1 of 6 files). Show all
Unified · Split
| @@ -10,8 +10,9 @@ · 9 unmodified lines | ||
| 10 | 10 | // |
| 11 | 11 | |
| 12 | 12 | #include <QtDebug> |
| 13 | +#include <QScriptValue> | |
| 13 | 14 | |
| 14 | −#ifdef HAVE_SDL | |
| 15 | +#ifdef HAVE_SDL2 | |
| 15 | 16 | #include <SDL.h> |
| 16 | 17 | #undef main |
| 17 | 18 | #endif |
| @@ -20,104 +21,103 @@ · 2 unmodified lines | ||
| 20 | 21 | |
| 21 | 22 | #include "JoystickScriptingInterface.h" |
| 22 | 23 | |
| 24 | +#ifdef HAVE_SDL2 | |
| 25 | +SDL_JoystickID getInstanceId(SDL_GameController* controller) { | |
| 26 | + SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller); | |
| 27 | + return SDL_JoystickInstanceID(joystick); | |
| 28 | +} | |
| 29 | +#endif | |
| 30 | + | |
| 23 | 31 | JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { |
| 24 | 32 | static JoystickScriptingInterface sharedInstance; |
| 25 | 33 | return sharedInstance; |
| 26 | 34 | } |
| 27 | 35 | |
| 28 | 36 | JoystickScriptingInterface::JoystickScriptingInterface() : |
| 37 | +#ifdef HAVE_SDL2 | |
| 29 | 38 | _openJoysticks(), |
| 30 | − _availableDeviceNames(), | |
| 39 | +#endif | |
| 31 | 40 | _isInitialized(false) |
| 32 | 41 | { |
| 33 | − reset(); | |
| 42 | +#ifdef HAVE_SDL2 | |
| 43 | + bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER) == 0); | |
| 44 | + | |
| 45 | + if (initSuccess) { | |
| 46 | + int joystickCount = SDL_NumJoysticks(); | |
| 47 | + | |
| 48 | + for (int i = 0; i < joystickCount; i++) { | |
| 49 | + SDL_GameController* controller = SDL_GameControllerOpen(i); | |
| 50 | + SDL_JoystickID id = getInstanceId(controller); | |
| 51 | + Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller); | |
| 52 | + _openJoysticks[id] = joystick; | |
| 53 | + } | |
| 54 | + | |
| 55 | + _isInitialized = true; | |
| 56 | + } else { | |
| 57 | + qDebug() << "Error initializing SDL"; | |
| 58 | + } | |
| 59 | +#endif | |
| 34 | 60 | } |
| 35 | 61 | |
| 36 | 62 | JoystickScriptingInterface::~JoystickScriptingInterface() { |
| 63 | +#ifdef HAVE_SDL2 | |
| 37 | 64 | qDeleteAll(_openJoysticks); |
| 38 | 65 | |
| 39 | −#ifdef HAVE_SDL | |
| 40 | 66 | SDL_Quit(); |
| 41 | − _isInitialized = false; | |
| 42 | 67 | #endif |
| 43 | 68 | } |
| 44 | 69 | |
| 45 | −void JoystickScriptingInterface::reset() { | |
| 46 | −#ifdef HAVE_SDL | |
| 47 | − | |
| 48 | − if (_isInitialized) { | |
| 49 | − _isInitialized = false; | |
| 50 | − | |
| 51 | − // close all the open joysticks before we quit | |
| 52 | − foreach(Joystick* openJoystick, _openJoysticks) { | |
| 53 | − openJoystick->closeJoystick(); | |
| 54 | − } | |
| 55 | − | |
| 56 | − SDL_Quit(); | |
| 57 | − } | |
| 58 | − | |
| 59 | − bool initSuccess = (SDL_Init(SDL_INIT_JOYSTICK) == 0); | |
| 60 | − | |
| 61 | − if (initSuccess) { | |
| 62 | − | |
| 63 | − int joystickCount = SDL_NumJoysticks(); | |
| 64 | − | |
| 65 | − for (int i = 0; i < joystickCount; i++) { | |
| 66 | − _availableDeviceNames << SDL_JoystickName(i); | |
| 67 | − } | |
| 68 | − | |
| 69 | − foreach(const QString& joystickName, _openJoysticks.keys()) { | |
| 70 | − _openJoysticks[joystickName]->setSDLJoystick(openSDLJoystickWithName(joystickName)); | |
| 71 | − } | |
| 72 | − | |
| 73 | − _isInitialized = true; | |
| 70 | +const QObjectList JoystickScriptingInterface::getAllJoysticks() const { | |
| 71 | + QObjectList objectList; | |
| 72 | +#ifdef HAVE_SDL2 | |
| 73 | + const QList<Joystick*> joystickList = _openJoysticks.values(); | |
| 74 | + for (int i = 0; i < joystickList.length(); i++) { | |
| 75 | + objectList << joystickList[i]; | |
| 74 | 76 | } |
| 75 | 77 | #endif |
| 78 | + return objectList; | |
| 76 | 79 | } |
| 77 | 80 | |
| 78 | 81 | void JoystickScriptingInterface::update() { |
| 79 | −#ifdef HAVE_SDL | |
| 82 | +#ifdef HAVE_SDL2 | |
| 80 | 83 | if (_isInitialized) { |
| 81 | 84 | PerformanceTimer perfTimer("JoystickScriptingInterface::update"); |
| 82 | − SDL_JoystickUpdate(); | |
| 83 | − | |
| 84 | − foreach(Joystick* joystick, _openJoysticks) { | |
| 85 | − joystick->update(); | |
| 86 | − } | |
| 87 | − } | |
| 88 | −#endif | |
| 89 | −} | |
| 85 | + SDL_GameControllerUpdate(); | |
| 86 | + SDL_Event event; | |
| 87 | + while (SDL_PollEvent(&event)) { | |
| 88 | + if (event.type == SDL_CONTROLLERAXISMOTION) { | |
| 89 | + Joystick* joystick = _openJoysticks[event.caxis.which]; | |
| 90 | + if (joystick) { | |
| 91 | + joystick->handleAxisEvent(event.caxis); | |
| 92 | + } | |
| 93 | + } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) { | |
| 94 | + Joystick* joystick = _openJoysticks[event.cbutton.which]; | |
| 95 | + if (joystick) { | |
| 96 | + joystick->handleButtonEvent(event.cbutton); | |
| 97 | + } | |
| 98 | + } else if (event.type == SDL_CONTROLLERDEVICEADDED) { | |
| 99 | + SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which); | |
| 90 | 100 | |
| 91 | −Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { | |
| 92 | − Joystick* matchingJoystick = _openJoysticks.value(name); | |
| 93 | −#ifdef HAVE_SDL | |
| 94 | − if (!matchingJoystick) { | |
| 95 | − SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name); | |
| 96 | − | |
| 97 | − if (openSDLJoystick) { | |
| 98 | − matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value(); | |
| 99 | − } else { | |
| 100 | − qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; | |
| 101 | + SDL_JoystickID id = getInstanceId(controller); | |
| 102 | + Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller); | |
| 103 | + _openJoysticks[id] = joystick; | |
| 104 | + emit joystickAdded(joystick); | |
| 105 | + } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { | |
| 106 | + Joystick* joystick = _openJoysticks[event.cdevice.which]; | |
| 107 | + _openJoysticks.remove(event.cdevice.which); | |
| 108 | + emit joystickRemoved(joystick); | |
| 109 | + } | |
| 101 | 110 | } |
| 102 | 111 | } |
| 103 | 112 | #endif |
| 104 | − | |
| 105 | − return matchingJoystick; | |
| 106 | 113 | } |
| 107 | 114 | |
| 115 | +#ifdef HAVE_SDL2 | |
| 108 | 116 | |
| 109 | −#ifdef HAVE_SDL | |
| 110 | − | |
| 111 | −SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString &name) { | |
| 117 | +SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString& name) { | |
| 112 | 118 | // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists |
| 113 | 119 | int joystickCount = SDL_NumJoysticks(); |
| 114 | 120 | |
| 115 | − for (int i = 0; i < joystickCount; i++) { | |
| 116 | − if (SDL_JoystickName(i) == name) { | |
| 117 | − return SDL_JoystickOpen(i); | |
| 118 | − break; | |
| 119 | − } | |
| 120 | − } | |
| 121 | 121 | |
| 122 | 122 | return NULL; |
| 123 | 123 | } |