Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit 620127d

Browse files
committed
Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
In my system SDL2 is dropping a chunk of joystick events, which result in a 'stuck brake/accelerator' whilst playing a racing simulator. This basically means SDL2 is unsuitable for use at this point... The patch below detects this situation and forces a re-read of all attached joystick axis - thus resync to the correct/current pedal positions.
1 parent 7a31eb6 commit 620127d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/joystick/linux/SDL_sysjoystick.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,9 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
763763
/* Get the number of buttons and axes on the joystick */
764764
ConfigJoystick(joystick, fd);
765765

766+
// mark joystick as fresh and ready
767+
joystick->hwdata->fresh = 1;
768+
766769
return (0);
767770
}
768771

@@ -833,13 +836,56 @@ AxisCorrect(SDL_Joystick * joystick, int which, int value)
833836
return value;
834837
}
835838

839+
static __inline__ void
840+
PollAllValues(SDL_Joystick * joystick)
841+
{
842+
struct input_absinfo absinfo;
843+
int a, b = 0;
844+
845+
// Poll all axis
846+
for (a = ABS_X; b < ABS_MAX; a++) {
847+
switch (a) {
848+
case ABS_HAT0X:
849+
case ABS_HAT0Y:
850+
case ABS_HAT1X:
851+
case ABS_HAT1Y:
852+
case ABS_HAT2X:
853+
case ABS_HAT2Y:
854+
case ABS_HAT3X:
855+
case ABS_HAT3Y:
856+
// ingore hats
857+
break;
858+
default:
859+
if (joystick->hwdata->abs_correct[b].used) {
860+
if (ioctl(joystick->hwdata->fd, EVIOCGABS(a), &absinfo) >= 0) {
861+
absinfo.value = AxisCorrect(joystick, b, absinfo.value);
862+
863+
#ifdef DEBUG_INPUT_EVENTS
864+
printf("Joystick : Re-read Axis %d (%d) val= %d\n",
865+
joystick->hwdata->abs_map[b], a, absinfo.value);
866+
#endif
867+
SDL_PrivateJoystickAxis(joystick,
868+
joystick->hwdata->abs_map[b],
869+
absinfo.value);
870+
}
871+
}
872+
b++;
873+
}
874+
}
875+
}
876+
836877
static __inline__ void
837878
HandleInputEvents(SDL_Joystick * joystick)
838879
{
839880
struct input_event events[32];
840881
int i, len;
841882
int code;
842883

884+
if (joystick->hwdata->fresh) {
885+
PollAllValues(joystick);
886+
joystick->hwdata->fresh = 0;
887+
}
888+
843889
while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
844890
len /= sizeof(events[0]);
845891
for (i = 0; i < len; ++i) {
@@ -890,6 +936,17 @@ HandleInputEvents(SDL_Joystick * joystick)
890936
break;
891937
}
892938
break;
939+
case EV_SYN:
940+
switch (code) {
941+
case SYN_DROPPED :
942+
#ifdef DEBUG_INPUT_EVENTS
943+
printf("Event SYN_DROPPED dectected\n");
944+
#endif
945+
PollAllValues(joystick);
946+
break;
947+
default:
948+
break;
949+
}
893950
default:
894951
break;
895952
}

src/joystick/linux/SDL_sysjoystick_c.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ struct joystick_hwdata
5050
int used;
5151
int coef[3];
5252
} abs_correct[ABS_MAX];
53+
54+
int fresh;
5355
};
56+
57+
/* vi: set ts=4 sw=4 expandtab: */

0 commit comments

Comments
 (0)