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

Commit 4cc839fe authored by Garfield Tan's avatar Garfield Tan
Browse files

Add ID to native events.

To separate this big work into pieces I introduced a placeholder in
InputTransport used to initialize native input events received from
InputFlinger until InputFlinger can generate random sequence numbers.

The work in InputDispatcher wires up ID between events and notify args
as well.

Bug: 144889238
Test: Builds and some smoke tests.
Test: atest libinput_tests
Change-Id: I1ef7f243cc89a8b6e07ba9ba30a43c21eedb16ce
parent 84b087ec
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -397,6 +397,8 @@ public:

    virtual int32_t getType() const = 0;

    inline int32_t getId() const { return mId; }

    inline int32_t getDeviceId() const { return mDeviceId; }

    inline uint32_t getSource() const { return mSource; }
@@ -409,11 +411,15 @@ public:

    inline std::array<uint8_t, 32> getHmac() const { return mHmac; }

    static int32_t nextId();

protected:
    void initialize(int32_t deviceId, uint32_t source, int32_t displayId,
    void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
                    std::array<uint8_t, 32> hmac);

    void initialize(const InputEvent& from);

    int32_t mId;
    int32_t mDeviceId;
    uint32_t mSource;
    int32_t mDisplayId;
@@ -450,7 +456,7 @@ public:
    static const char* getLabel(int32_t keyCode);
    static int32_t getKeyCodeFromLabel(const char* label);

    void initialize(int32_t deviceId, uint32_t source, int32_t displayId,
    void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
                    std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, int32_t keyCode,
                    int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime,
                    nsecs_t eventTime);
@@ -674,7 +680,7 @@ public:

    ssize_t findPointerIndex(int32_t pointerId) const;

    void initialize(int32_t deviceId, uint32_t source, int32_t displayId,
    void initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
                    std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
                    int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
                    MotionClassification classification, float xScale, float yScale, float xOffset,
@@ -754,7 +760,7 @@ public:

    inline bool getInTouchMode() const { return mInTouchMode; }

    void initialize(bool hasFocus, bool inTouchMode);
    void initialize(int32_t id, bool hasFocus, bool inTouchMode);

    void initialize(const FocusEvent& from);

+18 −8
Original line number Diff line number Diff line
@@ -110,8 +110,9 @@ VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event)
            event.getButtonState()};
}

void InputEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
                            std::array<uint8_t, 32> hmac) {
    mId = id;
    mDeviceId = deviceId;
    mSource = source;
    mDisplayId = displayId;
@@ -119,12 +120,18 @@ void InputEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId
}

void InputEvent::initialize(const InputEvent& from) {
    mId = from.mId;
    mDeviceId = from.mDeviceId;
    mSource = from.mSource;
    mDisplayId = from.mDisplayId;
    mHmac = from.mHmac;
}

int32_t InputEvent::nextId() {
    static IdGenerator idGen(IdGenerator::Source::OTHER);
    return idGen.nextId();
}

// --- KeyEvent ---

const char* KeyEvent::getLabel(int32_t keyCode) {
@@ -135,11 +142,11 @@ int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
    return getKeyCodeByLabel(label);
}

void KeyEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
                          std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
                          int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
                          nsecs_t downTime, nsecs_t eventTime) {
    InputEvent::initialize(deviceId, source, displayId, hmac);
    InputEvent::initialize(id, deviceId, source, displayId, hmac);
    mAction = action;
    mFlags = flags;
    mKeyCode = keyCode;
@@ -298,7 +305,7 @@ void PointerProperties::copyFrom(const PointerProperties& other) {

// --- MotionEvent ---

void MotionEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
                             std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
                             int32_t flags, int32_t edgeFlags, int32_t metaState,
                             int32_t buttonState, MotionClassification classification, float xScale,
@@ -307,7 +314,7 @@ void MotionEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayI
                             nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
                             const PointerProperties* pointerProperties,
                             const PointerCoords* pointerCoords) {
    InputEvent::initialize(deviceId, source, displayId, hmac);
    InputEvent::initialize(id, deviceId, source, displayId, hmac);
    mAction = action;
    mActionButton = actionButton;
    mFlags = flags;
@@ -332,7 +339,8 @@ void MotionEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayI
}

void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
    InputEvent::initialize(other->mDeviceId, other->mSource, other->mDisplayId, other->mHmac);
    InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
                           other->mHmac);
    mAction = other->mAction;
    mActionButton = other->mActionButton;
    mFlags = other->mFlags;
@@ -540,6 +548,7 @@ status_t MotionEvent::readFromParcel(Parcel* parcel) {
        return BAD_VALUE;
    }

    mId = parcel->readInt32();
    mDeviceId = parcel->readInt32();
    mSource = parcel->readUint32();
    mDisplayId = parcel->readInt32();
@@ -601,6 +610,7 @@ status_t MotionEvent::writeToParcel(Parcel* parcel) const {
    parcel->writeInt32(pointerCount);
    parcel->writeInt32(sampleCount);

    parcel->writeInt32(mId);
    parcel->writeInt32(mDeviceId);
    parcel->writeUint32(mSource);
    parcel->writeInt32(mDisplayId);
@@ -670,8 +680,8 @@ int32_t MotionEvent::getAxisFromLabel(const char* label) {

// --- FocusEvent ---

void FocusEvent::initialize(bool hasFocus, bool inTouchMode) {
    InputEvent::initialize(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
    InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
                           ADISPLAY_ID_NONE, INVALID_HMAC);
    mHasFocus = hasFocus;
    mInTouchMode = inTouchMode;
+22 −15
Original line number Diff line number Diff line
@@ -63,6 +63,10 @@ static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
// far into the future.  This time is further bounded by 50% of the last time delta.
static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;

// A placeholder sequence number used to initialize native input events before InputFlinger is
// migrated to new sequence number system.
static constexpr int32_t INPUT_FLINGER_SEQUENCE_NUM = 0;

/**
 * System property for enabling / disabling touch resampling.
 * Resampling extrapolates / interpolates the reported touch event coordinates to better
@@ -1142,14 +1146,16 @@ ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
}

void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
    event->initialize(msg->body.key.deviceId, msg->body.key.source, msg->body.key.displayId,
                      msg->body.key.hmac, msg->body.key.action, msg->body.key.flags,
                      msg->body.key.keyCode, msg->body.key.scanCode, msg->body.key.metaState,
                      msg->body.key.repeatCount, msg->body.key.downTime, msg->body.key.eventTime);
    event->initialize(INPUT_FLINGER_SEQUENCE_NUM, msg->body.key.deviceId, msg->body.key.source,
                      msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
                      msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
                      msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
                      msg->body.key.eventTime);
}

void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
    event->initialize(msg->body.focus.hasFocus == 1, msg->body.focus.inTouchMode == 1);
    event->initialize(INPUT_FLINGER_SEQUENCE_NUM, msg->body.focus.hasFocus == 1,
                      msg->body.focus.inTouchMode == 1);
}

void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
@@ -1161,16 +1167,17 @@ void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage
        pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
    }

    event->initialize(msg->body.motion.deviceId, msg->body.motion.source,
                      msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
                      msg->body.motion.actionButton, msg->body.motion.flags,
                      msg->body.motion.edgeFlags, msg->body.motion.metaState,
                      msg->body.motion.buttonState, msg->body.motion.classification,
                      msg->body.motion.xScale, msg->body.motion.yScale, msg->body.motion.xOffset,
                      msg->body.motion.yOffset, msg->body.motion.xPrecision,
                      msg->body.motion.yPrecision, msg->body.motion.xCursorPosition,
                      msg->body.motion.yCursorPosition, msg->body.motion.downTime,
                      msg->body.motion.eventTime, pointerCount, pointerProperties, pointerCoords);
    event->initialize(INPUT_FLINGER_SEQUENCE_NUM, msg->body.motion.deviceId,
                      msg->body.motion.source, msg->body.motion.displayId, msg->body.motion.hmac,
                      msg->body.motion.action, msg->body.motion.actionButton,
                      msg->body.motion.flags, msg->body.motion.edgeFlags,
                      msg->body.motion.metaState, msg->body.motion.buttonState,
                      msg->body.motion.classification, msg->body.motion.xScale,
                      msg->body.motion.yScale, msg->body.motion.xOffset, msg->body.motion.yOffset,
                      msg->body.motion.xPrecision, msg->body.motion.yPrecision,
                      msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
                      msg->body.motion.downTime, msg->body.motion.eventTime, pointerCount,
                      pointerProperties, pointerCoords);
}

void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
+3 −3
Original line number Diff line number Diff line
@@ -487,9 +487,9 @@ void KeyCharacterMap::addKey(Vector<KeyEvent>& outEvents,
        int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time) {
    outEvents.push();
    KeyEvent& event = outEvents.editTop();
    event.initialize(deviceId, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, INVALID_HMAC,
                     down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, 0, keyCode, 0, metaState,
                     0, time, time);
    event.initialize(InputEvent::nextId(), deviceId, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
                     INVALID_HMAC, down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, 0, keyCode,
                     0, metaState, 0, time, time);
}

void KeyCharacterMap::addMetaKeys(Vector<KeyEvent>& outEvents,
+22 −15
Original line number Diff line number Diff line
@@ -182,10 +182,12 @@ TEST_F(KeyEventTest, Properties) {
    // Initialize and get properties.
    constexpr nsecs_t ARBITRARY_DOWN_TIME = 1;
    constexpr nsecs_t ARBITRARY_EVENT_TIME = 2;
    event.initialize(2, AINPUT_SOURCE_GAMEPAD, DISPLAY_ID, HMAC, AKEY_EVENT_ACTION_DOWN,
    const int32_t id = InputEvent::nextId();
    event.initialize(id, 2, AINPUT_SOURCE_GAMEPAD, DISPLAY_ID, HMAC, AKEY_EVENT_ACTION_DOWN,
                     AKEY_EVENT_FLAG_FROM_SYSTEM, AKEYCODE_BUTTON_X, 121, AMETA_ALT_ON, 1,
                     ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME);

    ASSERT_EQ(id, event.getId());
    ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event.getType());
    ASSERT_EQ(2, event.getDeviceId());
    ASSERT_EQ(AINPUT_SOURCE_GAMEPAD, event.getSource());
@@ -222,12 +224,16 @@ protected:
    static constexpr float X_OFFSET = 1;
    static constexpr float Y_OFFSET = 1.1;

    int32_t mId;

    void initializeEventWithHistory(MotionEvent* event);
    void assertEqualsEventWithHistory(const MotionEvent* event);
};


void MotionEventTest::initializeEventWithHistory(MotionEvent* event) {
    mId = InputEvent::nextId();

    PointerProperties pointerProperties[2];
    pointerProperties[0].clear();
    pointerProperties[0].id = 1;
@@ -257,10 +263,10 @@ void MotionEventTest::initializeEventWithHistory(MotionEvent* event) {
    pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 26);
    pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, 27);
    pointerCoords[1].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 28);
    event->initialize(2, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, HMAC, AMOTION_EVENT_ACTION_MOVE, 0,
                      AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED, AMOTION_EVENT_EDGE_FLAG_TOP,
                      AMETA_ALT_ON, AMOTION_EVENT_BUTTON_PRIMARY, MotionClassification::NONE,
                      X_SCALE, Y_SCALE, X_OFFSET, Y_OFFSET, 2.0f, 2.1f,
    event->initialize(mId, 2, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, HMAC,
                      AMOTION_EVENT_ACTION_MOVE, 0, AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED,
                      AMOTION_EVENT_EDGE_FLAG_TOP, AMETA_ALT_ON, AMOTION_EVENT_BUTTON_PRIMARY,
                      MotionClassification::NONE, X_SCALE, Y_SCALE, X_OFFSET, Y_OFFSET, 2.0f, 2.1f,
                      AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
                      ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME, 2, pointerProperties,
                      pointerCoords);
@@ -308,6 +314,7 @@ void MotionEventTest::initializeEventWithHistory(MotionEvent* event) {

void MotionEventTest::assertEqualsEventWithHistory(const MotionEvent* event) {
    // Check properties.
    ASSERT_EQ(mId, event->getId());
    ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
    ASSERT_EQ(2, event->getDeviceId());
    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, event->getSource());
@@ -577,8 +584,8 @@ TEST_F(MotionEventTest, Transform) {
        pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, angle);
    }
    MotionEvent event;
    event.initialize(0 /*deviceId*/, AINPUT_SOURCE_UNKNOWN, DISPLAY_ID, INVALID_HMAC,
                     AMOTION_EVENT_ACTION_MOVE, 0 /*actionButton*/, 0 /*flags*/,
    event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_UNKNOWN, DISPLAY_ID,
                     INVALID_HMAC, AMOTION_EVENT_ACTION_MOVE, 0 /*actionButton*/, 0 /*flags*/,
                     AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/,
                     MotionClassification::NONE, 1 /*xScale*/, 1 /*yScale*/, 0 /*xOffset*/,
                     0 /*yOffset*/, 0 /*xPrecision*/, 0 /*yPrecision*/,
@@ -642,10 +649,10 @@ TEST_F(MotionEventTest, Initialize_SetsClassification) {
    }

    for (MotionClassification classification : classifications) {
        event.initialize(0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, INVALID_HMAC,
                         AMOTION_EVENT_ACTION_DOWN, 0, 0, AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE,
                         0, classification, 1 /*xScale*/, 1 /*yScale*/, 0, 0, 0, 0,
                         AMOTION_EVENT_INVALID_CURSOR_POSITION,
        event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN,
                         DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
                         AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, classification, 1 /*xScale*/,
                         1 /*yScale*/, 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
                         AMOTION_EVENT_INVALID_CURSOR_POSITION, 0 /*downTime*/, 0 /*eventTime*/,
                         pointerCount, pointerProperties, pointerCoords);
        ASSERT_EQ(classification, event.getClassification());
@@ -663,10 +670,10 @@ TEST_F(MotionEventTest, Initialize_SetsCursorPosition) {
        pointerCoords[i].clear();
    }

    event.initialize(0 /*deviceId*/, AINPUT_SOURCE_MOUSE, DISPLAY_ID, INVALID_HMAC,
                     AMOTION_EVENT_ACTION_DOWN, 0, 0, AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0,
                     MotionClassification::NONE, 1 /*xScale*/, 1 /*yScale*/, 0, 0, 0, 0,
                     280 /*xCursorPosition*/, 540 /*yCursorPosition*/, 0 /*downTime*/,
    event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_MOUSE, DISPLAY_ID,
                     INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, AMOTION_EVENT_EDGE_FLAG_NONE,
                     AMETA_NONE, 0, MotionClassification::NONE, 1 /*xScale*/, 1 /*yScale*/, 0, 0, 0,
                     0, 280 /*xCursorPosition*/, 540 /*yCursorPosition*/, 0 /*downTime*/,
                     0 /*eventTime*/, pointerCount, pointerProperties, pointerCoords);
    event.offsetLocation(20, 60);
    ASSERT_EQ(280, event.getRawXCursorPosition());
Loading