Fixed issue where the throttle and other axes on racing wheels don't start at zero and show up as immediate input when the wheel is turned for the first time. Wait until they are actually moved before generating input from them.
authorSam Lantinga <slouken@libsdl.org>
Thu, 22 Dec 2016 17:33:45 -0800
changeset 10713580fcf8605e3
parent 10712 47a5366726c0
child 10714 a9c15922ad7b
Fixed issue where the throttle and other axes on racing wheels don't start at zero and show up as immediate input when the wheel is turned for the first time. Wait until they are actually moved before generating input from them.
src/joystick/SDL_gamecontroller.c
src/joystick/SDL_joystick.c
src/joystick/SDL_sysjoystick.h
     1.1 --- a/src/joystick/SDL_gamecontroller.c	Mon Dec 19 11:15:53 2016 -0800
     1.2 +++ b/src/joystick/SDL_gamecontroller.c	Thu Dec 22 17:33:45 2016 -0800
     1.3 @@ -1132,12 +1132,12 @@
     1.4          int leftTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERLEFT];
     1.5          int rightTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERRIGHT];
     1.6          if (leftTriggerMapping >= 0) {
     1.7 -            gamecontroller->joystick->axes[leftTriggerMapping] =
     1.8 -            gamecontroller->joystick->axes_zero[leftTriggerMapping] = (Sint16)-32768;
     1.9 +            gamecontroller->joystick->axes[leftTriggerMapping].value =
    1.10 +            gamecontroller->joystick->axes[leftTriggerMapping].zero = (Sint16)-32768;
    1.11          }
    1.12          if (rightTriggerMapping >= 0) {
    1.13 -            gamecontroller->joystick->axes[rightTriggerMapping] =
    1.14 -            gamecontroller->joystick->axes_zero[rightTriggerMapping] = (Sint16)-32768;
    1.15 +            gamecontroller->joystick->axes[rightTriggerMapping].value =
    1.16 +            gamecontroller->joystick->axes[rightTriggerMapping].zero = (Sint16)-32768;
    1.17          }
    1.18      }
    1.19  
     2.1 --- a/src/joystick/SDL_joystick.c	Mon Dec 19 11:15:53 2016 -0800
     2.2 +++ b/src/joystick/SDL_joystick.c	Thu Dec 22 17:33:45 2016 -0800
     2.3 @@ -170,20 +170,16 @@
     2.4          joystick->name = NULL;
     2.5  
     2.6      if (joystick->naxes > 0) {
     2.7 -        joystick->axes = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16));
     2.8 -        joystick->axes_zero = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16));
     2.9 +        joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo));
    2.10      }
    2.11      if (joystick->nhats > 0) {
    2.12 -        joystick->hats = (Uint8 *) SDL_malloc
    2.13 -            (joystick->nhats * sizeof(Uint8));
    2.14 +        joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8));
    2.15      }
    2.16      if (joystick->nballs > 0) {
    2.17 -        joystick->balls = (struct balldelta *) SDL_malloc
    2.18 -            (joystick->nballs * sizeof(*joystick->balls));
    2.19 +        joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
    2.20      }
    2.21      if (joystick->nbuttons > 0) {
    2.22 -        joystick->buttons = (Uint8 *) SDL_malloc
    2.23 -            (joystick->nbuttons * sizeof(Uint8));
    2.24 +        joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8));
    2.25      }
    2.26      if (((joystick->naxes > 0) && !joystick->axes)
    2.27          || ((joystick->nhats > 0) && !joystick->hats)
    2.28 @@ -194,20 +190,6 @@
    2.29          SDL_UnlockJoystickList();
    2.30          return NULL;
    2.31      }
    2.32 -    if (joystick->axes) {
    2.33 -        SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
    2.34 -        SDL_memset(joystick->axes_zero, 0, joystick->naxes * sizeof(Sint16));
    2.35 -    }
    2.36 -    if (joystick->hats) {
    2.37 -        SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
    2.38 -    }
    2.39 -    if (joystick->balls) {
    2.40 -        SDL_memset(joystick->balls, 0,
    2.41 -            joystick->nballs * sizeof(*joystick->balls));
    2.42 -    }
    2.43 -    if (joystick->buttons) {
    2.44 -        SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
    2.45 -    }
    2.46      joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
    2.47  
    2.48      /* Add joystick to list */
    2.49 @@ -302,7 +284,7 @@
    2.50          return (0);
    2.51      }
    2.52      if (axis < joystick->naxes) {
    2.53 -        state = joystick->axes[axis];
    2.54 +        state = joystick->axes[axis].value;
    2.55      } else {
    2.56          SDL_SetError("Joystick only has %d axes", joystick->naxes);
    2.57          state = 0;
    2.58 @@ -486,7 +468,6 @@
    2.59  
    2.60      /* Free the data associated with this joystick */
    2.61      SDL_free(joystick->axes);
    2.62 -    SDL_free(joystick->axes_zero);
    2.63      SDL_free(joystick->hats);
    2.64      SDL_free(joystick->balls);
    2.65      SDL_free(joystick->buttons);
    2.66 @@ -621,22 +602,34 @@
    2.67      if (axis >= joystick->naxes) {
    2.68          return 0;
    2.69      }
    2.70 -    if (value == joystick->axes[axis]) {
    2.71 +    if (value == joystick->axes[axis].value) {
    2.72          return 0;
    2.73      }
    2.74 +    if (!joystick->axes[axis].moved) {
    2.75 +        if (joystick->axes[axis].intial_value == 0) {
    2.76 +            joystick->axes[axis].intial_value = value;
    2.77 +            return 0;
    2.78 +        }
    2.79 +        if (value == joystick->axes[axis].intial_value) {
    2.80 +            return 0;
    2.81 +        }
    2.82 +
    2.83 +        /* We got more than 0 and the initial value from the joystick, consider it as having actually moved */
    2.84 +        joystick->axes[axis].moved = SDL_TRUE;
    2.85 +    }
    2.86  
    2.87      /* We ignore events if we don't have keyboard focus, except for centering
    2.88       * events.
    2.89       */
    2.90      if (SDL_PrivateJoystickShouldIgnoreEvent()) {
    2.91 -        if ((value > joystick->axes_zero[axis] && value >= joystick->axes[axis]) ||
    2.92 -            (value < joystick->axes_zero[axis] && value <= joystick->axes[axis])) {
    2.93 +        if ((value > joystick->axes[axis].zero && value >= joystick->axes[axis].value) ||
    2.94 +            (value < joystick->axes[axis].zero && value <= joystick->axes[axis].value)) {
    2.95              return 0;
    2.96          }
    2.97      }
    2.98  
    2.99      /* Update internal joystick state */
   2.100 -    joystick->axes[axis] = value;
   2.101 +    joystick->axes[axis].value = value;
   2.102  
   2.103      /* Post the event, if desired */
   2.104      posted = 0;
   2.105 @@ -807,7 +800,7 @@
   2.106  
   2.107              /* Tell the app that everything is centered/unpressed...  */
   2.108              for (i = 0; i < joystick->naxes; i++) {
   2.109 -                SDL_PrivateJoystickAxis(joystick, i, joystick->axes_zero[i]);
   2.110 +                SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
   2.111              }
   2.112  
   2.113              for (i = 0; i < joystick->nbuttons; i++) {
     3.1 --- a/src/joystick/SDL_sysjoystick.h	Mon Dec 19 11:15:53 2016 -0800
     3.2 +++ b/src/joystick/SDL_sysjoystick.h	Thu Dec 22 17:33:45 2016 -0800
     3.3 @@ -29,14 +29,21 @@
     3.4  #include "SDL_joystick_c.h"
     3.5  
     3.6  /* The SDL joystick structure */
     3.7 +typedef struct _SDL_JoystickAxisInfo
     3.8 +{
     3.9 +    Sint16 value;           /* Current axis states */
    3.10 +    Sint16 zero;            /* Zero point on the axis (-32768 for triggers) */
    3.11 +    Sint16 intial_value;    /* The very first value we saw on this axis */
    3.12 +    SDL_bool moved;         /* Whether the axis has moved */
    3.13 +} SDL_JoystickAxisInfo;
    3.14 +
    3.15  struct _SDL_Joystick
    3.16  {
    3.17      SDL_JoystickID instance_id; /* Device instance, monotonically increasing from 0 */
    3.18      char *name;                 /* Joystick name - system dependent */
    3.19  
    3.20      int naxes;                  /* Number of axis controls on the joystick */
    3.21 -    Sint16 *axes;               /* Current axis states */
    3.22 -    Sint16 *axes_zero;          /* Zero point on the axis (-32768 for triggers) */
    3.23 +    SDL_JoystickAxisInfo *axes;
    3.24  
    3.25      int nhats;                  /* Number of hats on the joystick */
    3.26      Uint8 *hats;                /* Current hat states */