History

Update to SDL2

Ryan Huffman committed 12 years ago

Commit 762cc711 parent f5ee55b6 files · +436 −114Browse files

Showing the changes to interface/src/scripting/JoystickScriptingInterface.cpp (1 of 6 files). Show all

Unified · Split

interface/src/scripting/JoystickScriptingInterface.cpp+64−64
@@ -10,8 +10,9 @@ · 9 unmodified lines
1010 //
1111
1212 #include <QtDebug>
13+#include <QScriptValue>
1314
14−#ifdef HAVE_SDL
15+#ifdef HAVE_SDL2
1516 #include <SDL.h>
1617 #undef main
1718 #endif
@@ -20,104 +21,103 @@ · 2 unmodified lines
2021
2122 #include "JoystickScriptingInterface.h"
2223
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+
2331 JoystickScriptingInterface& JoystickScriptingInterface::getInstance() {
2432 static JoystickScriptingInterface sharedInstance;
2533 return sharedInstance;
2634 }
2735
2836 JoystickScriptingInterface::JoystickScriptingInterface() :
37+#ifdef HAVE_SDL2
2938 _openJoysticks(),
30− _availableDeviceNames(),
39+#endif
3140 _isInitialized(false)
3241 {
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
3460 }
3561
3662 JoystickScriptingInterface::~JoystickScriptingInterface() {
63+#ifdef HAVE_SDL2
3764 qDeleteAll(_openJoysticks);
3865
39−#ifdef HAVE_SDL
4066 SDL_Quit();
41− _isInitialized = false;
4267 #endif
4368 }
4469
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];
7476 }
7577 #endif
78+ return objectList;
7679 }
7780
7881 void JoystickScriptingInterface::update() {
79−#ifdef HAVE_SDL
82+#ifdef HAVE_SDL2
8083 if (_isInitialized) {
8184 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);
90100
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+ }
101110 }
102111 }
103112 #endif
104−
105− return matchingJoystick;
106113 }
107114
115+#ifdef HAVE_SDL2
108116
109−#ifdef HAVE_SDL
110−
111−SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString &name) {
117+SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString& name) {
112118 // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
113119 int joystickCount = SDL_NumJoysticks();
114120
115− for (int i = 0; i < joystickCount; i++) {
116− if (SDL_JoystickName(i) == name) {
117− return SDL_JoystickOpen(i);
118− break;
119− }
120− }
121121
122122 return NULL;
123123 }