Oolite
Loading...
Searching...
No Matches
OOSDLJoystickManager.m
Go to the documentation of this file.
1/*
2
3OOSDLJoystickManager.m
4By Dylan Smith
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
27#import "OOLogging.h"
28
29#define kOOLogUnconvertedNSLog @"unclassified.OOSDLJoystickManager"
30
31@implementation OOSDLJoystickManager
32
33- (id)init
34{
35 int i;
36
37 NSMutableDictionary* idMap = [[[NSMutableDictionary alloc] init] autorelease];
38
39 // Find and open the sticks. Make sure that we don't fail if more joysticks than MAX_STICKS are detected.
40 SDL_JoystickID* joystickIds = SDL_GetJoysticks(&stickCount);
41 OOLog(@"joystick.init", @"Number of joysticks detected: %d", stickCount);
42 if (stickCount > MAX_STICKS) {
44 OOLog(@"joystick.init", @"Number of joysticks detected exceeds maximum number of joysticks allowed. Setting number of active joysticks to %d.", MAX_STICKS);
45 }
46 if (stickCount) {
47 for (i = 0; i < stickCount; i++) {
48 // it's doubtful MAX_STICKS will ever get exceeded, but
49 // we need to be defensive.
50 if (i > MAX_STICKS)
51 break;
52
53 stick[i] = SDL_OpenJoystick(joystickIds[i]);
54 if (stick[i]) {
55 [idMap setObject:[NSNumber numberWithInt:i] forKey:[NSString stringWithFormat:@"%d", joystickIds[i]]];
56 } else {
57 OOLog(@"joystick.init", @"Failed to open joystick #%d", i);
58 }
59 }
60 SDL_SetJoystickEventsEnabled(true);
61 }
62 SDL_free(joystickIds);
63 joystickIdMap = [idMap copy];
64 return [super init];
65}
66
67- (void)dealloc
68{
69 [joystickIdMap release];
70 [super dealloc];
71}
72
73- (NSInteger)getJoystickIndexFromId:(SDL_JoystickID)joystickId
74{
75 NSNumber* index = [joystickIdMap valueForKey:[NSString stringWithFormat:@"%d", joystickId]];
76 if (index) {
77 return [index integerValue];
78 }
79 return -1;
80}
81
82- (NSUInteger)joystickCount
83{
84 return stickCount;
85}
86
87- (NSString*)nameOfJoystick:(NSUInteger)stickNumber
88{
89 if (stickNumber >= stickCount)
90 return @"(unknown joystick)";
91 return [NSString stringWithUTF8String:SDL_GetJoystickName(stick[stickNumber])];
92}
93
94- (int16_t)getAxisWithStick:(NSUInteger)stickNum axis:(NSUInteger)axisNum
95{
96 return SDL_GetJoystickAxis(stick[stickNum], axisNum);
97}
98
99- (SDL_JoyAxisEvent)makeJoyAxisEvent:(SDL_JoyAxisEvent*)sdlevt
100{
101 SDL_JoyAxisEvent evt;
102 evt.type = sdlevt->type;
103 evt.which = [self getJoystickIndexFromId:sdlevt->which];
104 evt.axis = sdlevt->axis;
105 evt.value = sdlevt->value;
106 return evt;
107}
108
109- (SDL_JoyButtonEvent)makeJoyButtonEvent:(SDL_JoyButtonEvent*)sdlevt
110{
111 SDL_JoyButtonEvent evt;
112 evt.type = sdlevt->type;
113 evt.which = [self getJoystickIndexFromId:sdlevt->which];
114 evt.button = sdlevt->button;
115 evt.down = sdlevt->down;
116 return evt;
117}
118
119- (SDL_JoyHatEvent)makeJoyHatEvent:(SDL_JoyHatEvent*)sdlevt
120{
121 SDL_JoyHatEvent evt;
122 evt.type = sdlevt->type;
123 evt.which = [self getJoystickIndexFromId:sdlevt->which];
124 evt.hat = sdlevt->hat;
125 evt.value = sdlevt->value;
126 return evt;
127}
128
129- (void)decodeAxisEvent:(SDL_JoyAxisEvent*)evt
130{
131 // Which axis moved? Does the value need to be made to fit a
132 // certain function? Convert axis value to a double.
133 double axisvalue = (double)evt->value;
134
135 // First check if there is a callback and...
136 if (cbObject && (cbHardware & HW_AXIS)) {
137 // ...then check if axis moved more than AXCBTHRESH - (fix for BUG #17482)
138 if (axisvalue > AXCBTHRESH) {
139 NSDictionary* fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
140 [NSNumber numberWithBool:YES], STICK_ISAXIS,
141 [NSNumber numberWithInt:evt->which], STICK_NUMBER,
142 [NSNumber numberWithInt:evt->axis], STICK_AXBUT,
143 nil];
144 cbHardware = 0;
145 [cbObject performSelector:cbSelector withObject:fnDict];
146 cbObject = nil;
147 }
148
149 // we are done.
150 return;
151 }
152
153 // SDL seems to have some bizarre (perhaps a bug) behaviour when
154 // events get queued up because the game isn't ready to handle
155 // them (perhaps it's loading a commander and initializing the
156 // universe, and the main event loop is blocked).
157 // What happens is SDL lies about the axis that was triggered. For
158 // each queued event it adds 1 to the axis number!! This does
159 // not seem to happen with buttons.
160 int function;
161 if (evt->axis < MAX_AXES) {
162 function = axismap[evt->which][evt->axis];
163 } else {
164 OOLog(@"decodeAxisEvent", @"Stick axis out of range - axis was %d", evt->axis);
165 return;
166 }
167 switch (function) {
168 case STICK_NOFUNCTION:
169 // do nothing
170 break;
171 case AXIS_THRUST:
172 // Normalize the thrust setting.
173 axstate[function] = (float)(65536 - (axisvalue + 32768)) / 65536;
174 break;
175 case AXIS_ROLL:
176 case AXIS_PITCH:
177 case AXIS_YAW:
178 case AXIS_VIEWX:
179 case AXIS_VIEWY:
180 axstate[function] = axisvalue / STICK_NORMALDIV;
181 break;
182 // TODO AXIS_FIELD_OF_VIEW
183 default:
184 // set the state with no modification.
185 axstate[function] = axisvalue / 32768;
186 }
187 if ((function == AXIS_PITCH) && invertPitch)
188 axstate[function] = -1.0 * axstate[function];
189}
190
191- (void)decodeButtonEvent:(SDL_JoyButtonEvent*)evt
192{
193 BOOL bs = NO;
194
195 // Is there a callback we need to make?
196 if (cbObject && (cbHardware & HW_BUTTON)) {
197 NSDictionary* fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
198 [NSNumber numberWithBool:NO], STICK_ISAXIS,
199 [NSNumber numberWithInt:evt->which], STICK_NUMBER,
200 [NSNumber numberWithInt:evt->button], STICK_AXBUT,
201 nil];
202 cbHardware = 0;
203 [cbObject performSelector:cbSelector withObject:fnDict];
204 cbObject = nil;
205
206 // we are done.
207 return;
208 }
209
210 // Defensive measure - see comments in the axis handler for why.
211 int function;
212 int guifunction;
213 if (evt->button < MAX_BUTTONS) {
214 function = buttonmap[evt->which][evt->button];
215 guifunction = guibuttonmap[evt->which][evt->button];
216 } else {
217 OOLog(@"decodeButtonEvent", @"Joystick button out of range: %d", evt->button);
218 return;
219 }
220 if (evt->type == SDL_EVENT_JOYSTICK_BUTTON_DOWN) {
221 bs = YES;
222 if (function == BUTTON_PRECISION)
224 }
225 true_butstate[evt->which][evt->button] = bs;
226 if (function >= 0) {
227 butstate[function] = bs;
228 }
229 if (guifunction >= 0) {
230 butstate[guifunction] = bs;
231 }
232}
233
234- (void)decodeHatEvent:(SDL_JoyHatEvent*)evt
235{
236 // HACK: handle this as a set of buttons
237 int i;
238 SDL_JoyButtonEvent btn;
239
240 btn.which = evt->which;
241
242 for (i = 0; i < 4; ++i) {
243 if ((evt->value ^ hatstate[evt->which][evt->hat]) & (1 << i)) {
244 btn.type = (evt->value & (1 << i)) ? SDL_EVENT_JOYSTICK_BUTTON_DOWN : SDL_EVENT_JOYSTICK_BUTTON_UP;
245 btn.button = MAX_REAL_BUTTONS + i + evt->which * 4;
246 btn.down = (evt->value & (1 << i));
247 [self decodeButtonEvent:&btn];
248 }
249 }
250
251 hatstate[evt->which][evt->hat] = evt->value;
252}
253
254- (BOOL)handleSDLEvent:(SDL_Event*)evt
255{
256 BOOL rc = NO;
257 switch (evt->type) {
258 case SDL_EVENT_GAMEPAD_AXIS_MOTION:
259 case SDL_EVENT_JOYSTICK_AXIS_MOTION: {
260 SDL_JoyAxisEvent joyEvt = [self makeJoyAxisEvent:(SDL_JoyAxisEvent*)evt];
261 if (joyEvt.which >= 0) {
262 [self decodeAxisEvent:&joyEvt];
263 rc = YES;
264 }
265 break;
266 }
267
268 case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
269 case SDL_EVENT_GAMEPAD_BUTTON_UP:
270 case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
271 case SDL_EVENT_JOYSTICK_BUTTON_UP: {
272 SDL_JoyButtonEvent joyEvt = [self makeJoyButtonEvent:(SDL_JoyButtonEvent*)evt];
273 if (joyEvt.which >= 0) {
274 [self decodeButtonEvent:&joyEvt];
275 rc = YES;
276 }
277 break;
278 }
279
280 case SDL_EVENT_JOYSTICK_HAT_MOTION: {
281 SDL_JoyHatEvent joyEvt = [self makeJoyHatEvent:(SDL_JoyHatEvent*)evt];
282 if (joyEvt.which >= 0) {
283 [self decodeHatEvent:&joyEvt];
284 rc = YES;
285 }
286 break;
287 }
288
289 default:
290 OOLog(@"handleSDLEvent.unknownEvent", @"%@", @"JoystickHandler was sent an event it doesn't know");
291 }
292 return rc;
293}
294
295@end
#define MAX_AXES
#define MAX_STICKS
#define MAX_REAL_BUTTONS
#define STICK_NOFUNCTION
#define HW_BUTTON
#define MAX_BUTTONS
#define AXCBTHRESH
@ AXIS_VIEWX
@ AXIS_PITCH
@ AXIS_YAW
@ AXIS_THRUST
@ AXIS_ROLL
@ AXIS_VIEWY
#define STICK_NORMALDIV
#define HW_AXIS
@ BUTTON_PRECISION
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
uint8_t hatstate[MAX_STICKS][MAX_HATS]
int8_t axismap[MAX_STICKS][MAX_AXES]
int8_t guibuttonmap[MAX_STICKS][MAX_BUTTONS]
double axstate[AXIS_end]
BOOL butstate[BUTTON_end]
int8_t buttonmap[MAX_STICKS][MAX_BUTTONS]
BOOL true_butstate[MAX_STICKS][MAX_BUTTONS]
SDL_JoyButtonEvent makeJoyButtonEvent:(SDL_JoyButtonEvent *sdlevt)
void decodeAxisEvent:(SDL_JoyAxisEvent *evt)
SDL_JoyAxisEvent makeJoyAxisEvent:(SDL_JoyAxisEvent *sdlevt)
void decodeHatEvent:(SDL_JoyHatEvent *evt)
SDL_JoyHatEvent makeJoyHatEvent:(SDL_JoyHatEvent *sdlevt)
NSInteger getJoystickIndexFromId:(SDL_JoystickID joystickId)
void decodeButtonEvent:(SDL_JoyButtonEvent *evt)
SDL_Joystick * stick[MAX_STICKS]