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;
46 /* This struct is a "virtual" joystick, which includes all the axes
47 * and buttons of every joystick found.
49 static struct joyinfo {
52 struct joyaxis axes[JOY_MAX_AXES];
55 /* This struct is an array, with one entry for each physical joystick
63 int hat_map[MAX_HATS_PER_JOYSTICK]; //Note: Descent expects hats to be buttons, so these are indices into Joystick.buttons
64 int axis_map[MAX_AXES_PER_JOYSTICK];
65 int button_map[MAX_BUTTONS_PER_JOYSTICK];
66 } SDL_Joysticks[MAX_JOYSTICKS];
68 void joy_button_handler(SDL_JoyButtonEvent *jbe)
72 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
74 vkey_handler(KEY_JB1 + button, jbe->state == SDL_PRESSED);
77 void joy_hat_handler(SDL_JoyHatEvent *jhe)
79 int hat = SDL_Joysticks[jhe->which].hat_map[jhe->hat];
81 vkey_handler(KEY_JB1 + hat + 0, (jhe->value & SDL_HAT_UP ) > 0);
82 vkey_handler(KEY_JB1 + hat + 1, (jhe->value & SDL_HAT_RIGHT) > 0);
83 vkey_handler(KEY_JB1 + hat + 2, (jhe->value & SDL_HAT_DOWN ) > 0);
84 vkey_handler(KEY_JB1 + hat + 3, (jhe->value & SDL_HAT_LEFT ) > 0);
87 void joy_axis_handler(SDL_JoyAxisEvent *jae)
91 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
93 Joystick.axes[axis].value = jae->value;
97 /* ----------------------------------------------- */
104 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
105 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
109 memset(&Joystick,0,sizeof(Joystick));
110 memset(joyaxis_text, 0, JOY_MAX_AXES * sizeof(char *));
112 n = SDL_NumJoysticks();
114 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
115 for (i = 0; i < n; i++) {
116 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
117 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
118 if (SDL_Joysticks[num_joysticks].handle) {
121 SDL_Joysticks[num_joysticks].n_axes
122 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
123 if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
125 Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
126 SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
129 SDL_Joysticks[num_joysticks].n_buttons
130 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
131 if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
133 Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
134 SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
137 SDL_Joysticks[num_joysticks].n_hats
138 = SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
139 if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
141 Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
142 SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
145 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
146 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
147 con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
149 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
151 sprintf(temp, "J%d A%d", i + 1, j + 1);
152 joyaxis_text[Joystick.n_axes] = d_strdup(temp);
153 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
155 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
157 sprintf(temp, "J%dB%d", i + 1, j + 1);
158 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
159 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
161 for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
163 SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
164 //a hat counts as four buttons
166 sprintf(temp, "J%dH%dUP", i + 1, j + 1);
167 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
168 Joystick.n_buttons++;
170 sprintf(temp, "J%dH%dRIGHT", i + 1, j + 1);
171 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
172 Joystick.n_buttons++;
174 sprintf(temp, "J%dH%dDOWN", i + 1, j + 1);
175 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
176 Joystick.n_buttons++;
178 sprintf(temp, "J%dH%dLEFT", i + 1, j + 1);
179 key_text[KEY_JB1 + Joystick.n_buttons] = d_strdup(temp);
180 Joystick.n_buttons++;
186 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
188 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
189 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
192 joy_num_axes = Joystick.n_axes;
200 while (num_joysticks)
201 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
202 while (Joystick.n_axes--)
203 d_free(joyaxis_text[Joystick.n_axes]);
204 while (Joystick.n_buttons--)
205 d_free(key_text[KEY_JB1 + Joystick.n_buttons]);
208 void joy_get_pos(int *x, int *y)
210 int axis[JOY_MAX_AXES];
212 if (!num_joysticks) {
217 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
219 *x = joy_get_scaled_reading( axis[0], 0 );
220 *y = joy_get_scaled_reading( axis[1], 1 );
225 #if 0 // This is never used?
227 for (i=0; i++; i<buttons) {
228 switch (keyd_pressed[KEY_JB1 + i]) {
243 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
246 ubyte channel_masks = 0;
253 for (i = 0; i < Joystick.n_axes; i++)
255 if ((axis[i] = Joystick.axes[i].value))
256 channel_masks |= 1 << i;
259 return channel_masks;
268 int joy_get_button_state( int btn )
273 if(btn >= Joystick.n_buttons)
278 return keyd_pressed[KEY_JB1 + btn];
281 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
285 for (i = 0; i < Joystick.n_axes; i++)
287 axis_center[i] = Joystick.axes[i].center_val;
288 axis_min[i] = Joystick.axes[i].min_val;
289 axis_max[i] = Joystick.axes[i].max_val;
293 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
297 for (i = 0; i < Joystick.n_axes; i++)
299 Joystick.axes[i].center_val = axis_center[i];
300 Joystick.axes[i].min_val = axis_min[i];
301 Joystick.axes[i].max_val = axis_max[i];
305 int joy_get_scaled_reading( int raw, int axis_num )
312 raw -= Joystick.axes[axis_num].center_val;
315 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
317 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
322 x = ((raw << 7) / d);
331 d = (joy_deadzone) * 6;
332 if ((x > (-1*d)) && (x < d))
339 void joy_set_slow_reading( int flag )