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 | #include <QtDebug> | 12 | #include <QtDebug> |
| 13 | #include <QScriptValue> | ||
| 13 | 14 | ||
| 14 | #ifdef HAVE_SDL | 15 | #ifdef HAVE_SDL2 |
| 15 | #include <SDL.h> | 16 | #include <SDL.h> |
| 16 | #undef main | 17 | #undef main |
| 17 | #endif | 18 | #endif |
| @@ -20,104 +21,103 @@ · 2 unmodified lines | |||
| 20 | 21 | ||
| 21 | #include "JoystickScriptingInterface.h" | 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 | JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { | 31 | JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { |
| 24 | static JoystickScriptingInterface sharedInstance; | 32 | static JoystickScriptingInterface sharedInstance; |
| 25 | return sharedInstance; | 33 | return sharedInstance; |
| 26 | } | 34 | } |
| 27 | 35 | ||
| 28 | JoystickScriptingInterface::JoystickScriptingInterface() : | 36 | JoystickScriptingInterface::JoystickScriptingInterface() : |
| 37 | #ifdef HAVE_SDL2 | ||
| 29 | _openJoysticks(), | 38 | _openJoysticks(), |
| 30 | _availableDeviceNames(), | 39 | #endif |
| 31 | _isInitialized(false) | 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 | JoystickScriptingInterface::~JoystickScriptingInterface() { | 62 | JoystickScriptingInterface::~JoystickScriptingInterface() { |
| 63 | #ifdef HAVE_SDL2 | ||
| 37 | qDeleteAll(_openJoysticks); | 64 | qDeleteAll(_openJoysticks); |
| 38 | 65 | ||
| 39 | #ifdef HAVE_SDL | ||
| 40 | SDL_Quit(); | 66 | SDL_Quit(); |
| 41 | _isInitialized = false; | ||
| 42 | #endif | 67 | #endif |
| 43 | } | 68 | } |
| 44 | 69 | ||
| 45 | void JoystickScriptingInterface::reset() { | 70 | const QObjectList JoystickScriptingInterface::getAllJoysticks() const { |
| 46 | #ifdef HAVE_SDL | 71 | QObjectList objectList; |
| 47 | 72 | #ifdef HAVE_SDL2 | |
| 48 | if (_isInitialized) { | 73 | const QList<Joystick*> joystickList = _openJoysticks.values(); |
| 49 | _isInitialized = false; | 74 | for (int i = 0; i < joystickList.length(); i++) { |
| 50 | 75 | objectList << joystickList[i]; | |
| 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; | ||
| 74 | } | 76 | } |
| 75 | #endif | 77 | #endif |
| 78 | return objectList; | ||
| 76 | } | 79 | } |
| 77 | 80 | ||
| 78 | void JoystickScriptingInterface::update() { | 81 | void JoystickScriptingInterface::update() { |
| 79 | #ifdef HAVE_SDL | 82 | #ifdef HAVE_SDL2 |
| 80 | if (_isInitialized) { | 83 | if (_isInitialized) { |
| 81 | PerformanceTimer perfTimer("JoystickScriptingInterface::update"); | 84 | PerformanceTimer perfTimer("JoystickScriptingInterface::update"); |
| 82 | SDL_JoystickUpdate(); | 85 | SDL_GameControllerUpdate(); |
| 83 | 86 | SDL_Event event; | |
| 84 | foreach(Joystick* joystick, _openJoysticks) { | 87 | while (SDL_PollEvent(&event)) { |
| 85 | joystick->update(); | 88 | if (event.type == SDL_CONTROLLERAXISMOTION) { |
| 86 | } | 89 | Joystick* joystick = _openJoysticks[event.caxis.which]; |
| 87 | } | 90 | if (joystick) { |
| 88 | #endif | 91 | joystick->handleAxisEvent(event.caxis); |
| 89 | } | 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) { | 101 | SDL_JoystickID id = getInstanceId(controller); |
| 92 | Joystick* matchingJoystick = _openJoysticks.value(name); | 102 | Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller); |
| 93 | #ifdef HAVE_SDL | 103 | _openJoysticks[id] = joystick; |
| 94 | if (!matchingJoystick) { | 104 | emit joystickAdded(joystick); |
| 95 | SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name); | 105 | } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { |
| 96 | 106 | Joystick* joystick = _openJoysticks[event.cdevice.which]; | |
| 97 | if (openSDLJoystick) { | 107 | _openJoysticks.remove(event.cdevice.which); |
| 98 | matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value(); | 108 | emit joystickRemoved(joystick); |
| 99 | } else { | 109 | } |
| 100 | qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; | ||
| 101 | } | 110 | } |
| 102 | } | 111 | } |
| 103 | #endif | 112 | #endif |
| 104 | |||
| 105 | return matchingJoystick; | ||
| 106 | } | 113 | } |
| 107 | 114 | ||
| 115 | #ifdef HAVE_SDL2 | ||
| 108 | 116 | ||
| 109 | #ifdef HAVE_SDL | 117 | SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString& name) { |
| 110 | |||
| 111 | SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString &name) { | ||
| 112 | // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists | 118 | // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists |
| 113 | int joystickCount = SDL_NumJoysticks(); | 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 | return NULL; | 122 | return NULL; |
| 123 | } | 123 | } |