一 概述
上一篇文章介绍了输入系统的大体运作流程,这篇文章详细介绍 Input 系统的启动。
Input 模块的主要组成:
- Native 层的 InputReader 负责从 EventHub 取出事件并处理,再交给 InputDispatcher
- Native 层的 InputDispatcher 接收来自 InputReader 传递过来的输入事件,并记录 WMS 的窗口信息,用于派发事件到合适的窗口
- Java 层的 InputManagerService 跟 WMS 交互,WMS 记录所有窗口信息,并同步更新到 IMS,为 InputDispatcher 正确派发事件到 ViewRootImpl 提供保障
Input 相关的动态库:
libinputflinger.so:frameworks/native/services/inputflinger/
libinputservice.so:frameworks/base/libs/input/
libinput.so: frameworks/native/libs/input/
涉及代码如下:
frameworks/native/services/inputflinger/
- InputDispatcher.cpp
- InputReader.cpp
- InputManager.cpp
- EventHub.cpp
- InputListener.cpp
frameworks/native/libs/input/
- InputTransport.cpp
- Input.cpp
- InputDevice.cpp
- Keyboard.cpp
- KeyCharacterMap.cpp
- IInputFlinger.cpp
frameworks/base/services/core/
- java/com/android/server/input/InputManagerService.java
- jni/com_android_server_input_InputManagerService.cpp
1.1 整体框架类图
InputManagerService 作为 system_server 中的重要服务,继承于 IInputManager.Stub, 作为 Binder 服务端,那么 Client 位于 InputManager 的内部通过 IInputManager.Stub.asInterface() 获取 Binder 代理端,C/S 两端通信的协议是由 IInputManager.aidl 来定义的。
Input 模块所涉及的重要类的关系如下:
- InputManagerService 位于 Java 层的 InputManagerService.java 文件,其成员 mPtr 指向 Native 层的 NativeInputManager 对象
- NativeInputManager 位于 Native 层的 com_android_server_input_InputManagerService.cpp 文件,其成员 mServiceObj 指向 Java 层的 IMS 对象;其成员 mLooper 是指 “android.display” 线程的 Looper
- InputManager 位于 libinputflinger 中的 InputManager.cpp 文件
InputDispatcher 和 InputReader 的成员变量 mPolicy 都是指 NativeInputManager 对象
InputReader 的成员 mQueuedListener,数据类型为 QueuedInputListener;通过其内部成员变量 mInnerListener 指向 InputDispatcher 对象;这便是 InputReader 跟 InputDispatcher 交互的中间枢纽。
1.2 启动调用栈
IMS 服务是伴随着 system_server 进程的启动而启动的,整个调用过程如下:
InputManagerService(初始化)
nativeInit
NativeInputManager
InputManager
InputDispatcher
Looper
InputReader
EventHub
QueuedInputListener
InputReaderThread
InputDispatcherThread
IMS.start(启动)
nativeStart
InputManager.start
InputReaderThread->run
InputDispatcherThread->run
整个过程首先创建如下对象:NativeInputManager,EventHub,InputManager, InputDispatcher,InputReader,InputReaderThread,InputDispatcherThread。 接着便是启动两个工作线程 InputReaderThread 和 InputDispatcherThread。
二 启动过程
private void startOtherServices() {
......
InputManagerService inputManager = null;
//初始化IMS对象
inputManager = new InputManagerService(context);
//把inputManager的引用放到WMS中,建立联系
wm = WindowManagerService.main(context, inputManager,
mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
!mFirstBoot, mOnlyCore, new PhoneWindowManager());
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
//设置回调,将WMS中的InputManagerCallback设置到inputManager中
//这里说明了最终如何回调到PhoneWindowManager
inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());
inputManager.start();
......
}
2.1 InputManagerService
public InputManagerService(Context context) {
this.mContext = context;
// 创建了InputManagerHandler,其Looper是DisplayThead的Looper
// 运行在线程"android.display"
this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
......
//初始化native对象
mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
LocalServices.addService(InputManagerInternal.class, new LocalService());
}
2.2 nativeInit
static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
//获取native消息队列
sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(
env, messageQueueObj);
......
//创建Native的InputManager
NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
messageQueue->getLooper());
im->incStrong(0);
//返回Native对象的指针给IMS,IMS后续会用到。IMS保存在mPtr
return reinterpret_cast<jlong>(im);
}
2.3 NativeInputManager
NativeInputManager::NativeInputManager(jobject contextObj,
jobject serviceObj, const sp<Looper>& looper) :
mLooper(looper), mInteractive(true) {
JNIEnv* env = jniEnv();
mServiceObj = env->NewGlobalRef(serviceObj);//上层IMS对象
......
// 创建InputManager对象
mInputManager = new InputManager(this, this);
defaultServiceManager()->addService(String16("inputflinger"),
mInputManager, false);
}
此处的 mLooper 是指 “android.display” 线程的 Looper。
2.4 InputManager
sp<InputReaderInterface> mReader