SDL: Fixed accidentally deadlocking the rumble thread with combined Joy-Cons

From 483a010f0c598fc917c785f8f727aa830e8351c4 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 9 Aug 2022 15:39:39 -0700
Subject: [PATCH] Fixed accidentally deadlocking the rumble thread with
 combined Joy-Cons

Also added more accurate check for sensor data. At least one axis will always have acceleration because of gravity.
---
 src/joystick/hidapi/SDL_hidapi_switch.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 373d20e439a..08d3385a621 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -1984,8 +1984,9 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
     }
 
     if (ctx->m_bReportSensors) {
-        SDL_bool bHasSensorData = (packet->imuState[0].sAccelX != 0 ||
-                                   packet->imuState[0].sGyroX != 0);
+        SDL_bool bHasSensorData = (packet->imuState[0].sAccelZ != 0 ||
+                                   packet->imuState[0].sAccelY != 0 ||
+                                   packet->imuState[0].sAccelX != 0);
         if (bHasSensorData) {
             ctx->m_bHasSensorData = SDL_TRUE;
 
@@ -1999,9 +2000,17 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
 
         } else if (ctx->m_bHasSensorData) {
             /* Uh oh, someone turned off the IMU? */
-            SDL_UnlockMutex(ctx->device->dev_lock);
+            SDL_HIDAPI_Device *device = ctx->device;
+
+            if (device->updating) {
+                SDL_UnlockMutex(device->dev_lock);
+            }
+
             SetIMUEnabled(ctx, SDL_TRUE);
-            SDL_LockMutex(ctx->device->dev_lock);
+
+            if (device->updating) {
+                SDL_LockMutex(device->dev_lock);
+            }
 
         } else {
             /* We have never gotten IMU data, probably not supported on this device */