Oolite
Loading...
Searching...
No Matches
OOSDLJoystickManager Class Reference

#include <OOSDLJoystickManager.h>

Inheritance diagram for OOSDLJoystickManager:
Collaboration diagram for OOSDLJoystickManager:

Instance Methods

(id) - init
(void) - dealloc
(NSInteger) - getJoystickIndexFromId:
(SDL_JoyAxisEvent) - makeJoyAxisEvent:
(SDL_JoyButtonEvent) - makeJoyButtonEvent:
(SDL_JoyHatEvent) - makeJoyHatEvent:
(void) - decodeAxisEvent:
(void) - decodeButtonEvent:
(void) - decodeHatEvent:
(BOOL) - handleSDLEvent:
(NSUInteger) - joystickCount [implementation]
(NSString *) - nameOfJoystick: [implementation]
(int16_t) - getAxisWithStick:axis: [implementation]
Instance Methods inherited from OOJoystickManager
(NSPoint) - rollPitchAxis
(NSPoint) - viewAxis
(void) - setFunction:withDict:
(void) - unsetAxisFunction:
(void) - unsetButtonFunction:
(void) - unsetGuiButtonFunction:
(BOOL) - isButtonDown:stick:
(BOOL) - getButtonState:
(double) - getAxisState:
(double) - getSensitivity
(void) - setProfile:forAxis:
(OOJoystickAxisProfile *) - getProfileForAxis:
(void) - saveProfileForAxis:
(void) - loadProfileForAxis:
(const BOOL *) - getAllButtonStates
(NSArray *) - listSticks
(NSDictionary *) - axisFunctions
(NSDictionary *) - buttonFunctions
(NSDictionary *) - guiButtonFunctions
(void) - setCallback:object:hardware:
(void) - clearCallback
(void) - setDefaultMapping
(void) - clearMappings
(void) - clearStickStates
(void) - clearStickButtonState:
(void) - saveStickSettings
(void) - loadStickSettings
(void) - setFunctionForAxis:function:stick: [implementation]
(void) - setFunctionForButton:function:stick: [implementation]
(void) - setGuiFunctionForButton:function:stick: [implementation]

Private Attributes

NSDictionary * joystickIdMap
SDL_Joystick * stick [MAX_STICKS]
int stickCount

Additional Inherited Members

Class Methods inherited from OOJoystickManager
(id) + sharedStickHandler
(BOOL) + setStickHandlerClass:
Protected Attributes inherited from OOJoystickManager
int8_t axismap [MAX_STICKS][MAX_AXES]
int8_t buttonmap [MAX_STICKS][MAX_BUTTONS]
int8_t guibuttonmap [MAX_STICKS][MAX_BUTTONS]
BOOL true_butstate [MAX_STICKS][MAX_BUTTONS]
double axstate [AXIS_end]
BOOL butstate [BUTTON_end]
uint8_t hatstate [MAX_STICKS][MAX_HATS]
BOOL precisionMode
id cbObject
SEL cbSelector
char cbHardware
BOOL invertPitch

Detailed Description

Definition at line 40 of file OOSDLJoystickManager.h.

Method Documentation

◆ dealloc

- (void) dealloc

Definition at line 67 of file OOSDLJoystickManager.m.

68{
69 [joystickIdMap release];
70 [super dealloc];
71}

References dealloc.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decodeAxisEvent:

- (void) decodeAxisEvent: (SDL_JoyAxisEvent*) evt

Definition at line 129 of file OOSDLJoystickManager.m.

129 :(SDL_JoyAxisEvent*)evt
130{
131 // Which axis moved? Does the value need to be made to fit a
132 // certain function? Convert axis value to a double.
133 double axisvalue = (double)evt->value;
134
135 // First check if there is a callback and...
136 if (cbObject && (cbHardware & HW_AXIS)) {
137 // ...then check if axis moved more than AXCBTHRESH - (fix for BUG #17482)
138 if (axisvalue > AXCBTHRESH) {
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,
143 nil];
144 cbHardware = 0;
145 [cbObject performSelector:cbSelector withObject:fnDict];
146 cbObject = nil;
147 }
148
149 // we are done.
150 return;
151 }
152
153 // SDL seems to have some bizarre (perhaps a bug) behaviour when
154 // events get queued up because the game isn't ready to handle
155 // them (perhaps it's loading a commander and initializing the
156 // universe, and the main event loop is blocked).
157 // What happens is SDL lies about the axis that was triggered. For
158 // each queued event it adds 1 to the axis number!! This does
159 // not seem to happen with buttons.
160 int function;
161 if (evt->axis < MAX_AXES) {
162 function = axismap[evt->which][evt->axis];
163 } else {
164 OOLog(@"decodeAxisEvent", @"Stick axis out of range - axis was %d", evt->axis);
165 return;
166 }
167 switch (function) {
168 case STICK_NOFUNCTION:
169 // do nothing
170 break;
171 case AXIS_THRUST:
172 // Normalize the thrust setting.
173 axstate[function] = (float)(65536 - (axisvalue + 32768)) / 65536;
174 break;
175 case AXIS_ROLL:
176 case AXIS_PITCH:
177 case AXIS_YAW:
178 case AXIS_VIEWX:
179 case AXIS_VIEWY:
180 axstate[function] = axisvalue / STICK_NORMALDIV;
181 break;
182 // TODO AXIS_FIELD_OF_VIEW
183 default:
184 // set the state with no modification.
185 axstate[function] = axisvalue / 32768;
186 }
187 if ((function == AXIS_PITCH) && invertPitch)
188 axstate[function] = -1.0 * axstate[function];
189}
#define MAX_AXES
#define STICK_NOFUNCTION
#define AXCBTHRESH
@ AXIS_VIEWX
@ AXIS_PITCH
@ AXIS_YAW
@ AXIS_THRUST
@ AXIS_ROLL
@ AXIS_VIEWY
#define STICK_NORMALDIV
#define HW_AXIS
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
int8_t axismap[MAX_STICKS][MAX_AXES]
double axstate[AXIS_end]

References AXCBTHRESH, AXIS_PITCH, AXIS_ROLL, AXIS_THRUST, AXIS_VIEWX, AXIS_VIEWY, AXIS_YAW, OOJoystickManager::axismap, OOJoystickManager::axstate, OOJoystickManager::cbHardware, OOJoystickManager::cbObject, HW_AXIS, OOJoystickManager::invertPitch, MAX_AXES, nil, OOLog, STICK_NOFUNCTION, and STICK_NORMALDIV.

Referenced by handleSDLEvent:.

Here is the caller graph for this function:

◆ decodeButtonEvent:

- (void) decodeButtonEvent: (SDL_JoyButtonEvent*) evt

Definition at line 191 of file OOSDLJoystickManager.m.

191 :(SDL_JoyButtonEvent*)evt
192{
193 BOOL bs = NO;
194
195 // Is there a callback we need to make?
196 if (cbObject && (cbHardware & HW_BUTTON)) {
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,
201 nil];
202 cbHardware = 0;
203 [cbObject performSelector:cbSelector withObject:fnDict];
204 cbObject = nil;
205
206 // we are done.
207 return;
208 }
209
210 // Defensive measure - see comments in the axis handler for why.
211 int function;
212 int guifunction;
213 if (evt->button < MAX_BUTTONS) {
214 function = buttonmap[evt->which][evt->button];
215 guifunction = guibuttonmap[evt->which][evt->button];
216 } else {
217 OOLog(@"decodeButtonEvent", @"Joystick button out of range: %d", evt->button);
218 return;
219 }
220 if (evt->type == SDL_EVENT_JOYSTICK_BUTTON_DOWN) {
221 bs = YES;
222 if (function == BUTTON_PRECISION)
224 }
225 true_butstate[evt->which][evt->button] = bs;
226 if (function >= 0) {
227 butstate[function] = bs;
228 }
229 if (guifunction >= 0) {
230 butstate[guifunction] = bs;
231 }
232}
#define HW_BUTTON
#define MAX_BUTTONS
@ BUTTON_PRECISION
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]

References OOJoystickManager::butstate, BUTTON_PRECISION, OOJoystickManager::buttonmap, OOJoystickManager::cbHardware, OOJoystickManager::cbObject, OOJoystickManager::guibuttonmap, HW_BUTTON, MAX_BUTTONS, nil, OOLog, OOJoystickManager::precisionMode, and OOJoystickManager::true_butstate.

Referenced by decodeHatEvent:, and handleSDLEvent:.

Here is the caller graph for this function:

◆ decodeHatEvent:

- (void) decodeHatEvent: (SDL_JoyHatEvent*) evt

Definition at line 234 of file OOSDLJoystickManager.m.

234 :(SDL_JoyHatEvent*)evt
235{
236 // HACK: handle this as a set of buttons
237 int i;
238 SDL_JoyButtonEvent btn;
239
240 btn.which = evt->which;
241
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;
245 btn.button = MAX_REAL_BUTTONS + i + evt->which * 4;
246 btn.down = (evt->value & (1 << i));
247 [self decodeButtonEvent:&btn];
248 }
249 }
250
251 hatstate[evt->which][evt->hat] = evt->value;
252}
uint8_t hatstate[MAX_STICKS][MAX_HATS]

References decodeButtonEvent:, OOJoystickManager::hatstate, and MAX_REAL_BUTTONS.

Referenced by handleSDLEvent:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAxisWithStick:axis:

- (int16_t) getAxisWithStick: (NSUInteger) stickNum
axis: (NSUInteger) axisNum 
implementation

Reimplemented from OOJoystickManager.

Definition at line 94 of file OOSDLJoystickManager.m.

94 :(NSUInteger)stickNum axis:(NSUInteger)axisNum
95{
96 return SDL_GetJoystickAxis(stick[stickNum], axisNum);
97}
SDL_Joystick * stick[MAX_STICKS]

References stick.

◆ getJoystickIndexFromId:

- (NSInteger) getJoystickIndexFromId: (SDL_JoystickID) joystickId

Definition at line 73 of file OOSDLJoystickManager.m.

73 :(SDL_JoystickID)joystickId
74{
75 NSNumber* index = [joystickIdMap valueForKey:[NSString stringWithFormat:@"%d", joystickId]];
76 if (index) {
77 return [index integerValue];
78 }
79 return -1;
80}

Referenced by makeJoyAxisEvent:, makeJoyButtonEvent:, and makeJoyHatEvent:.

Here is the caller graph for this function:

◆ handleSDLEvent:

- (BOOL) handleSDLEvent: (SDL_Event*) evt

Definition at line 254 of file OOSDLJoystickManager.m.

254 :(SDL_Event*)evt
255{
256 BOOL rc = NO;
257 switch (evt->type) {
258 case SDL_EVENT_GAMEPAD_AXIS_MOTION:
259 case SDL_EVENT_JOYSTICK_AXIS_MOTION: {
260 SDL_JoyAxisEvent joyEvt = [self makeJoyAxisEvent:(SDL_JoyAxisEvent*)evt];
261 if (joyEvt.which >= 0) {
262 [self decodeAxisEvent:&joyEvt];
263 rc = YES;
264 }
265 break;
266 }
267
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: {
272 SDL_JoyButtonEvent joyEvt = [self makeJoyButtonEvent:(SDL_JoyButtonEvent*)evt];
273 if (joyEvt.which >= 0) {
274 [self decodeButtonEvent:&joyEvt];
275 rc = YES;
276 }
277 break;
278 }
279
280 case SDL_EVENT_JOYSTICK_HAT_MOTION: {
281 SDL_JoyHatEvent joyEvt = [self makeJoyHatEvent:(SDL_JoyHatEvent*)evt];
282 if (joyEvt.which >= 0) {
283 [self decodeHatEvent:&joyEvt];
284 rc = YES;
285 }
286 break;
287 }
288
289 default:
290 OOLog(@"handleSDLEvent.unknownEvent", @"%@", @"JoystickHandler was sent an event it doesn't know");
291 }
292 return rc;
293}

References decodeAxisEvent:, decodeButtonEvent:, decodeHatEvent:, makeJoyAxisEvent:, makeJoyButtonEvent:, makeJoyHatEvent:, and OOLog.

Referenced by MyOpenGLView(Input)::pollControls.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init

- (id) init

Reimplemented from OOJoystickManager.

Definition at line 33 of file OOSDLJoystickManager.m.

34{
35 int i;
36
37 NSMutableDictionary* idMap = [[[NSMutableDictionary alloc] init] autorelease];
38
39 // Find and open the sticks. Make sure that we don't fail if more joysticks than MAX_STICKS are detected.
40 SDL_JoystickID* joystickIds = SDL_GetJoysticks(&stickCount);
41 OOLog(@"joystick.init", @"Number of joysticks detected: %d", stickCount);
42 if (stickCount > MAX_STICKS) {
44 OOLog(@"joystick.init", @"Number of joysticks detected exceeds maximum number of joysticks allowed. Setting number of active joysticks to %d.", MAX_STICKS);
45 }
46 if (stickCount) {
47 for (i = 0; i < stickCount; i++) {
48 // it's doubtful MAX_STICKS will ever get exceeded, but
49 // we need to be defensive.
50 if (i > MAX_STICKS)
51 break;
52
53 stick[i] = SDL_OpenJoystick(joystickIds[i]);
54 if (stick[i]) {
55 [idMap setObject:[NSNumber numberWithInt:i] forKey:[NSString stringWithFormat:@"%d", joystickIds[i]]];
56 } else {
57 OOLog(@"joystick.init", @"Failed to open joystick #%d", i);
58 }
59 }
60 SDL_SetJoystickEventsEnabled(true);
61 }
62 SDL_free(joystickIds);
63 joystickIdMap = [idMap copy];
64 return [super init];
65}
#define MAX_STICKS

References OOJoystickManager::init, init, joystickIdMap, MAX_STICKS, OOLog, stick, and stickCount.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ joystickCount

- (NSUInteger) joystickCount
implementation

Reimplemented from OOJoystickManager.

Definition at line 82 of file OOSDLJoystickManager.m.

83{
84 return stickCount;
85}

References joystickCount, and stickCount.

Referenced by joystickCount.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ makeJoyAxisEvent:

- (SDL_JoyAxisEvent) makeJoyAxisEvent: (SDL_JoyAxisEvent*) sdlevt

Definition at line 99 of file OOSDLJoystickManager.m.

99 :(SDL_JoyAxisEvent*)sdlevt
100{
101 SDL_JoyAxisEvent evt;
102 evt.type = sdlevt->type;
103 evt.which = [self getJoystickIndexFromId:sdlevt->which];
104 evt.axis = sdlevt->axis;
105 evt.value = sdlevt->value;
106 return evt;
107}

References getJoystickIndexFromId:.

Referenced by handleSDLEvent:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ makeJoyButtonEvent:

- (SDL_JoyButtonEvent) makeJoyButtonEvent: (SDL_JoyButtonEvent*) sdlevt

Definition at line 109 of file OOSDLJoystickManager.m.

109 :(SDL_JoyButtonEvent*)sdlevt
110{
111 SDL_JoyButtonEvent evt;
112 evt.type = sdlevt->type;
113 evt.which = [self getJoystickIndexFromId:sdlevt->which];
114 evt.button = sdlevt->button;
115 evt.down = sdlevt->down;
116 return evt;
117}

References getJoystickIndexFromId:.

Referenced by handleSDLEvent:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ makeJoyHatEvent:

- (SDL_JoyHatEvent) makeJoyHatEvent: (SDL_JoyHatEvent*) sdlevt

Definition at line 119 of file OOSDLJoystickManager.m.

119 :(SDL_JoyHatEvent*)sdlevt
120{
121 SDL_JoyHatEvent evt;
122 evt.type = sdlevt->type;
123 evt.which = [self getJoystickIndexFromId:sdlevt->which];
124 evt.hat = sdlevt->hat;
125 evt.value = sdlevt->value;
126 return evt;
127}

References getJoystickIndexFromId:.

Referenced by handleSDLEvent:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nameOfJoystick:

- (NSString *) nameOfJoystick: (NSUInteger) stickNumber
implementation

Reimplemented from OOJoystickManager.

Definition at line 87 of file OOSDLJoystickManager.m.

87 :(NSUInteger)stickNumber
88{
89 if (stickNumber >= stickCount)
90 return @"(unknown joystick)";
91 return [NSString stringWithUTF8String:SDL_GetJoystickName(stick[stickNumber])];
92}

References stickCount.

Member Data Documentation

◆ joystickIdMap

- (NSDictionary*) joystickIdMap
private

Definition at line 42 of file OOSDLJoystickManager.h.

Referenced by init.

◆ stick

- (SDL_Joystick* stick[MAX_STICKS])
private

Definition at line 43 of file OOSDLJoystickManager.h.

Referenced by getAxisWithStick:axis:, and init.

◆ stickCount

- (int) stickCount
private

Definition at line 44 of file OOSDLJoystickManager.h.

Referenced by init, joystickCount, and nameOfJoystick:.


The documentation for this class was generated from the following files: