Showing the changes to interface/src/scripting/JoystickScriptingInterface.cpp (1 of 4 files). Show all
Unified · Split
| @@ -27,17 +27,10 @@ JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { · 26 unmodified lines | |||
| 27 | 27 | ||
| 28 | JoystickScriptingInterface::JoystickScriptingInterface() : | 28 | JoystickScriptingInterface::JoystickScriptingInterface() : |
| 29 | _openJoysticks(), | 29 | _openJoysticks(), |
| 30 | _availableDeviceNames() | 30 | _availableDeviceNames(), |
| 31 | _isInitialized(false) | ||
| 31 | { | 32 | { |
| 32 | #ifdef HAVE_SDL | 33 | reset(); |
| 33 | SDL_Init(SDL_INIT_JOYSTICK); | ||
| 34 | |||
| 35 | int joystickCount = SDL_NumJoysticks(); | ||
| 36 | |||
| 37 | for (int i = 0; i < joystickCount; i++) { | ||
| 38 | _availableDeviceNames << SDL_JoystickName(i); | ||
| 39 | } | ||
| 40 | #endif | ||
| 41 | } | 34 | } |
| 42 | 35 | ||
| 43 | JoystickScriptingInterface::~JoystickScriptingInterface() { | 36 | JoystickScriptingInterface::~JoystickScriptingInterface() { |
| @@ -45,18 +38,53 @@ JoystickScriptingInterface::~JoystickScriptingInterface() { · 1 unmodified line | |||
| 45 | 38 | ||
| 46 | #ifdef HAVE_SDL | 39 | #ifdef HAVE_SDL |
| 47 | SDL_Quit(); | 40 | SDL_Quit(); |
| 41 | _isInitialized = false; | ||
| 48 | #endif | 42 | #endif |
| 49 | } | 43 | } |
| 50 | 44 | ||
| 51 | void JoystickScriptingInterface::update() { | 45 | void JoystickScriptingInterface::reset() { |
| 52 | #ifdef HAVE_SDL | 46 | #ifdef HAVE_SDL |
| 53 | PerformanceTimer perfTimer("JoystickScriptingInterface::update"); | ||
| 54 | SDL_JoystickUpdate(); | ||
| 55 | 47 | ||
| 56 | foreach(Joystick* joystick, _openJoysticks) { | 48 | if (_isInitialized) { |
| 57 | joystick->update(); | 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(); | ||
| 58 | } | 57 | } |
| 59 | 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 | } | ||
| 75 | #endif | ||
| 76 | } | ||
| 77 | |||
| 78 | void JoystickScriptingInterface::update() { | ||
| 79 | #ifdef HAVE_SDL | ||
| 80 | if (_isInitialized) { | ||
| 81 | PerformanceTimer perfTimer("JoystickScriptingInterface::update"); | ||
| 82 | SDL_JoystickUpdate(); | ||
| 83 | |||
| 84 | foreach(Joystick* joystick, _openJoysticks) { | ||
| 85 | joystick->update(); | ||
| 86 | } | ||
| 87 | } | ||
| 60 | #endif | 88 | #endif |
| 61 | } | 89 | } |
| 62 | 90 | ||
| @@ -64,17 +92,13 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { · 1 unmodified line | |||
| 64 | Joystick* matchingJoystick = _openJoysticks.value(name); | 92 | Joystick* matchingJoystick = _openJoysticks.value(name); |
| 65 | #ifdef HAVE_SDL | 93 | #ifdef HAVE_SDL |
| 66 | if (!matchingJoystick) { | 94 | if (!matchingJoystick) { |
| 67 | // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists | 95 | SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name); |
| 68 | int joystickCount = SDL_NumJoysticks(); | ||
| 69 | 96 | ||
| 70 | for (int i = 0; i < joystickCount; i++) { | 97 | if (openSDLJoystick) { |
| 71 | if (SDL_JoystickName(i) == name) { | 98 | matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value(); |
| 72 | matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value(); | 99 | } else { |
| 73 | break; | 100 | qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; |
| 74 | } | ||
| 75 | } | 101 | } |
| 76 | |||
| 77 | qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; | ||
| 78 | } | 102 | } |
| 79 | #endif | 103 | #endif |
| 80 | 104 | ||
| @@ -82,3 +106,20 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { · 1 unmodified line | |||
| 82 | } | 106 | } |
| 83 | 107 | ||
| 84 | 108 | ||
| 109 | #ifdef HAVE_SDL | ||
| 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 | ||
| 113 | int joystickCount = SDL_NumJoysticks(); | ||
| 114 | |||
| 115 | for (int i = 0; i < joystickCount; i++) { | ||
| 116 | if (SDL_JoystickName(i) == name) { | ||
| 117 | return SDL_JoystickOpen(i); | ||
| 118 | break; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | return NULL; | ||
| 123 | } | ||
| 124 | |||
| 125 | #endif | ||