29#define kOOLogUnconvertedNSLog @"unclassified.OOSDLJoystickManager"
38 NSMutableDictionary *idMap = [[[NSMutableDictionary alloc] init] autorelease];
41 SDL_JoystickID *joystickIds = SDL_GetJoysticks(&
stickCount);
42 OOLog(
@"joystick.init",
@"Number of joysticks detected: %d",
stickCount);
46 OOLog(
@"joystick.init",
@"Number of joysticks detected exceeds maximum number of joysticks allowed. Setting number of active joysticks to %d.",
MAX_STICKS);
57 stick[i]=SDL_OpenJoystick(joystickIds[i]);
60 [idMap setObject: [NSNumber numberWithInt: i] forKey: [NSString stringWithFormat: @"%d", joystickIds[i]]];
64 OOLog(
@"joystick.init",
@"Failed to open joystick #%d", i);
67 SDL_SetJoystickEventsEnabled(
true);
69 SDL_free(joystickIds);
77 [joystickIdMap release];
82- (NSInteger) getJoystickIndexFromId: (SDL_JoystickID) joystickId
84 NSNumber *index = [joystickIdMap valueForKey: [NSString stringWithFormat: @"%d", joystickId]];
87 return [index integerValue];
93- (
JoyAxisEvent) makeJoyAxisEvent: (SDL_JoyAxisEvent*) sdlevt
96 evt.
type = sdlevt->type;
97 evt.
which = [
self getJoystickIndexFromId: sdlevt->which];
98 evt.
axis = sdlevt->axis;
99 evt.
value = sdlevt->value;
103- (
JoyButtonEvent) makeJoyButtonEvent: (SDL_JoyButtonEvent*) sdlevt
106 evt.
type = sdlevt->type;
107 evt.
which = [
self getJoystickIndexFromId: sdlevt->which];
108 evt.button = sdlevt->button;
109 evt.down = sdlevt->down;
114- (
JoyHatEvent) makeJoyHatEvent: (SDL_JoyHatEvent*) sdlevt
117 evt.
type = sdlevt->type;
118 evt.
which = [
self getJoystickIndexFromId: sdlevt->which];
119 evt.hat = sdlevt->hat;
120 evt.
value = sdlevt->value;
125- (BOOL) handleSDLEvent: (SDL_Event *)evt
130 case SDL_EVENT_GAMEPAD_AXIS_MOTION:
131 case SDL_EVENT_JOYSTICK_AXIS_MOTION:
133 JoyAxisEvent joyEvt = [
self makeJoyAxisEvent: (SDL_JoyAxisEvent*)evt];
134 if (joyEvt.
which >= 0)
136 [
self decodeAxisEvent: &joyEvt];
142 case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
143 case SDL_EVENT_GAMEPAD_BUTTON_UP:
144 case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
145 case SDL_EVENT_JOYSTICK_BUTTON_UP:
147 JoyButtonEvent joyEvt = [
self makeJoyButtonEvent: (SDL_JoyButtonEvent*)evt];
148 if (joyEvt.
which >= 0)
150 [
self decodeButtonEvent: &joyEvt];
156 case SDL_EVENT_JOYSTICK_HAT_MOTION:
158 JoyHatEvent joyEvt = [
self makeJoyHatEvent: (SDL_JoyHatEvent*)evt];
159 if (joyEvt.
which >= 0)
161 [
self decodeHatEvent: &joyEvt];
168 OOLog(
@"handleSDLEvent.unknownEvent",
@"%@",
@"JoystickHandler was sent an event it doesn't know");
182- (NSString *) nameOfJoystick:(NSUInteger)stickNumber
184 if (stickNumber >=
stickCount)
return @"(unknown joystick)";
185 return [NSString stringWithUTF8String:SDL_GetJoystickName(stick[stickNumber])];
189- (int16_t) getAxisWithStick:(NSUInteger) stickNum axis:(NSUInteger) axisNum
191 return SDL_GetJoystickAxis(
stick[stickNum], axisNum);
#define OOLog(class, format,...)
NSDictionary * joystickIdMap
SDL_Joystick * stick[MAX_STICKS]
NSUInteger joystickCount()