2 * $Source: /cvs/cvsroot/d2x/arch/sdl/joy.c,v $
5 * $Date: 2002-07-16 22:03:45 $
9 * $Log: not supported by cvs2svn $
10 * Revision 1.4 2002/03/23 09:13:00 bradleyb
11 * SDL Joystick works\!
13 * Revision 1.3 2002/03/05 12:13:33 bradleyb
14 * SDL joystick stuff mostly done
16 * Revision 1.2 2001/12/03 02:43:02 bradleyb
17 * lots of makefile fixes, and sdl joystick stuff
19 * Revision 1.1 2001/10/24 09:25:05 bradleyb
20 * Moved input stuff to arch subdirs, as in d1x.
22 * Revision 1.1 2001/10/10 03:01:29 bradleyb
23 * Replacing win32 joystick (broken) with SDL joystick (stubs)
40 #define MAX_JOYSTICKS 16
43 #define MAX_AXES_PER_JOYSTICK 8
44 #define MAX_BUTTONS_PER_JOYSTICK 16
47 int num_joysticks = 0;
65 static struct joyinfo {
68 struct joyaxis axes[MAX_AXES];
69 struct joybutton buttons[MAX_BUTTONS];
76 int axis_map[MAX_AXES_PER_JOYSTICK];
77 int button_map[MAX_BUTTONS_PER_JOYSTICK];
78 } SDL_Joysticks[MAX_JOYSTICKS];
80 void joy_button_handler(SDL_JoyButtonEvent *jbe)
84 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
86 Joystick.buttons[button].state = jbe->state;
89 case SDL_JOYBUTTONDOWN:
90 Joystick.buttons[button].time_went_down
91 = timer_get_fixed_seconds();
92 Joystick.buttons[button].num_downs++;
95 Joystick.buttons[button].num_ups++;
100 void joy_axis_handler(SDL_JoyAxisEvent *jae)
104 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
106 Joystick.axes[axis].value = jae->value;
110 /* ----------------------------------------------- */
116 memset(&Joystick,0,sizeof(Joystick));
118 n = SDL_NumJoysticks();
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) {
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++;
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);
148 while (num_joysticks)
149 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
152 void joy_get_pos(int *x, int *y)
156 if (!num_joysticks) {
161 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
163 *x = joy_get_scaled_reading( axis[0], 0 );
164 *y = joy_get_scaled_reading( axis[1], 1 );
169 #if 0 // This is never used?
171 for (i=0; i++; i<buttons) {
172 switch (Joystick.buttons[i].state) {
186 int joy_get_button_down_cnt( int btn )
195 num_downs = Joystick.buttons[btn].num_downs;
196 Joystick.buttons[btn].num_downs = 0;
201 fix joy_get_button_down_time(int btn)
210 switch (Joystick.buttons[btn].state) {
212 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
213 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
223 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
232 for (i = 0; i <= JOY_NUM_AXES; i++) {
233 axis[i] = Joystick.axes[i].value;
246 for (i = 0; i < Joystick.n_buttons; i++) {
247 Joystick.buttons[i].time_went_down = 0;
248 Joystick.buttons[i].num_downs = 0;
253 int joy_get_button_state( int btn )
258 if(btn >= Joystick.n_buttons)
263 return Joystick.buttons[btn].state;
266 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
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;
277 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
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];
288 int joy_get_scaled_reading( int raw, int axis_num )
295 raw -= Joystick.axes[axis_num].center_val;
298 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
300 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
305 x = ((raw << 7) / d);
314 d = (joy_deadzone) * 6;
315 if ((x > (-1*d)) && (x < d))
322 void joy_set_slow_reading( int flag )