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 | 28 | JoystickScriptingInterface::JoystickScriptingInterface() : |
| 29 | 29 | _openJoysticks(), |
| 30 | − _availableDeviceNames() | |
| 30 | + _availableDeviceNames(), | |
| 31 | + _isInitialized(false) | |
| 31 | 32 | { |
| 32 | −#ifdef HAVE_SDL | |
| 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 | |
| 33 | + reset(); | |
| 41 | 34 | } |
| 42 | 35 | |
| 43 | 36 | JoystickScriptingInterface::~JoystickScriptingInterface() { |
| @@ -45,18 +38,53 @@ JoystickScriptingInterface::~JoystickScriptingInterface() { · 1 unmodified line | ||
| 45 | 38 | |
| 46 | 39 | #ifdef HAVE_SDL |
| 47 | 40 | SDL_Quit(); |
| 41 | + _isInitialized = false; | |
| 48 | 42 | #endif |
| 49 | 43 | } |
| 50 | 44 | |
| 51 | −void JoystickScriptingInterface::update() { | |
| 45 | +void JoystickScriptingInterface::reset() { | |
| 52 | 46 | #ifdef HAVE_SDL |
| 53 | − PerformanceTimer perfTimer("JoystickScriptingInterface::update"); | |
| 54 | − SDL_JoystickUpdate(); | |
| 55 | 47 | |
| 56 | − foreach(Joystick* joystick, _openJoysticks) { | |
| 57 | − joystick->update(); | |
| 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(); | |
| 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 | 88 | #endif |
| 61 | 89 | } |
| 62 | 90 | |
| @@ -64,17 +92,13 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { · 1 unmodified line | ||
| 64 | 92 | Joystick* matchingJoystick = _openJoysticks.value(name); |
| 65 | 93 | #ifdef HAVE_SDL |
| 66 | 94 | if (!matchingJoystick) { |
| 67 | − // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists | |
| 68 | − int joystickCount = SDL_NumJoysticks(); | |
| 95 | + SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name); | |
| 69 | 96 | |
| 70 | − for (int i = 0; i < joystickCount; i++) { | |
| 71 | − if (SDL_JoystickName(i) == name) { | |
| 72 | − matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value(); | |
| 73 | − break; | |
| 74 | − } | |
| 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."; | |
| 75 | 101 | } |
| 76 | − | |
| 77 | − qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; | |
| 78 | 102 | } |
| 79 | 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 | |