InputManagerService分发输入事件给应用程序(上)
1.简介
在InputManagerService服务初始化时,会在Native层创建两个线程:InputDispatcherThread和InputReaderThread。InputReader线程负责读取输入事件,并把输入事件传递给InputDispatcher线程,然后由InputDispatcher将输入事件分发给处于激活的窗口。
接下来将从源码的角度来分析输入事件的一个分发过程。
2.源码分析
在InputReaderThread线程中,将会循环执行其threadLoop()方法。
2.1 InputReaderThread.threadLoop()
bool InputReaderThread::threadLoop() {
mReader->loopOnce();//调用InputReader的loopOnce()方法,见2.2
return true;
}
当threadLoop()方法返回true时,代表将循环调用loopOnce()方法。当threadLoop()方法返回false时,代表退出循环。
2.2 InputReader.loopOnce()
void InputReader::loopOnce() {
int32_t oldGeneration;
int32_t timeoutMillis;
bool inputDevicesChanged = false;
Vector<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);// 1.从EventHub中获取输入事件,见2.3
{ // acquire lock
AutoMutex _l(mLock);
mReaderIsAliveCondition.broadcast();
if (count) {
processEventsLocked(mEventBuffer, count);//2.如果获取到了输入事件,则处理该输入事件,见2.4
}
if (mNextTimeout != LLONG_MAX) {
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
if (now >= mNextTimeout) {
mNextTimeout = LLONG_MAX;
timeoutExpiredLocked(now);
}
}
if (oldGeneration != mGeneration) {
inputDevicesChanged = true;
getInputDevicesLocked(inputDevices);
}
} // release lock
if (inputDevicesChanged) {// 输入设备发生了变化
mPolicy->notifyInputDevicesChanged(inputDevices);
}
mQueuedListener->flush();// 3.发送输入事件到InputDispatcher,见2.12
}
在loopOnce()方法中,主要做了三件事情:
- 通过EventHub的getEvents()方法获取输入事件,该方法将返回准备好的输入事件;
- 调用processEventsLocked()方法来处理输入事件;
- 通过mQueuedListener的flush()方法将输入事件发送到InputDispatcher;
接下来将分别围绕这三个过程来讲解。
2.3 EventHub.getEvents()
size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
ALOG_ASSERT(bufferSize >= 1);
AutoMutex _l(mLock);
struct input_event readBuffer[bufferSize];
RawEvent* event = buffer;//原始事件
size_t capacity = bufferSize;//容量大小为256
bool awoken = false;
for (;;) {
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);// 当前时间
// 是否需要重新打开设备
if (mNeedToReopenDevices) {
mNeedToReopenDevices = false;
ALOGI("Reopening all input devices due to a configuration change.");
closeAllDevicesLocked();
mNeedToScanDevices = true;
break;
}
// 记录最近被删除的设备
while (mClosingDevices) {
Device* device = mClosingDevices;
mClosingDevices = device->next;
event->when = now;
event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
event->type = DEVICE_REMOVED;//事件类型为设备删除
event += 1;
delete device;
mNeedToSendFinishedDeviceScan = true;
if (--capacity == 0) {
break;
}
}
// 需要扫描设备
if (mNeedToScanDevices) {
mNeedToScanDevices = false;
scanDev