一:概述
由前文Android输入系统之启动篇(AndroidV)-CSDN博客可知,InputReader线程用于读取Input事件,是input系统的起始点,下面来看下InputReader线程是如何工作的。
二:InputReader的启动
InputReader线程启动后,先从EventHub中读取input事件,具体事件的读取流程可以参考Android输入系统之EventHub(AndroidV)_eventhub: no input device configuration file found-CSDN博客,这里就不多介绍了。读取到有input事件发生后,会把事件校准、包装然后发送给InputDispatcher。
//frameworks/native/services/inputflinger/reader/InputReader.cpp
void InputReader::loopOnce() {
...
//从EventHub中读取事件
std::vector<RawEvent> events = mEventHub->getEvents(timeoutMillis);
{ // acquire lock
std::scoped_lock _l(mLock);
...
//处理事件,把将要分发的事件包装成NotifyMotionArgs并赋值给mPendingArgs
if (!events.empty()) {
mPendingArgs += processEventsLocked(events.data(), events.size());
}
...
//把mPendingArgs赋值给notifyArgs
std::swap(notifyArgs, mPendingArgs);
...
} // release lock
...
//发送事件到InputDispatcher
for (const NotifyArgs& args : notifyArgs) {
mNextListener.notify(args);
}
...
//输入设备发生改变
if (inputDevicesChanged) {
mPolicy->notifyInputDevicesChanged(inputDevices);
}
...
}
三:input事件的处理
3.1 InputReader.processEventsLocked
根据事件的类型(设备添加、设备移除、设备搜索完成、input),执行不同的逻辑
//frameworks/native/services/inputflinger/reader/InputReader.cpp
std::list<NotifyArgs> InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
std::list<NotifyArgs> out;
for (const RawEvent* rawEvent = rawEvents; count;) {
int32_t type = rawEvent->type;
size_t batchSize = 1;
if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
...
//处理输入事件
out += processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
} else {
switch (rawEvent->type) {
case EventHubInterface::DEVICE_ADDED://设备添加
addDeviceLocked(rawEvent->when, rawEvent->deviceId);
break;
case EventHubInterface::DEVICE_REMOVED://设备移除
removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
break;
case EventHubInterface::FINISHED_DEVICE_SCAN://设备搜索完成
handleConfigurationChangedLocked(rawEvent->when);
break;
default:
ALOG_ASSERT(false); // can't happen
break;
}
}
count -= batchSize;
rawEvent += batchSize;
}
return out;
}
3.2 InputReader.addDeviceLocked
input事件触发的前提必然是设备已经正常添加,所以在梳理input事件处理流程之前,先看下设备添加事件的处理流程。大致流程如下
- 创建InputDevice对象
- 根据设备的类型创建对应的InputMapper
- 把InputDevice对象添加到mDevices中
//frameworks/native/services/inputflinger/reader/InputReader.cpp
void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
...
//创建InputDevice对象
std::shared_ptr<InputDevice> device = createDeviceLocked(when, eventHubId, identifier);
...
//把InputDevice对象添加到mDevices中
mDevices.emplace(eventHubId, device);
//在mDeviceToEventHubIdsMap通过device查找看是否已经保存该设备
const auto mapIt = mDeviceToEventHubIdsMap.find(device);
if (mapIt == mDeviceToEventHubIdsMap.end()) {//没有保存
std::vector<int32_t> ids = {eventHubId};
//把eventHubId和device保存到mDeviceToEventHubIdsMap中
mDeviceToEventHubIdsMap.emplace(device, ids);
} else {
mapIt->second.push_back(eventHubId);
}
...
}
//frameworks/native/services/inputflinger/reader/InputReader.cpp
std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier) {
...
if (deviceIt != mDevices.end()) {
device = deviceIt->second;
} else {
...
//创建InputDevice对象
device = std::make_shared<InputDevice>

最低0.47元/天 解锁文章
568

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



