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 *joyaxis_text[]; //from kconfig.c
33 int num_joysticks = 0;
54 /* This struct is a "virtual" joystick, which includes all the axes
55 * and buttons of every joystick found.
57 static struct joyinfo {
60 struct joyaxis axes[JOY_MAX_AXES];
61 struct joybutton buttons[JOY_MAX_BUTTONS];
64 /* This struct is an array, with one entry for each physical joystick
72 int hat_map[MAX_HATS_PER_JOYSTICK]; //Note: Descent expects hats to be buttons, so these are indices into Joystick.buttons
73 int axis_map[MAX_AXES_PER_JOYSTICK];
74 int button_map[MAX_BUTTONS_PER_JOYSTICK];
75 } SDL_Joysticks[MAX_JOYSTICKS];
77 void joy_button_handler(SDL_JoyButtonEvent *jbe)
81 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
83 vkey_handler(KEY_JB1 + button, jbe->state == SDL_JOYBUTTONDOWN);
85 Joystick.buttons[button].state = jbe->state;
88 case SDL_JOYBUTTONDOWN:
89 Joystick.buttons[button].time_went_down
90 = timer_get_fixed_seconds();
91 Joystick.buttons[button].num_downs++;
94 Joystick.buttons[button].num_ups++;
99 void joy_hat_handler(SDL_JoyHatEvent *jhe)
101 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
104 //Save last state of the hat-button
105 Joystick.buttons[hat ].last_state = Joystick.buttons[hat ].state;
106 Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
107 Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
108 Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
110 //get current state of the hat-button
111 Joystick.buttons[hat ].state = ((jhe->value & SDL_HAT_UP)>0);
112 Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
113 Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
114 Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
116 //determine if a hat-button up or down event based on state and last_state
117 for(hbi=0;hbi<4;hbi++)
119 if( !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
121 Joystick.buttons[hat+hbi].time_went_down
122 = timer_get_fixed_seconds();
123 Joystick.buttons[hat+hbi].num_downs++;
125 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state) //last_state down, current state up
127 Joystick.buttons[hat+hbi].num_ups++;
132 void joy_axis_handler(SDL_JoyAxisEvent *jae)
136 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
138 Joystick.axes[axis].value = jae->value;
142 /* ----------------------------------------------- */
149 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
150 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
154 memset(&Joystick,0,sizeof(Joystick));
155 memset(joyaxis_text, 0, JOY_MAX_AXES * 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%dB%d", i + 1, j + 1);
203 key_text[KEY_JB1 + 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
211 sprintf(temp, "J%dH%dUP", i + 1, j + 1);
212 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
213 Joystick.n_buttons++;
215 sprintf(temp, "J%dH%dRIGHT", i + 1, j + 1);
216 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
217 Joystick.n_buttons++;
219 sprintf(temp, "J%dH%dDOWN", i + 1, j + 1);
220 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
221 Joystick.n_buttons++;
223 sprintf(temp, "J%dH%dLEFT", i + 1, j + 1);
224 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
225 Joystick.n_buttons++;
231 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
233 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
234 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
237 joy_num_axes = Joystick.n_axes;
245 while (num_joysticks)
246 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
247 while (Joystick.n_axes--)
248 d_free(joyaxis_text[Joystick.n_axes]);
249 while (Joystick.n_buttons--)
250 d_free(key_text[KEY_JB1 + Joystick.n_buttons]);
253 void joy_get_pos(int *x, int *y)
255 int axis[JOY_MAX_AXES];
257 if (!num_joysticks) {
262 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
264 *x = joy_get_scaled_reading( axis[0], 0 );
265 *y = joy_get_scaled_reading( axis[1], 1 );
270 #if 0 // This is never used?
272 for (i=0; i++; i<buttons) {
273 switch (Joystick.buttons[i].state) {
287 int joy_get_button_down_cnt( int btn )
296 num_downs = Joystick.buttons[btn].num_downs;
297 Joystick.buttons[btn].num_downs = 0;
302 fix joy_get_button_down_time(int btn)
311 switch (Joystick.buttons[btn].state) {
313 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
314 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
324 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
327 ubyte channel_masks = 0;
334 for (i = 0; i < Joystick.n_axes; i++)
336 if ((axis[i] = Joystick.axes[i].value))
337 channel_masks |= 1 << i;
340 return channel_masks;
350 for (i = 0; i < Joystick.n_buttons; i++) {
351 Joystick.buttons[i].time_went_down = 0;
352 Joystick.buttons[i].num_downs = 0;
357 int joy_get_button_state( int btn )
362 if(btn >= Joystick.n_buttons)
367 return Joystick.buttons[btn].state;
370 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
374 for (i = 0; i < Joystick.n_axes; i++)
376 axis_center[i] = Joystick.axes[i].center_val;
377 axis_min[i] = Joystick.axes[i].min_val;
378 axis_max[i] = Joystick.axes[i].max_val;
382 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
386 for (i = 0; i < Joystick.n_axes; i++)
388 Joystick.axes[i].center_val = axis_center[i];
389 Joystick.axes[i].min_val = axis_min[i];
390 Joystick.axes[i].max_val = axis_max[i];
394 int joy_get_scaled_reading( int raw, int axis_num )
401 raw -= Joystick.axes[axis_num].center_val;
404 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
406 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
411 x = ((raw << 7) / d);
420 d = (joy_deadzone) * 6;
421 if ((x > (-1*d)) && (x < d))
428 void joy_set_slow_reading( int flag )