1 /* $Id: joy.c,v 1.14 2004-11-22 23:32:54 btb Exp $ */
13 #include <string.h> // for memset
23 #define MAX_JOYSTICKS 16
25 #define MAX_AXES_PER_JOYSTICK 8
26 #define MAX_BUTTONS_PER_JOYSTICK 16
27 #define MAX_HATS_PER_JOYSTICK 4
29 extern int joybutton_text[]; //from kconfig.c
32 int num_joysticks = 0;
53 /* This struct is a "virtual" joystick, which includes all the axes
54 * and buttons of every joystick found.
56 static struct joyinfo {
59 struct joyaxis axes[JOY_MAX_AXES];
60 struct joybutton buttons[MAX_BUTTONS];
63 /* This struct is an array, with one entry for each physical joystick
71 int hat_map[MAX_HATS_PER_JOYSTICK]; //Note: Descent expects hats to be buttons, so these are indices into Joystick.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_hat_handler(SDL_JoyHatEvent *jhe)
98 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
101 //Save last state of the hat-button
102 Joystick.buttons[hat ].last_state = Joystick.buttons[hat ].state;
103 Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
104 Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
105 Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
107 //get current state of the hat-button
108 Joystick.buttons[hat ].state = ((jhe->value & SDL_HAT_UP)>0);
109 Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
110 Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
111 Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
113 //determine if a hat-button up or down event based on state and last_state
114 for(hbi=0;hbi<4;hbi++)
116 if( !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
118 Joystick.buttons[hat+hbi].time_went_down
119 = timer_get_fixed_seconds();
120 Joystick.buttons[hat+hbi].num_downs++;
122 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state) //last_state down, current state up
124 Joystick.buttons[hat+hbi].num_ups++;
129 void joy_axis_handler(SDL_JoyAxisEvent *jae)
133 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
135 Joystick.axes[axis].value = jae->value;
139 /* ----------------------------------------------- */
145 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
146 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
150 memset(&Joystick,0,sizeof(Joystick));
152 n = SDL_NumJoysticks();
154 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
155 for (i = 0; i < n; i++) {
156 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
157 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
158 if (SDL_Joysticks[num_joysticks].handle) {
161 SDL_Joysticks[num_joysticks].n_axes
162 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
163 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
165 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
166 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
169 SDL_Joysticks[num_joysticks].n_buttons
170 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
171 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
173 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
174 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
177 SDL_Joysticks[num_joysticks].n_hats
178 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
179 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
181 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
182 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
185 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
186 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
187 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
189 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
190 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
191 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
192 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
193 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
195 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
196 //a hat counts as four buttons
197 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_U:TNUM_HAT_U;
198 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_R:TNUM_HAT_R;
199 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_D:TNUM_HAT_D;
200 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_L:TNUM_HAT_L;
206 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
208 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
209 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
212 joy_num_axes = Joystick.n_axes;
219 while (num_joysticks)
220 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
223 void joy_get_pos(int *x, int *y)
225 int axis[JOY_MAX_AXES];
227 if (!num_joysticks) {
232 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
234 *x = joy_get_scaled_reading( axis[0], 0 );
235 *y = joy_get_scaled_reading( axis[1], 1 );
240 #if 0 // This is never used?
242 for (i=0; i++; i<buttons) {
243 switch (Joystick.buttons[i].state) {
257 int joy_get_button_down_cnt( int btn )
266 num_downs = Joystick.buttons[btn].num_downs;
267 Joystick.buttons[btn].num_downs = 0;
272 fix joy_get_button_down_time(int btn)
281 switch (Joystick.buttons[btn].state) {
283 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
284 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
294 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
297 ubyte channel_masks = 0;
304 for (i = 0; i < Joystick.n_axes; i++) {
305 if ((axis[i] = Joystick.axes[i].value))
306 channel_masks |= 1 << i;
309 return channel_masks;
319 for (i = 0; i < Joystick.n_buttons; i++) {
320 Joystick.buttons[i].time_went_down = 0;
321 Joystick.buttons[i].num_downs = 0;
326 int joy_get_button_state( int btn )
331 if(btn >= Joystick.n_buttons)
336 return Joystick.buttons[btn].state;
339 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
343 for (i = 0; i < Joystick.n_axes; i++) {
344 axis_center[i] = Joystick.axes[i].center_val;
345 axis_min[i] = Joystick.axes[i].min_val;
346 axis_max[i] = Joystick.axes[i].max_val;
350 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
354 for (i = 0; i < Joystick.n_axes; i++) {
355 Joystick.axes[i].center_val = axis_center[i];
356 Joystick.axes[i].min_val = axis_min[i];
357 Joystick.axes[i].max_val = axis_max[i];
361 int joy_get_scaled_reading( int raw, int axis_num )
368 raw -= Joystick.axes[axis_num].center_val;
371 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
373 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
378 x = ((raw << 7) / d);
387 d = (joy_deadzone) * 6;
388 if ((x > (-1*d)) && (x < d))
395 void joy_set_slow_reading( int flag )