1 /**********************************************************************
2 *   This file is part of Search and Rescue II (SaR2).                 *
3 *                                                                     *
4 *   SaR2 is free software: you can redistribute it and/or modify      *
5 *   it under the terms of the GNU General Public License v.2 as       *
6 *   published by the Free Software Foundation.                        *
7 *                                                                     *
8 *   SaR2 is distributed in the hope that it will be useful, but       *
9 *   WITHOUT ANY WARRANTY; without even the implied warranty of        *
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See          *
11 *   the GNU General Public License for more details.                  *
12 *                                                                     *
13 *   You should have received a copy of the GNU General Public License *
14 *   along with SaR2.  If not, see <http://www.gnu.org/licenses/>.     *
15 ***********************************************************************/
16 
17 /*
18 			       Game Controller
19  */
20 
21 #ifndef GCTL_H
22 #define GCTL_H
23 
24 #include <SDL/SDL.h>
25 #include <sys/types.h>
26 #include "gw.h"
27 
28 
29 /*
30  *	Game Controller:
31  *
32  *	Determines which controller(s) are in use or to be used.
33  */
34 typedef enum {
35 	GCTL_CONTROLLER_KEYBOARD		= (1 << 0),
36 	GCTL_CONTROLLER_POINTER			= (1 << 1),
37 	GCTL_CONTROLLER_JOYSTICK		= (1 << 2)
38 } gctl_controllers;
39 
40 
41 /*
42  *	Options:
43  */
44 typedef enum {
45 	GCTL_OPTION_NONE			= (1 << 0)
46 } gctl_options;
47 
48 
49 /*
50  *	Joystick Priorities:
51  */
52 typedef enum {
53 	GCTL_JS_PRIORITY_BACKGROUND,
54 	GCTL_JS_PRIORITY_FOREGROUND,
55 	GCTL_JS_PRIORITY_PREEMPT
56 } gctl_js_priority;
57 
58 /*
59  *	Joystick Connections:
60  */
61 typedef enum {
62 	GCTL_JS_CONNECTION_STANDARD		= 0,
63 	GCTL_JS_CONNECTION_USB			= 1
64 } gctl_js_connection;
65 
66 /*
67  *	Joystick Axis Roles:
68  */
69 typedef enum {
70 	GCTL_JS_AXIS_ROLE_PITCH			= (1 << 0),
71 	GCTL_JS_AXIS_ROLE_BANK			= (1 << 1),
72 	GCTL_JS_AXIS_ROLE_HEADING		= (1 << 2),
73 	GCTL_JS_AXIS_ROLE_THROTTLE		= (1 << 3),
74 	GCTL_JS_AXIS_ROLE_HAT			= (1 << 4),
75 	/* Throttle and rudder unit joystick */
76 	GCTL_JS_AXIS_ROLE_AS_THROTTLE_AND_RUDDER	= (1 << 6),
77 	/* Joystick unit with no axises (only buttons) */
78 	GCTL_JS_AXIS_ROLE_NONE			= (1 << 7)
79 } gctl_js_axis_roles;
80 
81 
82 /*
83  *	Pointer Buttons:
84  */
85 typedef enum {
86 	GCTL_POINTER_BUTTON1			= (1 << 0),
87 	GCTL_POINTER_BUTTON2			= (1 << 1),
88 	GCTL_POINTER_BUTTON3			= (1 << 2)
89 } gctl_pointer_buttons;
90 
91 
92 /*
93  *	Game Controller Joystick Values:
94  */
95 typedef struct {
96 
97 	/* Abstract string idenifying the device.
98 	 *
99 	 * When using libjsw this would be "/dev/js#" (where # is a
100 	 * number) or "/dev/input/js#" for libjsw USB devices
101 	 */
102 	char		*device;
103 
104 	/* Priority, one of GCTL_JS_PRIORITY_* */
105 	gctl_js_priority	priority;
106 
107 	/* Standard or USB connection, one of GCTL_JS_CONNECTION_* */
108 	gctl_js_connection	connection;
109 
110 	/* Pointer to the toplevel window handle (for Win32) */
111 	void		*window;
112 
113 	/* Axis role flags (any of GCTL_JS_AXIS_ROLE_*), this basically
114 	 * specifies how many axises there are and each of their roles
115 	 */
116 	gctl_js_axis_roles	axis_role;
117 
118 	/* Joystick button mappings, indicates which joystick button
119 	 * number corresponds with which action
120 	 */
121 	int		button_rotate,		/* Button that treats
122 						 * bank as heading axis
123 						 */
124 			button_air_brakes,
125 			button_wheel_brakes,
126 			button_zoom_in,
127 			button_zoom_out,
128 			button_hoist_up,
129 			button_hoist_down;
130 
131 } gctl_values_js_struct;
132 #define GCTL_VALUES_JS(p)	((gctl_values_js_struct *)(p))
133 
134 /*
135  *	Game Controller Values:
136  */
137 typedef struct {
138 
139 	/* Specifies which game controller type to check for (any of
140 	 * GCTL_CONTROLLER_*)
141 	 */
142 	gctl_controllers	controllers;
143 
144 	/* General options (any of GCTL_OPT_*) */
145 	gctl_options		options;
146 
147 	/* Joystick values, each structure reffers to a joystick */
148 	gctl_values_js_struct	*joystick;
149 	int			total_joysticks;
150 
151 } gctl_values_struct;
152 #define GCTL_VALUES(p)	((gctl_values_struct *)(p))
153 
154 
155 /*
156  *	Game Controller Joystick:
157  *
158  *	Used in gctl_struct
159  */
160 typedef struct {
161 
162 	/* Joystick device specific data handle
163 	 *
164 	 * For example, when using libjsw the void *joystick would be
165 	 * pointing to a js_data_struct structure
166 	 */
167 	void		*data;
168 
169 	/* Axis role mappings, reffers an axis by number to a specific
170 	 * role
171 	 *
172 	 * The axis number can be -1 for non-existant
173 	 */
174 	int		axis_heading,
175 			axis_bank,
176 			axis_pitch,
177 			axis_throttle,
178 			axis_hat_x,
179 			axis_hat_y;
180 
181 	/* Joystick button role mappings, reffers a button by number to
182 	 * a specific role (-1 for none)
183 	 */
184 	int		button_rotate,		/* Button that treats
185 						 * bank as heading axis
186 						 */
187 			button_air_brakes,
188 			button_wheel_brakes,
189 			button_zoom_in,
190 			button_zoom_out,
191 			button_hoist_up,
192 			button_hoist_down;
193 
194 } gctl_js_struct;
195 #define GCTL_JS(p)	((gctl_js_struct *)(p))
196 
197 /*
198  *	Game Controller:
199  *
200  *	Contains controller positions, values, and resources.
201  *
202  *	This is passed to all GCTL*() functions.
203  */
204 typedef struct {
205 
206 	/* Specifies which controllers are checked for, any of
207 	 * GCTL_CONTROLLER_*
208 	 */
209 	gctl_controllers	controllers;
210 
211 	/* General options (any of GCTL_OPT_*) */
212 	gctl_options		options;
213 
214 	/* Last time GCtlUpdate() was called passing this structure,
215 	 * in milliseconds
216 	 */
217 	time_t		last_updated;
218 
219 	/* Control positions */
220 	float		heading,	/* -1.0 to 1.0 */
221 			pitch,		/* -1.0 to 1.0 */
222 			bank,		/* -1.0 to 1.0 */
223 			throttle,	/*  0.0 to 1.0 */
224 			hat_x,		/* -1.0 to 1.0 */
225 			hat_y;		/* -1.0 to 1.0 */
226 
227 	/* Zoom in and out:
228 	 *      *_state members specifies a boolean on/off value
229 	 *      *_kb_last members record the last keyboard event for this
230 	 *      behavour
231 	 *      *_coeff members determines the magnitude [0.0 to 1.0] of
232 	 *      the state regardless if it is on or off. This is for when
233 	 *      the game controller has gone from off to on to off in one
234 	 *      call to GCtlUpdate()
235 	 */
236 	Boolean		zoom_in_state,
237 			zoom_out_state;
238 	time_t		zoom_in_kb_last,	/* In milliseconds */
239 			zoom_out_kb_last;
240 	float		zoom_in_coeff,		/* 0.0 to 1.0 */
241 			zoom_out_coeff;
242 
243 	/* Hoist up and down:
244 	 *      *_state members specifies a boolean on/off value.
245 	 *      *_kb_last members record the last keyboard event for this
246 	 *      behavour.
247 	 *      *_coeff members determines the magnitude [0.0 to 1.0] of
248 	 *      the state regardless if it is on or off. This is for when
249 	 *      the game controller has gone from off to on to off in one
250 	 *      call to GCtlUpdate().
251 	 */
252 	Boolean		hoist_up_state,
253 			hoist_down_state;
254 	time_t		hoist_up_kb_last,	/* In milliseconds */
255 			hoist_down_kb_last;
256 	float		hoist_up_coeff,		/* 0.0 to 1.0 */
257 			hoist_down_coeff;
258 
259 	/* Alt, ctrl, and shift (not nessasarly keyboard) states */
260 	Boolean		alt_state,
261 			ctrl_state,
262 			shift_state;
263 
264 	/* Wheel and air brakes:
265 	 *	*_state members specifies a boolean on/off value
266 	 *	*_kb_last members record the last keyboard event for this
267 	 *	behavior
268 	 *	*_coeff members determines the magnitude [0.0 to 1.0] of
269 	 *	the state regardless if it is on or off. This is for when
270 	 *	the game controller has gone from off to on to off in one
271 	 *	call to GCtlUpdate()
272 	 */
273 	Boolean		air_brakes_state;
274 	int		wheel_brakes_state;	/* 0 = off
275 						 * 1 = on
276 						 * 2 = parking brakes
277 						 */
278 	time_t		air_brakes_kb_last,	/* In milliseconds */
279 			wheel_brakes_kb_last;
280 	float		air_brakes_coeff,	/* 0.0 to 1.0 */
281 			wheel_brakes_coeff;
282 
283 
284 	/* Keyboard Specific Values */
285 
286 	/* Keyboard key states */
287 	Boolean		heading_kb_state,
288 			pitch_kb_state,
289 			bank_kb_state,
290 			throttle_kb_state,
291 			hat_x_kb_state,
292 			hat_y_kb_state;
293 
294 	/* Keyboard last press time stamps for control positions */
295 	time_t		heading_kb_last,	/* In milliseconds */
296 			pitch_kb_last,
297 			bank_kb_last,
298 			throttle_kb_last,
299 			hat_x_kb_last,
300 			hat_y_kb_last;
301 
302 	/* Set to True if a key is currently down and that key
303 	 * would/should override a joystick or pointer controlled
304 	 * axis.
305 	 */
306 	Boolean		axis_kb_state;
307 
308 	/* Set to True if a key is currently down and that key
309 	 * would/should override a joystick or pointer controlled
310 	 * button
311 	 */
312 	Boolean		button_kb_state;
313 
314 
315 	/* Pointer Specific Values */
316 
317 	/* Current pressed pointer button(s), any flag set to true
318 	 * means that button is pressed
319 	 */
320 	gctl_pointer_buttons	pointer_buttons;
321 
322 	/* Pointer box position and size in window coordinates, upper-left
323 	 * corner oriented
324 	 */
325 	int		pointer_box_x,
326 			pointer_box_y,
327 			pointer_box_width,
328 			pointer_box_height;
329 
330 	/* Last pointer event coordinates, in window coordinates relative
331 	 * to pointer_box_x and pointer_box_y (upper-left corner oriented)
332 	 */
333 	int		pointer_x,
334 			pointer_y;
335 
336 
337 	/* Joystick specific stuff */
338 
339 	/* Each structure reffers to a joystick */
340 	gctl_js_struct	*joystick;
341     int		total_joysticks;
342     /* we add a an array for SDL joystick here. */
343     SDL_Joystick **sdljoystick;
344 
345 } gctl_struct;
346 #define GCTL(p)		((gctl_struct *)(p))
347 
348 
349 extern char *GCtlGetError(void);
350 extern gctl_struct *GCtlNew(gctl_values_struct *v);
351 extern void GCtlUpdate(
352 	gctl_struct *gc,
353 	Boolean heading_has_nz,		/* Include heading pitch nullzone */
354 	Boolean pitch_has_nz,		/* Include pitch nullzone */
355 	Boolean bank_has_nz,		/* Include bank nullzone */
356 	time_t cur_ms, time_t lapsed_ms,
357 	float time_compensation
358 );
359 extern void GCtlHandlePointer(
360 	gw_display_struct *display, gctl_struct *gc,
361 	gw_event_type type,
362 	int button,		/* Button number */
363 	int x, int y,
364 	time_t t,		/* Time stamp (in ms) */
365 	time_t lapsed_ms
366 );
367 extern void GCtlHandleKey(
368 	gw_display_struct *display, gctl_struct *gc,
369 	int k, Boolean state,
370 	Boolean alt_state, Boolean ctrl_state, Boolean shift_state,
371 	time_t t,			/* Time stamp (in ms) */
372 	time_t lapsed_ms
373 );
374 extern void GCtlResetValues(gctl_struct *gc);
375 extern void GCtlResetTimmers(gctl_struct *gc);
376 extern void GCtlDelete(gctl_struct *gc);
377 
378 
379 #endif	/* GCTL_H */
380