reader线程做三件事
1、获得事件
eventhub.cpp文件事件用Rawevent结构体表示
struct RawEvent { nsecs_t when; int32_t deviceId; int32_t type; int32_t code; int32_t value; };2、简单处理
根据RawEvent的type来处理
1、Add Device
2、Remove Device
3、真正的输入事件
在得到事件之后,调用
size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); { // acquire lock AutoMutex _l(mLock); mReaderIsAliveCondition.broadcast();
if (count) { processEventsLocked(mEventBuffer, count); }
void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { for (const RawEvent* rawEvent = rawEvents; count;) { int32_t type = rawEvent->type; size_t batchSize = 1; if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { int32_t deviceId = rawEvent->deviceId; while (batchSize < count) { if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT || rawEvent[batchSize].deviceId != deviceId) { break; } batchSize += 1; } #if DEBUG_RAW_EVENTS ALOGD("BatchSize: %d Count: %d", batchSize, count); #endif 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; } }
对于每一个rawevent事件做处理
对于添加设备
void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) { ssize_t deviceIndex = mDevices.indexOfKey(deviceId); if (deviceIndex >= 0) { ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); return; } InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId); uint32_t classes = mEventHub->getDeviceClasses(deviceId); int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId); InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes); device->configure(when, &mConfig, 0); device->reset(when); if (device->isIgnored()) { ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, identifier.name.string()); } else { ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, identifier.name.string(), device->getSources()); } mDevices.add(deviceId, device); bumpGenerationLocked(); }
在EventHub.cpp层创建了Device结构体。
在inputreader.cpp层又创建了inputdevice结构体
InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes) { InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(), controllerNumber, identifier, classes); // External devices. if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { device->setExternal(true); } // Switch-like devices. if (classes & INPUT_DEVICE_CLASS_SWITCH) { device->addMapper(new SwitchInputMapper(device)); }
这个函数根据类别添加Mapper
最后在getEvent之后调用
processEventsLocked
这个函数里面调用
addDeviceLocked
它调用
createDeviceLocked
然后
KeyedVector<int32_t, InputDevice*> mDevices;
mDevices.add(deviceId, device);
为什么在eventhub添加之后
void EventHub::addDeviceLocked(Device* device) { mDevices.add(device->id, device);又在inputreader又调用添加。可以通过inputreader这层的id访问eventhub的mDevices的结构体中的东西。
eventhub层读取事件在上一层inputreader处理事件。通过
Vector<InputMapper*> mMappers;
用这个对象处理事件
对于其他输入事件会调用这个函数
processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
device->process(rawEvents, count);
for (size_t i = 0; i < numMappers; i++) { InputMapper* mapper = mMappers[i]; mapper->process(rawEvent); }
对于键盘是这个处理函数:
processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); getListener()->notifyKey(&args);
构造args。谁来监听这个线程?Dispatcher线程!传给这个线程。
总结: