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

Commit

Permalink
Fix for dropped joystick events contributed by Simon <simon@mungewell…
Browse files Browse the repository at this point in the history
….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.
  • Loading branch information
slouken committed Feb 12, 2013
1 parent 7a31eb6 commit 620127d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/joystick/linux/SDL_sysjoystick.c
Expand Up @@ -763,6 +763,9 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
/* Get the number of buttons and axes on the joystick */
ConfigJoystick(joystick, fd);

// mark joystick as fresh and ready
joystick->hwdata->fresh = 1;

return (0);
}

Expand Down Expand Up @@ -833,13 +836,56 @@ AxisCorrect(SDL_Joystick * joystick, int which, int value)
return value;
}

static __inline__ void
PollAllValues(SDL_Joystick * joystick)
{
struct input_absinfo absinfo;
int a, b = 0;

// Poll all axis
for (a = ABS_X; b < ABS_MAX; a++) {
switch (a) {
case ABS_HAT0X:
case ABS_HAT0Y:
case ABS_HAT1X:
case ABS_HAT1Y:
case ABS_HAT2X:
case ABS_HAT2Y:
case ABS_HAT3X:
case ABS_HAT3Y:
// ingore hats
break;
default:
if (joystick->hwdata->abs_correct[b].used) {
if (ioctl(joystick->hwdata->fd, EVIOCGABS(a), &absinfo) >= 0) {
absinfo.value = AxisCorrect(joystick, b, absinfo.value);

#ifdef DEBUG_INPUT_EVENTS
printf("Joystick : Re-read Axis %d (%d) val= %d\n",
joystick->hwdata->abs_map[b], a, absinfo.value);
#endif
SDL_PrivateJoystickAxis(joystick,
joystick->hwdata->abs_map[b],
absinfo.value);
}
}
b++;
}
}
}

static __inline__ void
HandleInputEvents(SDL_Joystick * joystick)
{
struct input_event events[32];
int i, len;
int code;

if (joystick->hwdata->fresh) {
PollAllValues(joystick);
joystick->hwdata->fresh = 0;
}

while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
len /= sizeof(events[0]);
for (i = 0; i < len; ++i) {
Expand Down Expand Up @@ -890,6 +936,17 @@ HandleInputEvents(SDL_Joystick * joystick)
break;
}
break;
case EV_SYN:
switch (code) {
case SYN_DROPPED :
#ifdef DEBUG_INPUT_EVENTS
printf("Event SYN_DROPPED dectected\n");
#endif
PollAllValues(joystick);
break;
default:
break;
}
default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/joystick/linux/SDL_sysjoystick_c.h
Expand Up @@ -50,4 +50,8 @@ struct joystick_hwdata
int used;
int coef[3];
} abs_correct[ABS_MAX];

int fresh;
};

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 620127d

Please sign in to comment.