SDL: Fixed sensor timestamp units for third-party PS5 controllers

From a72dfa6a5fe8d8901175db852de8686b83487522 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 18 Sep 2023 12:55:43 -0700
Subject: [PATCH] Fixed sensor timestamp units for third-party PS5 controllers

---
 src/joystick/hidapi/SDL_hidapi_ps5.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
index 9d1e98a16604..81b77e1d8fc3 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps5.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -725,7 +725,7 @@ static void HIDAPI_DriverPS5_CheckPendingLEDReset(SDL_DriverPS5_Context *ctx)
 {
     SDL_bool led_reset_complete = SDL_FALSE;
 
-    if (ctx->enhanced_reports && ctx->sensors_supported) {
+    if (ctx->enhanced_reports && ctx->sensors_supported && !ctx->use_alternate_report) {
         const PS5StatePacketCommon_t *packet = &ctx->last_state.state;
 
         /* Check the timer to make sure the Bluetooth connection LED animation is complete */
@@ -1310,12 +1310,12 @@ static void HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL
     SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
 
     if (ctx->report_sensors) {
-        Uint32 delta;
         Uint64 sensor_timestamp;
         float data[3];
 
         if (ctx->use_alternate_report) {
             /* 16-bit timestamp */
+            Uint16 delta;
             Uint16 tick = LOAD16(packet->rgucSensorTimestamp[0],
                                  packet->rgucSensorTimestamp[1]);
             if (ctx->last_tick < tick) {
@@ -1324,8 +1324,13 @@ static void HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL
                 delta = (SDL_MAX_UINT16 - ctx->last_tick + tick + 1);
             }
             ctx->last_tick = tick;
+            ctx->sensor_ticks += delta;
+
+            /* Sensor timestamp is in 1us units */
+            sensor_timestamp = SDL_US_TO_NS(ctx->sensor_ticks);
         } else {
             /* 32-bit timestamp */
+            Uint32 delta;
             Uint32 tick = LOAD32(packet->rgucSensorTimestamp[0],
                                  packet->rgucSensorTimestamp[1],
                                  packet->rgucSensorTimestamp[2],
@@ -1336,11 +1341,11 @@ static void HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL
                 delta = (SDL_MAX_UINT32 - ctx->last_tick + tick + 1);
             }
             ctx->last_tick = tick;
-        }
-        ctx->sensor_ticks += delta;
+            ctx->sensor_ticks += delta;
 
-        /* Sensor timestamp is in 0.33us units */
-        sensor_timestamp = (ctx->sensor_ticks * SDL_NS_PER_US) / 3;
+            /* Sensor timestamp is in 0.33us units */
+            sensor_timestamp = (ctx->sensor_ticks * SDL_NS_PER_US) / 3;
+        }
 
         data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 0, LOAD16(packet->rgucGyroX[0], packet->rgucGyroX[1]));
         data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 1, LOAD16(packet->rgucGyroY[0], packet->rgucGyroY[1]));