Oolite
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 <SDL3/SDL.h>
168
169enum
170{
171 JOYAXISMOTION = SDL_EVENT_JOYSTICK_AXIS_MOTION,
172 JOYBUTTONDOWN = SDL_EVENT_JOYSTICK_BUTTON_DOWN,
173 JOYBUTTONUP = SDL_EVENT_JOYSTICK_BUTTON_UP,
174 JOYHAT_MOTION = SDL_EVENT_JOYSTICK_HAT_MOTION,
175 JOYHAT_CENTERED = SDL_HAT_CENTERED,
176 JOYHAT_UP = SDL_HAT_UP,
177 JOYHAT_RIGHT = SDL_HAT_RIGHT,
178 JOYHAT_DOWN = SDL_HAT_DOWN,
179 JOYHAT_LEFT = SDL_HAT_LEFT,
180 JOYHAT_RIGHTUP = SDL_HAT_RIGHTUP,
181 JOYHAT_RIGHTDOWN = SDL_HAT_RIGHTDOWN,
182 JOYHAT_LEFTUP = SDL_HAT_LEFTUP,
183 JOYHAT_LEFTDOWN = SDL_HAT_LEFTDOWN,
186};
187
188typedef SDL_JoyButtonEvent JoyButtonEvent;
189typedef SDL_JoyAxisEvent JoyAxisEvent;
190typedef SDL_JoyHatEvent JoyHatEvent;
191
192#else
193
194enum
195{
202
204 JOYHAT_UP = 0x01,
212};
213
214// Abstracted SDL event types
215typedef struct
216{
217 uint32_t type;
218 int32_t which;
219 uint8_t axis;
220 int16_t value;
222
223typedef struct
224{
225 uint32_t type;
226 int32_t which;
227 uint8_t button;
228 bool down;
229
231
232typedef struct
233{
234 uint32_t type;
235 int32_t which;
236 uint8_t hat;
237 uint8_t value;
238 uint8_t padding;
240
241#endif //OOLITE_SDL
242
243
244#import "OOJoystickProfile.h"
245
246@interface OOJoystickManager: NSObject
247{
248@private
249 // Axis/button mapping arrays
250 int8_t axismap[MAX_STICKS][MAX_AXES];
251 int8_t buttonmap[MAX_STICKS][MAX_BUTTONS];
252 BOOL true_butstate[MAX_STICKS][MAX_BUTTONS];
253 double axstate[AXIS_end];
254 BOOL butstate[BUTTON_end];
255 uint8_t hatstate[MAX_STICKS][MAX_HATS];
260
261 // Handle callbacks - the object, selector to call
262 // the desired function, and the hardware (axis or button etc.)
267
268}
269
270+ (id) sharedStickHandler;
271+ (BOOL) setStickHandlerClass:(Class)aClass;
272
273// General.
274// Note: handleSDLEvent returns a BOOL (YES we handled it or NO we
275// didn't) so in the future when more handler classes are written,
276// the GameView event loop can just go through an NSArray of handlers
277// until it finds a handler that handles the event.
278- (id) init;
279
280// Roll/pitch axis
281- (NSPoint) rollPitchAxis;
282
283// View axis
284- (NSPoint) viewAxis;
285
286// convert a dictionary into the internal function map
287- (void) setFunction:(int)function withDict: (NSDictionary *)stickFn;
288- (void) unsetAxisFunction:(int)function;
289- (void) unsetButtonFunction:(int)function;
290
291// Accessors and discovery about the hardware.
292// These work directly on the internal lookup table so to be fast
293// since they are likely to be called by the game loop.
294- (NSUInteger) joystickCount;
295- (BOOL) isButtonDown:(int)button stick:(int)stickNum;
296- (BOOL) getButtonState:(int)function;
297- (double) getAxisState:(int)function;
298- (double) getSensitivity;
299
300// Axis profile handling
301- (void) setProfile: (OOJoystickAxisProfile *) profile forAxis:(int) axis;
302- (OOJoystickAxisProfile *) getProfileForAxis: (int) axis;
303- (void) saveProfileForAxis: (int) axis;
304- (void) loadProfileForAxis: (int) axis;
305
306// This one just returns a pointer to the entire state array to
307// allow for multiple lookups with only one objc_sendMsg
308- (const BOOL *) getAllButtonStates;
309
310// Hardware introspection.
311- (NSArray *) listSticks;
312
313// These use NSDictionary/NSArray since they are used outside the game
314// loop and are needed for loading/saving defaults.
315- (NSDictionary *) axisFunctions;
316- (NSDictionary *) buttonFunctions;
317
318// Set a callback for the next moved axis/pressed button. hwflags
319// is in the form HW_AXIS | HW_BUTTON (or just one of).
320- (void)setCallback:(SEL)selector
321 object:(id)obj
322 hardware:(char)hwflags;
323- (void)clearCallback;
324
325// Methods generally only used by this class.
326- (void) setDefaultMapping;
327- (void) clearMappings;
328- (void) clearStickStates;
329- (void) clearStickButtonState: (int)stickButton;
330- (void) decodeAxisEvent: (JoyAxisEvent *)evt;
331- (void) decodeButtonEvent: (JoyButtonEvent *)evt;
332- (void) decodeHatEvent: (JoyHatEvent *)evt;
333- (void) saveStickSettings;
334- (void) loadStickSettings;
335
336
337//Methods that should be overridden by all subclasses
338- (NSString *) nameOfJoystick:(NSUInteger)stickNumber;
339- (int16_t) getAxisWithStick:(NSUInteger) stickNum axis:(NSUInteger)axisNum;
340
341@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]