From b940cc08e5fa13f220d992ca619d54788874aa04 Mon Sep 17 00:00:00 2001
From: Sharaf Zaman <sharafzaz121@gmail.com>
Date: Tue, 10 Nov 2020 17:11:02 +0000
Subject: [PATCH] Android: Pass the button state through JNI

Mapping between MotionEvent#BUTTON_* and Qt's Qt::MouseButtons is
similar so explicity mapping seems unnecessary.
---
 .../jar/src/org/qtproject/qt5/android/QtNative.java  |  4 ++--
 src/plugins/platforms/android/androidjniinput.cpp    | 12 ++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java
index f4f8914d5a..6c39883eda 100644
--- a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java
+++ b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java
@@ -753,7 +753,7 @@ public class QtNative
                 break;
 
             case MotionEvent.ACTION_DOWN:
-                mouseDown(id, (int) event.getX(), (int) event.getY(), event.getMetaState());
+                mouseDown(id, (int) event.getX(), (int) event.getY(), event.getMetaState(), event.getButtonState());
                 m_oldx = (int) event.getX();
                 m_oldy = (int) event.getY();
                 break;
@@ -1259,7 +1259,7 @@ public class QtNative
     // screen methods
 
     // pointer methods
-    public static native void mouseDown(int winId, int x, int y, int modifier);
+    public static native void mouseDown(int winId, int x, int y, int modifier, int actionButton);
     public static native void mouseUp(int winId, int x, int y, int modifiers);
     public static native void mouseMove(int winId, int x, int y, int modifier);
     public static native void mouseWheel(int winId, int x, int y, float hdelta, float vdelta);
diff --git a/src/plugins/platforms/android/androidjniinput.cpp b/src/plugins/platforms/android/androidjniinput.cpp
index 0d2eaa6f8a..e287f4121f 100644
--- a/src/plugins/platforms/android/androidjniinput.cpp
+++ b/src/plugins/platforms/android/androidjniinput.cpp
@@ -68,6 +68,7 @@ namespace QtAndroidInput
     static QList<QWindowSystemInterface::TouchPoint> m_touchPoints;
 
     static QPointer<QWindow> m_mouseGrabber;
+    static int m_mouseActionButton;
 
     void updateSelection(int selStart, int selEnd, int candidatesStart, int candidatesEnd)
     {
@@ -134,7 +135,8 @@ namespace QtAndroidInput
                                                   anchor.x(), anchor.y(), rtl);
     }
 
-    static void mouseDown(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/, jint x, jint y, jint modifier)
+    static void mouseDown(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/,
+                          jint x, jint y, jint modifier, jint actionButton)
     {
         if (m_ignoreMouseEvents)
             return;
@@ -147,10 +149,12 @@ namespace QtAndroidInput
             localPos = platformWindow ? platformWindow->mapFromGlobal(globalPos) : globalPos;
         }
         m_mouseGrabber = tlw;
+        // NOTE: mapping between MotionEvent's BUTTON states and Qt seem consistent
+        m_mouseActionButton = actionButton;
         QWindowSystemInterface::handleMouseEvent(tlw,
                                                  localPos,
                                                  globalPos,
-                                                 Qt::MouseButtons(Qt::LeftButton),
+                                                 Qt::MouseButton(m_mouseActionButton),
                                                  mapAndroidModifiers(modifier));
     }
 
@@ -190,7 +194,7 @@ namespace QtAndroidInput
         QWindowSystemInterface::handleMouseEvent(tlw,
                                                  localPos,
                                                  globalPos,
-                                                 Qt::MouseButtons(m_mouseGrabber ? Qt::LeftButton : Qt::NoButton),
+                                                 Qt::MouseButtons(m_mouseGrabber ? m_mouseActionButton : Qt::NoButton),
                                                  mapAndroidModifiers(modifier));
     }
 
@@ -886,7 +890,7 @@ namespace QtAndroidInput
         {"touchBegin","(I)V",(void*)touchBegin},
         {"touchAdd","(IIIZIIFFFF)V",(void*)touchAdd},
         {"touchEnd","(II)V",(void*)touchEnd},
-        {"mouseDown", "(IIII)V", (void *)mouseDown},
+        {"mouseDown", "(IIIII)V", (void *)mouseDown},
         {"mouseUp", "(IIII)V", (void *)mouseUp},
         {"mouseMove", "(IIII)V", (void *)mouseMove},
         {"mouseWheel", "(IIIFF)V", (void *)mouseWheel},
-- 
2.28.0

