Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit edf6ce71 authored by Harry Cutts's avatar Harry Cutts
Browse files

TouchpadInputMapper: rotate pointer movement with display

Bug: 263378323
Test: Rotate device to landscape, check pointer movements are rotated
Test: atest inputflinger_tests
Change-Id: I548ec8b31a90a575253a8fe004c67cb1fbbf51e8
parent 990ff7ba
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -16,11 +16,14 @@

#include "../Macros.h"

#include <optional>

#include <android/input.h>
#include <linux/input-event-codes.h>
#include <log/log_main.h>
#include "TouchCursorInputMapperCommon.h"
#include "TouchpadInputMapper.h"
#include "ui/Rotation.h"

namespace android {

@@ -111,6 +114,22 @@ uint32_t TouchpadInputMapper::getSources() const {
    return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
}

std::list<NotifyArgs> TouchpadInputMapper::configure(nsecs_t when,
                                                     const InputReaderConfiguration* config,
                                                     uint32_t changes) {
    if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
        std::optional<int32_t> displayId = mPointerController->getDisplayId();
        ui::Rotation orientation = ui::ROTATION_0;
        if (displayId.has_value()) {
            if (auto viewport = config->getDisplayViewportById(*displayId); viewport) {
                orientation = getInverseRotation(viewport->orientation);
            }
        }
        mGestureConverter.setOrientation(orientation);
    }
    return {};
}

std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
    mStateConverter.reset();
    mGestureConverter.reset();
+4 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include "EventHub.h"
#include "InputDevice.h"
#include "InputMapper.h"
#include "InputReaderBase.h"
#include "NotifyArgs.h"
#include "gestures/GestureConverter.h"
#include "gestures/HardwareStateConverter.h"
@@ -39,6 +40,9 @@ public:
    ~TouchpadInputMapper();

    uint32_t getSources() const override;
    [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
                                                  const InputReaderConfiguration* config,
                                                  uint32_t changes) override;
    [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
    [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;

+7 −3
Original line number Diff line number Diff line
@@ -72,8 +72,12 @@ NotifyArgs GestureConverter::handleMove(nsecs_t when, nsecs_t readTime, const Ge
    props.id = 0;
    props.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;

    float deltaX = gesture.details.move.dx;
    float deltaY = gesture.details.move.dy;
    rotateDelta(mOrientation, &deltaX, &deltaY);

    mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
    mPointerController->move(gesture.details.move.dx, gesture.details.move.dy);
    mPointerController->move(deltaX, deltaY);
    mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
    float xCursorPosition, yCursorPosition;
    mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
@@ -82,8 +86,8 @@ NotifyArgs GestureConverter::handleMove(nsecs_t when, nsecs_t readTime, const Ge
    coords.clear();
    coords.setAxisValue(AMOTION_EVENT_AXIS_X, xCursorPosition);
    coords.setAxisValue(AMOTION_EVENT_AXIS_Y, yCursorPosition);
    coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, gesture.details.move.dx);
    coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, gesture.details.move.dy);
    coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
    coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
    const bool down = isPointerDown(mButtonState);
    coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);

+3 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@

#include "InputReaderContext.h"
#include "NotifyArgs.h"
#include "ui/Rotation.h"

#include "include/gestures.h"

@@ -35,6 +36,7 @@ class GestureConverter {
public:
    GestureConverter(InputReaderContext& readerContext, int32_t deviceId);

    void setOrientation(ui::Rotation orientation) { mOrientation = orientation; }
    void reset();

    [[nodiscard]] std::list<NotifyArgs> handleGesture(nsecs_t when, nsecs_t readTime,
@@ -56,6 +58,7 @@ private:
    InputReaderContext& mReaderContext;
    std::shared_ptr<PointerControllerInterface> mPointerController;

    ui::Rotation mOrientation = ui::ROTATION_0;
    // The current button state according to the gestures library, but converted into MotionEvent
    // button values (AMOTION_EVENT_BUTTON_...).
    uint32_t mButtonState = 0;
+18 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include "TestInputListener.h"
#include "TestInputListenerMatchers.h"
#include "include/gestures.h"
#include "ui/Rotation.h"

namespace android {

@@ -77,6 +78,23 @@ TEST_F(GestureConverterTest, Move) {
    ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(95, 210));
}

TEST_F(GestureConverterTest, Move_Rotated) {
    GestureConverter converter(*mReader->getContext(), DEVICE_ID);
    converter.setOrientation(ui::ROTATION_90);

    Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
    std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
    ASSERT_EQ(1u, args.size());

    ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
                AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
                      WithCoords(POINTER_X + 10, POINTER_Y + 5), WithRelativeMotion(10, 5),
                      WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithButtonState(0),
                      WithPressure(0.0f)));

    ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110, 205));
}

TEST_F(GestureConverterTest, ButtonsChange) {
    GestureConverter converter(*mReader->getContext(), DEVICE_ID);