input事件处理流程(三)

inputreader对input事件的处理

上面我们可以知道读取出了事件之后,需要进行处理,那么处理的函数就是

386        if (count) {
387            processEventsLocked(mEventBuffer, count);
388        }

422void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
423    for (const RawEvent* rawEvent = rawEvents; count;) {
424        int32_t type = rawEvent->type;
425        size_t batchSize = 1;
426        if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
427            int32_t deviceId = rawEvent->deviceId;
428            while (batchSize < count) {
429                if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
430                        || rawEvent[batchSize].deviceId != deviceId) {
431                    break;
432                }
433                batchSize += 1;
434            }
435#if DEBUG_RAW_EVENTS
436            ALOGD("BatchSize: %d Count: %d", batchSize, count);
437#endif
    //我们其实只需要关注正常的事件而不用关注device添加和remove的事情
438            processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
439        } else {
440            switch (rawEvent->type) {
441            case EventHubInterface::DEVICE_ADDED:
442                addDeviceLocked(rawEvent->when, rawEvent->deviceId);
443                break;
444            case EventHubInterface::DEVICE_REMOVED:
445                removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
446                break;
447            case EventHubInterface::FINISHED_DEVICE_SCAN:
448                handleConfigurationChangedLocked(rawEvent->when);
449                break;
450            default:
451                ALOG_ASSERT(false); // can't happen
452                break;
453            }
454        }
455        count -= batchSize;
456        rawEvent += batchSize;
457    }
458}

所以可以看到,事件被分成了两种,目前我们只需要看正常的时间,而不需要关注device的添加删除等时间。

594void InputReader::processEventsForDeviceLocked(int32_t deviceId,
595        const RawEvent* rawEvents, size_t count) {
596    ssize_t deviceIndex = mDevices.indexOfKey(deviceId);//获取到对应的devices
597    if (deviceIndex < 0) {
598        ALOGW("Discarding event for unknown deviceId %d.", deviceId);
599        return;
600    }
601
602    InputDevice* device = mDevices.valueAt(deviceIndex);
603    if (device->isIgnored()) {
604        //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
605        return;
606    }
607
608    device->process(rawEvents, count);//调用device的process
609}

那么调用到了process方法,process的话是由device发起的,device是在之前的device添加事件中生成的,所以可以在源码中查看相应的device是如何添加的

1174void InputDevice::process(const RawEvent* rawEvents, size_t count) {
...
1204            for (size_t i = 0; i < numMappers; i++) {
1205                InputMapper* mapper = mMappers[i];
1206                mapper->process(rawEvent);
1207            }
1208        }
1209    }
1210}

查看了之后也就会知道mapper是如何来的了,就是通过对应的device和对应的事件联系在一起,然后调用mapper的process方法,先看下mapper是如何添加的:

519InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
520        const InputDeviceIdentifier& identifier, uint32_t classes) {
521    InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
522            controllerNumber, identifier, classes);
523
...
544    // Vibrator-like devices.
545    if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
546        device->addMapper(new VibratorInputMapper(device));
547    }

我们继续来看一下对应的process方法:

2349void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2350    switch (rawEvent->type) {
...
2356        if (isKeyboardOrGamepadKey(scanCode)) {
2357            processKey(rawEvent->when, rawEvent->value != 0, scanCode, usageCode);
2358        }
...
2360    }
2411void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t scanCode,
2412        int32_t usageCode) {
...
2492    NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
2493            down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2494            AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState, downTime);
2495    getListener()->notifyKey(&args);
2496}

所以首先是组合成了一个notifykeyargs数据结构,同时调用了listener的notifykey方法。

155void QueuedInputListener::notifyKey(const NotifyKeyArgs* args) {
156    mArgsQueue.push(new NotifyKeyArgs(*args));
157}

也就是将事件放在了queue中而已。

这些做完以后,inputreader调用了flush方法,将事件传递给了dispatcher。

171void QueuedInputListener::flush() {
172    size_t count = mArgsQueue.size();
173    for (size_t i = 0; i < count; i++) {
174        NotifyArgs* args = mArgsQueue[i];
175        args->notify(mInnerListener);//通过notifyargs来notify
176        delete args;
177    }
178    mArgsQueue.clear();
179}
62void NotifyKeyArgs::notify(const sp<InputListenerInterface>& listener) const {
63    listener->notifyKey(this);
64}

到这里呢,整个的input事件的获取就已经完成了,下面就是input事件的分发。

UML时序图

input事件的获取和解析