Skip to content

Commit

Permalink
Fixed binding the D-pad on some Super NES style controllers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Jan 4, 2017
1 parent d359180 commit 082132a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 77 deletions.
67 changes: 53 additions & 14 deletions src/joystick/SDL_joystick.c
Expand Up @@ -113,6 +113,34 @@ SDL_JoystickNameForIndex(int device_index)
return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
}

/*
* Return true if this joystick is known to have all axes centered at zero
* This isn't generally needed unless the joystick never generates an initial axis value near zero,
* e.g. it's emulating axes with digital buttons
*/
static SDL_bool
SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
{
struct {
Uint16 vendor;
Uint16 product;
} zero_centered_joysticks[] = {
{ 0x0e8f, 0x3013 }, /* Unknown Super NES USB adapter */
};

int i;
Uint16 vendor = SDL_JoystickGetVendor(joystick);
Uint16 product = SDL_JoystickGetProduct(joystick);

for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
if (vendor == zero_centered_joysticks[i].vendor &&
product == zero_centered_joysticks[i].product) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}

/*
* Open a joystick for use - the index passed as an argument refers to
* the N'th joystick on the system. This index is the value which will
Expand Down Expand Up @@ -192,6 +220,15 @@ SDL_JoystickOpen(int device_index)
}
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;

/* If this joystick is known to have all zero centered axes, skip the auto-centering code */
if (SDL_JoystickAxesCenteredAtZero(joystick)) {
int i;

for (i = 0; i < joystick->naxes; ++i) {
joystick->axes[i].has_initial_value = SDL_TRUE;
}
}

/* Add joystick to list */
++joystick->ref_count;
/* Link the joystick in the list */
Expand Down Expand Up @@ -597,25 +634,25 @@ int
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
{
int posted;
const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 256;

/* Make sure we're not getting garbage or duplicate events */
if (axis >= joystick->naxes) {
return 0;
}
if (value == joystick->axes[axis].value) {
if (!joystick->axes[axis].has_initial_value) {
joystick->axes[axis].value = value;
joystick->axes[axis].zero = value;
joystick->axes[axis].has_initial_value = SDL_TRUE;
}
if (SDL_abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) {
return 0;
}
if (!joystick->axes[axis].moved) {
if (joystick->axes[axis].intial_value == 0) {
joystick->axes[axis].intial_value = value;
return 0;
}
if (value == joystick->axes[axis].intial_value) {
return 0;
}

/* We got more than 0 and the initial value from the joystick, consider it as having actually moved */
joystick->axes[axis].moved = SDL_TRUE;
if (!joystick->axes[axis].sent_initial_value) {
int initial_value = joystick->axes[axis].value;
joystick->axes[axis].sent_initial_value = SDL_TRUE;
joystick->axes[axis].value = value; /* Just so we pass the check above */
SDL_PrivateJoystickAxis(joystick, axis, initial_value);
}

/* We ignore events if we don't have keyboard focus, except for centering
Expand Down Expand Up @@ -798,9 +835,11 @@ SDL_JoystickUpdate(void)
if (joystick->force_recentering) {
int i;

/* Tell the app that everything is centered/unpressed... */
/* Tell the app that everything is centered/unpressed... */
for (i = 0; i < joystick->naxes; i++) {
SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
if (joystick->axes[i].has_initial_value) {
SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
}
}

for (i = 0; i < joystick->nbuttons; i++) {
Expand Down
8 changes: 4 additions & 4 deletions src/joystick/SDL_sysjoystick.h
Expand Up @@ -31,10 +31,10 @@
/* The SDL joystick structure */
typedef struct _SDL_JoystickAxisInfo
{
Sint16 value; /* Current axis states */
Sint16 zero; /* Zero point on the axis (-32768 for triggers) */
Sint16 intial_value; /* The very first value we saw on this axis */
SDL_bool moved; /* Whether the axis has moved */
Sint16 value; /* Current axis states */
Sint16 zero; /* Zero point on the axis (-32768 for triggers) */
SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */
SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
} SDL_JoystickAxisInfo;

struct _SDL_Joystick
Expand Down
16 changes: 4 additions & 12 deletions src/joystick/bsd/SDL_sysjoystick.c
Expand Up @@ -522,12 +522,8 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
v *= 32768 / ((ymax - ymin + 1) / 2);
SDL_PrivateJoystickAxis(joy, 1, v);
}
if (gameport.b1 != joy->buttons[0]) {
SDL_PrivateJoystickButton(joy, 0, gameport.b1);
}
if (gameport.b2 != joy->buttons[1]) {
SDL_PrivateJoystickButton(joy, 1, gameport.b2);
}
SDL_PrivateJoystickButton(joy, 0, gameport.b1);
SDL_PrivateJoystickButton(joy, 1, gameport.b2);
}
return;
}
Expand Down Expand Up @@ -563,9 +559,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
v *= 32768 /
((hitem.logical_maximum -
hitem.logical_minimum + 1) / 2);
if (v != joy->axes[naxe].value) {
SDL_PrivateJoystickAxis(joy, naxe, v);
}
SDL_PrivateJoystickAxis(joy, naxe, v);
} else if (usage == HUG_HAT_SWITCH) {
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
SDL_PrivateJoystickHat(joy, 0,
Expand All @@ -576,9 +570,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
}
case HUP_BUTTON:
v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
if (joy->buttons[nbutton] != v) {
SDL_PrivateJoystickButton(joy, nbutton, v);
}
SDL_PrivateJoystickButton(joy, nbutton, v);
nbutton++;
break;
default:
Expand Down
12 changes: 3 additions & 9 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -692,9 +692,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
i = 0;
while (element) {
value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
if (value != joystick->axes[i].value) {
SDL_PrivateJoystickAxis(joystick, i, value);
}
SDL_PrivateJoystickAxis(joystick, i, value);
element = element->pNext;
++i;
}
Expand All @@ -706,9 +704,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
if (value > 1) { /* handle pressure-sensitive buttons */
value = 1;
}
if (value != joystick->buttons[i]) {
SDL_PrivateJoystickButton(joystick, i, value);
}
SDL_PrivateJoystickButton(joystick, i, value);
element = element->pNext;
++i;
}
Expand Down Expand Up @@ -759,9 +755,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
break;
}

if (pos != joystick->hats[i]) {
SDL_PrivateJoystickHat(joystick, i, pos);
}
SDL_PrivateJoystickHat(joystick, i, pos);

element = element->pNext;
++i;
Expand Down
2 changes: 1 addition & 1 deletion src/joystick/emscripten/SDL_sysjoystick.c
Expand Up @@ -146,7 +146,7 @@ Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gam
/* Need to decrement the joystick count before we post the event */
--numjoysticks;

SDL_PrivateJoystickRemoved(item->device_instance);
SDL_PrivateJoystickRemoved(item->device_instance);

#ifdef DEBUG_JOYSTICK
SDL_Log("Removed joystick with id %d", item->device_instance);
Expand Down
14 changes: 3 additions & 11 deletions src/joystick/haiku/SDL_haikujoystick.cc
Expand Up @@ -176,7 +176,6 @@ extern "C"
SDL_HAT_LEFT,
SDL_HAT_LEFTUP
};
const int JITTER = (32768 / 10); /* 10% jitter threshold (ok?) */

BJoystick *stick;
int i, change;
Expand All @@ -197,24 +196,17 @@ extern "C"

/* Generate axis motion events */
for (i = 0; i < joystick->naxes; ++i) {
change = ((int32) axes[i] - joystick->axes[i].value);
if ((change > JITTER) || (change < -JITTER)) {
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}

/* Generate hat change events */
for (i = 0; i < joystick->nhats; ++i) {
if (hats[i] != joystick->hats[i]) {
SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
}
SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
}

/* Generate button events */
for (i = 0; i < joystick->nbuttons; ++i) {
if ((buttons & 0x01) != joystick->buttons[i]) {
SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
}
SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
buttons >>= 1;
}
}
Expand Down
26 changes: 5 additions & 21 deletions src/joystick/windows/SDL_mmjoystick.c
Expand Up @@ -41,8 +41,6 @@
#define MAX_JOYSTICKS 16
#define MAX_AXES 6 /* each joystick can have up to 6 axes */
#define MAX_BUTTONS 32 /* and 32 buttons */
/* limit axis to 256 possible positions to filter out noise */
#define JOY_AXIS_THRESHOLD (((SDL_JOYSTICK_AXIS_MAX)-(SDL_JOYSTICK_AXIS_MIN))/256)
#define JOY_BUTTON_FLAG(n) (1<<n)


Expand Down Expand Up @@ -340,30 +338,18 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
transaxis = joystick->hwdata->transaxis;
for (i = 0; i < joystick->naxes; i++) {
if (joyinfo.dwFlags & flags[i]) {
value =
(int) (((float) pos[i] +
transaxis[i].offset) * transaxis[i].scale);
change = (value - joystick->axes[i].value);
if ((change < -JOY_AXIS_THRESHOLD)
|| (change > JOY_AXIS_THRESHOLD)) {
SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
}
value = (int) (((float) pos[i] + transaxis[i].offset) * transaxis[i].scale);
SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
}
}

/* joystick button events */
if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
for (i = 0; i < joystick->nbuttons; ++i) {
if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
if (!joystick->buttons[i]) {
SDL_PrivateJoystickButton(joystick, (Uint8) i,
SDL_PRESSED);
}
SDL_PrivateJoystickButton(joystick, (Uint8) i, SDL_PRESSED);
} else {
if (joystick->buttons[i]) {
SDL_PrivateJoystickButton(joystick, (Uint8) i,
SDL_RELEASED);
}
SDL_PrivateJoystickButton(joystick, (Uint8) i, SDL_RELEASED);
}
}
}
Expand All @@ -373,9 +359,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
Uint8 pos;

pos = TranslatePOV(joyinfo.dwPOV);
if (pos != joystick->hats[0]) {
SDL_PrivateJoystickHat(joystick, 0, pos);
}
SDL_PrivateJoystickHat(joystick, 0, pos);
}
}

Expand Down
42 changes: 37 additions & 5 deletions test/controllermap.c
Expand Up @@ -197,6 +197,7 @@ StandardizeAxisValue(int nValue)
static void
SetCurrentBinding(int iBinding)
{
int iIndex;
SDL_GameControllerExtendedBind *pBinding;

if (iBinding < 0) {
Expand All @@ -213,11 +214,39 @@ SetCurrentBinding(int iBinding)
pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
SDL_zerop(pBinding);

SDL_memset(s_arrAxisState, 0, s_nNumAxes*sizeof(*s_arrAxisState));
for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
}

s_unPendingAdvanceTime = 0;
}

static SDL_bool
BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
{
if (pBindingA->bindType != pBindingB->bindType)
{
return SDL_FALSE;
}
switch (pBindingA->bindType)
{
case SDL_CONTROLLER_BINDTYPE_AXIS:
if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
return SDL_FALSE;
}
{
int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
return (minA <= minB && maxA >= maxB);
}
/* Not reached */
default:
return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
}
}

static void
ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
{
Expand All @@ -227,7 +256,8 @@ ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)

/* Do we already have this binding? */
for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
if (SDL_memcmp(pBinding, &s_arrBindings[iIndex], sizeof(*pBinding)) == 0) {
pCurrent = &s_arrBindings[iIndex];
if (BBindingContainsBinding(pCurrent, pBinding)) {
if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
/* Skip to the next binding */
SetCurrentBinding(s_iCurrentBinding + 1);
Expand Down Expand Up @@ -263,7 +293,9 @@ ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)

bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
if (bNativeAxis == bCurrentAxis) {
if (bNativeAxis == bCurrentAxis &&
(pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
/* We already have a binding of the type we want, ignore the new one */
return;
}
Expand Down Expand Up @@ -412,8 +444,8 @@ WatchJoystick(SDL_Joystick * joystick)
if (nCurrentDistance > nFarthestDistance) {
pAxisState->m_nFarthestValue = nValue;
}
if (nCurrentDistance <= 8000 && nFarthestDistance >= 20000) {
/* We've gone out and back, let's bind this axis */
if (nFarthestDistance >= 20000 && nCurrentDistance <= 10000) {
/* We've gone out far enough and started to come back, let's bind this axis */
SDL_GameControllerExtendedBind binding;
SDL_zero(binding);
binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
Expand Down

0 comments on commit 082132a

Please sign in to comment.