我们在上一遍看了输入子系统的启动流程,最后,我们看到运行了两个线程:
mDispatcherThread->run mReaderThread->run.
单从字面上来理解,一个是分发线程,另一个是读取线程.没有读取哪来分发?接下来我们先看一下读取线程是干了什么.
InputReader.cpp
bool InputReaderThread::threadLoop() {
mReader->loopOnce();
return true;
}
这个函数很简单,直接调用了mReader->loopOnce().
接下来,我们逐句分析loopOnce的实现.当然,本章只讲设备注册部分.
void InputReader::loopOnce() {
int32_t oldGeneration;
int32_t timeoutMillis;
bool inputDevicesChanged = false;
Vector<InputDeviceInfo> inputDevices;
这里可以看到InputDeviceInfo类型的矢量 inputDevices 这个是输入子系统的重点,即然是输入子系统,就一定要有输入设备嘛,那就是它了.
{ // acquire lock
AutoMutex _l(mLock);
oldGeneration = mGeneration;
timeoutMillis = -1;
uint32_t changes = mConfigurationChangesToRefresh;
if (changes) {
mConfigurationChangesToRefresh = 0;
timeoutMillis = 0;
refreshConfigurationLocked(changes);
} else if (mNextTimeout != LLONG_MAX) {
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
}
} // release lock
这一部分是设备参数的刷新,我们以后来讲,在此带过.
size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
接下来,我们看到了重点的一行.getEvents,就是获取事件嘛.返回事件的个数,待后续逐个处理.timoutMillis就是超时返回撒,mEventBuffer 是事件数组的指针,用于收集事件,