36@interface OOJoystickManager (Private)
39- (void) setFunctionForAxis:(
int)axis
40 function:(
int)function
43- (void) setFunctionForButton:(
int)button
44 function:(
int)function
53+ (id) sharedStickHandler
64+ (BOOL) setStickHandlerClass:(Class)aClass
66 NSAssert(
sStickHandlerClass ==
nil,
@"Can't set joystick handler class after joystick handler is initialized.");
67 NSParameterAssert(aClass == Nil || [aClass isSubclassOfClass:[
OOJoystickManager class]]);
76 if ((
self = [super init]))
84 [
self clearStickStates];
88 [
self loadStickSettings];
97- (NSPoint) rollPitchAxis
109- (BOOL) getButtonState: (
int)function
111 return butstate[function];
115- (const BOOL *)getAllButtonStates
120- (BOOL) isButtonDown:(
int)button stick:(
int)stickNum
122 return true_butstate[stickNum][button];
125- (double) getAxisState: (
int)function
140 return [roll_profile value:axstate[function]];
149 return [pitch_profile value:axstate[function]];
158 return [yaw_profile value:axstate[function]];
161 return axstate[function];
166- (double) getSensitivity
176 [roll_profile release];
177 roll_profile = [profile retain];
181 [pitch_profile release];
182 pitch_profile = [profile retain];
186 [yaw_profile release];
187 yaw_profile = [profile retain];
200 return pitch_profile;
208- (void) saveProfileForAxis: (
int) axis
210 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
211 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
215 NSArray *controlPoints;
216 NSMutableArray *points;
220 profile = [
self getProfileForAxis: axis];
221 if (!profile)
return;
222 [dict setObject: [NSNumber numberWithDouble: [profile
deadzone]] forKey: @"Deadzone"];
226 [dict setObject: @"Standard" forKey: @"Type"];
227 [dict setObject: [NSNumber numberWithDouble: [standard_profile
power]] forKey: @"Power"];
228 [dict setObject: [NSNumber numberWithDouble: [standard_profile
parameter]] forKey: @"Parameter"];
233 [dict setObject: @"Spline" forKey: @"Type"];
234 controlPoints = [NSArray arrayWithArray: [spline_profile
controlPoints]];
235 points = [[NSMutableArray alloc] initWithCapacity: [controlPoints count]];
236 for (i = 0; i < [controlPoints count]; i++)
238 point = [[controlPoints objectAtIndex: i] pointValue];
239 [points addObject: [NSArray arrayWithObjects:
240 [NSNumber numberWithFloat: point.x],
241 [NSNumber numberWithFloat: point.y],
244 [dict setObject: points forKey: @"ControlPoints"];
248 [dict setObject: @"Standard" forKey: @"Type"];
252 [defaults setObject: dict forKey: STICK_ROLL_AXIS_PROFILE_SETTING];
256 [defaults setObject: dict forKey: STICK_PITCH_AXIS_PROFILE_SETTING];
260 [defaults setObject: dict forKey: STICK_YAW_AXIS_PROFILE_SETTING];
267- (void) loadProfileForAxis: (
int) axis
269 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
276 dict = [defaults objectForKey: STICK_ROLL_AXIS_PROFILE_SETTING];
280 dict = [defaults objectForKey: STICK_PITCH_AXIS_PROFILE_SETTING];
284 dict = [defaults objectForKey: STICK_YAW_AXIS_PROFILE_SETTING];
291 NSString *type = [dict objectForKey: @"Type"];
292 if ([type isEqualToString:
@"Standard"])
295 [standard_profile
setDeadzone: [[dict objectForKey: @"Deadzone"] doubleValue]];
296 [standard_profile
setPower: [[dict objectForKey: @"Power"] doubleValue]];
297 [standard_profile
setParameter: [[dict objectForKey: @"Parameter"] doubleValue]];
298 [
self setProfile: [standard_profile autorelease] forAxis: axis];
300 else if([type isEqualToString:
@"Spline"])
303 [spline_profile
setDeadzone: [[dict objectForKey: @"Deadzone"] doubleValue]];
304 NSArray *points = [dict objectForKey: @"ControlPoints"], *pointArray;
308 for (i = 0; i < [points count]; i++)
310 pointArray = [points objectAtIndex: i];
311 if ([pointArray
count] >= 2)
313 point = NSMakePoint([[pointArray objectAtIndex: 0] floatValue], [[pointArray objectAtIndex: 1] floatValue]);
317 [
self setProfile: [spline_profile autorelease] forAxis: axis];
325- (NSArray *)listSticks
327 NSUInteger i, stickCount = [
self joystickCount];
329 NSMutableArray *stickList = [NSMutableArray array];
330 for (i = 0; i < stickCount; i++)
332 [stickList addObject:[
self nameOfJoystick:i]];
338- (NSDictionary *) axisFunctions
341 NSMutableDictionary *fnList = [NSMutableDictionary dictionary];
348 if(axismap[j][i] >= 0)
350 NSDictionary *fnDict=[NSDictionary dictionaryWithObjectsAndKeys:
351 [NSNumber numberWithBool:YES], STICK_ISAXIS,
352 [NSNumber numberWithInt:j], STICK_NUMBER,
353 [NSNumber numberWithInt:i], STICK_AXBUT,
355 [fnList setValue: fnDict
356 forKey: ENUMKEY(axismap[j][i])];
364- (NSDictionary *)buttonFunctions
367 NSMutableDictionary *fnList = [NSMutableDictionary dictionary];
374 if(buttonmap[j][i] >= 0)
376 NSDictionary *fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
377 [NSNumber numberWithBool:NO], STICK_ISAXIS,
378 [NSNumber numberWithInt:j], STICK_NUMBER,
379 [NSNumber numberWithInt:i], STICK_AXBUT,
381 [fnList setValue:fnDict
382 forKey:ENUMKEY(buttonmap[j][i])];
390- (void) setFunction:(
int)function withDict:(NSDictionary *)stickFn
392 BOOL isAxis = [stickFn oo_boolForKey:STICK_ISAXIS];
393 int stickNum = [stickFn oo_intForKey:STICK_NUMBER];
394 int stickAxBt = [stickFn oo_intForKey:STICK_AXBUT];
398 [
self setFunctionForAxis:stickAxBt
404 [
self setFunctionForButton:stickAxBt
411- (void) setFunctionForAxis:(
int)axis
412 function:(
int)function
417 int16_t axisvalue = [
self getAxisWithStick:stickNum axis:axis];
418 [
self unsetAxisFunction:function];
419 axismap[stickNum][axis] =
function;
426 axstate[function] = (float)(65536 - (axisvalue + 32768)) / 65536;
435- (void) setFunctionForButton:(
int)button
436 function:(
int)function
446 if (buttonmap[j][i] ==
function)
453 buttonmap[stickNum][button] =
function;
457- (void) unsetAxisFunction:(
int)function
464 if (axismap[j][i] ==
function)
475- (void) unsetButtonFunction:(
int)function
482 if(buttonmap[j][i] ==
function)
492- (void) setDefaultMapping
504- (void) clearMappings
511- (void) clearStickStates
526 true_butstate[j][i] = NO;
532- (void) clearStickButtonState:(
int)stickButton
534 if (stickButton >= 0 && stickButton <
BUTTON_end)
536 butstate[stickButton] = 0;
541- (void)setCallback:(
SEL) selector
543 hardware:(
char)hwflags
546 cbSelector = selector;
547 cbHardware = hwflags;
562 double axisvalue = (double)evt->
value;
565 if(cbObject && (cbHardware &
HW_AXIS))
570 NSDictionary *fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
571 [NSNumber numberWithBool: YES], STICK_ISAXIS,
572 [NSNumber numberWithInt: evt->which], STICK_NUMBER,
573 [NSNumber numberWithInt: evt->axis], STICK_AXBUT,
576 [cbObject performSelector:cbSelector withObject:fnDict];
594 function = axismap[evt->which][evt->axis];
598 OOLog(
@"decodeAxisEvent",
@"Stick axis out of range - axis was %d", evt->axis);
608 axstate[function] = (float)(65536 - (axisvalue + 32768)) / 65536;
620 axstate[function] = axisvalue / 32768;
622 if ((
function ==
AXIS_PITCH) && invertPitch) axstate[
function] = -1.0*axstate[
function];
633 NSDictionary *fnDict = [NSDictionary dictionaryWithObjectsAndKeys:
634 [NSNumber numberWithBool: NO], STICK_ISAXIS,
635 [NSNumber numberWithInt: evt->which], STICK_NUMBER,
636 [NSNumber numberWithInt: evt->button], STICK_AXBUT,
639 [cbObject performSelector:cbSelector withObject:fnDict];
650 function = buttonmap[evt->which][evt->button];
654 OOLog(
@"decodeButtonEvent",
@"Joystick button out of range: %d", evt->button);
660 if(function == BUTTON_PRECISION)
661 precisionMode = !precisionMode;
666 butstate[function]=bs;
680 for (i = 0; i < 4; ++i)
682 if ((evt->
value ^ hatstate[evt->
which][evt->
hat]) & (1 << i))
684 btn.type = (evt->value & (1 << i)) ? JOYBUTTONDOWN : JOYBUTTONUP;
685 btn.button = MAX_REAL_BUTTONS + i + evt->which * 4;
686 btn.state = (evt->value & (1 << i)) ? JOYBUTTON_PRESSED : JOYBUTTON_RELEASED;
687 [self decodeButtonEvent:&btn];
695- (NSUInteger) joystickCount
701- (void) saveStickSettings
703 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
705 [defaults setObject:[
self axisFunctions]
706 forKey:AXIS_SETTINGS];
707 [defaults setObject:[
self buttonFunctions]
708 forKey:BUTTON_SETTINGS];
709 [
self saveProfileForAxis: AXIS_ROLL];
710 [
self saveProfileForAxis: AXIS_PITCH];
711 [
self saveProfileForAxis: AXIS_YAW];
712 [defaults synchronize];
716- (void) loadStickSettings
719 [
self clearMappings];
720 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
721 NSDictionary *axisSettings = [defaults objectForKey: AXIS_SETTINGS];
722 NSDictionary *buttonSettings = [defaults objectForKey: BUTTON_SETTINGS];
725 NSArray *keys = [axisSettings allKeys];
726 for (i = 0; i < [keys count]; i++)
728 NSString *key = [keys objectAtIndex: i];
729 [
self setFunction: [key intValue]
730 withDict: [axisSettings objectForKey: key]];
735 NSArray *keys = [buttonSettings allKeys];
736 for (i = 0; i < [keys count]; i++)
738 NSString *key = [keys objectAtIndex: i];
739 [
self setFunction:[key intValue]
740 withDict:[buttonSettings objectForKey: key]];
746 [
self setDefaultMapping];
748 [
self loadProfileForAxis: AXIS_ROLL];
749 [
self loadProfileForAxis: AXIS_PITCH];
750 [
self loadProfileForAxis: AXIS_YAW];
755- (NSString *) nameOfJoystick:(NSUInteger)stickNumber
757 return @"Dummy joystick";
760- (int16_t) getAxisWithStick:(NSUInteger)stickNum axis:(NSUInteger)axisNum
#define STICK_PRECISIONFAC
#define STICK_AXISUNASSIGNED
static Class sStickHandlerClass
static id sSharedStickHandler
void setDeadzone:(double newValue)
int addControl:(NSPoint point)
NSMutableArray * controlPoints
void setPower:(double newValue)
void setParameter:(double newValue)