12 #include <string.h> // for memset
26 #define MAX_JOYSTICKS 16
28 #define MAX_AXES_PER_JOYSTICK 8
29 #define MAX_BUTTONS_PER_JOYSTICK 16
30 #define MAX_HATS_PER_JOYSTICK 4
32 extern char *joyaxis_text[]; //from kconfig.c
35 int num_joysticks = 0;
48 /* This struct is a "virtual" joystick, which includes all the axes
49 * and buttons of every joystick found.
51 static struct joyinfo {
54 struct joyaxis axes[JOY_MAX_AXES];
57 /* This struct is an array, with one entry for each physical joystick
65 int hat_map[MAX_HATS_PER_JOYSTICK]; //Note: Descent expects hats to be buttons, so these are indices into Joystick.buttons
66 int axis_map[MAX_AXES_PER_JOYSTICK];
67 int button_map[MAX_BUTTONS_PER_JOYSTICK];
68 } SDL_Joysticks[MAX_JOYSTICKS];
73 // 1 = move forward/back
75 // 3 = move left/right
76 // 4 = look left/right
78 // 6 = bank left/right
80 cvar_t joy_advaxes[] = {
81 { "joy_advaxisx", "4", 1 },
82 { "joy_advaxisy", "2", 1 },
83 { "joy_advaxisz", "0", 1 },
84 { "joy_advaxisr", "0", 1 },
85 { "joy_advaxisu", "0", 1 },
86 { "joy_advaxisv", "0", 1 },
90 void joy_cmd_joyadvancedupdate(int argc, char **argv)
93 con_printf(CON_NORMAL, "%s\n", argv[0]);
94 con_printf(CON_NORMAL, " updates current joystick and mouse axis settings\n");
102 void joy_button_handler(SDL_JoyButtonEvent *jbe)
106 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
108 vkey_handler(KEY_JB1 + button, jbe->state == SDL_PRESSED);
111 void joy_hat_handler(SDL_JoyHatEvent *jhe)
113 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
115 vkey_handler(KEY_JB1 + hat + 0, (jhe->value & SDL_HAT_UP ) > 0);
116 vkey_handler(KEY_JB1 + hat + 1, (jhe->value & SDL_HAT_RIGHT) > 0);
117 vkey_handler(KEY_JB1 + hat + 2, (jhe->value & SDL_HAT_DOWN ) > 0);
118 vkey_handler(KEY_JB1 + hat + 3, (jhe->value & SDL_HAT_LEFT ) > 0);
121 void joy_axis_handler(SDL_JoyAxisEvent *jae)
125 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
127 Joystick.axes[axis].value = jae->value;
131 /* ----------------------------------------------- */
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));
144 memset(joyaxis_text, 0, JOY_MAX_AXES * sizeof(char *));
146 for (i = 0; i < 6; i++)
147 cvar_registervariable(&joy_advaxes[i]);
149 cmd_addcommand("joyadvancedupdate", joy_cmd_joyadvancedupdate);
151 n = SDL_NumJoysticks();
153 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
154 for (i = 0; i < n; i++) {
155 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
156 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
157 if (SDL_Joysticks[num_joysticks].handle) {
160 SDL_Joysticks[num_joysticks].n_axes
161 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
162 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
164 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
165 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
168 SDL_Joysticks[num_joysticks].n_buttons
169 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
170 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
172 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
173 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
176 SDL_Joysticks[num_joysticks].n_hats
177 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
178 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
180 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
181 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
184 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
185 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
186 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
188 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
190 sprintf(temp, "J%d A%d", i + 1, j + 1);
191 joyaxis_text[Joystick.n_axes] = d_strdup(temp);
192 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
194 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
196 sprintf(temp, "J%dB%d", i + 1, j + 1);
197 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
198 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
200 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
202 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
203 //a hat counts as four buttons
205 sprintf(temp, "J%dH%dUP", i + 1, j + 1);
206 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
207 Joystick.n_buttons++;
209 sprintf(temp, "J%dH%dRIGHT", i + 1, j + 1);
210 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
211 Joystick.n_buttons++;
213 sprintf(temp, "J%dH%dDOWN", i + 1, j + 1);
214 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
215 Joystick.n_buttons++;
217 sprintf(temp, "J%dH%dLEFT", i + 1, j + 1);
218 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
219 Joystick.n_buttons++;
225 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
227 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
228 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
231 joy_num_axes = Joystick.n_axes;
239 while (num_joysticks)
240 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
241 while (Joystick.n_axes--)
242 d_free(joyaxis_text[Joystick.n_axes]);
243 while (Joystick.n_buttons--)
244 d_free(key_text[KEY_JB1 + Joystick.n_buttons]);
247 void joy_get_pos(int *x, int *y)
249 int axis[JOY_MAX_AXES];
251 if (!num_joysticks) {
256 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
258 *x = joy_get_scaled_reading( axis[0], 0 );
259 *y = joy_get_scaled_reading( axis[1], 1 );
264 #if 0 // This is never used?
266 for (i=0; i++; i<buttons) {
267 switch (keyd_pressed[KEY_JB1 + i]) {
282 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
285 ubyte channel_masks = 0;
292 for (i = 0; i < Joystick.n_axes; i++)
294 if ((axis[i] = Joystick.axes[i].value))
295 channel_masks |= 1 << i;
298 return channel_masks;
307 int joy_get_button_state( int btn )
312 if(btn >= Joystick.n_buttons)
317 return keyd_pressed[KEY_JB1 + btn];
320 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
324 for (i = 0; i < Joystick.n_axes; i++)
326 axis_center[i] = Joystick.axes[i].center_val;
327 axis_min[i] = Joystick.axes[i].min_val;
328 axis_max[i] = Joystick.axes[i].max_val;
332 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
336 for (i = 0; i < Joystick.n_axes; i++)
338 Joystick.axes[i].center_val = axis_center[i];
339 Joystick.axes[i].min_val = axis_min[i];
340 Joystick.axes[i].max_val = axis_max[i];
344 int joy_get_scaled_reading( int raw, int axis_num )
351 raw -= Joystick.axes[axis_num].center_val;
354 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
356 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
361 x = ((raw << 7) / d);
370 d = (joy_deadzone) * 6;
371 if ((x > (-1*d)) && (x < d))
378 void joy_set_slow_reading( int flag )