InputManagerService

Input框架

每增加一个WindowState.java,WMS都会通过registerInputChannel向InputDispatcher注册一个connection,这个connection用于后续event查找当前的触摸位置属于哪一个window

event 主要通过两种途径返回到framework

图片.png

1、key发往PhoneWindowManager

  • 一路通过InputMonitor(继承于WindowManagerCallbacks)传给PhoneWindowManager来做系统输入事件的处理,核心回调函数interceptKeyBeforeQueueing
    图片.png

2、motion发往window

  • 另一方面通过InputDispatcher中的socket将这些事件传给焦点及监视窗口(ViewRootImpl),然后会调用相应View的onTouchEvent,核心是publishKeyEvent/publishMotionEvent

图片.png

  • InputDispatcher会维护和WMS中所有窗口的连接,虽然一般只会往焦点窗口发事件。

    ViewRootImpl中维护了pending input event的列表,用mPendingInputEventHead和mPendingInputEventTail指示,其中的元素为QueuedInputEvent类型。当consumeBatchedInputEvents调用后,会

回调enqueueInputEvent()加入event元素,然后ViewRootImpl延时或非延时在doProcessInputEvents()中读出并处理。
图片.png

总结

  • Input系统—InputReader线程:通过EventHub从/dev/input节点获取事件,转换成EventEntry事件加入到InputDispatcher的mInboundQueue。

  • Input系统—InputDispatcher线程:从mInboundQueue队列取出事件,转换成DispatchEntry事件加入到connection的outboundQueue队列。再然后开始处理分发事件,取出outbound队列(开始像view分发事件),同时在分发开始的是否将此事件放入waitQueue中,以便当view中事件处理完成回调到InputDispatcher中时,能找到对应的事件。

    • 每次dispatch motion 之前都要检查接收者的状态。如果上一个event没有返回或者超时,则不会再次dispatch,而是等待。
  • Input系统—UI线程:创建socket pair,分别位于”InputDispatcher”线程和focused窗口所在进程的UI主线程,可相互通信。

    • UI主线程:通过setFdEvents(), 监听socket客户端,收到消息后回调NativeInputEventReceiver();

    • “InputDispatcher”线程: 通过IMS.registerInputChannel(),监听socket服务端,收到消息后回调handleReceiveCallback;

猜你喜欢

转载自blog.csdn.net/xiabodan/article/details/80258742