2 * $Source: /cvs/cvsroot/d2x/arch/sdl/joy.c,v $
5 * $Date: 2002-03-23 09:13:00 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.3 2002/03/05 12:13:33 bradleyb
11 * SDL joystick stuff mostly done
13 * Revision 1.2 2001/12/03 02:43:02 bradleyb
14 * lots of makefile fixes, and sdl joystick stuff
16 * Revision 1.1 2001/10/24 09:25:05 bradleyb
17 * Moved input stuff to arch subdirs, as in d1x.
19 * Revision 1.1 2001/10/10 03:01:29 bradleyb
20 * Replacing win32 joystick (broken) with SDL joystick (stubs)
25 #define JOYSTICK_DEBUG
39 #define MAX_JOYSTICKS 16
42 #define MAX_AXES_PER_JOYSTICK 8
43 #define MAX_BUTTONS_PER_JOYSTICK 16
46 int num_joysticks = 0;
64 static struct joyinfo {
67 struct joyaxis axes[MAX_AXES];
68 struct joybutton buttons[MAX_BUTTONS];
75 int axis_map[MAX_AXES_PER_JOYSTICK];
76 int button_map[MAX_BUTTONS_PER_JOYSTICK];
77 } SDL_Joysticks[MAX_JOYSTICKS];
79 void joy_button_handler(SDL_JoyButtonEvent *jbe)
83 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
85 Joystick.buttons[button].state = jbe->state;
88 case SDL_JOYBUTTONDOWN:
89 Joystick.buttons[button].time_went_down
90 = timer_get_fixed_seconds();
91 Joystick.buttons[button].num_downs++;
94 Joystick.buttons[button].num_ups++;
99 void joy_axis_handler(SDL_JoyAxisEvent *jae)
103 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
105 Joystick.axes[axis].value = jae->value;
109 /* ----------------------------------------------- */
115 memset(&Joystick,0,sizeof(Joystick));
117 n = SDL_NumJoysticks();
119 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
120 for (i = 0; i < n; i++) {
121 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
122 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
123 if (SDL_Joysticks[num_joysticks].handle) {
125 SDL_Joysticks[num_joysticks].n_axes
126 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
127 SDL_Joysticks[num_joysticks].n_buttons
128 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
129 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
130 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
131 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
132 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
133 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
134 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
137 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
138 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
139 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
147 while (num_joysticks)
148 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
151 void joy_get_pos(int *x, int *y)
155 if (!num_joysticks) {
160 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
162 *x = joy_get_scaled_reading( axis[0], 0 );
163 *y = joy_get_scaled_reading( axis[1], 1 );
168 #if 0 // This is never used?
170 for (i=0; i++; i<buttons) {
171 switch (Joystick.buttons[i].state) {
185 int joy_get_button_down_cnt( int btn )
194 num_downs = Joystick.buttons[btn].num_downs;
195 Joystick.buttons[btn].num_downs = 0;
200 fix joy_get_button_down_time(int btn)
209 switch (Joystick.buttons[btn].state) {
211 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
212 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
222 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
231 for (i = 0; i <= JOY_NUM_AXES; i++) {
232 axis[i] = Joystick.axes[i].value;
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 )
294 raw -= Joystick.axes[axis_num].center_val;
297 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
299 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
304 x = ((raw << 7) / d);
313 d = (joy_deadzone) * 6;
314 if ((x > (-1*d)) && (x < d))
321 void joy_set_slow_reading( int flag )