summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOzkan Sezer <sezeroz@gmail.com>2021-01-02 20:35:00 +0300
committerOzkan Sezer <sezeroz@gmail.com>2021-01-02 20:35:00 +0300
commit6dd3dd8ab009e164580906dbdfe65a5a860165f0 (patch)
treebb0bd0e9b92db7e7e7aabc66bd80a7360447ed8c
parent88f88fe5d2813ff1811d0b1f90ab3591ce11862e (diff)
downloadsdl-6dd3dd8ab009e164580906dbdfe65a5a860165f0.tar.gz
joystick/linux: remove deadzone and output axis values in correct range
fixes bug #5241 ( https://bugzilla.libsdl.org/show_bug.cgi?id=5241 ) -- patch from pj5085.
-rw-r--r--WhatsNew2
-rw-r--r--docs.html4
-rw-r--r--src/joystick/linux/SDL_sysjoystick.c43
3 files changed, 24 insertions, 25 deletions
diff --git a/WhatsNew b/WhatsNew
index 7335f5355..7ebf39e23 100644
--- a/WhatsNew
+++ b/WhatsNew
@@ -70,6 +70,8 @@ Changes include:
- Windows: Visual Studio project file updates.
- OS/2: fixes and Watcom makefile updates.
- Linux, joystick: fix invalid coef when axis min > axis max.
+- Linux, joystick: remove deadzone and output axis values in correct
+ range (bug 5241.)
- Linux, joystick: enhance detection of gamepads.
- Linux, joystick: allow for custom joystick names.
- FreeBSD, joystick: compile fixes.
diff --git a/docs.html b/docs.html
index 3a83580a2..b28238fda 100644
--- a/docs.html
+++ b/docs.html
@@ -169,6 +169,10 @@ Changes include:
Linux, joystick: fix invalid coef when axis&nbsp;min &gt; axis&nbsp;max.
</P>
<P>
+ Linux, joystick: remove deadzone and output axis values in correct
+ range (bug <a href="https://bugzilla.libsdl.org/show_bug.cgi?id=5241">5241</a>.)
+</P>
+<P>
Linux, joystick: enhance detection of gamepads.
</P>
<P>
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 7ee6833c7..18b5761c4 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -25,6 +25,7 @@
/* This is the system specific header for the SDL joystick API */
+#include <math.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
@@ -290,7 +291,8 @@ struct joystick_hwdata {
Uint8 abs_map[ABS_MAX];
struct axis_correct {
int used;
- int coef[3];
+ int minimum;
+ int maximum;
} abs_correct[ABS_MAX];
#endif
};
@@ -672,7 +674,7 @@ static SDL_bool JS_ConfigJoystick(SDL_Joystick *joystick, int fd)
static SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd)
{
- int i, t;
+ int i;
unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
unsigned long relbit[NBITS(REL_MAX)] = { 0 };
@@ -726,19 +728,8 @@ static SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd)
joystick->hwdata->abs_correct[i].used = 0;
} else {
joystick->hwdata->abs_correct[i].used = 1;
- 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;
- if (absinfo.maximum > absinfo.minimum)
- t = ((absinfo.maximum - absinfo.minimum) / 2 - 2 * absinfo.flat);
- else
- 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;
- }
+ joystick->hwdata->abs_correct[i].minimum = absinfo.minimum;
+ joystick->hwdata->abs_correct[i].maximum = absinfo.maximum;
}
++joystick->naxes;
}
@@ -1063,16 +1054,18 @@ static __inline__ int EV_AxisCorrect(SDL_Joystick *joystick, int which, int valu
correct = &joystick->hwdata->abs_correct[which];
if ( correct->used ) {
- 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;
+ const int original_value = value;
+ const float ideal_minimum = -32768;
+ const float ideal_maximum = +32767;
+ const float ideal_range = ideal_maximum - ideal_minimum;
+ const float actual_range = (correct->maximum - correct->minimum);
+ const float coefficient = ideal_range / actual_range;
+ const float new_float = (original_value - correct->minimum) * coefficient + ideal_minimum;
+ const int new_integer = round(new_float);
+ /*
+ if (which == 0) printf("Axis %d: old=%+6d new=%+6d\n", which, value, new_integer);
+ */
+ value = new_integer;
}
/* Clamp and return */