joystick: Fix underflow with 0 delta timestamp

Some sensors will occasionally report two identical timestamps in a row.
This leads to the timestamp wrapping calculation to underflow, subtracting
0x80000000 from the timestamp whenever it happens. By adjusting the wrap
test, we can just directly add zero to the timestamp, fixing the underflow.
This commit is contained in:
Vicki Pfau 2026-05-29 20:42:23 -07:00 committed by Sam Lantinga
parent 9c2f143ca3
commit 687a59f277
5 changed files with 5 additions and 5 deletions

View file

@ -595,7 +595,7 @@ static void HIDAPI_Driver8BitDo_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
Uint32 tick = LOAD32(data[27], data[28], data[29], data[30]);
if (ctx->last_tick) {
if (ctx->last_tick < tick) {
if (ctx->last_tick <= tick) {
delta = (tick - ctx->last_tick);
} else {
delta = (SDL_MAX_UINT32 - ctx->last_tick + tick + 1);

View file

@ -1150,7 +1150,7 @@ static void HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_d
float data[3];
tick = LOAD16(packet->rgucTimestamp[0], packet->rgucTimestamp[1]);
if (ctx->last_tick < tick) {
if (ctx->last_tick <= tick) {
delta = (tick - ctx->last_tick);
} else {
delta = (SDL_MAX_UINT16 - ctx->last_tick + tick + 1);

View file

@ -1364,7 +1364,7 @@ static void HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL
Uint32 delta;
Uint16 tick = LOAD16(packet->rgucSensorTimestamp[0],
packet->rgucSensorTimestamp[1]);
if (ctx->last_tick < tick) {
if (ctx->last_tick <= tick) {
delta = (tick - ctx->last_tick);
} else {
delta = (SDL_MAX_UINT16 - ctx->last_tick + tick + 1);

View file

@ -309,7 +309,7 @@ static void HIDAPI_DriverSteamHori_HandleStatePacket(SDL_Joystick *joystick, SDL
Uint32 delta;
Uint16 tick = LOAD16(data[10],
data[11]);
if (ctx->last_tick < tick) {
if (ctx->last_tick <= tick) {
delta = (tick - ctx->last_tick);
} else {
delta = (SDL_MAX_UINT16 - ctx->last_tick + tick + 1);

View file

@ -2086,7 +2086,7 @@ static void HandleInputEvents(SDL_Joystick *joystick)
if (code == MSC_TIMESTAMP) {
Sint32 tick = event->value;
Sint32 delta;
if (joystick->hwdata->last_tick < tick) {
if (joystick->hwdata->last_tick <= tick) {
delta = (tick - joystick->hwdata->last_tick);
} else {
delta = (SDL_MAX_SINT32 - joystick->hwdata->last_tick + tick + 1);