1 /* $Id: joy.c,v 1.13 2004-05-15 16:25:35 schaffner Exp $ */
13 #include <string.h> // for memset
23 #define MAX_JOYSTICKS 16
26 #define MAX_AXES_PER_JOYSTICK 8
27 #define MAX_BUTTONS_PER_JOYSTICK 16
28 #define MAX_HATS_PER_JOYSTICK 4
30 extern int joybutton_text[]; //from kconfig.c
33 int num_joysticks = 0;
52 static struct joyinfo {
55 struct joyaxis axes[MAX_AXES];
56 struct joybutton buttons[MAX_BUTTONS];
64 int hat_map[MAX_HATS_PER_JOYSTICK]; //Note: Descent expects hats to be buttons, so these are indices into Joystick.buttons
65 int axis_map[MAX_AXES_PER_JOYSTICK];
66 int button_map[MAX_BUTTONS_PER_JOYSTICK];
67 } SDL_Joysticks[MAX_JOYSTICKS];
69 void joy_button_handler(SDL_JoyButtonEvent *jbe)
73 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
75 Joystick.buttons[button].state = jbe->state;
78 case SDL_JOYBUTTONDOWN:
79 Joystick.buttons[button].time_went_down
80 = timer_get_fixed_seconds();
81 Joystick.buttons[button].num_downs++;
84 Joystick.buttons[button].num_ups++;
89 void joy_hat_handler(SDL_JoyHatEvent *jhe)
91 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
94 //Save last state of the hat-button
95 Joystick.buttons[hat ].last_state = Joystick.buttons[hat ].state;
96 Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
97 Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
98 Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
100 //get current state of the hat-button
101 Joystick.buttons[hat ].state = ((jhe->value & SDL_HAT_UP)>0);
102 Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
103 Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
104 Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
106 //determine if a hat-button up or down event based on state and last_state
107 for(hbi=0;hbi<4;hbi++)
109 if( !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
111 Joystick.buttons[hat+hbi].time_went_down
112 = timer_get_fixed_seconds();
113 Joystick.buttons[hat+hbi].num_downs++;
115 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state) //last_state down, current state up
117 Joystick.buttons[hat+hbi].num_ups++;
122 void joy_axis_handler(SDL_JoyAxisEvent *jae)
126 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
128 Joystick.axes[axis].value = jae->value;
132 /* ----------------------------------------------- */
138 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
139 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
143 memset(&Joystick,0,sizeof(Joystick));
145 n = SDL_NumJoysticks();
147 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
148 for (i = 0; i < n; i++) {
149 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
150 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
151 if (SDL_Joysticks[num_joysticks].handle) {
154 SDL_Joysticks[num_joysticks].n_axes
155 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
156 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
158 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
159 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
162 SDL_Joysticks[num_joysticks].n_buttons
163 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
164 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
166 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
167 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
170 SDL_Joysticks[num_joysticks].n_hats
171 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
172 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
174 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
175 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
178 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
179 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
180 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
182 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
183 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
184 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
185 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
186 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
188 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
189 //a hat counts as four buttons
190 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_U:TNUM_HAT_U;
191 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_R:TNUM_HAT_R;
192 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_D:TNUM_HAT_D;
193 joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_L:TNUM_HAT_L;
199 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
201 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
202 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
210 while (num_joysticks)
211 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
214 void joy_get_pos(int *x, int *y)
218 if (!num_joysticks) {
223 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
225 *x = joy_get_scaled_reading( axis[0], 0 );
226 *y = joy_get_scaled_reading( axis[1], 1 );
231 #if 0 // This is never used?
233 for (i=0; i++; i<buttons) {
234 switch (Joystick.buttons[i].state) {
248 int joy_get_button_down_cnt( int btn )
257 num_downs = Joystick.buttons[btn].num_downs;
258 Joystick.buttons[btn].num_downs = 0;
263 fix joy_get_button_down_time(int btn)
272 switch (Joystick.buttons[btn].state) {
274 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
275 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
285 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
288 ubyte channel_masks = 0;
295 for (i = 0; i < JOY_NUM_AXES; i++) {
296 if ((axis[i] = Joystick.axes[i].value))
297 channel_masks |= 1 << i;
300 return channel_masks;
310 for (i = 0; i < Joystick.n_buttons; i++) {
311 Joystick.buttons[i].time_went_down = 0;
312 Joystick.buttons[i].num_downs = 0;
317 int joy_get_button_state( int btn )
322 if(btn >= Joystick.n_buttons)
327 return Joystick.buttons[btn].state;
330 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
334 for (i = 0; i < JOY_NUM_AXES; i++) {
335 axis_center[i] = Joystick.axes[i].center_val;
336 axis_min[i] = Joystick.axes[i].min_val;
337 axis_max[i] = Joystick.axes[i].max_val;
341 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
345 for (i = 0; i < JOY_NUM_AXES; i++) {
346 Joystick.axes[i].center_val = axis_center[i];
347 Joystick.axes[i].min_val = axis_min[i];
348 Joystick.axes[i].max_val = axis_max[i];
352 int joy_get_scaled_reading( int raw, int axis_num )
359 raw -= Joystick.axes[axis_num].center_val;
362 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
364 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
369 x = ((raw << 7) / d);
378 d = (joy_deadzone) * 6;
379 if ((x > (-1*d)) && (x < d))
386 void joy_set_slow_reading( int flag )