29#define kOOLogUnconvertedNSLog @"unclassified.OOSDLJoystickManager"
40 SDL_JoystickID* joystickIds = SDL_GetJoysticks(&
stickCount);
41 OOLog(
@"joystick.init",
@"Number of joysticks detected: %d",
stickCount);
44 OOLog(
@"joystick.init",
@"Number of joysticks detected exceeds maximum number of joysticks allowed. Setting number of active joysticks to %d.",
MAX_STICKS);
53 stick[
i] = SDL_OpenJoystick(joystickIds[i]);
57 OOLog(
@"joystick.init",
@"Failed to open joystick #%d", i);
60 SDL_SetJoystickEventsEnabled(
true);
62 SDL_free(joystickIds);
69 [joystickIdMap release];
73- (NSInteger)getJoystickIndexFromId:(SDL_JoystickID)joystickId
75 NSNumber* index = [joystickIdMap valueForKey:[
NSString stringWithFormat:@"%d", joystickId]];
77 return [
index integerValue];
87- (NSString*)nameOfJoystick:(NSUInteger)stickNumber
90 return @"(unknown joystick)";
94- (int16_t)getAxisWithStick:(NSUInteger)stickNum axis:(NSUInteger)axisNum
96 return SDL_GetJoystickAxis(
stick[stickNum], axisNum);
99- (SDL_JoyAxisEvent)makeJoyAxisEvent:(SDL_JoyAxisEvent*)sdlevt
101 SDL_JoyAxisEvent evt;
102 evt.type = sdlevt->type;
104 evt.axis = sdlevt->axis;
105 evt.value = sdlevt->value;
109- (SDL_JoyButtonEvent)makeJoyButtonEvent:(SDL_JoyButtonEvent*)sdlevt
111 SDL_JoyButtonEvent evt;
112 evt.type = sdlevt->type;
114 evt.button = sdlevt->button;
115 evt.down = sdlevt->down;
119- (SDL_JoyHatEvent)makeJoyHatEvent:(SDL_JoyHatEvent*)sdlevt
122 evt.type = sdlevt->type;
124 evt.hat = sdlevt->hat;
125 evt.value = sdlevt->value;
129- (void)decodeAxisEvent:(SDL_JoyAxisEvent*)evt
133 double axisvalue = (double)evt->value;
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,
145 [cbObject performSelector:cbSelector withObject:fnDict];
164 OOLog(
@"decodeAxisEvent",
@"Stick axis out of range - axis was %d", evt->axis);
191- (void)decodeButtonEvent:(SDL_JoyButtonEvent*)evt
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,
203 [cbObject performSelector:cbSelector withObject:fnDict];
217 OOLog(
@"decodeButtonEvent",
@"Joystick button out of range: %d", evt->button);
220 if (evt->type == SDL_EVENT_JOYSTICK_BUTTON_DOWN) {
229 if (guifunction >= 0) {
234- (void)decodeHatEvent:(SDL_JoyHatEvent*)evt
238 SDL_JoyButtonEvent btn;
240 btn.which = evt->which;
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;
246 btn.down = (evt->value & (1 << i));
254- (BOOL)handleSDLEvent:(SDL_Event*)evt
258 case SDL_EVENT_GAMEPAD_AXIS_MOTION:
259 case SDL_EVENT_JOYSTICK_AXIS_MOTION: {
261 if (joyEvt.which >= 0) {
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: {
273 if (joyEvt.which >= 0) {
280 case SDL_EVENT_JOYSTICK_HAT_MOTION: {
282 if (joyEvt.which >= 0) {
290 OOLog(
@"handleSDLEvent.unknownEvent",
@"%@",
@"JoystickHandler was sent an event it doesn't know");
#define OOLog(class, format,...)
uint8_t hatstate[MAX_STICKS][MAX_HATS]
int8_t axismap[MAX_STICKS][MAX_AXES]
int8_t guibuttonmap[MAX_STICKS][MAX_BUTTONS]
BOOL butstate[BUTTON_end]
int8_t buttonmap[MAX_STICKS][MAX_BUTTONS]
BOOL true_butstate[MAX_STICKS][MAX_BUTTONS]
NSDictionary * joystickIdMap
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]
NSUInteger joystickCount()