1 /* $Id: joy.c,v 1.9 2003-01-15 02:42:41 btb Exp $ */
13 #include <string.h> // for memset
22 #define MAX_JOYSTICKS 16
25 #define MAX_AXES_PER_JOYSTICK 8
26 #define MAX_BUTTONS_PER_JOYSTICK 16
29 int num_joysticks = 0;
47 static struct joyinfo {
50 struct joyaxis axes[MAX_AXES];
51 struct joybutton buttons[MAX_BUTTONS];
58 int axis_map[MAX_AXES_PER_JOYSTICK];
59 int button_map[MAX_BUTTONS_PER_JOYSTICK];
60 } SDL_Joysticks[MAX_JOYSTICKS];
62 void joy_button_handler(SDL_JoyButtonEvent *jbe)
66 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
68 Joystick.buttons[button].state = jbe->state;
71 case SDL_JOYBUTTONDOWN:
72 Joystick.buttons[button].time_went_down
73 = timer_get_fixed_seconds();
74 Joystick.buttons[button].num_downs++;
77 Joystick.buttons[button].num_ups++;
82 void joy_axis_handler(SDL_JoyAxisEvent *jae)
86 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
88 Joystick.axes[axis].value = jae->value;
92 /* ----------------------------------------------- */
98 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
99 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
103 memset(&Joystick,0,sizeof(Joystick));
105 n = SDL_NumJoysticks();
107 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
108 for (i = 0; i < n; i++) {
109 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
110 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
111 if (SDL_Joysticks[num_joysticks].handle) {
113 SDL_Joysticks[num_joysticks].n_axes
114 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
115 SDL_Joysticks[num_joysticks].n_buttons
116 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
117 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
118 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
119 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
120 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
121 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
122 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
125 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
126 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
127 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
135 while (num_joysticks)
136 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
139 void joy_get_pos(int *x, int *y)
143 if (!num_joysticks) {
148 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
150 *x = joy_get_scaled_reading( axis[0], 0 );
151 *y = joy_get_scaled_reading( axis[1], 1 );
156 #if 0 // This is never used?
158 for (i=0; i++; i<buttons) {
159 switch (Joystick.buttons[i].state) {
173 int joy_get_button_down_cnt( int btn )
182 num_downs = Joystick.buttons[btn].num_downs;
183 Joystick.buttons[btn].num_downs = 0;
188 fix joy_get_button_down_time(int btn)
197 switch (Joystick.buttons[btn].state) {
199 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
200 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
210 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
219 for (i = 0; i <= JOY_NUM_AXES; i++) {
220 axis[i] = Joystick.axes[i].value;
233 for (i = 0; i < Joystick.n_buttons; i++) {
234 Joystick.buttons[i].time_went_down = 0;
235 Joystick.buttons[i].num_downs = 0;
240 int joy_get_button_state( int btn )
245 if(btn >= Joystick.n_buttons)
250 return Joystick.buttons[btn].state;
253 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
257 for (i = 0; i < JOY_NUM_AXES; i++) {
258 axis_center[i] = Joystick.axes[i].center_val;
259 axis_min[i] = Joystick.axes[i].min_val;
260 axis_max[i] = Joystick.axes[i].max_val;
264 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
268 for (i = 0; i < JOY_NUM_AXES; i++) {
269 Joystick.axes[i].center_val = axis_center[i];
270 Joystick.axes[i].min_val = axis_min[i];
271 Joystick.axes[i].max_val = axis_max[i];
275 int joy_get_scaled_reading( int raw, int axis_num )
282 raw -= Joystick.axes[axis_num].center_val;
285 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
287 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
292 x = ((raw << 7) / d);
301 d = (joy_deadzone) * 6;
302 if ((x > (-1*d)) && (x < d))
309 void joy_set_slow_reading( int flag )