OSDN Git Service

inputflinger: treat tablet-style inputs as absolute coordinate mouse pointer
[android-x86/frameworks-native.git] / services / inputflinger / reader / mapper / CursorInputMapper.cpp
index 2ac41b1..46d5783 100644 (file)
@@ -47,15 +47,29 @@ void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
         switch (rawEvent->code) {
             case REL_X:
                 mRelX = rawEvent->value;
+                mMoved = true;
                 break;
             case REL_Y:
                 mRelY = rawEvent->value;
+                mMoved = true;
+                break;
+        }
+    } else if (rawEvent->type == EV_ABS) {
+        switch (rawEvent->code) {
+            case ABS_X:
+                mAbsX = rawEvent->value;
+                mMoved = true;
+                break;
+            case ABS_Y:
+                mAbsY = rawEvent->value;
+                mMoved = true;
                 break;
         }
     }
 }
 
 void CursorMotionAccumulator::finishSync() {
+    mMoved = false;
     clearRelativeAxes();
 }
 
@@ -135,6 +149,10 @@ void CursorInputMapper::configure(nsecs_t when, const InputReaderConfiguration*
                 [[fallthrough]];
             case Parameters::MODE_POINTER:
                 mSource = AINPUT_SOURCE_MOUSE;
+                if (mParameters.hasAbsAxis) {
+                    getAbsoluteAxisInfo(ABS_X, &mRawAbsXInfo);
+                    getAbsoluteAxisInfo(ABS_Y, &mRawAbsYInfo);
+                }
                 mXPrecision = 1.0f;
                 mYPrecision = 1.0f;
                 mXScale = 1.0f;
@@ -209,12 +227,18 @@ void CursorInputMapper::configure(nsecs_t when, const InputReaderConfiguration*
                 }
             }
         } else {
-            if (isOrientedDevice) {
-                std::optional<DisplayViewport> internalViewport =
-                        config->getDisplayViewportByType(ViewportType::INTERNAL);
-                if (internalViewport) {
+            std::optional<DisplayViewport> internalViewport =
+                    config->getDisplayViewportByType(ViewportType::INTERNAL);
+            if (internalViewport) {
+                if (isOrientedDevice) {
                     mOrientation = internalViewport->orientation;
                 }
+                if (mParameters.hasAbsAxis) {
+                    mXScale = float(internalViewport->logicalRight - internalViewport->logicalLeft)/(mRawAbsXInfo.maxValue - mRawAbsXInfo.minValue + 1);
+                    mYScale = float(internalViewport->logicalBottom - internalViewport->logicalTop)/(mRawAbsYInfo.maxValue - mRawAbsYInfo.minValue + 1);
+                    mXPrecision = 1.0f / mXScale;
+                    mYPrecision = 1.0f / mYScale;
+                }
             }
         }
 
@@ -242,6 +266,11 @@ void CursorInputMapper::configureParameters() {
     if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
         mParameters.hasAssociatedDisplay = true;
     }
+
+    mParameters.hasAbsAxis = false;
+    if (mParameters.mode == Parameters::MODE_POINTER) {
+        mParameters.hasAbsAxis = getDeviceContext().hasAbsoluteAxis(ABS_X) && getDeviceContext().hasAbsoluteAxis(ABS_Y) ? true : false;
+    }
 }
 
 void CursorInputMapper::dumpParameters(std::string& dump) {
@@ -264,6 +293,7 @@ void CursorInputMapper::dumpParameters(std::string& dump) {
     }
 
     dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
+    dump += StringPrintf(INDENT4 "Absolute Axis: %s\n", toString(mParameters.hasAbsAxis));
 }
 
 void CursorInputMapper::reset(nsecs_t when) {
@@ -291,6 +321,28 @@ void CursorInputMapper::process(const RawEvent* rawEvent) {
     }
 }
 
+void CursorInputMapper::rotateAbsolute(float* absX, float* absY) {
+    float temp;
+    switch (mOrientation) {
+    case DISPLAY_ORIENTATION_90:
+        temp = *absX;
+        *absX = *absY;
+        *absY = ((mRawAbsXInfo.maxValue - mRawAbsXInfo.minValue) + 1) - temp;
+        break;
+
+    case DISPLAY_ORIENTATION_180:
+        *absX = ((mRawAbsXInfo.maxValue - mRawAbsXInfo.minValue) + 1) - *absX;
+        *absY = ((mRawAbsYInfo.maxValue - mRawAbsYInfo.minValue) + 1) - *absY;
+        break;
+
+    case DISPLAY_ORIENTATION_270:
+        temp = *absX;
+        *absX = ((mRawAbsYInfo.maxValue - mRawAbsYInfo.minValue) + 1) - *absY;
+        *absY = temp;
+        break;
+    }
+}
+
 void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
     int32_t lastButtonState = mButtonState;
     int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
@@ -312,14 +364,7 @@ void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
     int32_t buttonsPressed = currentButtonState & ~lastButtonState;
     int32_t buttonsReleased = lastButtonState & ~currentButtonState;
 
-    float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
-    float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
-    bool moved = deltaX != 0 || deltaY != 0;
-
-    // Rotate delta according to orientation.
-    rotateDelta(mOrientation, &deltaX, &deltaY);
-
-    // Move the pointer.
+    bool moved = false;
     PointerProperties pointerProperties;
     pointerProperties.clear();
     pointerProperties.id = 0;
@@ -328,6 +373,44 @@ void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
     PointerCoords pointerCoords;
     pointerCoords.clear();
 
+    if (!mParameters.hasAbsAxis) {
+        float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
+        float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
+        moved = deltaX != 0 || deltaY != 0;
+
+        // Rotate delta according to orientation if needed.
+        rotateDelta(mOrientation, &deltaX, &deltaY);
+        mPointerVelocityControl.move(when, &deltaX, &deltaY);
+        if (mPointerController != NULL) {
+            if (moved) {
+                mPointerController->move(deltaX, deltaY);
+            }
+            float x, y;
+            mPointerController->getPosition(&x, &y);
+            pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
+            pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
+            pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
+            pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
+        } else {
+            pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
+            pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
+        }
+    } else {
+        float absX = mCursorMotionAccumulator.getAbsoluteX() - mRawAbsXInfo.minValue;
+        float absY = mCursorMotionAccumulator.getAbsoluteY() - mRawAbsYInfo.minValue;
+        if (mParameters.orientationAware) {
+            rotateAbsolute(&absX, &absY);
+        }
+        absX = absX * mXScale;
+        absY = absY * mYScale;
+        moved = mCursorMotionAccumulator.hasMoved();
+        if (moved) {
+            mPointerController->setPosition(absX, absY);
+        }
+        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, absX);
+        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, absY);
+    }
+
     float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
     float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
     bool scrolled = vscroll != 0 || hscroll != 0;
@@ -335,8 +418,6 @@ void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
     mWheelYVelocityControl.move(when, nullptr, &vscroll);
     mWheelXVelocityControl.move(when, &hscroll, nullptr);
 
-    mPointerVelocityControl.move(when, &deltaX, &deltaY);
-
     int32_t displayId = ADISPLAY_ID_NONE;
     float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
     float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION;
@@ -344,18 +425,6 @@ void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
         if (moved || scrolled || buttonsChanged) {
             mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
 
-            if (moved) {
-                float dx = deltaX;
-                float dy = deltaY;
-                if (isPerWindowInputRotationEnabled()) {
-                    // Rotate the delta from InputReader's un-rotated coordinate space to
-                    // PointerController's rotated coordinate space that is oriented with the
-                    // viewport.
-                    rotateDelta(getInverseRotation(mOrientation), &dx, &dy);
-                }
-                mPointerController->move(dx, dy);
-            }
-
             if (buttonsChanged) {
                 mPointerController->setButtonState(currentButtonState);
             }
@@ -370,17 +439,7 @@ void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
             rotatePoint(mOrientation, xCursorPosition /*byRef*/, yCursorPosition /*byRef*/,
                         mDisplayWidth, mDisplayHeight);
         }
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, xCursorPosition);
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, yCursorPosition);
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
         displayId = mPointerController->getDisplayId();
-    } else {
-        // Pointer capture and navigation modes
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
-        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
     }
 
     pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);