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