gcc 3.2 warnings
[btb/d2x.git] / arch / sdl / joy.c
1 /* $Id: joy.c,v 1.8 2003-01-11 02:57:39 btb Exp $ */
2 /*
3  *
4  * SDL joystick support
5  *
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <conf.h>
11 #endif
12
13 #include <string.h>   // for memset
14 #include <SDL/SDL.h>
15
16 #include "joy.h"
17 #include "error.h"
18 #include "timer.h"
19 #include "console.h"
20 #include "event.h"
21
22 #define MAX_JOYSTICKS 16
23 #define MAX_AXES 32
24
25 #define MAX_AXES_PER_JOYSTICK 8
26 #define MAX_BUTTONS_PER_JOYSTICK 16
27
28 char joy_present = 0;
29 int num_joysticks = 0;
30
31 int joy_deadzone = 0;
32
33 struct joybutton {
34         int state;
35         fix time_went_down;
36         int num_downs;
37         int num_ups;
38 };
39
40 struct joyaxis {
41         int             value;
42         int             min_val;
43         int             center_val;
44         int             max_val;
45 };
46
47 static struct joyinfo {
48         int n_axes;
49         int n_buttons;
50         struct joyaxis axes[MAX_AXES];
51         struct joybutton buttons[MAX_BUTTONS];
52 } Joystick;
53
54 static struct {
55         SDL_Joystick *handle;
56         int n_axes;
57         int n_buttons;
58         int axis_map[MAX_AXES_PER_JOYSTICK];
59         int button_map[MAX_BUTTONS_PER_JOYSTICK];
60 } SDL_Joysticks[MAX_JOYSTICKS];
61
62 void joy_button_handler(SDL_JoyButtonEvent *jbe)
63 {
64         int button;
65
66         button = SDL_Joysticks[jbe->which].button_map[jbe->button];
67
68         Joystick.buttons[button].state = jbe->state;
69
70         switch (jbe->type) {
71         case SDL_JOYBUTTONDOWN:
72                 Joystick.buttons[button].time_went_down
73                         = timer_get_fixed_seconds();
74                 Joystick.buttons[button].num_downs++;
75                 break;
76         case SDL_JOYBUTTONUP:
77                 Joystick.buttons[button].num_ups++;
78                 break;
79         }
80 }
81
82 void joy_axis_handler(SDL_JoyAxisEvent *jae)
83 {
84         int axis;
85
86         axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
87         
88         Joystick.axes[axis].value = jae->value;
89 }
90
91
92 /* ----------------------------------------------- */
93
94 int joy_init()
95 {
96         int i,j,n;
97
98         if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
99                 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
100                 return 0;
101         }
102
103         memset(&Joystick,0,sizeof(Joystick));
104
105         n = SDL_NumJoysticks();
106
107         con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
108         for (i = 0; i < n; i++) {
109                 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
110                 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
111                 if (SDL_Joysticks[num_joysticks].handle) {
112                         joy_present = 1;
113                         SDL_Joysticks[num_joysticks].n_axes
114                                 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
115                         SDL_Joysticks[num_joysticks].n_buttons
116                                 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
117                         con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
118                         con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
119                         for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
120                                 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
121                         for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
122                                 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
123                         num_joysticks++;
124                 } else
125                         con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
126                 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
127                 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
128         }
129                 
130         return joy_present;
131 }
132
133 void joy_close()
134 {
135         while (num_joysticks)
136                 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
137 }
138
139 void joy_get_pos(int *x, int *y)
140 {
141         int axis[MAX_AXES];
142
143         if (!num_joysticks) {
144                 *x=*y=0;
145                 return;
146         }
147
148         joystick_read_raw_axis (JOY_ALL_AXIS, axis);
149
150         *x = joy_get_scaled_reading( axis[0], 0 );
151         *y = joy_get_scaled_reading( axis[1], 1 );
152 }
153
154 int joy_get_btns()
155 {
156 #if 0 // This is never used?
157         int i, buttons = 0;
158         for (i=0; i++; i<buttons) {
159                 switch (Joystick.buttons[i].state) {
160                 case SDL_PRESSED:
161                         buttons |= 1<<i;
162                         break;
163                 case SDL_RELEASED:
164                         break;
165                 }
166         }
167         return buttons;
168 #else
169         return 0;
170 #endif
171 }
172
173 int joy_get_button_down_cnt( int btn )
174 {
175         int num_downs;
176
177         if (!num_joysticks)
178                 return 0;
179
180         event_poll();
181
182         num_downs = Joystick.buttons[btn].num_downs;
183         Joystick.buttons[btn].num_downs = 0;
184
185         return num_downs;
186 }
187
188 fix joy_get_button_down_time(int btn)
189 {
190         fix time = F0_0;
191
192         if (!num_joysticks)
193                 return 0;
194
195         event_poll();
196
197         switch (Joystick.buttons[btn].state) {
198         case SDL_PRESSED:
199                 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
200                 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
201                 break;
202         case SDL_RELEASED:
203                 time = 0;
204                 break;
205         }
206
207         return time;
208 }
209
210 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
211 {
212         int i;
213         
214         if (!num_joysticks)
215                 return 0;
216
217         event_poll();
218
219         for (i = 0; i <= JOY_NUM_AXES; i++) {
220                 axis[i] = Joystick.axes[i].value;
221         }
222
223         return 0;
224 }
225
226 void joy_flush()
227 {
228         int i;
229
230         if (!num_joysticks)
231                 return;
232
233         for (i = 0; i < Joystick.n_buttons; i++) {
234                 Joystick.buttons[i].time_went_down = 0;
235                 Joystick.buttons[i].num_downs = 0;
236         }
237         
238 }
239
240 int joy_get_button_state( int btn )
241 {
242         if (!num_joysticks)
243                 return 0;
244
245         if(btn >= Joystick.n_buttons)
246                 return 0;
247
248         event_poll();
249
250         return Joystick.buttons[btn].state;
251 }
252
253 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
254 {
255         int i;
256
257         for (i = 0; i < JOY_NUM_AXES; i++) {
258                 axis_center[i] = Joystick.axes[i].center_val;
259                 axis_min[i] = Joystick.axes[i].min_val;
260                 axis_max[i] = Joystick.axes[i].max_val;
261         }
262 }
263
264 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
265 {
266         int i;
267
268         for (i = 0; i < JOY_NUM_AXES; i++) {
269                 Joystick.axes[i].center_val = axis_center[i];
270                 Joystick.axes[i].min_val = axis_min[i];
271                 Joystick.axes[i].max_val = axis_max[i];
272         }
273 }
274
275 int joy_get_scaled_reading( int raw, int axis_num )
276 {
277 #if 1
278         return raw/256;
279 #else
280         int d, x;
281
282         raw -= Joystick.axes[axis_num].center_val;
283         
284         if (raw < 0)
285                 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
286         else if (raw > 0)
287                 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
288         else
289                 d = 0;
290         
291         if (d)
292                 x = ((raw << 7) / d);
293         else
294                 x = 0;
295         
296         if ( x < -128 )
297                 x = -128;
298         if ( x > 127 )
299                 x = 127;
300         
301         d =  (joy_deadzone) * 6;
302         if ((x > (-1*d)) && (x < d))
303                 x = 0;
304         
305         return x;
306 #endif
307 }
308
309 void joy_set_slow_reading( int flag )
310 {
311 }