12 #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 vkey_handler(KEY_JB1 + button, jbe->state == SDL_JOYBUTTONDOWN);
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_hat_handler(SDL_JoyHatEvent *jhe)
102 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
105 //Save last state of the hat-button
106 Joystick.buttons[hat ].last_state = Joystick.buttons[hat ].state;
107 Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
108 Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
109 Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
111 //get current state of the hat-button
112 Joystick.buttons[hat ].state = ((jhe->value & SDL_HAT_UP)>0);
113 Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
114 Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
115 Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
117 //determine if a hat-button up or down event based on state and last_state
118 for(hbi=0;hbi<4;hbi++)
120 if( !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
122 Joystick.buttons[hat+hbi].time_went_down
123 = timer_get_fixed_seconds();
124 Joystick.buttons[hat+hbi].num_downs++;
126 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state) //last_state down, current state up
128 Joystick.buttons[hat+hbi].num_ups++;
133 void joy_axis_handler(SDL_JoyAxisEvent *jae)
137 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
139 Joystick.axes[axis].value = jae->value;
143 /* ----------------------------------------------- */
150 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
151 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
155 memset(&Joystick,0,sizeof(Joystick));
156 memset(joyaxis_text, 0, JOY_MAX_AXES * sizeof(char *));
157 memset(joybutton_text, 0, JOY_MAX_BUTTONS * sizeof(char *));
159 n = SDL_NumJoysticks();
161 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
162 for (i = 0; i < n; i++) {
163 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
164 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
165 if (SDL_Joysticks[num_joysticks].handle) {
168 SDL_Joysticks[num_joysticks].n_axes
169 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
170 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
172 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
173 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
176 SDL_Joysticks[num_joysticks].n_buttons
177 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
178 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
180 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
181 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
184 SDL_Joysticks[num_joysticks].n_hats
185 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
186 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
188 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
189 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
192 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
193 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
194 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
196 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
198 sprintf(temp, "J%d A%d", i + 1, j + 1);
199 joyaxis_text[Joystick.n_axes] = d_strdup(temp);
200 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
202 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
204 sprintf(temp, "J%dB%d", i + 1, j + 1);
205 key_text[KEY_JB1 + Joystick.n_buttons] =
206 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
207 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
209 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
211 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
212 //a hat counts as four buttons
214 sprintf(temp, "J%dH%dUP", i + 1, j + 1);
215 key_text[KEY_JB1 + Joystick.n_buttons] =
216 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
217 Joystick.n_buttons++;
219 sprintf(temp, "J%dH%dRIGHT", i + 1, j + 1);
220 key_text[KEY_JB1 + Joystick.n_buttons] =
221 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
222 Joystick.n_buttons++;
224 sprintf(temp, "J%dH%dDOWN", i + 1, j + 1);
225 key_text[KEY_JB1 + Joystick.n_buttons] =
226 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
227 Joystick.n_buttons++;
229 sprintf(temp, "J%dH%dLEFT", i + 1, j + 1);
230 key_text[KEY_JB1 + Joystick.n_buttons] =
231 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
232 Joystick.n_buttons++;
238 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
240 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
241 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
244 joy_num_axes = Joystick.n_axes;
252 while (num_joysticks)
253 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
254 while (Joystick.n_axes--)
255 d_free(joyaxis_text[Joystick.n_axes]);
256 while (Joystick.n_buttons--)
257 d_free(joybutton_text[Joystick.n_buttons]);
260 void joy_get_pos(int *x, int *y)
262 int axis[JOY_MAX_AXES];
264 if (!num_joysticks) {
269 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
271 *x = joy_get_scaled_reading( axis[0], 0 );
272 *y = joy_get_scaled_reading( axis[1], 1 );
277 #if 0 // This is never used?
279 for (i=0; i++; i<buttons) {
280 switch (Joystick.buttons[i].state) {
294 int joy_get_button_down_cnt( int btn )
303 num_downs = Joystick.buttons[btn].num_downs;
304 Joystick.buttons[btn].num_downs = 0;
309 fix joy_get_button_down_time(int btn)
318 switch (Joystick.buttons[btn].state) {
320 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
321 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
331 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
334 ubyte channel_masks = 0;
341 for (i = 0; i < Joystick.n_axes; i++)
343 if ((axis[i] = Joystick.axes[i].value))
344 channel_masks |= 1 << i;
347 return channel_masks;
357 for (i = 0; i < Joystick.n_buttons; i++) {
358 Joystick.buttons[i].time_went_down = 0;
359 Joystick.buttons[i].num_downs = 0;
364 int joy_get_button_state( int btn )
369 if(btn >= Joystick.n_buttons)
374 return Joystick.buttons[btn].state;
377 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
381 for (i = 0; i < Joystick.n_axes; i++)
383 axis_center[i] = Joystick.axes[i].center_val;
384 axis_min[i] = Joystick.axes[i].min_val;
385 axis_max[i] = Joystick.axes[i].max_val;
389 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
393 for (i = 0; i < Joystick.n_axes; i++)
395 Joystick.axes[i].center_val = axis_center[i];
396 Joystick.axes[i].min_val = axis_min[i];
397 Joystick.axes[i].max_val = axis_max[i];
401 int joy_get_scaled_reading( int raw, int axis_num )
408 raw -= Joystick.axes[axis_num].center_val;
411 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
413 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
418 x = ((raw << 7) / d);
427 d = (joy_deadzone) * 6;
428 if ((x > (-1*d)) && (x < d))
435 void joy_set_slow_reading( int flag )