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