]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/joy.c
change MAX_BUTTONS to JOY_MAX_BUTTONS
[btb/d2x.git] / arch / sdl / joy.c
1 /* $Id: joy.c,v 1.17 2005-04-04 09:18:08 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.h>
15
16 #include "joy.h"
17 #include "error.h"
18 #include "timer.h"
19 #include "console.h"
20 #include "event.h"
21 #include "text.h"
22 #include "u_mem.h"
23
24 #define MAX_JOYSTICKS 16
25
26 #define MAX_AXES_PER_JOYSTICK 8
27 #define MAX_BUTTONS_PER_JOYSTICK 16
28 #define MAX_HATS_PER_JOYSTICK 4
29
30 extern char *joybutton_text[]; //from kconfig.c
31 extern char *joyaxis_text[]; //from kconfig.c
32
33 char joy_present = 0;
34 int num_joysticks = 0;
35
36 int joy_deadzone = 0;
37
38 int joy_num_axes = 0;
39
40 struct joybutton {
41         int state;
42         int last_state;
43         fix time_went_down;
44         int num_downs;
45         int num_ups;
46 };
47
48 struct joyaxis {
49         int             value;
50         int             min_val;
51         int             center_val;
52         int             max_val;
53 };
54
55 /* This struct is a "virtual" joystick, which includes all the axes
56  * and buttons of every joystick found.
57  */
58 static struct joyinfo {
59         int n_axes;
60         int n_buttons;
61         struct joyaxis axes[JOY_MAX_AXES];
62         struct joybutton buttons[JOY_MAX_BUTTONS];
63 } Joystick;
64
65 /* This struct is an array, with one entry for each physical joystick
66  * found.
67  */
68 static struct {
69         SDL_Joystick *handle;
70         int n_axes;
71         int n_buttons;
72         int n_hats;
73         int hat_map[MAX_HATS_PER_JOYSTICK];  //Note: Descent expects hats to be buttons, so these are indices into Joystick.buttons
74         int axis_map[MAX_AXES_PER_JOYSTICK];
75         int button_map[MAX_BUTTONS_PER_JOYSTICK];
76 } SDL_Joysticks[MAX_JOYSTICKS];
77
78 void joy_button_handler(SDL_JoyButtonEvent *jbe)
79 {
80         int button;
81
82         button = SDL_Joysticks[jbe->which].button_map[jbe->button];
83
84         Joystick.buttons[button].state = jbe->state;
85
86         switch (jbe->type) {
87         case SDL_JOYBUTTONDOWN:
88                 Joystick.buttons[button].time_went_down
89                         = timer_get_fixed_seconds();
90                 Joystick.buttons[button].num_downs++;
91                 break;
92         case SDL_JOYBUTTONUP:
93                 Joystick.buttons[button].num_ups++;
94                 break;
95         }
96 }
97
98 void joy_hat_handler(SDL_JoyHatEvent *jhe)
99 {
100         int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
101         int hbi;
102
103         //Save last state of the hat-button
104         Joystick.buttons[hat  ].last_state = Joystick.buttons[hat  ].state;
105         Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
106         Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
107         Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
108
109         //get current state of the hat-button
110         Joystick.buttons[hat  ].state = ((jhe->value & SDL_HAT_UP)>0);
111         Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
112         Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
113         Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
114
115         //determine if a hat-button up or down event based on state and last_state
116         for(hbi=0;hbi<4;hbi++)
117         {
118                 if(     !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
119                 {
120                         Joystick.buttons[hat+hbi].time_went_down
121                                 = timer_get_fixed_seconds();
122                         Joystick.buttons[hat+hbi].num_downs++;
123                 }
124                 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state)  //last_state down, current state up
125                 {
126                         Joystick.buttons[hat+hbi].num_ups++;
127                 }
128         }
129 }
130
131 void joy_axis_handler(SDL_JoyAxisEvent *jae)
132 {
133         int axis;
134
135         axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
136         
137         Joystick.axes[axis].value = jae->value;
138 }
139
140
141 /* ----------------------------------------------- */
142
143 int joy_init()
144 {
145         int i,j,n;
146         char temp[10];
147
148         if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
149                 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
150                 return 0;
151         }
152
153         memset(&Joystick,0,sizeof(Joystick));
154
155         n = SDL_NumJoysticks();
156
157         con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
158         for (i = 0; i < n; i++) {
159                 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
160                 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
161                 if (SDL_Joysticks[num_joysticks].handle) {
162                         joy_present = 1;
163
164                         SDL_Joysticks[num_joysticks].n_axes
165                                 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
166                         if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
167                         {
168                                 Warning("sdl-joystick: found %d axes, only %d supported.  Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
169                                 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
170                         }
171
172                         SDL_Joysticks[num_joysticks].n_buttons
173                                 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
174                         if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
175                         {
176                                 Warning("sdl-joystick: found %d buttons, only %d supported.  Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
177                                 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
178                         }
179
180                         SDL_Joysticks[num_joysticks].n_hats
181                                 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
182                         if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
183                         {
184                                 Warning("sdl-joystick: found %d hats, only %d supported.  Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
185                                 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
186                         }
187
188                         con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
189                         con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
190                         con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
191
192                         for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
193                         {
194                                 sprintf(temp, "J%d A%d", i + 1, j + 1);
195                                 joyaxis_text[Joystick.n_axes] = d_strdup(temp);
196                                 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
197                         }
198                         for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
199                         {
200                                 sprintf(temp, "J%d B%d", i + 1, j + 1);
201                                 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
202                                 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
203                         }
204                         for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
205                         {
206                                 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
207                                 //a hat counts as four buttons
208                                 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0202);
209                                 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
210                                 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0177);
211                                 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
212                                 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0200);
213                                 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
214                                 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0201);
215                                 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
216                         }
217
218                         num_joysticks++;
219                 }
220                 else
221                         con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
222
223                 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
224                 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
225         }
226
227         joy_num_axes = Joystick.n_axes;
228         atexit(joy_close);
229
230         return joy_present;
231 }
232
233 void joy_close()
234 {
235         while (num_joysticks)
236                 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
237         while (Joystick.n_axes--)
238                 d_free(joyaxis_text[Joystick.n_axes]);
239         while (Joystick.n_buttons--)
240                 d_free(joybutton_text[Joystick.n_buttons]);
241 }
242
243 void joy_get_pos(int *x, int *y)
244 {
245         int axis[JOY_MAX_AXES];
246
247         if (!num_joysticks) {
248                 *x=*y=0;
249                 return;
250         }
251
252         joystick_read_raw_axis (JOY_ALL_AXIS, axis);
253
254         *x = joy_get_scaled_reading( axis[0], 0 );
255         *y = joy_get_scaled_reading( axis[1], 1 );
256 }
257
258 int joy_get_btns()
259 {
260 #if 0 // This is never used?
261         int i, buttons = 0;
262         for (i=0; i++; i<buttons) {
263                 switch (Joystick.buttons[i].state) {
264                 case SDL_PRESSED:
265                         buttons |= 1<<i;
266                         break;
267                 case SDL_RELEASED:
268                         break;
269                 }
270         }
271         return buttons;
272 #else
273         return 0;
274 #endif
275 }
276
277 int joy_get_button_down_cnt( int btn )
278 {
279         int num_downs;
280
281         if (!num_joysticks)
282                 return 0;
283
284         event_poll();
285
286         num_downs = Joystick.buttons[btn].num_downs;
287         Joystick.buttons[btn].num_downs = 0;
288
289         return num_downs;
290 }
291
292 fix joy_get_button_down_time(int btn)
293 {
294         fix time = F0_0;
295
296         if (!num_joysticks)
297                 return 0;
298
299         event_poll();
300
301         switch (Joystick.buttons[btn].state) {
302         case SDL_PRESSED:
303                 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
304                 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
305                 break;
306         case SDL_RELEASED:
307                 time = 0;
308                 break;
309         }
310
311         return time;
312 }
313
314 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
315 {
316         int i;
317         ubyte channel_masks = 0;
318         
319         if (!num_joysticks)
320                 return 0;
321
322         event_poll();
323
324         for (i = 0; i < Joystick.n_axes; i++)
325         {
326                 if ((axis[i] = Joystick.axes[i].value))
327                         channel_masks |= 1 << i;
328         }
329
330         return channel_masks;
331 }
332
333 void joy_flush()
334 {
335         int i;
336
337         if (!num_joysticks)
338                 return;
339
340         for (i = 0; i < Joystick.n_buttons; i++) {
341                 Joystick.buttons[i].time_went_down = 0;
342                 Joystick.buttons[i].num_downs = 0;
343         }
344         
345 }
346
347 int joy_get_button_state( int btn )
348 {
349         if (!num_joysticks)
350                 return 0;
351
352         if(btn >= Joystick.n_buttons)
353                 return 0;
354
355         event_poll();
356
357         return Joystick.buttons[btn].state;
358 }
359
360 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
361 {
362         int i;
363
364         for (i = 0; i < Joystick.n_axes; i++)
365         {
366                 axis_center[i] = Joystick.axes[i].center_val;
367                 axis_min[i] = Joystick.axes[i].min_val;
368                 axis_max[i] = Joystick.axes[i].max_val;
369         }
370 }
371
372 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
373 {
374         int i;
375
376         for (i = 0; i < Joystick.n_axes; i++)
377         {
378                 Joystick.axes[i].center_val = axis_center[i];
379                 Joystick.axes[i].min_val = axis_min[i];
380                 Joystick.axes[i].max_val = axis_max[i];
381         }
382 }
383
384 int joy_get_scaled_reading( int raw, int axis_num )
385 {
386 #if 1
387         return raw/256;
388 #else
389         int d, x;
390
391         raw -= Joystick.axes[axis_num].center_val;
392         
393         if (raw < 0)
394                 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
395         else if (raw > 0)
396                 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
397         else
398                 d = 0;
399         
400         if (d)
401                 x = ((raw << 7) / d);
402         else
403                 x = 0;
404         
405         if ( x < -128 )
406                 x = -128;
407         if ( x > 127 )
408                 x = 127;
409         
410         d =  (joy_deadzone) * 6;
411         if ((x > (-1*d)) && (x < d))
412                 x = 0;
413         
414         return x;
415 #endif
416 }
417
418 void joy_set_slow_reading( int flag )
419 {
420 }