Fixed binding the D-pad on some Super NES style controllers
authorSam Lantinga <slouken@libsdl.org>
Tue, 03 Jan 2017 23:39:28 -0800
changeset 107457461fcef6ae2
parent 10744 f42f107a6035
child 10746 95c57a177719
Fixed binding the D-pad on some Super NES style controllers
Fixed a case where partial trigger pull could be bound to another button

There is a fundamental problem not resolved by this commit:

Some controllers have axes (triggers, pedals, etc.) that don't start at zero, but we're guaranteed that if we get a value that it's correct. For these controllers, the current code works, where we take the first value we get and use that as the zero point and generate axis motion starting from that point on.

Other controllers have digital axes (D-pad) that assume a zero starting point, and the first value we get is the min or max axis value when the D-pad is moved. For these controllers, the current code thinks that the zero point is the axis value after the D-pad motion and this doesn't work.

My hypothesis is that the first class of devices is more common and that we should solve for that, and add an exception to SDL_JoystickAxesCenteredAtZero() as needed for the second class of devices.
src/joystick/SDL_joystick.c
src/joystick/SDL_sysjoystick.h
src/joystick/bsd/SDL_sysjoystick.c
src/joystick/darwin/SDL_sysjoystick.c
src/joystick/emscripten/SDL_sysjoystick.c
src/joystick/haiku/SDL_haikujoystick.cc
src/joystick/windows/SDL_mmjoystick.c
test/controllermap.c
     1.1 --- a/src/joystick/SDL_joystick.c	Tue Jan 03 00:44:05 2017 -0800
     1.2 +++ b/src/joystick/SDL_joystick.c	Tue Jan 03 23:39:28 2017 -0800
     1.3 @@ -114,6 +114,34 @@
     1.4  }
     1.5  
     1.6  /*
     1.7 + * Return true if this joystick is known to have all axes centered at zero
     1.8 + * This isn't generally needed unless the joystick never generates an initial axis value near zero,
     1.9 + * e.g. it's emulating axes with digital buttons
    1.10 + */
    1.11 +static SDL_bool
    1.12 +SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
    1.13 +{
    1.14 +    struct {
    1.15 +        Uint16 vendor;
    1.16 +        Uint16 product;
    1.17 +    } zero_centered_joysticks[] = {
    1.18 +        { 0x0e8f, 0x3013 }, /* Unknown Super NES USB adapter */
    1.19 +    };
    1.20 +
    1.21 +    int i;
    1.22 +    Uint16 vendor = SDL_JoystickGetVendor(joystick);
    1.23 +    Uint16 product = SDL_JoystickGetProduct(joystick);
    1.24 +
    1.25 +    for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
    1.26 +        if (vendor == zero_centered_joysticks[i].vendor &&
    1.27 +            product == zero_centered_joysticks[i].product) {
    1.28 +            return SDL_TRUE;
    1.29 +        }
    1.30 +    }
    1.31 +    return SDL_FALSE;
    1.32 +}
    1.33 +
    1.34 +/*
    1.35   * Open a joystick for use - the index passed as an argument refers to
    1.36   * the N'th joystick on the system.  This index is the value which will
    1.37   * identify this joystick in future joystick events.
    1.38 @@ -192,6 +220,15 @@
    1.39      }
    1.40      joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
    1.41  
    1.42 +    /* If this joystick is known to have all zero centered axes, skip the auto-centering code */
    1.43 +    if (SDL_JoystickAxesCenteredAtZero(joystick)) {
    1.44 +        int i;
    1.45 +
    1.46 +        for (i = 0; i < joystick->naxes; ++i) {
    1.47 +            joystick->axes[i].has_initial_value = SDL_TRUE;
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51      /* Add joystick to list */
    1.52      ++joystick->ref_count;
    1.53      /* Link the joystick in the list */
    1.54 @@ -597,25 +634,25 @@
    1.55  SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
    1.56  {
    1.57      int posted;
    1.58 +    const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 256;
    1.59  
    1.60      /* Make sure we're not getting garbage or duplicate events */
    1.61      if (axis >= joystick->naxes) {
    1.62          return 0;
    1.63      }
    1.64 -    if (value == joystick->axes[axis].value) {
    1.65 +    if (!joystick->axes[axis].has_initial_value) {
    1.66 +        joystick->axes[axis].value = value;
    1.67 +        joystick->axes[axis].zero = value;
    1.68 +        joystick->axes[axis].has_initial_value = SDL_TRUE;
    1.69 +    }
    1.70 +    if (SDL_abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) {
    1.71          return 0;
    1.72      }
    1.73 -    if (!joystick->axes[axis].moved) {
    1.74 -        if (joystick->axes[axis].intial_value == 0) {
    1.75 -            joystick->axes[axis].intial_value = value;
    1.76 -            return 0;
    1.77 -        }
    1.78 -        if (value == joystick->axes[axis].intial_value) {
    1.79 -            return 0;
    1.80 -        }
    1.81 -
    1.82 -        /* We got more than 0 and the initial value from the joystick, consider it as having actually moved */
    1.83 -        joystick->axes[axis].moved = SDL_TRUE;
    1.84 +    if (!joystick->axes[axis].sent_initial_value) {
    1.85 +        int initial_value = joystick->axes[axis].value;
    1.86 +        joystick->axes[axis].sent_initial_value = SDL_TRUE;
    1.87 +        joystick->axes[axis].value = value; /* Just so we pass the check above */
    1.88 +        SDL_PrivateJoystickAxis(joystick, axis, initial_value);
    1.89      }
    1.90  
    1.91      /* We ignore events if we don't have keyboard focus, except for centering
    1.92 @@ -798,9 +835,11 @@
    1.93          if (joystick->force_recentering) {
    1.94              int i;
    1.95  
    1.96 -            /* Tell the app that everything is centered/unpressed...  */
    1.97 +            /* Tell the app that everything is centered/unpressed... */
    1.98              for (i = 0; i < joystick->naxes; i++) {
    1.99 -                SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
   1.100 +                if (joystick->axes[i].has_initial_value) {
   1.101 +                    SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
   1.102 +                }
   1.103              }
   1.104  
   1.105              for (i = 0; i < joystick->nbuttons; i++) {
     2.1 --- a/src/joystick/SDL_sysjoystick.h	Tue Jan 03 00:44:05 2017 -0800
     2.2 +++ b/src/joystick/SDL_sysjoystick.h	Tue Jan 03 23:39:28 2017 -0800
     2.3 @@ -31,10 +31,10 @@
     2.4  /* The SDL joystick structure */
     2.5  typedef struct _SDL_JoystickAxisInfo
     2.6  {
     2.7 -    Sint16 value;           /* Current axis states */
     2.8 -    Sint16 zero;            /* Zero point on the axis (-32768 for triggers) */
     2.9 -    Sint16 intial_value;    /* The very first value we saw on this axis */
    2.10 -    SDL_bool moved;         /* Whether the axis has moved */
    2.11 +    Sint16 value;               /* Current axis states */
    2.12 +    Sint16 zero;                /* Zero point on the axis (-32768 for triggers) */
    2.13 +    SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */
    2.14 +    SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
    2.15  } SDL_JoystickAxisInfo;
    2.16  
    2.17  struct _SDL_Joystick
     3.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Tue Jan 03 00:44:05 2017 -0800
     3.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Tue Jan 03 23:39:28 2017 -0800
     3.3 @@ -522,12 +522,8 @@
     3.4                  v *= 32768 / ((ymax - ymin + 1) / 2);
     3.5                  SDL_PrivateJoystickAxis(joy, 1, v);
     3.6              }
     3.7 -            if (gameport.b1 != joy->buttons[0]) {
     3.8 -                SDL_PrivateJoystickButton(joy, 0, gameport.b1);
     3.9 -            }
    3.10 -            if (gameport.b2 != joy->buttons[1]) {
    3.11 -                SDL_PrivateJoystickButton(joy, 1, gameport.b2);
    3.12 -            }
    3.13 +            SDL_PrivateJoystickButton(joy, 0, gameport.b1);
    3.14 +            SDL_PrivateJoystickButton(joy, 1, gameport.b2);
    3.15          }
    3.16          return;
    3.17      }
    3.18 @@ -563,9 +559,7 @@
    3.19                              v *= 32768 /
    3.20                                  ((hitem.logical_maximum -
    3.21                                    hitem.logical_minimum + 1) / 2);
    3.22 -                            if (v != joy->axes[naxe].value) {
    3.23 -                                SDL_PrivateJoystickAxis(joy, naxe, v);
    3.24 -                            }
    3.25 +                            SDL_PrivateJoystickAxis(joy, naxe, v);
    3.26                          } else if (usage == HUG_HAT_SWITCH) {
    3.27                              v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
    3.28                              SDL_PrivateJoystickHat(joy, 0,
    3.29 @@ -576,9 +570,7 @@
    3.30                      }
    3.31                  case HUP_BUTTON:
    3.32                      v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
    3.33 -                    if (joy->buttons[nbutton] != v) {
    3.34 -                        SDL_PrivateJoystickButton(joy, nbutton, v);
    3.35 -                    }
    3.36 +                    SDL_PrivateJoystickButton(joy, nbutton, v);
    3.37                      nbutton++;
    3.38                      break;
    3.39                  default:
     4.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Tue Jan 03 00:44:05 2017 -0800
     4.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Tue Jan 03 23:39:28 2017 -0800
     4.3 @@ -692,9 +692,7 @@
     4.4      i = 0;
     4.5      while (element) {
     4.6          value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
     4.7 -        if (value != joystick->axes[i].value) {
     4.8 -            SDL_PrivateJoystickAxis(joystick, i, value);
     4.9 -        }
    4.10 +        SDL_PrivateJoystickAxis(joystick, i, value);
    4.11          element = element->pNext;
    4.12          ++i;
    4.13      }
    4.14 @@ -706,9 +704,7 @@
    4.15          if (value > 1) {          /* handle pressure-sensitive buttons */
    4.16              value = 1;
    4.17          }
    4.18 -        if (value != joystick->buttons[i]) {
    4.19 -            SDL_PrivateJoystickButton(joystick, i, value);
    4.20 -        }
    4.21 +        SDL_PrivateJoystickButton(joystick, i, value);
    4.22          element = element->pNext;
    4.23          ++i;
    4.24      }
    4.25 @@ -759,9 +755,7 @@
    4.26              break;
    4.27          }
    4.28  
    4.29 -        if (pos != joystick->hats[i]) {
    4.30 -            SDL_PrivateJoystickHat(joystick, i, pos);
    4.31 -        }
    4.32 +        SDL_PrivateJoystickHat(joystick, i, pos);
    4.33  
    4.34          element = element->pNext;
    4.35          ++i;
     5.1 --- a/src/joystick/emscripten/SDL_sysjoystick.c	Tue Jan 03 00:44:05 2017 -0800
     5.2 +++ b/src/joystick/emscripten/SDL_sysjoystick.c	Tue Jan 03 23:39:28 2017 -0800
     5.3 @@ -146,7 +146,7 @@
     5.4      /* Need to decrement the joystick count before we post the event */
     5.5      --numjoysticks;
     5.6  
     5.7 -	SDL_PrivateJoystickRemoved(item->device_instance);
     5.8 +    SDL_PrivateJoystickRemoved(item->device_instance);
     5.9  
    5.10  #ifdef DEBUG_JOYSTICK
    5.11      SDL_Log("Removed joystick with id %d", item->device_instance);
     6.1 --- a/src/joystick/haiku/SDL_haikujoystick.cc	Tue Jan 03 00:44:05 2017 -0800
     6.2 +++ b/src/joystick/haiku/SDL_haikujoystick.cc	Tue Jan 03 23:39:28 2017 -0800
     6.3 @@ -176,7 +176,6 @@
     6.4              SDL_HAT_LEFT,
     6.5              SDL_HAT_LEFTUP
     6.6          };
     6.7 -        const int JITTER = (32768 / 10);        /* 10% jitter threshold (ok?) */
     6.8  
     6.9          BJoystick *stick;
    6.10          int i, change;
    6.11 @@ -197,24 +196,17 @@
    6.12  
    6.13          /* Generate axis motion events */
    6.14          for (i = 0; i < joystick->naxes; ++i) {
    6.15 -            change = ((int32) axes[i] - joystick->axes[i].value);
    6.16 -            if ((change > JITTER) || (change < -JITTER)) {
    6.17 -                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
    6.18 -            }
    6.19 +            SDL_PrivateJoystickAxis(joystick, i, axes[i]);
    6.20          }
    6.21  
    6.22          /* Generate hat change events */
    6.23          for (i = 0; i < joystick->nhats; ++i) {
    6.24 -            if (hats[i] != joystick->hats[i]) {
    6.25 -                SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
    6.26 -            }
    6.27 +            SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
    6.28          }
    6.29  
    6.30          /* Generate button events */
    6.31          for (i = 0; i < joystick->nbuttons; ++i) {
    6.32 -            if ((buttons & 0x01) != joystick->buttons[i]) {
    6.33 -                SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
    6.34 -            }
    6.35 +            SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
    6.36              buttons >>= 1;
    6.37          }
    6.38      }
     7.1 --- a/src/joystick/windows/SDL_mmjoystick.c	Tue Jan 03 00:44:05 2017 -0800
     7.2 +++ b/src/joystick/windows/SDL_mmjoystick.c	Tue Jan 03 23:39:28 2017 -0800
     7.3 @@ -41,8 +41,6 @@
     7.4  #define MAX_JOYSTICKS   16
     7.5  #define MAX_AXES    6       /* each joystick can have up to 6 axes */
     7.6  #define MAX_BUTTONS 32      /* and 32 buttons                      */
     7.7 -/* limit axis to 256 possible positions to filter out noise */
     7.8 -#define JOY_AXIS_THRESHOLD      (((SDL_JOYSTICK_AXIS_MAX)-(SDL_JOYSTICK_AXIS_MIN))/256)
     7.9  #define JOY_BUTTON_FLAG(n)  (1<<n)
    7.10  
    7.11  
    7.12 @@ -340,14 +338,8 @@
    7.13      transaxis = joystick->hwdata->transaxis;
    7.14      for (i = 0; i < joystick->naxes; i++) {
    7.15          if (joyinfo.dwFlags & flags[i]) {
    7.16 -            value =
    7.17 -                (int) (((float) pos[i] +
    7.18 -                        transaxis[i].offset) * transaxis[i].scale);
    7.19 -            change = (value - joystick->axes[i].value);
    7.20 -            if ((change < -JOY_AXIS_THRESHOLD)
    7.21 -                || (change > JOY_AXIS_THRESHOLD)) {
    7.22 -                SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
    7.23 -            }
    7.24 +            value = (int) (((float) pos[i] + transaxis[i].offset) * transaxis[i].scale);
    7.25 +            SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
    7.26          }
    7.27      }
    7.28  
    7.29 @@ -355,15 +347,9 @@
    7.30      if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
    7.31          for (i = 0; i < joystick->nbuttons; ++i) {
    7.32              if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
    7.33 -                if (!joystick->buttons[i]) {
    7.34 -                    SDL_PrivateJoystickButton(joystick, (Uint8) i,
    7.35 -                                              SDL_PRESSED);
    7.36 -                }
    7.37 +                SDL_PrivateJoystickButton(joystick, (Uint8) i, SDL_PRESSED);
    7.38              } else {
    7.39 -                if (joystick->buttons[i]) {
    7.40 -                    SDL_PrivateJoystickButton(joystick, (Uint8) i,
    7.41 -                                              SDL_RELEASED);
    7.42 -                }
    7.43 +                SDL_PrivateJoystickButton(joystick, (Uint8) i, SDL_RELEASED);
    7.44              }
    7.45          }
    7.46      }
    7.47 @@ -373,9 +359,7 @@
    7.48          Uint8 pos;
    7.49  
    7.50          pos = TranslatePOV(joyinfo.dwPOV);
    7.51 -        if (pos != joystick->hats[0]) {
    7.52 -            SDL_PrivateJoystickHat(joystick, 0, pos);
    7.53 -        }
    7.54 +        SDL_PrivateJoystickHat(joystick, 0, pos);
    7.55      }
    7.56  }
    7.57  
     8.1 --- a/test/controllermap.c	Tue Jan 03 00:44:05 2017 -0800
     8.2 +++ b/test/controllermap.c	Tue Jan 03 23:39:28 2017 -0800
     8.3 @@ -197,6 +197,7 @@
     8.4  static void
     8.5  SetCurrentBinding(int iBinding)
     8.6  {
     8.7 +    int iIndex;
     8.8      SDL_GameControllerExtendedBind *pBinding;
     8.9  
    8.10      if (iBinding < 0) {
    8.11 @@ -213,11 +214,39 @@
    8.12      pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
    8.13      SDL_zerop(pBinding);
    8.14  
    8.15 -    SDL_memset(s_arrAxisState, 0, s_nNumAxes*sizeof(*s_arrAxisState));
    8.16 +    for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
    8.17 +        s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
    8.18 +    }
    8.19  
    8.20      s_unPendingAdvanceTime = 0;
    8.21  }
    8.22  
    8.23 +static SDL_bool
    8.24 +BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
    8.25 +{
    8.26 +    if (pBindingA->bindType != pBindingB->bindType)
    8.27 +    {
    8.28 +        return SDL_FALSE;
    8.29 +    }
    8.30 +    switch (pBindingA->bindType)
    8.31 +    {
    8.32 +    case SDL_CONTROLLER_BINDTYPE_AXIS:
    8.33 +        if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
    8.34 +            return SDL_FALSE;
    8.35 +        }
    8.36 +        {
    8.37 +            int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
    8.38 +            int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
    8.39 +            int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
    8.40 +            int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
    8.41 +            return (minA <= minB && maxA >= maxB);
    8.42 +        }
    8.43 +        /* Not reached */
    8.44 +    default:
    8.45 +        return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
    8.46 +    }
    8.47 +}
    8.48 +
    8.49  static void
    8.50  ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
    8.51  {
    8.52 @@ -227,7 +256,8 @@
    8.53  
    8.54      /* Do we already have this binding? */
    8.55      for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
    8.56 -        if (SDL_memcmp(pBinding, &s_arrBindings[iIndex], sizeof(*pBinding)) == 0) {
    8.57 +        pCurrent = &s_arrBindings[iIndex];
    8.58 +        if (BBindingContainsBinding(pCurrent, pBinding)) {
    8.59              if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
    8.60                  /* Skip to the next binding */
    8.61                  SetCurrentBinding(s_iCurrentBinding + 1);
    8.62 @@ -263,7 +293,9 @@
    8.63  
    8.64          bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
    8.65          bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
    8.66 -        if (bNativeAxis == bCurrentAxis) {
    8.67 +        if (bNativeAxis == bCurrentAxis &&
    8.68 +            (pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
    8.69 +             pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
    8.70              /* We already have a binding of the type we want, ignore the new one */
    8.71              return;
    8.72          }
    8.73 @@ -412,8 +444,8 @@
    8.74                      if (nCurrentDistance > nFarthestDistance) {
    8.75                          pAxisState->m_nFarthestValue = nValue;
    8.76                      }
    8.77 -                    if (nCurrentDistance <= 8000 && nFarthestDistance >= 20000) {
    8.78 -                        /* We've gone out and back, let's bind this axis */
    8.79 +                    if (nFarthestDistance >= 20000 && nCurrentDistance <= 10000) {
    8.80 +                        /* We've gone out far enough and started to come back, let's bind this axis */
    8.81                          SDL_GameControllerExtendedBind binding;
    8.82                          SDL_zero(binding);
    8.83                          binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;