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

Commit 7cdf8ef1 authored by Antonio Kantek's avatar Antonio Kantek
Browse files

TouchEvent (1/n): Adding TouchModeEvent to InputChannel

This CL detaches the touch mode state update from focus update. It does
that by introducing a new internal event (TouchModeEvent). This CL also
adds this event to InputChannel and related processing logic to
InputPublisher and InputConsumer. InputDispatcher will process two
different events now: FocusEvent when gaining/losing focus and
TouchModeEvent when entering/leaving touch mode.

Test: atest libinput_tests
Bug: 193718270
Change-Id: Ie4e5b6e8e798f12d7203127b4559fa40d38788de
parent 81fc7380
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -169,6 +169,9 @@ enum {


    /** Drag event */
    /** Drag event */
    AINPUT_EVENT_TYPE_DRAG = 5,
    AINPUT_EVENT_TYPE_DRAG = 5,

    /** TouchMode event */
    AINPUT_EVENT_TYPE_TOUCH_MODE = 6,
};
};


/**
/**
+24 −0
Original line number Original line Diff line number Diff line
@@ -883,6 +883,25 @@ protected:
    float mX, mY;
    float mX, mY;
};
};


/*
 * Touch mode events.
 */
class TouchModeEvent : public InputEvent {
public:
    virtual ~TouchModeEvent() {}

    virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_TOUCH_MODE; }

    inline bool isInTouchMode() const { return mIsInTouchMode; }

    void initialize(int32_t id, bool isInTouchMode);

    void initialize(const TouchModeEvent& from);

protected:
    bool mIsInTouchMode;
};

/**
/**
 * Base class for verified events.
 * Base class for verified events.
 * Do not create a VerifiedInputEvent explicitly.
 * Do not create a VerifiedInputEvent explicitly.
@@ -947,6 +966,7 @@ public:
    virtual FocusEvent* createFocusEvent() = 0;
    virtual FocusEvent* createFocusEvent() = 0;
    virtual CaptureEvent* createCaptureEvent() = 0;
    virtual CaptureEvent* createCaptureEvent() = 0;
    virtual DragEvent* createDragEvent() = 0;
    virtual DragEvent* createDragEvent() = 0;
    virtual TouchModeEvent* createTouchModeEvent() = 0;
};
};


/*
/*
@@ -963,6 +983,7 @@ public:
    virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
    virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
    virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
    virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
    virtual DragEvent* createDragEvent() override { return &mDragEvent; }
    virtual DragEvent* createDragEvent() override { return &mDragEvent; }
    virtual TouchModeEvent* createTouchModeEvent() override { return &mTouchModeEvent; }


private:
private:
    KeyEvent mKeyEvent;
    KeyEvent mKeyEvent;
@@ -970,6 +991,7 @@ private:
    FocusEvent mFocusEvent;
    FocusEvent mFocusEvent;
    CaptureEvent mCaptureEvent;
    CaptureEvent mCaptureEvent;
    DragEvent mDragEvent;
    DragEvent mDragEvent;
    TouchModeEvent mTouchModeEvent;
};
};


/*
/*
@@ -985,6 +1007,7 @@ public:
    virtual FocusEvent* createFocusEvent() override;
    virtual FocusEvent* createFocusEvent() override;
    virtual CaptureEvent* createCaptureEvent() override;
    virtual CaptureEvent* createCaptureEvent() override;
    virtual DragEvent* createDragEvent() override;
    virtual DragEvent* createDragEvent() override;
    virtual TouchModeEvent* createTouchModeEvent() override;


    void recycle(InputEvent* event);
    void recycle(InputEvent* event);


@@ -996,6 +1019,7 @@ private:
    std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
    std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
    std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
    std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
    std::queue<std::unique_ptr<DragEvent>> mDragEventPool;
    std::queue<std::unique_ptr<DragEvent>> mDragEventPool;
    std::queue<std::unique_ptr<TouchModeEvent>> mTouchModeEventPool;
};
};


} // namespace android
} // namespace android
+20 −0
Original line number Original line Diff line number Diff line
@@ -72,6 +72,7 @@ struct InputMessage {
        CAPTURE,
        CAPTURE,
        DRAG,
        DRAG,
        TIMELINE,
        TIMELINE,
        TOUCH_MODE,
    };
    };


    struct Header {
    struct Header {
@@ -206,6 +207,15 @@ struct InputMessage {


            inline size_t size() const { return sizeof(Timeline); }
            inline size_t size() const { return sizeof(Timeline); }
        } timeline;
        } timeline;

        struct TouchMode {
            int32_t eventId;
            // The following 2 fields take up 4 bytes total
            bool isInTouchMode;
            uint8_t empty[3];

            inline size_t size() const { return sizeof(TouchMode); }
        } touchMode;
    } __attribute__((aligned(8))) body;
    } __attribute__((aligned(8))) body;


    bool isValid(size_t actualSize) const;
    bool isValid(size_t actualSize) const;
@@ -387,6 +397,15 @@ public:
     */
     */
    status_t publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, bool isExiting);
    status_t publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, bool isExiting);


    /* Publishes a touch mode event to the input channel.
     *
     * Returns OK on success.
     * Returns WOULD_BLOCK if the channel is full.
     * Returns DEAD_OBJECT if the channel's peer has been closed.
     * Other errors probably indicate that the channel is broken.
     */
    status_t publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode);

    struct Finished {
    struct Finished {
        uint32_t seq;
        uint32_t seq;
        bool handled;
        bool handled;
@@ -657,6 +676,7 @@ private:
    static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
    static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
    static void initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg);
    static void initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg);
    static void initializeDragEvent(DragEvent* event, const InputMessage* msg);
    static void initializeDragEvent(DragEvent* event, const InputMessage* msg);
    static void initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg);
    static void addSample(MotionEvent* event, const InputMessage* msg);
    static void addSample(MotionEvent* event, const InputMessage* msg);
    static bool canAddSample(const Batch& batch, const InputMessage* msg);
    static bool canAddSample(const Batch& batch, const InputMessage* msg);
    static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
    static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
+25 −0
Original line number Original line Diff line number Diff line
@@ -163,6 +163,9 @@ const char* inputEventTypeToString(int32_t type) {
        case AINPUT_EVENT_TYPE_DRAG: {
        case AINPUT_EVENT_TYPE_DRAG: {
            return "DRAG";
            return "DRAG";
        }
        }
        case AINPUT_EVENT_TYPE_TOUCH_MODE: {
            return "TOUCH_MODE";
        }
    }
    }
    return "UNKNOWN";
    return "UNKNOWN";
}
}
@@ -882,6 +885,19 @@ void DragEvent::initialize(const DragEvent& from) {
    mY = from.mY;
    mY = from.mY;
}
}


// --- TouchModeEvent ---

void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
    InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
                           ADISPLAY_ID_NONE, INVALID_HMAC);
    mIsInTouchMode = isInTouchMode;
}

void TouchModeEvent::initialize(const TouchModeEvent& from) {
    InputEvent::initialize(from);
    mIsInTouchMode = from.mIsInTouchMode;
}

// --- PooledInputEventFactory ---
// --- PooledInputEventFactory ---


PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
@@ -936,6 +952,15 @@ DragEvent* PooledInputEventFactory::createDragEvent() {
    return event;
    return event;
}
}


TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
    if (mTouchModeEventPool.empty()) {
        return new TouchModeEvent();
    }
    TouchModeEvent* event = mTouchModeEventPool.front().release();
    mTouchModeEventPool.pop();
    return event;
}

void PooledInputEventFactory::recycle(InputEvent* event) {
void PooledInputEventFactory::recycle(InputEvent* event) {
    switch (event->getType()) {
    switch (event->getType()) {
    case AINPUT_EVENT_TYPE_KEY:
    case AINPUT_EVENT_TYPE_KEY:
+42 −0
Original line number Original line Diff line number Diff line
@@ -116,6 +116,7 @@ bool InputMessage::isValid(size_t actualSize) const {
        case Type::FOCUS:
        case Type::FOCUS:
        case Type::CAPTURE:
        case Type::CAPTURE:
        case Type::DRAG:
        case Type::DRAG:
        case Type::TOUCH_MODE:
            return true;
            return true;
        case Type::TIMELINE: {
        case Type::TIMELINE: {
            const nsecs_t gpuCompletedTime =
            const nsecs_t gpuCompletedTime =
@@ -151,6 +152,8 @@ size_t InputMessage::size() const {
            return sizeof(Header) + body.drag.size();
            return sizeof(Header) + body.drag.size();
        case Type::TIMELINE:
        case Type::TIMELINE:
            return sizeof(Header) + body.timeline.size();
            return sizeof(Header) + body.timeline.size();
        case Type::TOUCH_MODE:
            return sizeof(Header) + body.touchMode.size();
    }
    }
    return sizeof(Header);
    return sizeof(Header);
}
}
@@ -291,6 +294,10 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
            msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
            break;
            break;
        }
        }
        case InputMessage::Type::TOUCH_MODE: {
            msg->body.touchMode.eventId = body.touchMode.eventId;
            msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
        }
    }
    }
}
}


@@ -661,6 +668,22 @@ status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x
    return mChannel->sendMessage(&msg);
    return mChannel->sendMessage(&msg);
}
}


status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
    if (ATRACE_ENABLED()) {
        std::string message =
                StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
                             mChannel->getName().c_str(), toString(isInTouchMode));
        ATRACE_NAME(message.c_str());
    }

    InputMessage msg;
    msg.header.type = InputMessage::Type::TOUCH_MODE;
    msg.header.seq = seq;
    msg.body.touchMode.eventId = eventId;
    msg.body.touchMode.isInTouchMode = isInTouchMode;
    return mChannel->sendMessage(&msg);
}

android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
    if (DEBUG_TRANSPORT_ACTIONS) {
    if (DEBUG_TRANSPORT_ACTIONS) {
        ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
        ALOGD("channel '%s' publisher ~ %s", mChannel->getName().c_str(), __func__);
@@ -862,6 +885,16 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum
                *outEvent = dragEvent;
                *outEvent = dragEvent;
                break;
                break;
            }
            }

            case InputMessage::Type::TOUCH_MODE: {
                TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
                if (!touchModeEvent) return NO_MEMORY;

                initializeTouchModeEvent(touchModeEvent, &mMsg);
                *outSeq = mMsg.header.seq;
                *outEvent = touchModeEvent;
                break;
            }
        }
        }
    }
    }
    return OK;
    return OK;
@@ -1366,6 +1399,10 @@ void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage
                      pointerProperties, pointerCoords);
                      pointerProperties, pointerCoords);
}
}


void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
    event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
}

void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
    uint32_t pointerCount = msg->body.motion.pointerCount;
    uint32_t pointerCount = msg->body.motion.pointerCount;
    PointerCoords pointerCoords[pointerCount];
    PointerCoords pointerCoords[pointerCount];
@@ -1472,6 +1509,11 @@ std::string InputConsumer::dump() const {
                                                       presentTime);
                                                       presentTime);
                    break;
                    break;
                }
                }
                case InputMessage::Type::TOUCH_MODE: {
                    out += android::base::StringPrintf("isInTouchMode=%s",
                                                       toString(msg.body.touchMode.isInTouchMode));
                    break;
                }
            }
            }
            out += "\n";
            out += "\n";
        }
        }
Loading