SDL/src
Sam Lantinga db0a2025c3 Fixed bug 5241 - SDL on Linux needs a way to turn deadzones off
pj5085

It occurred to me that my simple patch that comments out a few lines of code does not correctly remove the dead zone since the calculation presumably assumes the dead zone has been cut out of the range.  Then, while looking into how to make it output the correct range of values, I realized SDL wasn't returning the correct range of values to begin with.

This line of code was already present:

printf("Values = { %d, %d, %d, %d, %d }\n", absinfo.value, absinfo.minimum, absinfo.maximum, absinfo.fuzz, absinfo.flat);

For my joystick this yeilds:

Values = { 0, -127, 127, 0, 15 }

Then this code calculates the coefficients:

In SDL1:
joystick->hwdata->abs_correct[i].coef[0] = (absinfo.maximum + absinfo.minimum) / 2 - absinfo.flat;
joystick->hwdata->abs_correct[i].coef[1] = (absinfo.maximum + absinfo.minimum) / 2 + absinfo.flat;
t = ((absinfo.maximum - absinfo.minimum) / 2 - 2 * absinfo.flat);
if ( t != 0 ) {
  joystick->hwdata->abs_correct[i].coef[2] = (1 << 29) / t;
} else {
  joystick->hwdata->abs_correct[i].coef[2] = 0;
}

In SDL2:
joystick->hwdata->abs_correct[i].coef[0] = (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
joystick->hwdata->abs_correct[i].coef[1] = (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
if (t != 0) {
  joystick->hwdata->abs_correct[i].coef[2] = (1 << 28) / t;
} else {
  joystick->hwdata->abs_correct[i].coef[2] = 0;
}

Neither calculates the correct coefficients for the code in the AxisCorrect function.

In SDL1:
if ( value > correct->coef[0] ) {
  if ( value < correct->coef[1] ) {
    return 0;
  }
  value -= correct->coef[1];
} else {
  value -= correct->coef[0];
}
value *= correct->coef[2];
value >>= 14;

In SDL2:
value *= 2;
if (value > correct->coef[0]) {
  if (value < correct->coef[1]) {
    return 0;
  }
  value -= correct->coef[1];
} else {
  value -= correct->coef[0];
}

In SDL1, the calculated coefficients are coef[0]=15, coef[1]=-15 and coef[2]=5534751.  So with a full-scale input of 127, it calculates an output value of 37835, which is considerably out of range.

In SDL2, the calculated coefficients are coef[0]=30, coef[1]=-30, and coef[2]=1383687.  So with a full-scale input of 127, it calculates the same output value of 37835.

I tested it with the 3 joysticks I have, and it produces out-of-range values for all of them.

Anyway, since dead zones are garbage, I just deleted all of that junk and wrote some code that takes the absinfo.minimum and absinfo.maximum values and uses them to scale the axis range to -32767 through +32767.

I also made it detect when a range doesn't have an integer center point, e.g. the center of -128 to + 127 is -0.5.  In such cases, if either value to the side of the center is provided, it zeros it, but it otherwise doesn't implement any kind of dead zone.  This seemed important with my gamepad which provides only the values of 0, 127, and 255, since without this hack it would never be centered.

Also, the previous minimum output value was -32768, but as that creates an output range that has no true center, I changed the minimum value to -32767.

I tested it with the 3 joystick devices I have and it seems to create correct values for all of them.
2020-12-12 23:48:02 -08:00
..
atomic Add basic support for compiling on RISC OS 2020-02-13 20:50:47 +00:00
audio Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
core Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
cpuinfo Add Zhaoxin processor support 2020-11-09 20:36:35 -08:00
dynapi Added SDL_wcscasecmp() and SDL_wcsncasecmp() 2020-11-24 12:43:01 -08:00
events Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
file Fixed compile warning 2020-02-10 13:59:05 -08:00
filesystem Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
haptic Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
hidapi Fixed bug 5222 - Crash when running with -DHIDAPI=ON 2020-12-08 19:03:50 -08:00
joystick Fixed bug 5241 - SDL on Linux needs a way to turn deadzones off 2020-12-12 23:48:02 -08:00
libm k_rem_pio2.c: fix typo in second memset(): iq, not q. fixes bug #5309. 2020-10-08 11:51:02 +03:00
loadso os2: a _lot_ of coding style cleanup, sot that they match the SDL style. 2020-10-15 21:37:30 +03:00
locale Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
main Updated SDL to 2.0.14 in preparation for release candidate 2020-12-08 18:56:06 -08:00
misc riscos: Implement SDL_OpenURL() 2020-10-11 17:32:32 +01:00
power Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
render Fixed bug 5213 - Add support to metal in iOS/tvOS simulator 2020-12-09 07:23:47 -08:00
sensor Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
stdlib Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
test fix watcom build of SDL_test_common.c 2020-06-14 12:05:56 +03:00
thread Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
timer Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
video remove a few stale NULL message/title checks after commit e2b729b1756a 2020-12-10 12:24:24 +03:00
SDL.c os2: integrate the port into main tree. 2020-10-14 23:01:06 +03:00
SDL_assert.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
SDL_assert_c.h Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
SDL_dataqueue.c Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
SDL_dataqueue.h Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
SDL_error.c SDL_error: simplified error string management. 2020-04-21 01:30:36 -04:00
SDL_error_c.h SDL_error: simplified error string management. 2020-04-21 01:30:36 -04:00
SDL_hints.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
SDL_hints_c.h Updated copyright date for 2020 2020-01-16 20:49:25 -08:00
SDL_internal.h Fixed bug 5235 - All internal sources should include SDL_assert.h 2020-12-09 07:16:22 -08:00
SDL_log.c Updated copyright date for 2020 2020-01-16 20:49:25 -08:00