/
SDLJoystick.cpp
127 lines (64 loc) · 1.94 KB
/
SDLJoystick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "SDLJoystick.h"
namespace lime {
static SDL_Joystick* accelerometer = 0;
static SDL_JoystickID accelerometerID = -1;
std::map<int, int> joystickIDs = std::map<int, int> ();
std::map<int, SDL_Joystick*> joysticks = std::map<int, SDL_Joystick*> ();
bool SDLJoystick::Connect (int deviceID) {
if (deviceID != accelerometerID) {
SDL_Joystick* joystick = SDL_JoystickOpen (deviceID);
int id = SDL_JoystickInstanceID (joystick);
if (joystick) {
joysticks[id] = joystick;
joystickIDs[deviceID] = id;
return true;
}
}
return false;
}
bool SDLJoystick::Disconnect (int id) {
if (joysticks.find (id) != joysticks.end ()) {
SDL_Joystick* joystick = joysticks[id];
SDL_JoystickClose (joystick);
joysticks.erase (id);
return true;
}
return false;
}
int SDLJoystick::GetInstanceID (int deviceID) {
return joystickIDs[deviceID];
}
void SDLJoystick::Init () {
#if defined(IPHONE) || defined(ANDROID) || defined(TVOS)
for (int i = 0; i < SDL_NumJoysticks (); i++) {
if (strstr (SDL_JoystickNameForIndex (i), "Accelerometer")) {
accelerometer = SDL_JoystickOpen (i);
accelerometerID = SDL_JoystickInstanceID (accelerometer);
}
}
#endif
}
bool SDLJoystick::IsAccelerometer (int id) {
return (id == accelerometerID);
}
const char* Joystick::GetDeviceGUID (int id) {
char* guid = new char[64];
SDL_JoystickGetGUIDString (SDL_JoystickGetGUID (joysticks[id]), guid, 64);
return guid;
}
const char* Joystick::GetDeviceName (int id) {
return SDL_JoystickName (joysticks[id]);
}
int Joystick::GetNumAxes (int id) {
return SDL_JoystickNumAxes (joysticks[id]);
}
int Joystick::GetNumButtons (int id) {
return SDL_JoystickNumButtons (joysticks[id]);
}
int Joystick::GetNumHats (int id) {
return SDL_JoystickNumHats (joysticks[id]);
}
int Joystick::GetNumTrackballs (int id) {
return SDL_JoystickNumBalls (joysticks[id]);
}
}