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

Commit 0c18aac2 authored by Garfield Tan's avatar Garfield Tan Committed by Android (Google) Code Review
Browse files

Merge changes from topics "input_events_sequence_num", "wire_input_event_id"

* changes:
  Let InputFlinger generate event IDs.
  Rename sequenceNum to ID.
  Add ID to native events.
  Add event ID generator.
parents 219643ee ff1f1bb9
Loading
Loading
Loading
Loading
+42 −4
Original line number Diff line number Diff line
@@ -265,6 +265,38 @@ enum class MotionClassification : uint8_t {
 */
const char* motionClassificationToString(MotionClassification classification);

/**
 * Generator of unique numbers used to identify input events.
 *
 * Layout of ID:
 *     |--------------------------|---------------------------|
 *     |   2 bits for source      | 30 bits for random number |
 *     |--------------------------|---------------------------|
 */
class IdGenerator {
private:
    static constexpr uint32_t SOURCE_SHIFT = 30;

public:
    // Used to divide integer space to ensure no conflict among these sources./
    enum class Source : int32_t {
        INPUT_READER = 0x0 << SOURCE_SHIFT,
        INPUT_DISPATCHER = 0x1 << SOURCE_SHIFT,
        OTHER = 0x3 << SOURCE_SHIFT, // E.g. app injected events
    };
    IdGenerator(Source source);

    int32_t nextId() const;

    // Extract source from given id.
    static inline Source getSource(int32_t id) { return static_cast<Source>(SOURCE_MASK & id); }

private:
    const Source mSource;

    static constexpr int32_t SOURCE_MASK = 0x3 << SOURCE_SHIFT;
};

/**
 * Invalid value for cursor position. Used for non-mouse events, tests and injected events. Don't
 * use it for direct comparison with any other value, because NaN isn't equal to itself according to
@@ -365,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; }
@@ -377,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;
@@ -418,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);
@@ -642,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,
@@ -722,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);

+17 −14
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ struct InputMessage {
    union Body {
        struct Key {
            uint32_t seq;
            uint32_t empty1;
            int32_t eventId;
            nsecs_t eventTime __attribute__((aligned(8)));
            int32_t deviceId;
            int32_t source;
@@ -102,7 +102,7 @@ struct InputMessage {

        struct Motion {
            uint32_t seq;
            uint32_t empty1;
            int32_t eventId;
            nsecs_t eventTime __attribute__((aligned(8)));
            int32_t deviceId;
            int32_t source;
@@ -159,6 +159,8 @@ struct InputMessage {

        struct Focus {
            uint32_t seq;
            int32_t eventId;
            uint32_t empty1;
            // The following two fields take up 4 bytes total
            uint16_t hasFocus;    // actually a bool
            uint16_t inTouchMode; // actually a bool, but we must maintain 8-byte alignment
@@ -276,9 +278,9 @@ public:
     * Returns BAD_VALUE if seq is 0.
     * Other errors probably indicate that the channel is broken.
     */
    status_t publishKeyEvent(uint32_t seq, int32_t deviceId, int32_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,
    status_t publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_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);

    /* Publishes a motion event to the input channel.
@@ -289,14 +291,15 @@ public:
     * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS.
     * Other errors probably indicate that the channel is broken.
     */
    status_t publishMotionEvent(uint32_t seq, int32_t deviceId, int32_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, float yOffset,
                                float xPrecision, float yPrecision, float xCursorPosition,
                                float yCursorPosition, nsecs_t downTime, nsecs_t eventTime,
                                uint32_t pointerCount, const PointerProperties* pointerProperties,
    status_t publishMotionEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_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, float yOffset, float xPrecision, float yPrecision,
                                float xCursorPosition, float yCursorPosition, nsecs_t downTime,
                                nsecs_t eventTime, uint32_t pointerCount,
                                const PointerProperties* pointerProperties,
                                const PointerCoords* pointerCoords);

    /* Publishes a focus event to the input channel.
@@ -306,7 +309,7 @@ public:
     * Returns DEAD_OBJECT if the channel's peer has been closed.
     * Other errors probably indicate that the channel is broken.
     */
    status_t publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode);
    status_t publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus, bool inTouchMode);

    /* Receives the finished signal from the consumer in reply to the original dispatch signal.
     * If a signal was received, returns the message sequence number,
+47 −8
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@
#define LOG_TAG "Input"
//#define LOG_NDEBUG 0

#include <cutils/compiler.h>
#include <limits.h>
#include <string.h>

#include <input/Input.h>
#include <input/InputDevice.h>
@@ -25,6 +27,7 @@

#ifdef __ANDROID__
#include <binder/Parcel.h>
#include <sys/random.h>
#endif

namespace android {
@@ -40,6 +43,32 @@ const char* motionClassificationToString(MotionClassification classification) {
    }
}

// --- IdGenerator ---
IdGenerator::IdGenerator(Source source) : mSource(source) {}

int32_t IdGenerator::nextId() const {
    constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
    int32_t id = 0;

// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
// use sequence number so just always return mSource.
#ifdef __ANDROID__
    constexpr size_t BUF_LEN = sizeof(id);
    size_t totalBytes = 0;
    while (totalBytes < BUF_LEN) {
        ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
        if (CC_UNLIKELY(bytes < 0)) {
            ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
            id = 0;
            break;
        }
        totalBytes += bytes;
    }
#endif // __ANDROID__

    return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
}

// --- InputEvent ---

const char* inputEventTypeToString(int32_t type) {
@@ -81,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;
@@ -90,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) {
@@ -106,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;
@@ -269,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,
@@ -278,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;
@@ -303,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;
@@ -511,6 +548,7 @@ status_t MotionEvent::readFromParcel(Parcel* parcel) {
        return BAD_VALUE;
    }

    mId = parcel->readInt32();
    mDeviceId = parcel->readInt32();
    mSource = parcel->readUint32();
    mDisplayId = parcel->readInt32();
@@ -572,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);
@@ -641,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;
+25 −13
Original line number Diff line number Diff line
@@ -139,6 +139,8 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
        case InputMessage::Type::KEY: {
            // uint32_t seq
            msg->body.key.seq = body.key.seq;
            // int32_t eventId
            msg->body.key.eventId = body.key.eventId;
            // nsecs_t eventTime
            msg->body.key.eventTime = body.key.eventTime;
            // int32_t deviceId
@@ -168,6 +170,8 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
        case InputMessage::Type::MOTION: {
            // uint32_t seq
            msg->body.motion.seq = body.motion.seq;
            // int32_t eventId
            msg->body.motion.eventId = body.motion.eventId;
            // nsecs_t eventTime
            msg->body.motion.eventTime = body.motion.eventTime;
            // int32_t deviceId
@@ -234,6 +238,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
        }
        case InputMessage::Type::FOCUS: {
            msg->body.focus.seq = body.focus.seq;
            msg->body.focus.eventId = body.focus.eventId;
            msg->body.focus.hasFocus = body.focus.hasFocus;
            msg->body.focus.inTouchMode = body.focus.inTouchMode;
            break;
@@ -432,11 +437,12 @@ InputPublisher::InputPublisher(const sp<InputChannel>& channel) :
InputPublisher::~InputPublisher() {
}

status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t deviceId, int32_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) {
status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
                                         int32_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) {
    if (ATRACE_ENABLED()) {
        std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
                mChannel->getName().c_str(), keyCode);
@@ -458,6 +464,7 @@ status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t deviceId, int32_t
    InputMessage msg;
    msg.header.type = InputMessage::Type::KEY;
    msg.body.key.seq = seq;
    msg.body.key.eventId = eventId;
    msg.body.key.deviceId = deviceId;
    msg.body.key.source = source;
    msg.body.key.displayId = displayId;
@@ -474,7 +481,7 @@ status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t deviceId, int32_t
}

status_t InputPublisher::publishMotionEvent(
        uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId,
        uint32_t seq, int32_t eventId, int32_t deviceId, int32_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,
@@ -515,6 +522,7 @@ status_t InputPublisher::publishMotionEvent(
    InputMessage msg;
    msg.header.type = InputMessage::Type::MOTION;
    msg.body.motion.seq = seq;
    msg.body.motion.eventId = eventId;
    msg.body.motion.deviceId = deviceId;
    msg.body.motion.source = source;
    msg.body.motion.displayId = displayId;
@@ -545,7 +553,8 @@ status_t InputPublisher::publishMotionEvent(
    return mChannel->sendMessage(&msg);
}

status_t InputPublisher::publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode) {
status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus,
                                           bool inTouchMode) {
    if (ATRACE_ENABLED()) {
        std::string message =
                StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s, inTouchMode=%s)",
@@ -557,6 +566,7 @@ status_t InputPublisher::publishFocusEvent(uint32_t seq, bool hasFocus, bool inT
    InputMessage msg;
    msg.header.type = InputMessage::Type::FOCUS;
    msg.body.focus.seq = seq;
    msg.body.focus.eventId = eventId;
    msg.body.focus.hasFocus = hasFocus ? 1 : 0;
    msg.body.focus.inTouchMode = inTouchMode ? 1 : 0;
    return mChannel->sendMessage(&msg);
@@ -1142,14 +1152,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(msg->body.key.eventId, 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(msg->body.focus.eventId, msg->body.focus.hasFocus == 1,
                      msg->body.focus.inTouchMode == 1);
}

void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
@@ -1161,7 +1173,7 @@ 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,
    event->initialize(msg->body.motion.eventId, 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,
+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,
Loading