2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
14 #include "osregistry.h"
19 static int Joy_inited = 0;
20 int Dead_zone_size = 10;
21 int Cur_joystick = -1;
22 int Joy_sensitivity = 9;
24 static int Joy_last_x_reading = 0;
25 static int Joy_last_y_reading = 0;
27 typedef struct joy_button_info {
28 int actual_state; // Set if the button is physically down
29 int state; // Set when the button goes from up to down, cleared on down to up. Different than actual_state after a flush.
33 uint last_down_check; // timestamp in milliseconds of last
36 static Joy_info joystick;
39 static SDL_JoystickID joy_id = -1;
41 joy_button_info joy_buttons[JOY_TOTAL_BUTTONS];
59 if (SDL_JoystickGetAttached(sdljoy)) {
60 SDL_JoystickClose(sdljoy);
65 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
73 max_count = SDL_NumJoysticks();
75 for (j = 0; j < max_count; j++) {
76 joy = SDL_JoystickOpen(j);
79 // nprintf (("JOYSTICK", "Joystick #%d: %s\n", j - JOYSTICKID1 + 1, SDL_JoystickName(j)));
80 mprintf(("Joystick #%d: %s %s\n", j + 1, SDL_JoystickName(joy), (j == Cur_joystick) ? "*" : " "));
81 mprintf((" Axes: %d\n", SDL_JoystickNumAxes(joy)));
82 mprintf((" Buttons: %d\n", SDL_JoystickNumButtons(joy)));
83 mprintf((" Hats: %d\n", SDL_JoystickNumHats(joy)));
84 mprintf((" Balls: %d\n", SDL_JoystickNumBalls(joy)));
85 mprintf((" Haptic: %s\n", SDL_JoystickIsHaptic(joy) ? "Yes" : "No"));
87 SDL_JoystickClose (joy);
102 if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS)) {
106 tmp = joy_buttons[btn].state;
111 int joy_down_count(int btn, int reset_count)
119 if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS)) {
123 tmp = joy_buttons[btn].down_count;
125 joy_buttons[btn].down_count = 0;
131 float joy_down_time(int btn)
134 unsigned int now, delta;
141 if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS)) {
145 bi = &joy_buttons[btn];
147 now = timer_get_milliseconds();
148 delta = now - bi->last_down_check;
150 if ( (now - bi->last_down_check) > 0)
151 rval = i2fl((now - bi->down_time)) / delta;
156 bi->last_down_check = now;
166 void joy_mark_button(int btn, int state)
175 if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS)) {
179 bi = &joy_buttons[btn];
186 bi->down_time = timer_get_milliseconds();
188 // toggle off other positions if hat
189 if (btn >= JOY_HATBACK) {
190 for (i = JOY_HATBACK; i < (JOY_HATBACK+JOY_NUM_HAT_POS); i++) {
192 joy_buttons[i].state = 0;
202 // special hat handling - make sure all hat pos are off
203 if (btn == JOY_HATBACK) {
204 for (i = JOY_HATBACK; i < (JOY_HATBACK+JOY_NUM_HAT_POS); i++) {
205 joy_buttons[i].state = 0;
220 for ( i = 0; i < JOY_TOTAL_BUTTONS; i++) {
221 bi = &joy_buttons[i];
226 bi->last_down_check = timer_get_milliseconds();
230 int joy_get_unscaled_reading(int axn)
238 if (axn >= joystick.num_axes) {
242 int raw = joystick.axis_current[axn];
244 rng = joystick.axis_max[axn] - joystick.axis_min[axn];
245 raw -= joystick.axis_min[axn]; // adjust for linear range starting at 0
253 return (int) ((unsigned int) raw * (unsigned int) F1_0 / (unsigned int) rng); // convert to 0 - F1_0 range.
256 // --------------------------------------------------------------
257 // joy_get_scaled_reading()
259 // input: raw => the raw value for an axis position
260 // axn => axis number, numbered starting at 0
262 // return: joy_get_scaled_reading will return a value that represents
263 // the joystick pos from -1 to +1 for the specified axis number 'axn', and
264 // the raw value 'raw'
266 int joy_get_scaled_reading(int axn)
268 int x, d, dead_zone, rng, raw;
269 float percent, sensitivity_percent, non_sensitivity_percent;
275 if (axn >= joystick.num_axes) {
279 raw = joystick.axis_current[axn] - joystick.axis_center[axn];
281 dead_zone = (joystick.axis_max[axn] - joystick.axis_min[axn]) * Dead_zone_size / 100;
283 if (raw < -dead_zone) {
284 rng = joystick.axis_center[axn] - joystick.axis_min[axn] - dead_zone;
285 d = -raw - dead_zone;
287 } else if (raw > dead_zone) {
288 rng = joystick.axis_max[axn] - joystick.axis_center[axn] - dead_zone;
298 SDL_assert(Joy_sensitivity >= 0 && Joy_sensitivity <= 9);
300 // compute percentages as a range between 0 and 1
301 sensitivity_percent = (float) Joy_sensitivity / 9.0f;
302 non_sensitivity_percent = (float) (9 - Joy_sensitivity) / 9.0f;
304 // find percent of max axis is at
305 percent = (float) d / (float) rng;
307 // work sensitivity on axis value
308 percent = (percent * sensitivity_percent + percent * percent * percent * percent * percent * non_sensitivity_percent);
310 x = (int) ((float) F1_0 * percent);
312 //nprintf(("AI", "d=%6i, sens=%3i, percent=%6.3f, val=%6i, ratio=%6.3f\n", d, Joy_sensitivity, percent, (raw<0) ? -x : x, (float) d/x));
321 // --------------------------------------------------------------
324 // input: x => OUTPUT PARAMETER: x-axis position of stick (-1 to 1)
325 // y => OUTPUT PARAMETER: y-axis position of stick (-1 to 1)
326 // z => OUTPUT PARAMETER: z-axis (throttle) position of stick (-1 to 1)
327 // r => OUTPUT PARAMETER: rudder position of stick (-1 to 1)
329 // return: success => 1
332 int joy_get_pos(int *x, int *y, int *z, int *rx)
343 // joy_get_scaled_reading will return a value represents the joystick
345 if (x && joystick.num_axes > 0) {
346 *x = joy_get_scaled_reading(0);
347 Joy_last_x_reading = *x;
350 if (y && joystick.num_axes > 1) {
351 *y = joy_get_scaled_reading(1);
352 Joy_last_y_reading = *y;
355 if (z && joystick.num_axes > 2) {
356 *z = joy_get_unscaled_reading(2);
359 if (rx && joystick.num_axes > 3) {
360 *rx = joy_get_scaled_reading(3);
374 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
375 mprintf(("Could not initialize joystick\n"));
379 num_sticks = SDL_NumJoysticks();
381 if (num_sticks < 1) {
382 mprintf(("No joysticks found\n"));
386 Cur_joystick = os_config_read_uint (NULL, "CurrentJoystick", 0);
388 if (Cur_joystick >= num_sticks) {
394 sdljoy = SDL_JoystickOpen(Cur_joystick);
396 if (sdljoy == NULL) {
397 mprintf(("Unable to init joystick %d\n", Cur_joystick));
403 joy_id = SDL_JoystickInstanceID(sdljoy);
405 joystick.num_axes = SDL_JoystickNumAxes(sdljoy);
409 // Fake a calibration
412 for (i = 0; i < JOY_NUM_AXES; i++) {
413 joystick.axis_min[i] = 0;
414 joystick.axis_max[i] = 65536;
415 joystick.axis_current[i] = 32768;
429 for (int i = 0; i < JOY_NUM_AXES; i++) {
430 if (i < joystick.num_axes) {
431 joystick.axis_center[i] = SDL_JoystickGetAxis(sdljoy, i) + 32768;
433 joystick.axis_center[i] = 32768;
438 int joystick_read_raw_axis(int num_axes, int *axis)
446 for (i = 0; i < num_axes; i++) {
447 if (i < joystick.num_axes) {
448 axis[i] = joystick.axis_current[i];
457 bool joy_axis_valid(int axis)
459 return (axis < joystick.num_axes);
462 void joystick_update_axis(int axis, int value)
464 if (axis < JOY_NUM_AXES) {
465 joystick.axis_current[axis] = value + 32768;