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

Commit 4bc561ca authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Look up devices by fd

Instead of storing deviceIndex inside epoll_data_t in the epoll_event,
store the file descriptors instead. Since we already have the file
descriptors for wake pipe and inotify, we can avoid storing magic
numbers. More importantly, this allows us to add video device
monitoring. For video devices, the old approach would no longer work,
because the same device index would apply to both video device file
descriptor and the input device file descriptor.
By storing the file descriptor, we can differentiate between the two
portions of the same EventHub::Device.

Test: atest inputflinger_tests libinput_tests
Test: touch interaction with the booted phone
Bug: 111480215
Change-Id: Iadb95f2b14499d600167c89f86fde5db82608af0
parent 6bdb3cac
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -193,8 +193,6 @@ bool EventHub::Device::hasValidFd() {

// --- EventHub ---

const uint32_t EventHub::EPOLL_ID_INOTIFY;
const uint32_t EventHub::EPOLL_ID_WAKE;
const int EventHub::EPOLL_SIZE_HINT;
const int EventHub::EPOLL_MAX_EVENTS;

@@ -217,7 +215,7 @@ EventHub::EventHub(void) :
    struct epoll_event eventItem;
    memset(&eventItem, 0, sizeof(eventItem));
    eventItem.events = EPOLLIN;
    eventItem.data.u32 = EPOLL_ID_INOTIFY;
    eventItem.data.fd = mINotifyFd;
    result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance.  errno=%d", errno);

@@ -236,7 +234,7 @@ EventHub::EventHub(void) :
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking.  errno=%d",
            errno);

    eventItem.data.u32 = EPOLL_ID_WAKE;
    eventItem.data.fd = mWakeReadPipeFd;
    result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance.  errno=%d",
            errno);
@@ -735,6 +733,16 @@ EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const
    return nullptr;
}

EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const {
    for (size_t i = 0; i < mDevices.size(); i++) {
        Device* device = mDevices.valueAt(i);
        if (device->fd == fd) {
            return device;
        }
    }
    return nullptr;
}

size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
    ALOG_ASSERT(bufferSize >= 1);

@@ -811,7 +819,7 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
        bool deviceChanged = false;
        while (mPendingEventIndex < mPendingEventCount) {
            const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
            if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
            if (eventItem.data.fd == mINotifyFd) {
                if (eventItem.events & EPOLLIN) {
                    mPendingINotify = true;
                } else {
@@ -820,7 +828,7 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
                continue;
            }

            if (eventItem.data.u32 == EPOLL_ID_WAKE) {
            if (eventItem.data.fd == mWakeReadPipeFd) {
                if (eventItem.events & EPOLLIN) {
                    ALOGV("awoken after wake()");
                    awoken = true;
@@ -836,14 +844,13 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
                continue;
            }

            ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
            if (deviceIndex < 0) {
                ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
                        eventItem.events, eventItem.data.u32);
            Device* device = getDeviceByFdLocked(eventItem.data.fd);
            if (device == nullptr) {
                ALOGW("Received unexpected epoll event 0x%08x for unknown device fd %d.",
                        eventItem.events, eventItem.data.fd);
                continue;
            }

            Device* device = mDevices.valueAt(deviceIndex);
            if (eventItem.events & EPOLLIN) {
                int32_t readSize = read(device->fd, readBuffer,
                        sizeof(struct input_event) * capacity);
@@ -1089,7 +1096,7 @@ status_t EventHub::registerDeviceForEpollLocked(Device* device) {
    if (mUsingEpollWakeup) {
        eventItem.events |= EPOLLWAKEUP;
    }
    eventItem.data.u32 = device->id;
    eventItem.data.fd = device->fd;
    if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, device->fd, &eventItem)) {
        ALOGE("Could not add device fd to epoll instance.  errno=%d", errno);
        return -errno;
+1 −4
Original line number Diff line number Diff line
@@ -418,6 +418,7 @@ private:
    Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
    Device* getDeviceLocked(int32_t deviceId) const;
    Device* getDeviceByPathLocked(const char* devicePath) const;
    Device* getDeviceByFdLocked(int fd) const;

    bool hasKeycodeLocked(Device* device, int keycode) const;

@@ -466,10 +467,6 @@ private:
    int mWakeReadPipeFd;
    int mWakeWritePipeFd;

    // Ids used for epoll notifications not associated with devices.
    static const uint32_t EPOLL_ID_INOTIFY = 0x80000001;
    static const uint32_t EPOLL_ID_WAKE = 0x80000002;

    // Epoll FD list size hint.
    static const int EPOLL_SIZE_HINT = 8;