Return events from EventHub
Rather than filling some buffer, EventHub will now return events
explicitly. This will simplify the interface and avoid storing the
buffer when no events are being actively produced.
Bug: 211379801
Test: atest libinput_tests inputflinger_tests
Change-Id: Ic4ba7789f899b2de9fbe5a0abe54475b74eb5f82
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index 5351a51..5c24244 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -76,6 +76,8 @@
static constexpr int32_t FF_STRONG_MAGNITUDE_CHANNEL_IDX = 0;
static constexpr int32_t FF_WEAK_MAGNITUDE_CHANNEL_IDX = 1;
+static constexpr size_t EVENT_BUFFER_SIZE = 256;
+
// Mapping for input battery class node IDs lookup.
// https://www.kernel.org/doc/Documentation/power/power_supply_class.txt
static const std::unordered_map<std::string, InputBatteryClass> BATTERY_CLASSES =
@@ -1633,14 +1635,13 @@
return std::nullopt;
}
-size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
- ALOG_ASSERT(bufferSize >= 1);
-
+std::vector<RawEvent> EventHub::getEvents(int timeoutMillis) {
std::scoped_lock _l(mLock);
+ constexpr size_t bufferSize = EVENT_BUFFER_SIZE;
struct input_event readBuffer[bufferSize];
- RawEvent* event = buffer;
+ std::vector<RawEvent> events;
size_t capacity = bufferSize;
bool awoken = false;
for (;;) {
@@ -1661,15 +1662,17 @@
for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) {
std::unique_ptr<Device> device = std::move(*it);
ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str());
- event->when = now;
- event->deviceId = (device->id == mBuiltInKeyboardId)
+ const int32_t deviceId = (device->id == mBuiltInKeyboardId)
? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID
: device->id;
- event->type = DEVICE_REMOVED;
- event += 1;
+ events.push_back({
+ .when = now,
+ .deviceId = deviceId,
+ .type = DEVICE_REMOVED,
+ });
it = mClosingDevices.erase(it);
mNeedToSendFinishedDeviceScan = true;
- if (--capacity == 0) {
+ if (events.size() == capacity) {
break;
}
}
@@ -1684,10 +1687,12 @@
std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());
mOpeningDevices.pop_back();
ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str());
- event->when = now;
- event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
- event->type = DEVICE_ADDED;
- event += 1;
+ const int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
+ events.push_back({
+ .when = now,
+ .deviceId = deviceId,
+ .type = DEVICE_ADDED,
+ });
// Try to find a matching video device by comparing device names
for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
@@ -1705,17 +1710,18 @@
ALOGW("Device id %d exists, replaced.", device->id);
}
mNeedToSendFinishedDeviceScan = true;
- if (--capacity == 0) {
+ if (events.size() == capacity) {
break;
}
}
if (mNeedToSendFinishedDeviceScan) {
mNeedToSendFinishedDeviceScan = false;
- event->when = now;
- event->type = FINISHED_DEVICE_SCAN;
- event += 1;
- if (--capacity == 0) {
+ events.push_back({
+ .when = now,
+ .type = FINISHED_DEVICE_SCAN,
+ });
+ if (events.size() == capacity) {
break;
}
}
@@ -1794,21 +1800,21 @@
} else if ((readSize % sizeof(struct input_event)) != 0) {
ALOGE("could not get event (wrong size: %d)", readSize);
} else {
- int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
+ const int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
- size_t count = size_t(readSize) / sizeof(struct input_event);
+ const size_t count = size_t(readSize) / sizeof(struct input_event);
for (size_t i = 0; i < count; i++) {
struct input_event& iev = readBuffer[i];
- event->when = processEventTimestamp(iev);
- event->readTime = systemTime(SYSTEM_TIME_MONOTONIC);
- event->deviceId = deviceId;
- event->type = iev.type;
- event->code = iev.code;
- event->value = iev.value;
- event += 1;
- capacity -= 1;
+ events.push_back({
+ .when = processEventTimestamp(iev),
+ .readTime = systemTime(SYSTEM_TIME_MONOTONIC),
+ .deviceId = deviceId,
+ .type = iev.type,
+ .code = iev.code,
+ .value = iev.value,
+ });
}
- if (capacity == 0) {
+ if (events.size() >= capacity) {
// The result buffer is full. Reset the pending event index
// so we will try to read the device again on the next iteration.
mPendingEventIndex -= 1;
@@ -1844,7 +1850,7 @@
}
// Return now if we have collected any events or if we were explicitly awoken.
- if (event != buffer || awoken) {
+ if (!events.empty() || awoken) {
break;
}
@@ -1890,7 +1896,7 @@
}
// All done, return the number of events we read.
- return event - buffer;
+ return events;
}
std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) {