input系统之启动介绍

IMS的整体架构diagram

请添加图片描述
输入事件的源头位于、dev/input下的设备节点,输入系统的终点是由WMS管理的某个窗口。Android输入系统的功能是读取设备节点中原始事件,加工封装后派发一个特定的窗口以及窗口控件,及扫描输入设备将设备封装到mDevices数组。

模块模块说明
linux 内核接受输入设备中断,将原始事件数据写入到设备节点
设备节点IMS可以从中读取事件
InputManagerService分为Java层和native层两部分,Java层负责与WMS的通信;Native层是InputReader和InputDispatcher两个输入系统关键组件的运行容器
EventHub直接访问所有的设备节点,通过一个名为getEvent()的函数将所有输入系统相关的处理底层事件返回给使用者
InputReader运行于独立线程,负责管理输入设备的列表和配置,以及进行输入事件的加工处理。通过线程循环不断地通过getEvent()函数从EventHub中将事件取出并进行处理。对于设备节点的增删事件,他会更新输入设备列表于配置。对于原始输入事件,InputReader对其进行翻译,组装封装包含更多的信息,然后交给InputDispatcher进行派发。
InputReaderPolicy为InputReader事件加工处理提供策略配置
InputDispatcherIMS的另一个组件。它也运行在一个独立线程中,InputDispatcher中保管了来自WMS所有窗口的信息,收到来自InputReader的输入事件后,会寻找合适的窗口,将事件派发给它。
InputDispatcherPolicy它为InputDispatcher的派发过程提供策略控制
WMS对InputDispatcher的正常工作起到了重要作用。新建窗口时,WMS为新窗口和IMS创建了事件传递所用的通道。另外,还将所有窗口的信息,包括窗口的可点击区域,焦点窗口等信息,实时更新到IMS的InputDispatcher中,使得InputDispatcher可以正确地将事件派发到指定窗口。
ViewRoot对于某些窗口,如壁纸窗口,SurfaceView的窗口来说,窗口即是输入事件派发的终点。而对于其他的Activity,对话框等使用了Android控件系统的窗口来说,输入事件的终点是控件(view)。ViewRootImpl将窗口所接受到的输入事件沿着控制树派发给View

内核将原始事件写入到设备节点中,InputReader不断地通过EventHub将原始事件取出来并加工成Android输入事件,交给InputDispatcher。InputDispatcher根据WMS提供的窗口信息将事件交给合适的窗口。窗口的ViewRootImpl对象再沿着控件树将事件派发给具体的控件。控件对其收到的事件作出响应,更新自己的画面、执行特定的动作。

IMS启动流程分析

Android的输入子系统是在InputManagerService启动的,而InputManagerService是在system_server中启动的。接下来从SystemServer的启动开始逐步分析:

//systemserver->startOtherServices
private void startOtherServices() {
   //...
   inputManager = new InputManagerService(context);//关键点1
   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);
   inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
   inputManager.start();//关键点2
}
InputManagerService
    public InputManagerService(Context context) {
        this.mContext = context;
        this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
        mUseDevInputEventForAudioJack =
                context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
        Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
                + mUseDevInputEventForAudioJack);
        //调用nativeInit来执行C++层的初始化操作
        mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
        LocalServices.addService(InputManagerInternal.class, new LocalService());
    }
com_android_server_input_InputManagerService.cpp
static jlong nativeInit(JNIEnv* env, jclass clazz,
        jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
    sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
    if (messageQueue == NULL) {
        jniThrowRuntimeException(env, "MessageQueue is not initialized.");
        return 0;
    }
    //构造一个NativeInputManagera对象
    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
            messageQueue->getLooper());
    im->incStrong(0);
    return reinterpret_cast<jlong>(im);
}

这里创建一个NativeInputManager的实例,并将其作为返回值保存在InputMangerService的mPtr字段里,继续分析NativeInputManager的构造器。

NativeInputManager::NativeInputManager(jobject contextObj,
        jobject serviceObj, const sp<Looper>& looper) :
        mLooper(looper), mInteractive(true) {
    JNIEnv* env = jniEnv();
    mContextObj = env->NewGlobalRef(contextObj);
    mServiceObj = env->NewGlobalRef(serviceObj);
    {
        AutoMutex _l(mLock);
        mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
        mLocked.pointerSpeed = 0;
        mLocked.pointerGesturesEnabled = true;
        mLocked.showTouches = false;
    }
    //构造一个EventHub对象,InputReaderPolicy,InputDispatcherPolicy,传递给InputManager
    sp<EventHub> eventHub = new EventHub();
    mInputManager = new InputManager(eventHub, this, this);
}

这里继续分析关键对象InputManager,代码如下:

InputManager::InputManager(
        const sp<EventHubInterface>& eventHub,
        const sp<InputReaderPolicyInterface>& readerPolicy,
        const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
    mDispatcher = new InputDispatcher(dispatcherPolicy);
    mReader = new InputReader(eventHub, readerPolicy, mDispatcher);
    initialize();
}

这里创建了一个InputReaderThread和InputDispatcherThread对象,前面构造器中创建InputReader是通过InputReaderThread来读取输入事件,而InputDispatcher实际通过InputDispatcherThread来发输入事件。
在这里插入图片描述
InputManager的创建过程分别为InputReader与InputDispatcher创建了线程,IMS的成员准备就绪。

InputManagerService线程启动分析

线程启动

inputManager.start();
---------------------------------------
public void start() {
    Slog.i(TAG, "Starting input manager");
    nativeStart(mPtr);
    //...
}
----------------------------------------
static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
    status_t result = im->getInputManager()->start();
}
---------------------------------------
status_t InputManager::start() {
    status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
    //...
    result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
    //...
    return OK;
}

InputManager启动了一个InputReaderThread和InputDispatcherThread来读取和分发消息,调用run之后,会进入threadLoop函数。这里start()函数的功能就是启动这两个线程,使得InputReader于InputDispatcher开始工作。当两个线程启动后,InputReader在其线程循环中不断地从EventHub中读取输入事件,加工处理后将事件放入InputDispatcher的派发发队列中,InputDispatcher在线程循环中将派发队列中的事件取出,查找合适窗口,将事件写入。窗口事件接收线程的Looper从管道中将事件取出,交由事件处理函数进行事件响应。
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值