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

Commit f404321f authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Misc fixes in InputDispatcher

Minor refactors to reduce the diff in the upcoming CLs

Bug: 211379801
Test: atest inputflinger_tests
Change-Id: I1f0fd663ba3b49fba4727a4e1a144f65ae3ad6f5
parent d76a77a2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4531,7 +4531,7 @@ InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* ev
    // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
    // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
    // from events that originate from actual hardware.
    int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
    DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
    if (policyFlags & POLICY_FLAG_FILTERED) {
        resolvedDeviceId = event->getDeviceId();
    }
+7 −2
Original line number Diff line number Diff line
@@ -83,6 +83,11 @@ bool InputState::trackKey(const KeyEntry& entry, int32_t action, int32_t flags)
    }
}

/**
 * Return:
 *  true if the incoming event was correctly tracked,
 *  false if the incoming event should be dropped.
 */
bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) {
    int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
    switch (actionMasked) {
@@ -310,7 +315,7 @@ std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents
        nsecs_t currentTime) {
    std::vector<std::unique_ptr<EventEntry>> events;
    for (MotionMemento& memento : mMotionMementos) {
        if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) {
        if (!isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) {
            continue;
        }

@@ -443,7 +448,7 @@ void InputState::mergePointerStateTo(InputState& other) {
        MotionMemento& memento = mMotionMementos[i];
        // Since we support split pointers we need to merge touch events
        // from the same source + device + screen.
        if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
        if (isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) {
            bool merged = false;
            for (size_t j = 0; j < other.mMotionMementos.size(); j++) {
                MotionMemento& otherMemento = other.mMotionMementos[j];
+6 −1
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

#include "InputTarget.h"

#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <input/PrintTools.h>
#include <inttypes.h>
#include <string>

@@ -34,7 +36,10 @@ void InputTarget::addPointers(std::bitset<MAX_POINTER_ID + 1> newPointerIds,
    }

    // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
    LOG_ALWAYS_FATAL_IF((pointerIds & newPointerIds).any());
    if ((pointerIds & newPointerIds).any()) {
        LOG(FATAL) << __func__ << " - overlap with incoming pointers "
                   << bitsetToString(newPointerIds) << " in " << *this;
    }

    pointerIds |= newPointerIds;
    for (size_t i = 0; i < newPointerIds.size(); i++) {
+22 −11
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <input/Input.h>
#include <input/PrintTools.h>
#include <linux/input.h>
#include <sys/epoll.h>

@@ -50,6 +51,8 @@ namespace android::inputdispatcher {
using namespace ftl::flag_operators;
using testing::AllOf;

namespace {

// An arbitrary time value.
static constexpr nsecs_t ARBITRARY_TIME = 1234;

@@ -136,6 +139,10 @@ struct PointF {
    auto operator<=>(const PointF&) const = default;
};

inline std::string pointFToString(const PointF& p) {
    return std::string("(") + std::to_string(p.x) + ", " + std::to_string(p.y) + ")";
}

/**
 * Return a DOWN key event with KEYCODE_A.
 */
@@ -148,13 +155,8 @@ static KeyEvent getTestKeyEvent() {
    return event;
}

static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
    ASSERT_EQ(expectedAction, receivedAction)
            << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
            << MotionEvent::actionToString(receivedAction);
}

MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
    *result_listener << "expected downTime " << downTime << ", but got " << arg.getDownTime();
    return arg.getDownTime() == downTime;
}

@@ -165,6 +167,7 @@ MATCHER_P(WithSource, source, "InputEvent with specified source") {
}

MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
    *result_listener << "expected flags " << std::hex << flags << ", but got " << arg.getFlags();
    return arg.getFlags() == flags;
}

@@ -173,10 +176,16 @@ MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
        *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
        return false;
    }
    return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
    const float receivedX = arg.getX(/*pointerIndex=*/0);
    const float receivedY = arg.getY(/*pointerIndex=*/0);
    *result_listener << "expected coords (" << x << ", " << y << "), but got (" << receivedX << ", "
                     << receivedY << ")";
    return receivedX == x && receivedY == y;
}

MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
    *result_listener << "expected pointerCount " << pointerCount << ", but got "
                     << arg.getPointerCount();
    return arg.getPointerCount() == pointerCount;
}

@@ -187,6 +196,8 @@ MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
        const int32_t pointerId = arg.getPointerId(pointerIndex);
        actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
    }
    *result_listener << "expected pointers " << dumpMap(pointers, constToString, pointFToString)
                     << ", but got " << dumpMap(actualPointers, constToString, pointFToString);
    return pointers == actualPointers;
}

@@ -617,6 +628,7 @@ private:
        mFilteredEvent = nullptr;
    }
};
} // namespace

// --- InputDispatcherTest ---

@@ -959,7 +971,7 @@ public:
        switch (expectedEventType) {
            case InputEventType::KEY: {
                const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
                EXPECT_EQ(expectedAction, keyEvent.getAction());
                ASSERT_THAT(keyEvent, WithKeyAction(expectedAction));
                if (expectedFlags.has_value()) {
                    EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
                }
@@ -967,8 +979,7 @@ public:
            }
            case InputEventType::MOTION: {
                const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
                assertMotionAction(expectedAction, motionEvent.getAction());

                ASSERT_THAT(motionEvent, WithMotionAction(expectedAction));
                if (expectedFlags.has_value()) {
                    EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
                }
@@ -6467,7 +6478,7 @@ protected:
        ASSERT_NE(nullptr, motionEvent)
                << name.c_str() << ": consumer should have returned non-NULL event.";

        assertMotionAction(expectedAction, motionEvent->getAction());
        ASSERT_THAT(*motionEvent, WithMotionAction(expectedAction));
        ASSERT_EQ(points.size(), motionEvent->getPointerCount());

        for (size_t i = 0; i < points.size(); i++) {