Oolite
Loading...
Searching...
No Matches
OOJoystickManager.m
Go to the documentation of this file.
1/*
2
3OOJoystickManager.m
4By Dylan Smith
5modified by Alex Smith
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
27#import "OOJoystickManager.h"
29#import "OOLogging.h"
30
31static Class sStickHandlerClass = Nil;
33
34@interface OOJoystickManager (Private)
35
36// Setting button and axis functions
37- (void)setFunctionForAxis:(int)axis
38 function:(int)function
39 stick:(int)stickNum;
40
41- (void)setFunctionForButton:(int)button
42 function:(int)function
43 stick:(int)stickNum;
44
45@end
46
47@implementation OOJoystickManager
48
50{
51 if (sSharedStickHandler == nil) {
52 if (sStickHandlerClass == Nil)
54 sSharedStickHandler = [[sStickHandlerClass alloc] init];
55 }
57}
58
59+ (BOOL)setStickHandlerClass:(Class)aClass
60{
61 NSAssert(sStickHandlerClass == nil, @"Can't set joystick handler class after joystick handler is initialized.");
62 NSParameterAssert(aClass == Nil || [aClass isSubclassOfClass:[OOJoystickManager class]]);
63
64 sStickHandlerClass = aClass;
65 return YES;
66}
67
68- (id)init
69{
70 if ((self = [super init])) {
71 // set initial values for stick buttons/axes (NO for buttons,
72 // STICK_AXISUNASSIGNED for axes). Caution: calling this again
73 // after axes have been assigned will set all the axes to
74 // STICK_AXISUNASSIGNED so if there is a need to do something
75 // like this, then do it some other way, or change this method
76 // so it doesn't do that.
77 [self clearStickStates];
78
79 // Make some sensible mappings. This also ensures unassigned
80 // axes and buttons are set to unassigned (STICK_NOFUNCTION).
81 [self loadStickSettings];
82 invertPitch = NO;
83 precisionMode = NO;
84 }
85 return self;
86}
87
88- (NSPoint)rollPitchAxis
89{
90 return NSMakePoint([self getAxisState:AXIS_ROLL], [self getAxisState:AXIS_PITCH]);
91}
92
93- (NSPoint)viewAxis
94{
95 return NSMakePoint(axstate[AXIS_VIEWX], axstate[AXIS_VIEWY]);
96}
97
98- (BOOL)getButtonState:(int)function
99{
100 return butstate[function];
101}
102
103- (const BOOL*)getAllButtonStates
104{
105 return butstate;
106}
107
108- (BOOL)isButtonDown:(int)button stick:(int)stickNum
109{
111}
112
113- (double)getAxisState:(int)function
114{
115 if (axstate[function] == STICK_AXISUNASSIGNED) {
117 }
118 switch (function) {
119 case AXIS_ROLL:
120 if (precisionMode) {
121 return [roll_profile value:axstate[function]] / STICK_PRECISIONFAC;
122 } else {
123 return [roll_profile value:axstate[function]];
124 }
125 case AXIS_PITCH:
126 if (precisionMode) {
128 } else {
129 return [pitch_profile value:axstate[function]];
130 }
131 case AXIS_YAW:
132 if (precisionMode) {
133 return [yaw_profile value:axstate[function]] / STICK_PRECISIONFAC;
134 } else {
135 return [yaw_profile value:axstate[function]];
136 }
137 default:
138 return axstate[function];
139 }
140}
141
143{
144 return precisionMode ? STICK_PRECISIONFAC : 1.0;
145}
146
147- (void)setProfile:(OOJoystickAxisProfile*)profile forAxis:(int)axis
148{
149 switch (axis) {
150 case AXIS_ROLL:
151 [roll_profile release];
152 roll_profile = [profile retain];
153 break;
154
155 case AXIS_PITCH:
156 [pitch_profile release];
157 pitch_profile = [profile retain];
158 break;
159
160 case AXIS_YAW:
161 [yaw_profile release];
162 yaw_profile = [profile retain];
163 break;
164 }
165 return;
166}
167
168- (OOJoystickAxisProfile*)getProfileForAxis:(int)axis
169{
170 switch (axis) {
171 case AXIS_ROLL:
172 return roll_profile;
173 case AXIS_PITCH:
174 return pitch_profile;
175 case AXIS_YAW:
176 return yaw_profile;
177 }
178 return nil;
179}
180
181- (void)saveProfileForAxis:(int)axis
182{
183 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
184 NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
185 OOJoystickAxisProfile* profile;
186 OOJoystickStandardAxisProfile* standard_profile;
187 OOJoystickSplineAxisProfile* spline_profile;
188 NSArray* controlPoints;
189 NSMutableArray* points;
190 NSPoint point;
191 NSUInteger i;
192
193 profile = [self getProfileForAxis:axis];
194 if (!profile)
195 return;
196 [dict setObject:[NSNumber numberWithDouble:[profile deadzone]] forKey:@"Deadzone"];
197 if ([profile isKindOfClass:[OOJoystickStandardAxisProfile class]]) {
198 standard_profile = (OOJoystickStandardAxisProfile*)profile;
199 [dict setObject:@"Standard" forKey:@"Type"];
200 [dict setObject:[NSNumber numberWithDouble:[standard_profile power]] forKey:@"Power"];
201 [dict setObject:[NSNumber numberWithDouble:[standard_profile parameter]] forKey:@"Parameter"];
202 } else if ([profile isKindOfClass:[OOJoystickSplineAxisProfile class]]) {
203 spline_profile = (OOJoystickSplineAxisProfile*)profile;
204 [dict setObject:@"Spline" forKey:@"Type"];
205 controlPoints = [NSArray arrayWithArray:[spline_profile controlPoints]];
206 points = [[NSMutableArray alloc] initWithCapacity:[controlPoints count]];
207 for (i = 0; i < [controlPoints count]; i++) {
208 point = [[controlPoints objectAtIndex:i] pointValue];
209 [points addObject:[NSArray arrayWithObjects:
210 [NSNumber numberWithFloat:point.x],
211 [NSNumber numberWithFloat:point.y],
212 nil]];
213 }
214 [dict setObject:points forKey:@"ControlPoints"];
215 } else {
216 [dict setObject:@"Standard" forKey:@"Type"];
217 }
218 if (axis == AXIS_ROLL) {
219 [defaults setObject:dict forKey:STICK_ROLL_AXIS_PROFILE_SETTING];
220 } else if (axis == AXIS_PITCH) {
221 [defaults setObject:dict forKey:STICK_PITCH_AXIS_PROFILE_SETTING];
222 } else if (axis == AXIS_YAW) {
223 [defaults setObject:dict forKey:STICK_YAW_AXIS_PROFILE_SETTING];
224 }
225 return;
226}
227
228- (void)loadProfileForAxis:(int)axis
229{
230 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
231 NSDictionary* dict;
232 OOJoystickStandardAxisProfile* standard_profile;
233 OOJoystickSplineAxisProfile* spline_profile;
234
235 if (axis == AXIS_ROLL) {
236 dict = [defaults objectForKey:STICK_ROLL_AXIS_PROFILE_SETTING];
237 } else if (axis == AXIS_PITCH) {
238 dict = [defaults objectForKey:STICK_PITCH_AXIS_PROFILE_SETTING];
239 } else if (axis == AXIS_YAW) {
240 dict = [defaults objectForKey:STICK_YAW_AXIS_PROFILE_SETTING];
241 } else {
242 return;
243 }
244
245 NSString* type = [dict objectForKey:@"Type"];
246 if ([type isEqualToString:@"Standard"]) {
247 standard_profile = [[OOJoystickStandardAxisProfile alloc] init];
248 [standard_profile setDeadzone:[[dict objectForKey:@"Deadzone"] doubleValue]];
249 [standard_profile setPower:[[dict objectForKey:@"Power"] doubleValue]];
250 [standard_profile setParameter:[[dict objectForKey:@"Parameter"] doubleValue]];
251 [self setProfile:[standard_profile autorelease] forAxis:axis];
252 } else if ([type isEqualToString:@"Spline"]) {
253 spline_profile = [[OOJoystickSplineAxisProfile alloc] init];
254 [spline_profile setDeadzone:[[dict objectForKey:@"Deadzone"] doubleValue]];
255 NSArray *points = [dict objectForKey:@"ControlPoints"], *pointArray;
256 NSPoint point;
257 NSUInteger i;
258
259 for (i = 0; i < [points count]; i++) {
260 pointArray = [points objectAtIndex:i];
261 if ([pointArray count] >= 2) {
262 point = NSMakePoint([[pointArray objectAtIndex:0] floatValue], [[pointArray objectAtIndex:1] floatValue]);
264 }
265 }
266 [self setProfile:[spline_profile autorelease] forAxis:axis];
267 } else {
268 [self setProfile:[[[OOJoystickStandardAxisProfile alloc] init] autorelease] forAxis:axis];
269 }
270}
271
272- (NSArray*)listSticks
273{
274 NSUInteger i, stickCount = [self joystickCount];
275
276 NSMutableArray* stickList = [NSMutableArray array];
277 for (i = 0; i < stickCount; i++) {
278 [stickList addObject:[self nameOfJoystick:i]];
279 }
280 return stickList;
281}
282
283- (NSDictionary*)axisFunctions
284{
285 int i, j;
286 NSMutableDictionary* fnList = [NSMutableDictionary dictionary];
287
288 // Add axes
289 for (i = 0; i < MAX_AXES; i++) {
290 for (j = 0; j < MAX_STICKS; j++) {
291 if (axismap[j][i] >= 0) {
292 NSDictionary* fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
293 [NSNumber numberWithBool:YES], STICK_ISAXIS,
294 [NSNumber numberWithInt:j], STICK_NUMBER,
295 [NSNumber numberWithInt:i], STICK_AXBUT,
296 nil];
297 [fnList setValue:fnDict
298 forKey:ENUMKEY(axismap[j][i])];
299 }
300 }
301 }
302 return fnList;
303}
304
305- (NSDictionary*)buttonFunctions
306{
307 int i, j;
308 NSMutableDictionary* fnList = [NSMutableDictionary dictionary];
309
310 // Add buttons
311 for (i = 0; i < MAX_BUTTONS; i++) {
312 for (j = 0; j < MAX_STICKS; j++) {
313 if (buttonmap[j][i] >= 0) {
314 NSDictionary* fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
315 [NSNumber numberWithBool:NO], STICK_ISAXIS,
316 [NSNumber numberWithInt:j], STICK_NUMBER,
317 [NSNumber numberWithInt:i], STICK_AXBUT,
318 nil];
319 [fnList setValue:fnDict
320 forKey:ENUMKEY(buttonmap[j][i])];
321 }
322 }
323 }
324 return fnList;
325}
326
327- (NSDictionary*)guiButtonFunctions
328{
329 int i, j;
330 NSMutableDictionary* fnList = [NSMutableDictionary dictionary];
331
332 // Add buttons
333 for (i = 0; i < MAX_BUTTONS; i++) {
334 for (j = 0; j < MAX_STICKS; j++) {
335 if (guibuttonmap[j][i] >= 0) {
336 NSDictionary* fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
337 [NSNumber numberWithBool:NO], STICK_ISAXIS,
338 [NSNumber numberWithInt:j], STICK_NUMBER,
339 [NSNumber numberWithInt:i], STICK_AXBUT,
340 nil];
341 [fnList setValue:fnDict
342 forKey:ENUMKEY(guibuttonmap[j][i])];
343 }
344 }
345 }
346 return fnList;
347}
348
349- (void)setFunction:(int)function withDict:(NSDictionary*)stickFn
350{
351 BOOL isAxis = [stickFn oo_boolForKey:STICK_ISAXIS];
352 int stickNum = [stickFn oo_intForKey:STICK_NUMBER];
353 int stickAxBt = [stickFn oo_intForKey:STICK_AXBUT];
354
355 if (isAxis) {
356 [self setFunctionForAxis:stickAxBt
357 function:function
358 stick:stickNum];
359 } else {
360 if (function >= BUTTON_GUILEFT && function <= BUTTON_GUISELECT)
361 {
362 [self setGuiFunctionForButton:stickAxBt
363 function:function
364 stick:stickNum];
365 }
366 else {
367 [self setFunctionForButton:stickAxBt
368 function:function
369 stick:stickNum];
370 }
371 }
372}
373
374- (void)setFunctionForAxis:(int)axis
375 function:(int)function
376 stick:(int)stickNum
377{
378 NSParameterAssert(axis < MAX_AXES && stickNum < MAX_STICKS);
379
380 int16_t axisvalue = [self getAxisWithStick:stickNum axis:axis];
381 [self unsetAxisFunction:function];
382 axismap[stickNum][axis] = function;
383
384 // initialize the throttle to what it's set to now (or else the
385 // commander has to waggle the throttle to wake it up). Other axes
386 // set as default.
387 if (function == AXIS_THRUST) {
388 axstate[function] = (float)(65536 - (axisvalue + 32768)) / 65536;
389 } else {
390 axstate[function] = (float)axisvalue / STICK_NORMALDIV;
391 }
392}
393
394- (void)setFunctionForButton:(int)button
395 function:(int)function
396 stick:(int)stickNum
397{
398 NSParameterAssert(button < MAX_BUTTONS && stickNum < MAX_STICKS);
399
400 int i, j;
401 for (i = 0; i < MAX_BUTTONS; i++) {
402 for (j = 0; j < MAX_STICKS; j++) {
403 if (buttonmap[j][i] == function) {
404 buttonmap[j][i] = STICK_NOFUNCTION;
405 break;
406 }
407 }
408 }
409 buttonmap[stickNum][button] = function;
410}
411
412- (void)setGuiFunctionForButton:(int)button
413 function:(int)function
414 stick:(int)stickNum
415{
416 NSParameterAssert(button < MAX_BUTTONS && stickNum < MAX_STICKS);
417
418 int i, j;
419 for (i = 0; i < MAX_BUTTONS; i++) {
420 for (j = 0; j < MAX_STICKS; j++) {
421 if (guibuttonmap[j][i] == function) {
423 break;
424 }
425 }
426 }
427 guibuttonmap[stickNum][button] = function;
428}
429
430- (void)unsetAxisFunction:(int)function
431{
432 int i, j;
433 for (i = 0; i < MAX_AXES; i++) {
434 for (j = 0; j < MAX_STICKS; j++) {
435 if (axismap[j][i] == function) {
438 break;
439 }
440 }
441 }
442}
443
444- (void)unsetButtonFunction:(int)function
445{
446 int i, j;
447 for (i = 0; i < MAX_BUTTONS; i++) {
448 for (j = 0; j < MAX_STICKS; j++) {
449 if (buttonmap[j][i] == function) {
451 break;
452 }
453 }
454 }
455}
456
457- (void)unsetGuiButtonFunction:(int)function
458{
459 int i, j;
460 for (i = 0; i < MAX_BUTTONS; i++) {
461 for (j = 0; j < MAX_STICKS; j++) {
462 if (guibuttonmap[j][i] == function) {
464 break;
465 }
466 }
467 }
468}
469
471{
472 // assign the simplest mapping: stick 0 having
473 // axis 0/1 being roll/pitch and button 0 being fire, 1 being missile
474 // All joysticks should at least have two axes and two buttons.
475 axismap[0][0] = AXIS_ROLL;
476 axismap[0][1] = AXIS_PITCH;
477 buttonmap[0][0] = BUTTON_FIRE;
479}
480
482{
483 memset(axismap, STICK_NOFUNCTION, sizeof axismap);
484 memset(buttonmap, STICK_NOFUNCTION, sizeof buttonmap);
485}
486
488{
489 int i, j;
490 for (i = 0; i < AXIS_end; i++) {
492 }
493 for (i = 0; i < BUTTON_end; i++) {
494 butstate[i] = 0;
495 }
496 for (i = 0; i < MAX_BUTTONS; i++) {
497 for (j = 0; j < MAX_STICKS; j++) {
498 true_butstate[j][i] = NO;
499 }
500 }
501}
502
503- (void)clearStickButtonState:(int)stickButton
504{
505 if (stickButton >= 0 && stickButton < BUTTON_end) {
507 }
508}
509
510- (void)setCallback:(SEL)selector
511 object:(id)obj
512 hardware:(char)hwflags
513{
514 cbObject = obj;
515 cbSelector = selector;
516 cbHardware = hwflags;
517}
518
520{
521 cbObject = nil;
522 cbHardware = 0;
523}
524
526{
527 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
528
529 [defaults setObject:[self axisFunctions]
530 forKey:AXIS_SETTINGS];
531 [defaults setObject:[self buttonFunctions]
532 forKey:BUTTON_SETTINGS];
533 [defaults setObject:[self guiButtonFunctions]
534 forKey:GUI_BUTTON_SETTINGS];
535 [self saveProfileForAxis:AXIS_ROLL];
536 [self saveProfileForAxis:AXIS_PITCH];
537 [self saveProfileForAxis:AXIS_YAW];
538 [defaults synchronize];
539}
540
542{
543 unsigned i;
544 [self clearMappings];
545 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
546 NSDictionary* axisSettings = [defaults objectForKey:AXIS_SETTINGS];
547 NSDictionary* buttonSettings = [defaults objectForKey:BUTTON_SETTINGS];
548 NSDictionary* guiButtonSettings = [defaults objectForKey:GUI_BUTTON_SETTINGS];
549
550 if (axisSettings) {
551 NSArray* keys = [axisSettings allKeys];
552 for (i = 0; i < [keys count]; i++) {
553 NSString* key = [keys objectAtIndex:i];
554 [self setFunction:[key intValue]
555 withDict:[axisSettings objectForKey:key]];
556 }
557 }
558 if (buttonSettings) {
559 NSArray* keys = [buttonSettings allKeys];
560 for (i = 0; i < [keys count]; i++) {
561 NSString* key = [keys objectAtIndex:i];
562 [self setFunction:[key intValue]
563 withDict:[buttonSettings objectForKey:key]];
564 }
565 } else {
566 // Nothing to load - set useful defaults
567 [self setDefaultMapping];
568 }
569 if (guiButtonSettings) {
570 NSArray* keys = [guiButtonSettings allKeys];
571 for (i = 0; i < [keys count]; i++) {
572 NSString* key = [keys objectAtIndex:i];
573 [self setFunction:[key intValue]
574 withDict:[guiButtonSettings objectForKey:key]];
575 }
576 }
577 [self loadProfileForAxis:AXIS_ROLL];
578 [self loadProfileForAxis:AXIS_PITCH];
579 [self loadProfileForAxis:AXIS_YAW];
580}
581
582// These get overidden by subclasses
583- (NSUInteger)joystickCount
584{
585 return 0;
586}
587
588- (NSString*)nameOfJoystick:(NSUInteger)stickNumber
589{
590 return @"Dummy joystick";
591}
592
593- (int16_t)getAxisWithStick:(NSUInteger)stickNum axis:(NSUInteger)axisNum
594{
595 return 0;
596}
597
598@end
#define MAX_AXES
#define MAX_STICKS
#define STICK_NOFUNCTION
#define MAX_BUTTONS
#define STICK_PRECISIONFAC
@ AXIS_VIEWX
@ AXIS_PITCH
@ AXIS_YAW
@ AXIS_end
@ AXIS_THRUST
@ AXIS_ROLL
@ AXIS_VIEWY
#define STICK_NORMALDIV
#define STICK_AXISUNASSIGNED
@ BUTTON_GUILEFT
@ BUTTON_end
@ BUTTON_LAUNCHMISSILE
@ BUTTON_GUISELECT
@ BUTTON_FIRE
static Class sStickHandlerClass
static id sSharedStickHandler
unsigned count
return nil
void setDeadzone:(double newValue)
NSDictionary * guiButtonFunctions()
void setProfile:forAxis:(OOJoystickAxisProfile *profile,[forAxis] int axis)
NSDictionary * buttonFunctions()
OOJoystickAxisProfile * roll_profile
int8_t axismap[MAX_STICKS][MAX_AXES]
void setFunction:withDict:(int function,[withDict] NSDictionary *stickFn)
NSString * nameOfJoystick:(NSUInteger stickNumber)
int8_t guibuttonmap[MAX_STICKS][MAX_BUTTONS]
NSDictionary * axisFunctions()
double axstate[AXIS_end]
OOJoystickAxisProfile * pitch_profile
void saveProfileForAxis:(int axis)
BOOL butstate[BUTTON_end]
void setFunctionForAxis:function:stick:(int axis, [function] int function, [stick] int stickNum)
void unsetAxisFunction:(int function)
void setGuiFunctionForButton:function:stick:(int button, [function] int function, [stick] int stickNum)
void loadProfileForAxis:(int axis)
int8_t buttonmap[MAX_STICKS][MAX_BUTTONS]
OOJoystickAxisProfile * yaw_profile
const BOOL * getAllButtonStates()
int16_t getAxisWithStick:axis:(NSUInteger stickNum,[axis] NSUInteger axisNum)
BOOL true_butstate[MAX_STICKS][MAX_BUTTONS]
void setFunctionForButton:function:stick:(int button, [function] int function, [stick] int stickNum)
OOJoystickAxisProfile * getProfileForAxis:(int axis)
void setParameter:(double newValue)