SDL: Improve Android mouse event handling, especially relative mode.

From d35636834af4cad1391cc385b44d920f08314395 Mon Sep 17 00:00:00 2001
From: Rachel Blackman <[EMAIL REDACTED]>
Date: Mon, 19 Oct 2026 08:05:39 -0700
Subject: [PATCH] Improve Android mouse event handling, especially relative
 mode.

---
 .../main/java/org/libsdl/app/SDLActivity.java |  7 --
 .../org/libsdl/app/SDLControllerManager.java  | 41 ++++++--
 .../main/java/org/libsdl/app/SDLSurface.java  |  7 +-
 src/video/android/SDL_androidmouse.c          | 95 +++++++++++++------
 4 files changed, 106 insertions(+), 44 deletions(-)

diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index 64bf2471541cd..1e5065c563fa0 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -1633,13 +1633,6 @@ public static boolean handleKeyEvent(View v, int keyCode, KeyEvent event, InputC
         int source = event.getSource();
         InputDevice device = InputDevice.getDevice(deviceId);
 
-        if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) != 0) {
-            // If this is a fallback event -- e.g., the mouse is being turned into dpad navigational keys 
-            // just eat it, since that's almost never what we actually want to have happen. This will 
-            // happen with Logitech mice on Amazon Fire TV devices and some Samsung devices.
-            return true; 
-        }
-
         if (source == InputDevice.SOURCE_UNKNOWN) {
             if (device != null) {
                 source = device.getSources();
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 4779adfeb3c91..4051af0d3e15e 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
@@ -810,7 +810,7 @@ synchronized protected SDLHaptic getHaptic(int device_id) {
     }
 }
 
-class SDLGenericMotionListener_API14 implements View.OnGenericMotionListener {
+class SDLGenericMotionListener_API14 implements View.OnGenericMotionListener, View.OnCapturedPointerListener {
     protected static final int SDL_PEN_DEVICE_TYPE_UNKNOWN = 0;
     protected static final int SDL_PEN_DEVICE_TYPE_DIRECT = 1;
     protected static final int SDL_PEN_DEVICE_TYPE_INDIRECT = 2;
@@ -831,6 +831,13 @@ public boolean onGenericMotion(View v, MotionEvent event) {
 
             if (toolType == MotionEvent.TOOL_TYPE_MOUSE) {
                 int buttonState = event.getButtonState();
+                boolean isRelative = checkRelativeEvent(event);
+
+                if (Build.VERSION.SDK_INT >= 26 /* Android 8.0 (O) */ && isRelative) {
+                    // We'll be using the onCapturedPointer event handler instead, so
+                    // just bounce.
+                    return false;
+                }
 
                 switch (action) {
                     case MotionEvent.ACTION_SCROLL:
@@ -844,17 +851,14 @@ public boolean onGenericMotion(View v, MotionEvent event) {
                     case MotionEvent.ACTION_MOVE:           // Relative mode mouse movement
                     case MotionEvent.ACTION_BUTTON_PRESS:
                     case MotionEvent.ACTION_BUTTON_RELEASE:
-                        boolean isRelative = checkRelativeEvent(event);
+                    case MotionEvent.ACTION_CANCEL:         // Mouse drag ended (e.g. button released)
+                    case MotionEvent.ACTION_HOVER_ENTER:
+                    case MotionEvent.ACTION_HOVER_EXIT:
                         x = getEventX(event, i);
                         y = getEventY(event, i);
 
-                        if (isRelative || (action != MotionEvent.ACTION_BUTTON_PRESS && action != MotionEvent.ACTION_BUTTON_RELEASE)) {
-                            // In absolute mode, we get touch events alongside the mouse clicks, and handle those elsewhere. 
-                            // In relative mode, we ONLY get the ACTION_BUTTON_PRESS/ACTION_BUTTON_RELEASE, so if we don't consume them they
-                            // will be turned into key events for Enter or Select or Back or something similar.
-                            SDLActivity.onNativeMouse(buttonState, action, x, y, isRelative);
-                            consumed = true;                            
-                        }
+                        SDLActivity.onNativeMouse(buttonState, action, x, y, isRelative);
+                        consumed = true;                            
                         break;
 
                     default:
@@ -921,6 +925,12 @@ float getEventY(MotionEvent event, int pointerIndex) {
     int getPenDeviceType(InputDevice penDevice) {
         return SDL_PEN_DEVICE_TYPE_UNKNOWN;
     }
+
+    @Override
+    public boolean onCapturedPointer(View view, MotionEvent event) {
+        // We can't use captured pointer listener until API 26 or later.
+        return false;
+    }
 }
 
 class SDLGenericMotionListener_API24 extends SDLGenericMotionListener_API14 {
@@ -987,6 +997,19 @@ boolean inRelativeMode() {
         return mRelativeModeEnabled;
     }
 
+    @Override
+    public boolean onCapturedPointer(View view, MotionEvent event) {
+
+        // Captured Pointer events are *always* relative
+        float x = getEventX(event, i);
+        float y = getEventY(event, i);
+        int buttonState = event.getButtonState();
+        int action = event.getActionMasked();
+
+        SDLActivity.onNativeMouse(buttonState, action, x, y, true);
+        return true;
+    }
+
     @Override
     boolean setRelativeMouseEnabled(boolean enabled) {
 
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
index 6841ed10aaacf..3e1332a0328d3 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
@@ -75,6 +75,10 @@ protected SDLSurface(Context context) {
         mHeight = 1.0f;
 
         mIsSurfaceReady = false;
+        if (Build.VERSION.SDK_INT >= 26) {
+            // We can use CapturedPointerListener and avoid many shenanigans and problems
+            setOnCapturedPointerListener(SDLActivity.getMotionListener());
+        }
     }
 
     protected void handlePause() {
@@ -281,7 +285,8 @@ public boolean onTouch(View v, MotionEvent event) {
                 y = motionListener.getEventY(event, i);
                 relative = motionListener.inRelativeMode();
 
-                SDLActivity.onNativeMouse(buttonState, action, x, y, relative);
+                SDLActivity.onNativeMouse(buttonState, action, x, y, relative);                 
+
             } else if (toolType == MotionEvent.TOOL_TYPE_STYLUS || toolType == MotionEvent.TOOL_TYPE_ERASER) {
                 pointerId = event.getPointerId(i);
                 x = event.getX(i);
diff --git a/src/video/android/SDL_androidmouse.c b/src/video/android/SDL_androidmouse.c
index 73898979d8592..da6b242b10ed8 100644
--- a/src/video/android/SDL_androidmouse.c
+++ b/src/video/android/SDL_androidmouse.c
@@ -30,16 +30,22 @@
 #include "../../core/android/SDL_android.h"
 
 // See Android's MotionEvent class for constants
-#define ACTION_DOWN       0
-#define ACTION_UP         1
-#define ACTION_MOVE       2
-#define ACTION_HOVER_MOVE 7
-#define ACTION_SCROLL     8
-#define BUTTON_PRIMARY    1
-#define BUTTON_SECONDARY  2
-#define BUTTON_TERTIARY   4
-#define BUTTON_BACK       8
-#define BUTTON_FORWARD    16
+#define ACTION_DOWN           0
+#define ACTION_UP             1
+#define ACTION_MOVE           2
+#define ACTION_CANCEL         3
+#define ACTION_HOVER_MOVE     7
+#define ACTION_SCROLL         8
+#define ACTION_HOVER_ENTER    9
+#define ACTION_HOVER_EXIT     10
+#define ACTION_BUTTON_PRESS   11
+#define ACTION_BUTTON_RELEASE 12
+
+#define BUTTON_PRIMARY        1
+#define BUTTON_SECONDARY      2
+#define BUTTON_TERTIARY       4
+#define BUTTON_BACK           8
+#define BUTTON_FORWARD        16
 
 struct SDL_CursorData
 {
@@ -47,6 +53,8 @@ struct SDL_CursorData
     int system_cursor;
 };
 
+static const char k_aAndroidButtons[] = { BUTTON_PRIMARY, BUTTON_SECONDARY, BUTTON_TERTIARY, BUTTON_BACK, BUTTON_FORWARD };
+
 // Last known Android mouse button state (includes all buttons)
 static int last_state;
 
@@ -208,7 +216,8 @@ static Uint8 TranslateButton(int state)
 
 void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y, bool relative)
 {
-    int changes;
+    int changes_press;
+    int changes_release;
     Uint8 button;
 
     if (!window) {
@@ -216,22 +225,6 @@ void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y
     }
 
     switch (action) {
-    case ACTION_DOWN:
-        changes = state & ~last_state;
-        button = TranslateButton(changes);
-        last_state = state;
-        SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
-        SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, true);
-        break;
-
-    case ACTION_UP:
-        changes = last_state & ~state;
-        button = TranslateButton(changes);
-        last_state = state;
-        SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
-        SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, false);
-        break;
-
     case ACTION_MOVE:
     case ACTION_HOVER_MOVE:
         SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
@@ -241,6 +234,54 @@ void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y
         SDL_SendMouseWheel(0, window, SDL_DEFAULT_MOUSE_ID, x, y, SDL_MOUSEWHEEL_NORMAL);
         break;
 
+    case ACTION_UP:
+    case ACTION_DOWN:
+    case ACTION_BUTTON_PRESS:
+    case ACTION_BUTTON_RELEASE:
+    case ACTION_HOVER_ENTER:
+    case ACTION_HOVER_EXIT:
+        // Some Android versions will send mouse buttons as ACTION_BUTTON_PRESS and 
+        // ACTION_BUTTON_RELEASE. Some will use ACTION_DOWN and ACTION_UP.
+        //
+        // Some will send ACTION_HOVER_EXIT when a button is pressed (to go into 
+        // drag/move mode) and ACTION_HOVER_ENTER when the button is released (to 
+        // go back into hover mode).
+        //
+        // Some will, in fact, cluster a BUTTON_PRIMARY release and BUTTON_SECONDARY 
+        // press into the same message, simply changing the button state.
+        //
+        // To ensure we work correctly on as many Android variants as possible, we
+        // take the button state on any of these and generate press/release events
+        // ourselves as needed when the button state changes.
+
+        changes_press = state & ~last_state;
+        changes_release = last_state & ~state;
+
+        SDL_SendMouseMotion(0, window, SDL_DEFAULT_MOUSE_ID, relative, x, y);
+
+        // Some versions of Android will send multiple mouse buttons in a single
+        // event, so read it bit-by-bit.
+        if (changes_press) {
+            for (int i = 0; i < sizeof(k_aAndroidButtons); i++) {
+                if (changes_press & k_aAndroidButtons[i]) {
+                    button = TranslateButton(k_aAndroidButtons[i]);
+                    SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, true);
+                }
+            }
+            last_state = state;
+        }
+
+        if (changes_release) {
+            for (int i = 0; i < sizeof(k_aAndroidButtons); i++) {
+                if (changes_release & k_aAndroidButtons[i]) {
+                    button = TranslateButton(k_aAndroidButtons[i]);
+                    SDL_SendMouseButton(0, window, SDL_DEFAULT_MOUSE_ID, button, false);
+                }
+            }
+            last_state = state;
+        }
+        break;        
+
     default:
         break;
     }