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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index c60b874..5485cb2 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -114,6 +114,34 @@ SDL_JoystickNameForIndex(int 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
* identify this joystick in future joystick events.
@@ -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 */
@@ -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
@@ -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++) {
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 250b703..09b7e58 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -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
diff --git a/src/joystick/bsd/SDL_sysjoystick.c b/src/joystick/bsd/SDL_sysjoystick.c
index 0ee5409..89e1d8f 100644
--- a/src/joystick/bsd/SDL_sysjoystick.c
+++ b/src/joystick/bsd/SDL_sysjoystick.c
@@ -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;
}
@@ -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,
@@ -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:
diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c
index 8f10b91..e8d04a6 100644
--- a/src/joystick/darwin/SDL_sysjoystick.c
+++ b/src/joystick/darwin/SDL_sysjoystick.c
@@ -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;
}
@@ -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;
}
@@ -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;
diff --git a/src/joystick/emscripten/SDL_sysjoystick.c b/src/joystick/emscripten/SDL_sysjoystick.c
index 485aa6c..9aae4d4 100644
--- a/src/joystick/emscripten/SDL_sysjoystick.c
+++ b/src/joystick/emscripten/SDL_sysjoystick.c
@@ -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);
diff --git a/src/joystick/haiku/SDL_haikujoystick.cc b/src/joystick/haiku/SDL_haikujoystick.cc
index 90ed9b3..07ee5a7 100644
--- a/src/joystick/haiku/SDL_haikujoystick.cc
+++ b/src/joystick/haiku/SDL_haikujoystick.cc
@@ -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;
@@ -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;
}
}
diff --git a/src/joystick/windows/SDL_mmjoystick.c b/src/joystick/windows/SDL_mmjoystick.c
index 6dcf6f9..c3148e8 100644
--- a/src/joystick/windows/SDL_mmjoystick.c
+++ b/src/joystick/windows/SDL_mmjoystick.c
@@ -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)
@@ -340,14 +338,8 @@ 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);
}
}
@@ -355,15 +347,9 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
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);
}
}
}
@@ -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);
}
}
diff --git a/test/controllermap.c b/test/controllermap.c
index df25af9..1cd0c0e 100644
--- a/test/controllermap.c
+++ b/test/controllermap.c
@@ -197,6 +197,7 @@ StandardizeAxisValue(int nValue)
static void
SetCurrentBinding(int iBinding)
{
+ int iIndex;
SDL_GameControllerExtendedBind *pBinding;
if (iBinding < 0) {
@@ -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)
{
@@ -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);
@@ -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;
}
@@ -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;