2 * $Source: /cvs/cvsroot/d2x/arch/sdl/joy.c,v $
5 * $Date: 2002-03-05 12:13:33 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.2 2001/12/03 02:43:02 bradleyb
11 * lots of makefile fixes, and sdl joystick stuff
13 * Revision 1.1 2001/10/24 09:25:05 bradleyb
14 * Moved input stuff to arch subdirs, as in d1x.
16 * Revision 1.1 2001/10/10 03:01:29 bradleyb
17 * Replacing win32 joystick (broken) with SDL joystick (stubs)
22 #define JOYSTICK_DEBUG
36 #define MAX_JOYSTICKS 16
39 #define MAX_AXES_PER_JOYSTICK 8
40 #define MAX_BUTTONS_PER_JOYSTICK 16
43 int num_joysticks = 0;
61 static struct joyinfo {
64 struct joyaxis axes[MAX_AXES];
65 struct joybutton buttons[MAX_BUTTONS];
72 int axis_map[MAX_AXES_PER_JOYSTICK];
73 int button_map[MAX_BUTTONS_PER_JOYSTICK];
74 } SDL_Joysticks[MAX_JOYSTICKS];
76 void joy_button_handler(SDL_JoyButtonEvent *jbe)
80 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
82 Joystick.buttons[button].state = jbe->state;
85 case SDL_JOYBUTTONDOWN:
86 Joystick.buttons[button].time_went_down
87 = timer_get_fixed_seconds();
88 Joystick.buttons[button].num_downs++;
91 Joystick.buttons[button].num_ups++;
96 void joy_axis_handler(SDL_JoyAxisEvent *jae)
100 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
102 Joystick.axes[axis].value = jae->value;
106 /* ----------------------------------------------- */
112 memset(&Joystick,0,sizeof(Joystick));
114 n = SDL_NumJoysticks();
116 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
117 for (i = 0; i < n; i++) {
118 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
119 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
120 if (SDL_Joysticks[num_joysticks].handle) {
122 SDL_Joysticks[num_joysticks].n_axes
123 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
124 SDL_Joysticks[num_joysticks].n_buttons
125 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
126 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
127 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
128 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
129 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
130 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
131 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
134 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
135 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
136 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
144 while (num_joysticks)
145 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
148 void joy_get_pos(int *x, int *y)
152 if (!num_joysticks) {
157 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
159 *x = joy_get_scaled_reading( axis[0], 0 );
160 *y = joy_get_scaled_reading( axis[1], 1 );
165 #if 0 // This is never used?
167 for (i=0; i++; i<buttons) {
168 switch (Joystick.buttons[i].state) {
182 int joy_get_button_down_cnt( int btn )
191 num_downs = Joystick.buttons[btn].num_downs;
192 Joystick.buttons[btn].num_downs = 0;
197 fix joy_get_button_down_time(int btn)
206 switch (Joystick.buttons[btn].state) {
208 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
209 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
219 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
228 for (i = 0; i <= JOY_NUM_AXES; i++) {
229 axis[i] = Joystick.axes[i].value;
230 #ifdef JOYSTICK_DEBUG
231 printf("sdl-joystick: axis %d: %d\n", i, axis[i]);
245 for (i = 0; i < Joystick.n_buttons; i++) {
246 Joystick.buttons[i].time_went_down = 0;
247 Joystick.buttons[i].num_downs = 0;
252 int joy_get_button_state( int btn )
257 if(btn >= Joystick.n_buttons)
262 return Joystick.buttons[btn].state;
265 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
269 for (i = 0; i < JOY_NUM_AXES; i++) {
270 axis_center[i] = Joystick.axes[i].center_val;
271 axis_min[i] = Joystick.axes[i].min_val;
272 axis_max[i] = Joystick.axes[i].max_val;
276 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
280 for (i = 0; i < JOY_NUM_AXES; i++) {
281 Joystick.axes[i].center_val = axis_center[i];
282 Joystick.axes[i].min_val = axis_min[i];
283 Joystick.axes[i].max_val = axis_max[i];
287 int joy_get_scaled_reading( int raw, int axis_num )
291 raw -= Joystick.axes[axis_num].center_val;
294 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
296 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
301 x = ((raw << 7) / d);
310 d = (joy_deadzone) * 6;
311 if ((x > (-1*d)) && (x < d))
314 #ifdef JOYSTICK_DEBUG
315 printf("scaled: axis %d: %d\n", axis_num, x);
321 void joy_set_slow_reading( int flag )