Android Input系统

216 阅读1分钟

本文基于Android7.1代码分析

一.Input启动

先上个流程图

InputManagerService的创建是在SystemServer,截取下关键代码

    inputManager = new InputManagerService(context);

    wm = WindowManagerService.main(context, inputManager,
                        mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
                        !mFirstBoot, mOnlyCore);
    ServiceManager.addService(Context.WINDOW_SERVICE, wm);
    ServiceManager.addService(Context.INPUT_SERVICE, inputManager);

    mActivityManagerService.setWindowManager(wm);

    inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
    inputManager.start();

后面就是InputReader读取事件,InputDispatch派发事件了

二.InputReader

1.InputManager在initialize时创建了InputReaderThread线程

2.InputManager在start时执行了InputReaderThread的run方法

InputReaderThread继承Thread,我们看下/system/core/include/utils/Thread.h

 // Start the thread in threadLoop() which needs to be implemented.
 virtual status_t    run(    const char* name = 0,
                             int32_t priority = PRIORITY_DEFAULT,
                             size_t stack = 0);

 // Derived class must implement threadLoop(). The thread starts its life
 // here. There are two ways of using the Thread object:
 // 1) loop: if threadLoop() returns true, it will be called again if
 //          requestExit() wasn't called.
 // 2) once: if threadLoop() returns false, the thread will exit upon return.
 virtual bool        threadLoop() = 0;

这里可以看出接下来应该是走到InputReaderThread的threadLoop方法,并且我们可以看到它返回true时,表示循环执行。也就是会循环执行loopOnce

上图是从loopOnce到InputDispatcher的事件传递流程,我们顺着流程来看

1.读取RawEvent(getEvents)

getEvents首先通过scanDevicesLocked-scanDirLocked扫描"/dev/input/"目录,然后分别打开这些设备,通过ioctl获取这些设备的信息,接下来将这些设备分别创建为Device对象,并将设备对象添加到epoll监控中,最后通过addDeviceLocked将设备对象添加到mDevices。

接下来将每个设备组成RawEvent结构,然后调用epoll_wait阻塞等待着输入事件的到来。

2.处理RawEvent(processEventsLocked)

事件类型分为两大类,一类是设备自身产生的event,一类是设备发生变化的event。