12 #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 char *joybutton_text[]; //from kconfig.c
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 Joystick.buttons[button].state = jbe->state;
86 case SDL_JOYBUTTONDOWN:
87 Joystick.buttons[button].time_went_down
88 = timer_get_fixed_seconds();
89 Joystick.buttons[button].num_downs++;
92 Joystick.buttons[button].num_ups++;
97 void joy_hat_handler(SDL_JoyHatEvent *jhe)
99 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
102 //Save last state of the hat-button
103 Joystick.buttons[hat ].last_state = Joystick.buttons[hat ].state;
104 Joystick.buttons[hat+1].last_state = Joystick.buttons[hat+1].state;
105 Joystick.buttons[hat+2].last_state = Joystick.buttons[hat+2].state;
106 Joystick.buttons[hat+3].last_state = Joystick.buttons[hat+3].state;
108 //get current state of the hat-button
109 Joystick.buttons[hat ].state = ((jhe->value & SDL_HAT_UP)>0);
110 Joystick.buttons[hat+1].state = ((jhe->value & SDL_HAT_RIGHT)>0);
111 Joystick.buttons[hat+2].state = ((jhe->value & SDL_HAT_DOWN)>0);
112 Joystick.buttons[hat+3].state = ((jhe->value & SDL_HAT_LEFT)>0);
114 //determine if a hat-button up or down event based on state and last_state
115 for(hbi=0;hbi<4;hbi++)
117 if( !Joystick.buttons[hat+hbi].last_state && Joystick.buttons[hat+hbi].state) //last_state up, current state down
119 Joystick.buttons[hat+hbi].time_went_down
120 = timer_get_fixed_seconds();
121 Joystick.buttons[hat+hbi].num_downs++;
123 else if(Joystick.buttons[hat+hbi].last_state && !Joystick.buttons[hat+hbi].state) //last_state down, current state up
125 Joystick.buttons[hat+hbi].num_ups++;
130 void joy_axis_handler(SDL_JoyAxisEvent *jae)
134 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
136 Joystick.axes[axis].value = jae->value;
140 /* ----------------------------------------------- */
147 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
148 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
152 memset(&Joystick,0,sizeof(Joystick));
153 memset(joyaxis_text, 0, JOY_MAX_AXES * sizeof(char *));
154 memset(joybutton_text, 0, JOY_MAX_BUTTONS * sizeof(char *));
156 n = SDL_NumJoysticks();
158 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
159 for (i = 0; i < n; i++) {
160 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
161 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
162 if (SDL_Joysticks[num_joysticks].handle) {
165 SDL_Joysticks[num_joysticks].n_axes
166 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
167 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
169 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
170 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
173 SDL_Joysticks[num_joysticks].n_buttons
174 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
175 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
177 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
178 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
181 SDL_Joysticks[num_joysticks].n_hats
182 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
183 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
185 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
186 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
189 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
190 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
191 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
193 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
195 sprintf(temp, "J%d A%d", i + 1, j + 1);
196 joyaxis_text[Joystick.n_axes] = d_strdup(temp);
197 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
199 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
201 sprintf(temp, "J%d B%d", i + 1, j + 1);
202 joybutton_text[Joystick.n_buttons] = d_strdup(temp);
203 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
205 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
207 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
208 //a hat counts as four buttons
209 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0202);
210 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
211 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0177);
212 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
213 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0200);
214 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
215 sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0201);
216 joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
222 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
224 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
225 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
228 joy_num_axes = Joystick.n_axes;
236 while (num_joysticks)
237 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
238 while (Joystick.n_axes--)
239 d_free(joyaxis_text[Joystick.n_axes]);
240 while (Joystick.n_buttons--)
241 d_free(joybutton_text[Joystick.n_buttons]);
244 void joy_get_pos(int *x, int *y)
246 int axis[JOY_MAX_AXES];
248 if (!num_joysticks) {
253 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
255 *x = joy_get_scaled_reading( axis[0], 0 );
256 *y = joy_get_scaled_reading( axis[1], 1 );
261 #if 0 // This is never used?
263 for (i=0; i++; i<buttons) {
264 switch (Joystick.buttons[i].state) {
278 int joy_get_button_down_cnt( int btn )
287 num_downs = Joystick.buttons[btn].num_downs;
288 Joystick.buttons[btn].num_downs = 0;
293 fix joy_get_button_down_time(int btn)
302 switch (Joystick.buttons[btn].state) {
304 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
305 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
315 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
318 ubyte channel_masks = 0;
325 for (i = 0; i < Joystick.n_axes; i++)
327 if ((axis[i] = Joystick.axes[i].value))
328 channel_masks |= 1 << i;
331 return channel_masks;
341 for (i = 0; i < Joystick.n_buttons; i++) {
342 Joystick.buttons[i].time_went_down = 0;
343 Joystick.buttons[i].num_downs = 0;
348 int joy_get_button_state( int btn )
353 if(btn >= Joystick.n_buttons)
358 return Joystick.buttons[btn].state;
361 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
365 for (i = 0; i < Joystick.n_axes; i++)
367 axis_center[i] = Joystick.axes[i].center_val;
368 axis_min[i] = Joystick.axes[i].min_val;
369 axis_max[i] = Joystick.axes[i].max_val;
373 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
377 for (i = 0; i < Joystick.n_axes; i++)
379 Joystick.axes[i].center_val = axis_center[i];
380 Joystick.axes[i].min_val = axis_min[i];
381 Joystick.axes[i].max_val = axis_max[i];
385 int joy_get_scaled_reading( int raw, int axis_num )
392 raw -= Joystick.axes[axis_num].center_val;
395 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
397 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
402 x = ((raw << 7) / d);
411 d = (joy_deadzone) * 6;
412 if ((x > (-1*d)) && (x < d))
419 void joy_set_slow_reading( int flag )