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 // Make sure it's calibrated properly.
243 if (joystick.axis_center[axn] - joystick.axis_min[axn] < 5)
246 if (joystick.axis_max[axn] - joystick.axis_center[axn] < 5)
249 int raw = joystick.axis_current[axn];
251 rng = joystick.axis_max[axn] - joystick.axis_min[axn];
252 raw -= joystick.axis_min[axn]; // adjust for linear range starting at 0
260 return (int) ((unsigned int) raw * (unsigned int) F1_0 / (unsigned int) rng); // convert to 0 - F1_0 range.
263 // --------------------------------------------------------------
264 // joy_get_scaled_reading()
266 // input: raw => the raw value for an axis position
267 // axn => axis number, numbered starting at 0
269 // return: joy_get_scaled_reading will return a value that represents
270 // the joystick pos from -1 to +1 for the specified axis number 'axn', and
271 // the raw value 'raw'
273 int joy_get_scaled_reading(int axn)
275 int x, d, dead_zone, rng, raw;
276 float percent, sensitivity_percent, non_sensitivity_percent;
282 if (axn >= joystick.num_axes) {
286 // Make sure it's calibrated properly.
287 if (joystick.axis_center[axn] - joystick.axis_min[axn] < 5) {
291 if (joystick.axis_max[axn] - joystick.axis_center[axn] < 5) {
295 raw = joystick.axis_current[axn] - joystick.axis_center[axn];
297 dead_zone = (joystick.axis_max[axn] - joystick.axis_min[axn]) * Dead_zone_size / 100;
299 if (raw < -dead_zone) {
300 rng = joystick.axis_center[axn] - joystick.axis_min[axn] - dead_zone;
301 d = -raw - dead_zone;
303 } else if (raw > dead_zone) {
304 rng = joystick.axis_max[axn] - joystick.axis_center[axn] - dead_zone;
314 Assert(Joy_sensitivity >= 0 && Joy_sensitivity <= 9);
316 // compute percentages as a range between 0 and 1
317 sensitivity_percent = (float) Joy_sensitivity / 9.0f;
318 non_sensitivity_percent = (float) (9 - Joy_sensitivity) / 9.0f;
320 // find percent of max axis is at
321 percent = (float) d / (float) rng;
323 // work sensitivity on axis value
324 percent = (percent * sensitivity_percent + percent * percent * percent * percent * percent * non_sensitivity_percent);
326 x = (int) ((float) F1_0 * percent);
328 //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));
337 // --------------------------------------------------------------
340 // input: x => OUTPUT PARAMETER: x-axis position of stick (-1 to 1)
341 // y => OUTPUT PARAMETER: y-axis position of stick (-1 to 1)
342 // z => OUTPUT PARAMETER: z-axis (throttle) position of stick (-1 to 1)
343 // r => OUTPUT PARAMETER: rudder position of stick (-1 to 1)
345 // return: success => 1
348 int joy_get_pos(int *x, int *y, int *z, int *rx)
359 // joy_get_scaled_reading will return a value represents the joystick
361 if (x && joystick.num_axes > 0) {
362 *x = joy_get_scaled_reading(0);
363 Joy_last_x_reading = *x;
366 if (y && joystick.num_axes > 1) {
367 *y = joy_get_scaled_reading(1);
368 Joy_last_y_reading = *y;
371 if (z && joystick.num_axes > 2) {
372 *z = joy_get_unscaled_reading(2);
375 if (rx && joystick.num_axes > 3) {
376 *rx = joy_get_scaled_reading(3);
390 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
391 mprintf(("Could not initialize joystick\n"));
395 num_sticks = SDL_NumJoysticks();
397 if (num_sticks < 1) {
398 mprintf(("No joysticks found\n"));
402 Cur_joystick = os_config_read_uint (NULL, "CurrentJoystick", 0);
404 if (Cur_joystick >= num_sticks) {
410 sdljoy = SDL_JoystickOpen(Cur_joystick);
412 if (sdljoy == NULL) {
413 mprintf(("Unable to init joystick %d\n", Cur_joystick));
419 joy_id = SDL_JoystickInstanceID(sdljoy);
421 joystick.num_axes = SDL_JoystickNumAxes(sdljoy);
425 // Fake a calibration
428 for (i = 0; i < JOY_NUM_AXES; i++) {
429 joystick.axis_min[i] = 0;
430 joystick.axis_max[i] = 65536;
431 joystick.axis_current[i] = 32768;
445 for (int i = 0; i < JOY_NUM_AXES; i++) {
446 // if (i < joystick.num_axes) {
447 // joystick.axis_center[i] = SDL_JoystickGetAxis(sdljoy, i) + 32768;
449 joystick.axis_center[i] = 32768;
454 int joystick_read_raw_axis(int num_axes, int *axis)
462 for (i = 0; i < num_axes; i++) {
463 if (i < joystick.num_axes) {
464 axis[i] = joystick.axis_current[i];
473 bool joy_axis_valid(int axis)
475 return (axis < joystick.num_axes);
478 void joystick_update_axis(int axis, int value)
480 if (axis < JOY_NUM_AXES) {
481 joystick.axis_current[axis] = value + 32768;
485 DCF(joytest, "Test joystick")
488 while (!key_pressed(SDLK_ESCAPE)) {
489 int x, y, axis[JOY_NUM_AXES];
494 joystick_read_raw_axis(JOY_NUM_AXES, axis);
496 x = joy_get_scaled_reading(0);
497 y = joy_get_scaled_reading(1);
499 mprintf(("X=%5d Y=%5d Calibrated X=%6d Y=%6d\n", axis[0], axis[1], x, y));
505 DCF(joytest2, "Test joystick (extended)")
508 while (!key_pressed(SDLK_ESCAPE)) {
509 int x, y, z, r, axis[JOY_NUM_AXES];
514 joystick_read_raw_axis(JOY_NUM_AXES, axis);
516 x = joy_get_scaled_reading(0);
517 y = joy_get_scaled_reading(1);
518 z = joy_get_unscaled_reading(2);
519 r = joy_get_scaled_reading(3);
521 mprintf(("X=%5d Y=%5d Z=%5d Rx=%5d Ry=%5d Rz=%5d Cal X=%6d Y=%6d Z=%6d R=%6d\n", axis[0], axis[1], axis[2], axis[3], axis[4], axis[5], x, y, z, r));