SDL: Fixed DualSense controllers not getting sensors when hotplugged on Android

From 4a30f31c045b0e5085b651767f9598ad4c9b2442 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 23 Oct 2026 21:38:42 -0700
Subject: [PATCH] Fixed DualSense controllers not getting sensors when
 hotplugged on Android

DualSense controllers come in as a collection of devices which the Android runtime stitches together and when the main controller arrives the sensors aren't available yet. This waits until device changes have settled for 50 ms before notifying SDL that a controller is available.

This also works around a potential deadlock condition in the Android runtime where sensors that are registered while device updates are happening can run into an internal ABBA deadlock.
---
 .../org/libsdl/app/SDLControllerManager.java  | 94 +++++++++++++++----
 1 file changed, 74 insertions(+), 20 deletions(-)

diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
index 55d39399e7791..db626a55f5bad 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
@@ -18,6 +18,9 @@
 import android.hardware.SensorManager;
 import android.graphics.Color;
 import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.SystemClock;
 import android.os.VibrationEffect;
 import android.os.Vibrator;
 import android.os.VibratorManager;
@@ -188,17 +191,13 @@ class SDLDeviceListener implements InputDeviceListener
 {
     @Override
     public void onInputDeviceAdded(int deviceId) {
-        if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
-            SDLControllerManager.mJoystickHandler.deviceAdded(deviceId);
-        }
+        SDLControllerManager.mJoystickHandler.deviceChanged(deviceId);
     }
 
     @Override
     public void onInputDeviceChanged(int deviceId) {
         // The controller might not have had joystick sources previously
-        if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
-            SDLControllerManager.mJoystickHandler.deviceAdded(deviceId);
-        }
+        SDLControllerManager.mJoystickHandler.deviceChanged(deviceId);
     }
 
     @Override
@@ -224,6 +223,8 @@ static class SDLJoystick {
         SDLJoySensorListener sensorListener;
         Sensor accelerometerSensor;
         Sensor gyroscopeSensor;
+        volatile boolean sensorsRequested;
+        boolean sensorsEnabled;
     }
     static class RangeComparator implements Comparator<InputDevice.MotionRange> {
         @Override
@@ -274,9 +275,45 @@ public int compare(InputDevice.MotionRange arg0, InputDevice.MotionRange arg1) {
 
     private final ArrayList<SDLJoystick> mJoysticks;
 
+    // Android builds controllers from several kernel devices, e.g. adding the motion sensors after the
+    // gamepad, so we wait for input devices to settle before adding a joystick. Android can also
+    // deadlock if a sensor listener is registered while an input device change is being processed,
+    // so we wait for input devices to settle before changing sensor registration.
+    private static final long INPUT_DEVICE_SETTLE_TIME_MS = 50;
+    private volatile long mLastInputDeviceChangeTime;
+    private final Handler mHandler;
+
     SDLJoystickHandler() {
 
         mJoysticks = new ArrayList<SDLJoystick>();
+        mHandler = new Handler(Looper.getMainLooper());
+    }
+
+    private void postWhenInputDevicesSettle(final Runnable action) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                long delay = mLastInputDeviceChangeTime + INPUT_DEVICE_SETTLE_TIME_MS - SystemClock.uptimeMillis();
+                if (delay > 0) {
+                    // Input devices are still changing, try again once they settle
+                    mHandler.postDelayed(this, delay);
+                    return;
+                }
+                action.run();
+            }
+        });
+    }
+
+    void deviceChanged(final int device_id) {
+        mLastInputDeviceChangeTime = SystemClock.uptimeMillis();
+        postWhenInputDevicesSettle(new Runnable() {
+            @Override
+            public void run() {
+                if (SDLControllerManager.isDeviceSDLJoystick(device_id)) {
+                    deviceAdded(device_id);
+                }
+            }
+        });
     }
 
     /**
@@ -371,6 +408,7 @@ void deviceAdded(int device_id) {
     }
 
     void deviceRemoved(int device_id) {
+        mLastInputDeviceChangeTime = SystemClock.uptimeMillis();
         for (int i = 0; i < mJoysticks.size(); i++) {
             if (mJoysticks.get(i).device_id == device_id) {
 
@@ -602,21 +640,37 @@ void setSensorsEnabled(int device_id, boolean enabled) {
         if (joystick == null || joystick.sensorManager == null) {
             return;
         }
-        if (enabled) {
-            if (joystick.accelerometerSensor != null) {
-                SDLSensorManager.registerListener(joystick.sensorManager, joystick.sensorListener, joystick.accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
-            }
-            if (joystick.gyroscopeSensor != null) {
-                SDLSensorManager.registerListener(joystick.sensorManager, joystick.sensorListener, joystick.gyroscopeSensor, SensorManager.SENSOR_DELAY_GAME);
-            }
-        } else {
-            if (joystick.accelerometerSensor != null) {
-                SDLSensorManager.unregisterListener(joystick.sensorManager, joystick.sensorListener, joystick.accelerometerSensor);
-            }
-            if (joystick.gyroscopeSensor != null) {
-                SDLSensorManager.unregisterListener(joystick.sensorManager, joystick.sensorListener, joystick.gyroscopeSensor);
+        joystick.sensorsRequested = enabled;
+        updateSensors(device_id);
+    }
+
+    private void updateSensors(final int device_id) {
+        postWhenInputDevicesSettle(new Runnable() {
+            @Override
+            public void run() {
+                SDLJoystick joystick = getJoystick(device_id);
+                if (joystick == null || joystick.sensorsEnabled == joystick.sensorsRequested) {
+                    return;
+                }
+                joystick.sensorsEnabled = joystick.sensorsRequested;
+
+                if (joystick.sensorsEnabled) {
+                    if (joystick.accelerometerSensor != null) {
+                        SDLSensorManager.registerListener(joystick.sensorManager, joystick.sensorListener, joystick.accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
+                    }
+                    if (joystick.gyroscopeSensor != null) {
+                        SDLSensorManager.registerListener(joystick.sensorManager, joystick.sensorListener, joystick.gyroscopeSensor, SensorManager.SENSOR_DELAY_GAME);
+                    }
+                } else {
+                    if (joystick.accelerometerSensor != null) {
+                        SDLSensorManager.unregisterListener(joystick.sensorManager, joystick.sensorListener, joystick.accelerometerSensor);
+                    }
+                    if (joystick.gyroscopeSensor != null) {
+                        SDLSensorManager.unregisterListener(joystick.sensorManager, joystick.sensorListener, joystick.gyroscopeSensor);
+                    }
+                }
             }
-        }
+        });
     }
 }