Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOJoystickManager.h
Go to the documentation of this file.
1/*
2
3OOJoystickManager.h
4By Dylan Smith
5modified by Alex Smith and Jens Ayton
6
7JoystickHandler handles joystick events from SDL, and translates them
8into the appropriate action via a lookup table. The lookup table is
9stored as a simple array rather than an ObjC dictionary since this
10will be examined fairly often (once per frame during gameplay).
11
12Conversion methods are provided to convert between the internal
13representation and an NSDictionary (for loading/saving user defaults
14and for use in areas where portability/ease of coding are more important
15than performance such as the GUI)
16
17
18Oolite
19Copyright (C) 2004-2013 Giles C Williams and contributors
20
21This program is free software; you can redistribute it and/or
22modify it under the terms of the GNU General Public License
23as published by the Free Software Foundation; either version 2
24of the License, or (at your option) any later version.
25
26This program is distributed in the hope that it will be useful,
27but WITHOUT ANY WARRANTY; without even the implied warranty of
28MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29GNU General Public License for more details.
30
31You should have received a copy of the GNU General Public License
32along with this program; if not, write to the Free Software
33Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
34MA 02110-1301, USA.
35
36*/
37
38#import "OOCocoa.h"
39
40
41// Enums are used here rather than a more complex ObjC object because
42// these are required very frequently (once per frame) so must be light
43// on CPU cycles (try and avoid too many objc sendmsgs).
44// Controls that can be an axis
45enum {
53#if OO_FOV_INFLIGHT_CONTROL_ENABLED
54 AXIS_FIELD_OF_VIEW,
55#endif
57};
58
59// Controls that can be a button
60enum {
82 BUTTON_ENERGYBOMB, // now fast activate B
87 BUTTON_CLOAK, // now fast activate A
97#if OO_FOV_INFLIGHT_CONTROL_ENABLED
98 BUTTON_INC_FIELD_OF_VIEW,
99 BUTTON_DEC_FIELD_OF_VIEW,
100#endif
115
116// Stick constants
117#define MAX_STICKS 4
118#define MAX_AXES 16
119#define MAX_REAL_BUTTONS 64
120#define MAX_HATS 4
121#define MAX_BUTTONS (MAX_REAL_BUTTONS + 4 * MAX_HATS)
122#define STICK_NOFUNCTION -1
123#define STICK_AXISUNASSIGNED -10.0
124
125#define STICK_PRECISIONFAC 3
126#define STICK_NORMALDIV 32768
127#define STICK_PRECISIONDIV (STICK_PRECISIONFAC*STICK_NORMALDIV)
128
129#if OOLITE_MAC_OS_X
130#define STICK_DEADZONE 0.0025
131#else
132#define STICK_DEADZONE 0.05
133#endif
134
135#define STICK_MAX_DEADZONE (STICK_DEADZONE * 2)
136
137
138// Kind of stick device (these are bits - if any more are added,
139// the next one is 4 and so on).
140#define HW_AXIS 1
141#define HW_BUTTON 2
142
143// The threshold at which an axis can trigger a call back.
144// The max of abs(axis) is 32767.
145#define AXCBTHRESH 20000
146
147// Dictionary keys - used in the defaults file
148#define AXIS_SETTINGS @"JoystickAxes" // NSUserDefaults
149#define BUTTON_SETTINGS @"JoystickButs" // NSUserDefaults
150#define STICK_ISAXIS @"isAxis" // YES=axis NO=button
151#define STICK_NUMBER @"stickNum" // Stick number 0 to 4
152#define STICK_AXBUT @"stickAxBt" // Axis or button number
153#define STICK_FUNCTION @"stickFunc" // Function of axis/button
154#define STICK_ROLL_AXIS_PROFILE_SETTING @"RollAxisProfile" // Joystick Profiles
155#define STICK_PITCH_AXIS_PROFILE_SETTING @"PitchAxisProfile" // Joystick Profiles
156#define STICK_YAW_AXIS_PROFILE_SETTING @"YawAxisProfile" // Joystick Profiles
157// shortcut to make code more readable when using enum as key for
158// an NSDictionary
159#define ENUMKEY(x) [NSString stringWithFormat: @"%d", x]
160
161
162
163//SDL Abstracted constants
164
165#if OOLITE_SDL
166
167#import <SDL.h>
168
169enum
170{
171 JOYAXISMOTION = SDL_JOYAXISMOTION,
172 JOYBUTTONDOWN = SDL_JOYBUTTONDOWN,
173 JOYBUTTONUP = SDL_JOYBUTTONUP,
174 JOYBUTTON_PRESSED = SDL_PRESSED,
175 JOYBUTTON_RELEASED = SDL_RELEASED,
176 JOYHAT_MOTION = SDL_JOYHATMOTION,
177
178 JOYHAT_CENTERED = SDL_HAT_CENTERED,
179 JOYHAT_UP = SDL_HAT_UP,
180 JOYHAT_RIGHT = SDL_HAT_RIGHT,
181 JOYHAT_DOWN = SDL_HAT_DOWN,
182 JOYHAT_LEFT = SDL_HAT_LEFT,
183 JOYHAT_RIGHTUP = SDL_HAT_RIGHTUP,
184 JOYHAT_RIGHTDOWN = SDL_HAT_RIGHTDOWN,
185 JOYHAT_LEFTUP = SDL_HAT_LEFTUP,
186 JOYHAT_LEFTDOWN = SDL_HAT_LEFTDOWN,
187};
188
189typedef SDL_JoyButtonEvent JoyButtonEvent;
190typedef SDL_JoyAxisEvent JoyAxisEvent;
191typedef SDL_JoyHatEvent JoyHatEvent;
192
193#else
194
195enum
196{
203
205 JOYHAT_UP = 0x01,
213};
214
215// Abstracted SDL event types
216typedef struct
217{
218 uint32_t type;
219 uint8_t which;
220 uint8_t axis;
221 int value;
223
224typedef struct
225{
226 uint32_t type;
227 uint8_t which;
228 uint8_t button;
229 int state;
230
232
233typedef struct
234{
235 uint32_t type;
236 uint8_t which;
237 uint8_t hat;
238 uint8_t value;
239 uint8_t padding;
241
242#endif //OOLITE_SDL
243
244
245#import "OOJoystickProfile.h"
246
247@interface OOJoystickManager: NSObject
248{
249@private
250 // Axis/button mapping arrays
251 int8_t axismap[MAX_STICKS][MAX_AXES];
252 int8_t buttonmap[MAX_STICKS][MAX_BUTTONS];
253 BOOL true_butstate[MAX_STICKS][MAX_BUTTONS];
254 double axstate[AXIS_end];
255 BOOL butstate[BUTTON_end];
256 uint8_t hatstate[MAX_STICKS][MAX_HATS];
261
262 // Handle callbacks - the object, selector to call
263 // the desired function, and the hardware (axis or button etc.)
268
269}
270
271+ (id) sharedStickHandler;
272+ (BOOL) setStickHandlerClass:(Class)aClass;
273
274// General.
275// Note: handleSDLEvent returns a BOOL (YES we handled it or NO we
276// didn't) so in the future when more handler classes are written,
277// the GameView event loop can just go through an NSArray of handlers
278// until it finds a handler that handles the event.
279- (id) init;
280
281// Roll/pitch axis
282- (NSPoint) rollPitchAxis;
283
284// View axis
285- (NSPoint) viewAxis;
286
287// convert a dictionary into the internal function map
288- (void) setFunction:(int)function withDict: (NSDictionary *)stickFn;
289- (void) unsetAxisFunction:(int)function;
290- (void) unsetButtonFunction:(int)function;
291
292// Accessors and discovery about the hardware.
293// These work directly on the internal lookup table so to be fast
294// since they are likely to be called by the game loop.
295- (NSUInteger) joystickCount;
296- (BOOL) isButtonDown:(int)button stick:(int)stickNum;
297- (BOOL) getButtonState:(int)function;
298- (double) getAxisState:(int)function;
299- (double) getSensitivity;
300
301// Axis profile handling
302- (void) setProfile: (OOJoystickAxisProfile *) profile forAxis:(int) axis;
303- (OOJoystickAxisProfile *) getProfileForAxis: (int) axis;
304- (void) saveProfileForAxis: (int) axis;
305- (void) loadProfileForAxis: (int) axis;
306
307// This one just returns a pointer to the entire state array to
308// allow for multiple lookups with only one objc_sendMsg
309- (const BOOL *) getAllButtonStates;
310
311// Hardware introspection.
312- (NSArray *) listSticks;
313
314// These use NSDictionary/NSArray since they are used outside the game
315// loop and are needed for loading/saving defaults.
316- (NSDictionary *) axisFunctions;
317- (NSDictionary *) buttonFunctions;
318
319// Set a callback for the next moved axis/pressed button. hwflags
320// is in the form HW_AXIS | HW_BUTTON (or just one of).
321- (void)setCallback:(SEL)selector
322 object:(id)obj
323 hardware:(char)hwflags;
324- (void)clearCallback;
325
326// Methods generally only used by this class.
327- (void) setDefaultMapping;
328- (void) clearMappings;
329- (void) clearStickStates;
330- (void) clearStickButtonState: (int)stickButton;
331- (void) decodeAxisEvent: (JoyAxisEvent *)evt;
332- (void) decodeButtonEvent: (JoyButtonEvent *)evt;
333- (void) decodeHatEvent: (JoyHatEvent *)evt;
334- (void) saveStickSettings;
335- (void) loadStickSettings;
336
337
338//Methods that should be overridden by all subclasses
339- (NSString *) nameOfJoystick:(NSUInteger)stickNumber;
340- (int16_t) getAxisWithStick:(NSUInteger) stickNum axis:(NSUInteger)axisNum;
341
342@end
@ BUTTON_MFDSELECTNEXT
@ BUTTON_DOCKINGCLEARANCE
@ BUTTON_end
@ BUTTON_LAUNCHMISSILE
@ BUTTON_DOCKINGMUSIC
@ BUTTON_FUELINJECT
@ BUTTON_ENERGYBOMB
@ BUTTON_TARGETINCOMINGMISSILE
@ BUTTON_PRIMEEQUIPMENT
@ BUTTON_COMPASSMODE
@ BUTTON_ROTATECARGO
@ BUTTON_VIEWSTARBOARD
@ BUTTON_VIEWAFT
@ BUTTON_GALACTICDRIVE
@ BUTTON_MFDCYCLENEXT
@ BUTTON_MFDCYCLEPREV
@ BUTTON_ARMMISSILE
@ BUTTON_HYPERDRIVE
@ BUTTON_ACTIVATEEQUIPMENT
@ BUTTON_CLOAK
@ BUTTON_NEXTTARGET
@ BUTTON_FIRE
@ BUTTON_UNARM
@ BUTTON_CYCLEMISSILE
@ BUTTON_VIEWPORT
@ BUTTON_SNAPSHOT
@ BUTTON_DOCKCPU
@ BUTTON_HYPERSPEED
@ BUTTON_ESCAPE
@ BUTTON_JETTISON
@ BUTTON_SCANNERUNZOOM
@ BUTTON_PRECISION
@ BUTTON_MODEEQUIPMENT
@ BUTTON_VIEWFORWARD
@ BUTTON_WEAPONSONLINETOGGLE
@ BUTTON_EXTVIEWCYCLE
@ BUTTON_COMMSLOG
@ BUTTON_INCTHRUST
@ BUTTON_SCANNERZOOM
@ BUTTON_ID
@ BUTTON_ECM
@ BUTTON_COMPASSMODE_PREV
@ BUTTON_TOGGLEHUD
@ BUTTON_DOCKCPUFAST
@ BUTTON_PREVTARGET
@ BUTTON_DECTHRUST
@ BUTTON_PRIMEEQUIPMENT_PREV
@ BUTTON_PAUSE
@ BUTTON_MFDSELECTPREV
@ AXIS_VIEWX
@ AXIS_PITCH
@ AXIS_YAW
@ AXIS_end
@ AXIS_THRUST
@ AXIS_ROLL
@ AXIS_PRECISION
@ AXIS_VIEWY
@ JOYHAT_MOTION
@ JOYBUTTON_RELEASED
@ JOYHAT_DOWN
@ JOYHAT_LEFTDOWN
@ JOYHAT_RIGHTUP
@ JOYHAT_RIGHT
@ JOYHAT_LEFT
@ JOYHAT_LEFTUP
@ JOYHAT_RIGHTDOWN
@ JOYBUTTON_PRESSED
@ JOYBUTTONUP
@ JOYHAT_CENTERED
@ JOYAXISMOTION
@ JOYBUTTONDOWN
@ JOYHAT_UP
uint8_t hatstate[MAX_STICKS][MAX_HATS]
OOJoystickAxisProfile * roll_profile
int8_t axismap[MAX_STICKS][MAX_AXES]
double axstate[AXIS_end]
OOJoystickAxisProfile * pitch_profile
BOOL butstate[BUTTON_end]
int8_t buttonmap[MAX_STICKS][MAX_BUTTONS]
OOJoystickAxisProfile * yaw_profile
BOOL true_butstate[MAX_STICKS][MAX_BUTTONS]