1 /* $Id: joy.c,v 1.18 2005-04-05 07:35:09 btb Exp $ */
13 #include <string.h> // for memset
24 #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 char *joybutton_text[]; //from kconfig.c
31 extern char *joyaxis_text[]; //from kconfig.c
34 int num_joysticks = 0;
55 /* This struct is a "virtual" joystick, which includes all the axes
56 * and buttons of every joystick found.
58 static struct joyinfo {
61 struct joyaxis axes[JOY_MAX_AXES];
62 struct joybutton buttons[JOY_MAX_BUTTONS];
65 /* This struct is an array, with one entry for each physical joystick
73 int hat_map[MAX_HATS_PER_JOYSTICK]; //Note: Descent expects hats to be buttons, so these are indices into Joystick.buttons
74 int axis_map[MAX_AXES_PER_JOYSTICK];
75 int button_map[MAX_BUTTONS_PER_JOYSTICK];
76 } SDL_Joysticks[MAX_JOYSTICKS];
78 void joy_button_handler(SDL_JoyButtonEvent *jbe)
82 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
84 Joystick.buttons[button].state = jbe->state;
87 case SDL_JOYBUTTONDOWN:
88 Joystick.buttons[button].time_went_down
89 = timer_get_fixed_seconds();
90 Joystick.buttons[button].num_downs++;
93 Joystick.buttons[button].num_ups++;
98 void joy_hat_handler(SDL_JoyHatEvent *jhe)
100 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
103 //Save last state of the hat-button
104 Joystick.buttons[hat ].last_state = Joystick.buttons[hat ].state;
105 Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
106 Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
107 Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
109 //get current state of the hat-button
110 Joystick.buttons[hat ].state = ((jhe->value & SDL_HAT_UP)>0);
111 Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
112 Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
113 Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
115 //determine if a hat-button up or down event based on state and last_state
116 for(hbi=0;hbi<4;hbi++)
118 if( !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
120 Joystick.buttons[hat+hbi].time_went_down
121 = timer_get_fixed_seconds();
122 Joystick.buttons[hat+hbi].num_downs++;
124 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state) //last_state down, current state up
126 Joystick.buttons[hat+hbi].num_ups++;
131 void joy_axis_handler(SDL_JoyAxisEvent *jae)
135 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
137 Joystick.axes[axis].value = jae->value;
141 /* ----------------------------------------------- */
148 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
149 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
153 memset(&Joystick,0,sizeof(Joystick));
154 memset(joyaxis_text, 0, JOY_MAX_AXES * sizeof(char *));
155 memset(joybutton_text, 0, JOY_MAX_BUTTONS * sizeof(char *));
157 n = SDL_NumJoysticks();
159 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
160 for (i = 0; i < n; i++) {
161 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
162 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
163 if (SDL_Joysticks[num_joysticks].handle) {
166 SDL_Joysticks[num_joysticks].n_axes
167 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
168 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
170 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
171 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
174 SDL_Joysticks[num_joysticks].n_buttons
175 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
176 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
178 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
179 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
182 SDL_Joysticks[num_joysticks].n_hats
183 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
184 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
186 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
187 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
190 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
191 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
192 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
194 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
196 sprintf(temp, "J%d A%d", i + 1, j + 1);
197 joyaxis_text[Joystick.n_axes] = d_strdup(temp);
198 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
200 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
202 sprintf(temp, "J%d B%d", i + 1, j + 1);
203 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
204 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
206 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
208 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
209 //a hat counts as four buttons
210 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0202);
211 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
212 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0177);
213 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
214 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0200);
215 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
216 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0201);
217 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
223 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
225 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
226 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
229 joy_num_axes = Joystick.n_axes;
237 while (num_joysticks)
238 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
239 while (Joystick.n_axes--)
240 d_free(joyaxis_text[Joystick.n_axes]);
241 while (Joystick.n_buttons--)
242 d_free(joybutton_text[Joystick.n_buttons]);
245 void joy_get_pos(int *x, int *y)
247 int axis[JOY_MAX_AXES];
249 if (!num_joysticks) {
254 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
256 *x = joy_get_scaled_reading( axis[0], 0 );
257 *y = joy_get_scaled_reading( axis[1], 1 );
262 #if 0 // This is never used?
264 for (i=0; i++; i<buttons) {
265 switch (Joystick.buttons[i].state) {
279 int joy_get_button_down_cnt( int btn )
288 num_downs = Joystick.buttons[btn].num_downs;
289 Joystick.buttons[btn].num_downs = 0;
294 fix joy_get_button_down_time(int btn)
303 switch (Joystick.buttons[btn].state) {
305 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
306 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
316 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
319 ubyte channel_masks = 0;
326 for (i = 0; i < Joystick.n_axes; i++)
328 if ((axis[i] = Joystick.axes[i].value))
329 channel_masks |= 1 << i;
332 return channel_masks;
342 for (i = 0; i < Joystick.n_buttons; i++) {
343 Joystick.buttons[i].time_went_down = 0;
344 Joystick.buttons[i].num_downs = 0;
349 int joy_get_button_state( int btn )
354 if(btn >= Joystick.n_buttons)
359 return Joystick.buttons[btn].state;
362 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
366 for (i = 0; i < Joystick.n_axes; i++)
368 axis_center[i] = Joystick.axes[i].center_val;
369 axis_min[i] = Joystick.axes[i].min_val;
370 axis_max[i] = Joystick.axes[i].max_val;
374 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
378 for (i = 0; i < Joystick.n_axes; i++)
380 Joystick.axes[i].center_val = axis_center[i];
381 Joystick.axes[i].min_val = axis_min[i];
382 Joystick.axes[i].max_val = axis_max[i];
386 int joy_get_scaled_reading( int raw, int axis_num )
393 raw -= Joystick.axes[axis_num].center_val;
396 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
398 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
403 x = ((raw << 7) / d);
412 d = (joy_deadzone) * 6;
413 if ((x > (-1*d)) && (x < d))
420 void joy_set_slow_reading( int flag )