[RK3399][Android7.1.1]Android->inputflinger:将鼠标操作模拟成触摸操作,修复部分应用不支持鼠标操作的问题

测试平台

Platform: RK3399
OS: Android 7.1.1

需求

将鼠标操作模拟成触摸操作

原因

此需求的原因主要有两个

  1. 部分应用只支持触摸操作,不支持鼠标点击操作:如kodi和王者荣耀
  2. 系统Settings语言设置:切换语言的时候鼠标点击所选项目时错乱的,但是触摸无问题

解决方案

From 9cdb6424ba6df9dea03b26ff93cf7f0d1686ff10 Mon Sep 17 00:00:00 2001
From: Rany <hyt@t-chip.com.cn>
Date: Tue, 8 Aug 2017 09:27:43 +0800
Subject: [PATCH] =?UTF-8?q?Android->inputflinger:=E5=B0=86=E9=BC=A0?=
 =?UTF-8?q?=E6=A0=87=E6=93=8D=E4=BD=9C=E6=A8=A1=E6=8B=9F=E6=88=90=E8=A7=A6?=
 =?UTF-8?q?=E6=91=B8=E6=93=8D=E4=BD=9C=EF=BC=8C=E4=BF=AE=E5=A4=8D=E8=AE=BE?=
 =?UTF-8?q?=E7=BD=AE-=E8=AF=AD=E8=A8=80=E8=AE=BE=E7=BD=AE=E7=AD=89listview?=
 =?UTF-8?q?=E9=BC=A0=E6=A0=87=E7=82=B9=E5=87=BB=E9=94=99=E4=B9=B1=E7=9A=84?=
 =?UTF-8?q?=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../native/services/inputflinger/InputReader.cpp   | 77 +++++++++++++++++-----
 .../native/services/inputflinger/InputReader.h     |  5 +-
 2 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/frameworks/native/services/inputflinger/InputReader.cpp b/frameworks/native/services/inputflinger/InputReader.cpp
index e897f57..a26fe05 100755
--- a/frameworks/native/services/inputflinger/InputReader.cpp
+++ b/frameworks/native/services/inputflinger/InputReader.cpp
@@ -2459,7 +2459,7 @@ void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
 // --- CursorInputMapper ---
 
 CursorInputMapper::CursorInputMapper(InputDevice* device) :
-        InputMapper(device) {
+        InputMapper(device), isMouseToTouch(false) {
 }
 
 CursorInputMapper::~CursorInputMapper() {
@@ -2732,7 +2732,7 @@ void CursorInputMapper::sync(nsecs_t when) {
     // Synthesize key down from buttons if needed.
     synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
             policyFlags, lastButtonState, currentButtonState);
-
+    bool release = false;
     // Send motion event.
     if (downChanged || moved || scrolled || buttonsChanged) {
         int32_t metaState = mContext->getGlobalMetaState();
@@ -2745,39 +2745,78 @@ void CursorInputMapper::sync(nsecs_t when) {
         } else {
             motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
         }
+        //mouse convert to touch
+          if(motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
+                isMouseToTouch = true;
+                ALOGD("CursorInputMapper::sync motionEventAction==DOWN or UP");
+                mSource = AINPUT_SOURCE_TOUCHSCREEN;//主要起作用的
+            }
+            if(isMouseToTouch) {
+                pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
+                pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.152941);
+                pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0);
+                pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 38.971809);
+                pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 38.971809);
+                pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 38.971809);
+                pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 38.971809);
+            }
+            if(motionEventAction == AMOTION_EVENT_ACTION_UP) {
+                release = true;
+            }
+        
 
         if (buttonsReleased) {
             BitSet32 released(buttonsReleased);
             while (!released.isEmpty()) {
                 int32_t actionButton = BitSet32::valueForBit(released.clearFirstMarkedBit());
                 buttonState &= ~actionButton;
-                NotifyMotionArgs releaseArgs(when, getDeviceId(), mSource, policyFlags,
-                        AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0,
-                        metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
-                        displayId, 1, &pointerProperties, &pointerCoords,
-                        mXPrecision, mYPrecision, downTime);
-                getListener()->notifyMotion(&releaseArgs);
+                if(isMouseToTouch) {
+                        NotifyMotionArgs releaseArgs(when, getDeviceId(), mSource, policyFlags,
+                                                     AMOTION_EVENT_ACTION_UP, actionButton, 0,
+                                                     metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
+                                                     displayId, 1, &pointerProperties, &pointerCoords,
+                                                     mXPrecision, mYPrecision, downTime);
+                        getListener()->notifyMotion(&releaseArgs);
+                    } else {
+                        NotifyMotionArgs releaseArgs(when, getDeviceId(), mSource, policyFlags,
+                                                     AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0,
+                                                     metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
+                                                     displayId, 1, &pointerProperties, &pointerCoords,
+                                                     mXPrecision, mYPrecision, downTime);
+                        getListener()->notifyMotion(&releaseArgs);
+                    }
             }
         }
 
-        NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
+        if( motionEventAction != AMOTION_EVENT_ACTION_DOWN)
+        {
+            NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
                 motionEventAction, 0, 0, metaState, currentButtonState,
                 AMOTION_EVENT_EDGE_FLAG_NONE,
                 displayId, 1, &pointerProperties, &pointerCoords,
                 mXPrecision, mYPrecision, downTime);
-        getListener()->notifyMotion(&args);
-
+            getListener()->notifyMotion(&args);
+        }
         if (buttonsPressed) {
             BitSet32 pressed(buttonsPressed);
             while (!pressed.isEmpty()) {
                 int32_t actionButton = BitSet32::valueForBit(pressed.clearFirstMarkedBit());
                 buttonState |= actionButton;
-                NotifyMotionArgs pressArgs(when, getDeviceId(), mSource, policyFlags,
-                        AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0,
-                        metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
-                        displayId, 1, &pointerProperties, &pointerCoords,
-                        mXPrecision, mYPrecision, downTime);
-                getListener()->notifyMotion(&pressArgs);
+                if(isMouseToTouch) {
+                        NotifyMotionArgs pressArgs(when, getDeviceId(), mSource, policyFlags,
+                                                   AMOTION_EVENT_ACTION_DOWN, actionButton, 0,
+                                                   metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
+                                                   displayId, 1, &pointerProperties, &pointerCoords,
+                                                   mXPrecision, mYPrecision, downTime);
+                          getListener()->notifyMotion(&pressArgs);
+                    } else {
+                        NotifyMotionArgs pressArgs(when, getDeviceId(), mSource, policyFlags,
+                                                   AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0,
+                                                   metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
+                                                   displayId, 1, &pointerProperties, &pointerCoords,
+                                                   mXPrecision, mYPrecision, downTime);
+                          getListener()->notifyMotion(&pressArgs);
+                    }
             }
         }
 
@@ -2814,6 +2853,10 @@ void CursorInputMapper::sync(nsecs_t when) {
 
     mCursorMotionAccumulator.finishSync();
     mCursorScrollAccumulator.finishSync();
+     if(release) {
+            mSource = AINPUT_SOURCE_MOUSE;
+            isMouseToTouch = false;
+        }
 }
 
 int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
diff --git a/frameworks/native/services/inputflinger/InputReader.h b/frameworks/native/services/inputflinger/InputReader.h
index 1f8645f..cee0e6e 100644
--- a/frameworks/native/services/inputflinger/InputReader.h
+++ b/frameworks/native/services/inputflinger/InputReader.h
@@ -1195,6 +1195,7 @@ private:
 
 class CursorInputMapper : public InputMapper {
 public:
+
     CursorInputMapper(InputDevice* device);
     virtual ~CursorInputMapper();
 
@@ -1212,7 +1213,7 @@ public:
 private:
     // Amount that trackball needs to move in order to generate a key event.
     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
-
+    bool isMouseToTouch;
     // Immutable configuration parameters.
     struct Parameters {
         enum Mode {
@@ -1228,13 +1229,11 @@ private:
     CursorButtonAccumulator mCursorButtonAccumulator;
     CursorMotionAccumulator mCursorMotionAccumulator;
     CursorScrollAccumulator mCursorScrollAccumulator;
-
     int32_t mSource;
     float mXScale;
     float mYScale;
     float mXPrecision;
     float mYPrecision;
-
     float mVWheelScale;
     float mHWheelScale;
 
-- 
2.7.4


你可能感兴趣的:(Android)