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
32@implementation OOSDLJoystickManager
33
34- (id) init
35{
36 int i;
37
38 NSMutableDictionary *idMap = [[[NSMutableDictionary alloc] init] autorelease];
39
40 // Find and open the sticks. Make sure that we don't fail if more joysticks than MAX_STICKS are detected.
41 SDL_JoystickID *joystickIds = SDL_GetJoysticks(&stickCount);
42 OOLog(@"joystick.init", @"Number of joysticks detected: %d", stickCount);
44 {
46 OOLog(@"joystick.init", @"Number of joysticks detected exceeds maximum number of joysticks allowed. Setting number of active joysticks to %d.", MAX_STICKS);
47 }
48 if(stickCount)
49 {
50 for(i = 0; i < stickCount; i++)
51 {
52 // it's doubtful MAX_STICKS will ever get exceeded, but
53 // we need to be defensive.
54 if(i > MAX_STICKS)
55 break;
56
57 stick[i]=SDL_OpenJoystick(joystickIds[i]);
58 if(stick[i])
59 {
60 [idMap setObject: [NSNumber numberWithInt: i] forKey: [NSString stringWithFormat: @"%d", joystickIds[i]]];
61 }
62 else
63 {
64 OOLog(@"joystick.init", @"Failed to open joystick #%d", i);
65 }
66 }
67 SDL_SetJoystickEventsEnabled(true);
68 }
69 SDL_free(joystickIds);
70 joystickIdMap = [idMap copy];
71 return [super init];
72}
73
74
75- (void) dealloc
76{
77 [joystickIdMap release];
78 [super dealloc];
79}
80
81
82- (NSInteger) getJoystickIndexFromId: (SDL_JoystickID) joystickId
83{
84 NSNumber *index = [joystickIdMap valueForKey: [NSString stringWithFormat: @"%d", joystickId]];
85 if (index)
86 {
87 return [index integerValue];
88 }
89 return -1;
90}
91
92
93- (JoyAxisEvent) makeJoyAxisEvent: (SDL_JoyAxisEvent*) sdlevt
94{
95 JoyAxisEvent evt;
96 evt.type = sdlevt->type;
97 evt.which = [self getJoystickIndexFromId: sdlevt->which];
98 evt.axis = sdlevt->axis;
99 evt.value = sdlevt->value;
100 return evt;
101}
102
103- (JoyButtonEvent) makeJoyButtonEvent: (SDL_JoyButtonEvent*) sdlevt
104{
105 JoyButtonEvent evt;
106 evt.type = sdlevt->type;
107 evt.which = [self getJoystickIndexFromId: sdlevt->which];
108 evt.button = sdlevt->button;
109 evt.down = sdlevt->down;
110 return evt;
111}
112
113
114- (JoyHatEvent) makeJoyHatEvent: (SDL_JoyHatEvent*) sdlevt
115{
116 JoyHatEvent evt;
117 evt.type = sdlevt->type;
118 evt.which = [self getJoystickIndexFromId: sdlevt->which];
119 evt.hat = sdlevt->hat;
120 evt.value = sdlevt->value;
121 return evt;
122}
123
124
125- (BOOL) handleSDLEvent: (SDL_Event *)evt
126{
127 BOOL rc=NO;
128 switch(evt->type)
129 {
130 case SDL_EVENT_GAMEPAD_AXIS_MOTION:
131 case SDL_EVENT_JOYSTICK_AXIS_MOTION:
132 {
133 JoyAxisEvent joyEvt = [self makeJoyAxisEvent: (SDL_JoyAxisEvent*)evt];
134 if (joyEvt.which >= 0)
135 {
136 [self decodeAxisEvent: &joyEvt];
137 rc=YES;
138 }
139 break;
140 }
141
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:
146 {
147 JoyButtonEvent joyEvt = [self makeJoyButtonEvent: (SDL_JoyButtonEvent*)evt];
148 if (joyEvt.which >= 0)
149 {
150 [self decodeButtonEvent: &joyEvt];
151 rc=YES;
152 }
153 break;
154 }
155
156 case SDL_EVENT_JOYSTICK_HAT_MOTION:
157 {
158 JoyHatEvent joyEvt = [self makeJoyHatEvent: (SDL_JoyHatEvent*)evt];
159 if (joyEvt.which >= 0)
160 {
161 [self decodeHatEvent: &joyEvt];
162 rc=YES;
163 }
164 break;
165 }
166
167 default:
168 OOLog(@"handleSDLEvent.unknownEvent", @"%@", @"JoystickHandler was sent an event it doesn't know");
169 }
170 return rc;
171}
172
173
174// Overrides
175
176- (NSUInteger) joystickCount
177{
178 return stickCount;
179}
180
181
182- (NSString *) nameOfJoystick:(NSUInteger)stickNumber
183{
184 if (stickNumber >= stickCount) return @"(unknown joystick)";
185 return [NSString stringWithUTF8String:SDL_GetJoystickName(stick[stickNumber])];
186}
187
188
189- (int16_t) getAxisWithStick:(NSUInteger) stickNum axis:(NSUInteger) axisNum
190{
191 return SDL_GetJoystickAxis(stick[stickNum], axisNum);
192}
193
194
195
196@end
#define MAX_STICKS
#define OOLog(class, format,...)
Definition OOLogging.h:88
SDL_Joystick * stick[MAX_STICKS]