InputManagerService分发输入事件给应用程序(上)

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()方法中,主要做了三件事情:

  1. 通过EventHub的getEvents()方法获取输入事件,该方法将返回准备好的输入事件;
  2. 调用processEventsLocked()方法来处理输入事件;
  3. 通过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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 系统的键盘事件 是由InputManagerService 来监控的, 而InputManagerService 是由 SystemServer 来启动的 创建了一个类的对象 WindowInputEvent Receiver InputEventRe ceiver的构造 函数 WindowInputEventReceiver extends InputEventReceiver nativeInit InputEventReceiver.cpp nativeInit android_view_InputE ventReceiver.cpp NativeInputE ventReceiver 的构造函数 LooperCallback: :handleEvent NativeInputEve ntReceiver::ha ndleEvent Looper::p ollInner Looper::p ollOnce NativeInputEventRecei ver : public LooperCallback 有方法 NativeInputEventReceiver:: handleEvent 没有键盘事件发生, InputManager 中的InputReader 正在睡眠等待键盘事件 的发生,而InputManager 中的 InputDispatcher 正在等待InputReader 从睡眠中醒过来并且唤醒它,而应用程 序也正在消息循环中等待InputDispatcher 从睡眠中醒过来并且唤醒它。 分析InputManager 分发键盘消息给应用程序的过程 InputReader .pollOnce EventHub.g etEvent InputReader::pr ocessEventsLock ed InputReader::pr ocessEventsForD eviceLocked InputDevic e::process 没有键盘事件发生, InputReaderThread 线程就会睡眠在EventHub.getEvent 函数 上,当键盘事件发生后,就会把这个事件封 装成一个RawEvent 对象,然后返回到 pollOnce函数中,执行此函数 有键盘事件 InputReader.cpp InputMapper::process( 这里 是KeyboardInputMapper) InputReader::createDeviceLock ed中根据类型创建的,负责处理轨迹 球事件的TrackballInputMapper 对 象以及负责处理触摸屏事件的 TouchInputMapper 对象等 KeyboardInputMap per::processKey 这个函数首先对对按 键作一些处理,例如 需要根据当时屏幕的 方向来调整键盘码 InputDispatcher::notify Key( 继承自 InputListenerInterface) KeyboardInputMappge r函数通知 InputDispatcher ,有 键盘事件发生了 1. 调用 validateKeyEvent 函数来验 证action参数是否正确 2. 参数action检查通过后,还通过 policyFlags 参数来检查一下同时是 否有ALT和SHIFT键被按下 3. 调用 enqueueInboundEventLocked 函数 把这个按键事件封装成一

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值