一:概述
由Android输入系统之启动篇(AndroidV)-优快云博客可知,EventHub是InputReader读取设备input事件的中间件,它采用INotify + epoll机制实现监听目录/dev/input下的所有设备节点,并将事件中指定的时间 + 单调时钟时间 + deviceId + input_event结构体转换成RawEvent结构体返回给InputReader。下面来看下EventHub是如何获取input事件及如何保存设备信息的。
二:获取input事件
EventHub通过epoll获取到有事件发生时,会保存在mPendingEventItems中。InputReader读取事件时,识别到有事件发生,会从设备中读取事件放入readBuffer中(readBuffer以input_event结构体保存事件),遍历所有事件,每次从readBuffer中获取单个事件,把input_event加上时间、deviceId等封装成RawEvent,遍历完毕后返回。
//frameworks/native/services/inputflinger/reader/EventHub.cpp
std::vector<RawEvent> EventHub::getEvents(int timeoutMillis) {
std::scoped_lock _l(mLock);
...
for (;;) {
...
if (mNeedToScanDevices) {
//搜索设备
scanDevicesLocked();
...
}
while (!mOpeningDevices.empty()) {
...
events.push_back({
.when = now,
.deviceId = deviceId,
.type = DEVICE_ADDED,//添加设备
});
...
}
if (mNeedToSendFinishedDeviceScan) {
...
events.push_back({
.when = now,
.type = FINISHED_DEVICE_SCAN,//设备检索结束
});
...
}
// Grab the next input event.
bool deviceChanged = false;

最低0.47元/天 解锁文章
1365

被折叠的 条评论
为什么被折叠?



