Input子系统架构包括内核层与框架层详解.doc
《Input子系统架构包括内核层与框架层详解.doc》由会员分享,可在线阅读,更多相关《Input子系统架构包括内核层与框架层详解.doc(42页珍藏版)》请在三一办公上搜索。
1、第1章 Android Input子系统架构1.1 Input服务的启动在Android的开机过程中,系统中的服务很多都是由SystemServer中启动的。SystemServer的代码中有这么一句话。Framework/base/services/java/com/android/server/SystemServer.javaSlog.i(TAG, Window Manager); wm = WindowManagerService.main(context, power, factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,!first
2、Boot);在这里new了一个WindowManagerService的类,我们找到这个类的构造函数。Framework/base/services/java/com/android/server/wm/WindowManagerServer.javaprivate WindowManagerService(Context context, PowerManagerService pm, boolean haveInputMethods, boolean showBootMsgs) mInputManager = new InputManager(context, this);mInputMa
3、nager.start();在WindowManagerService的构造函数中又new了一个InputManager类。InputManager类是整个android的input的上层代码最重要的类,就是通过这个类繁衍出了整个复杂的Android的input子系统。作用就好像Zygote的孕育着Android的各个服务,而InputManager就是负责将整个android的Input子系统。Framework/base/services/java/com/android/server/wm/WindowManagerServer.javapublic InputManager(Conte
4、xt context, WindowManagerService windowManagerService) Looper looper = windowManagerService.mH.getLooper();nativeInit(mContext, mCallbacks, looper.getQueue();在InputManger的构造函数中,调用了nativeInit这个方式,看到native开头或者结尾的函数,一般都是JNI。在InputManager的JNI可以找到这个函数的实现。Framework/base/services/jni/com_android_server_Inp
5、utManager.javastatic JNINativeMethod gInputManagerMethods = nativeInit, (Landroid/content/Context;Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V, (void*) android_server_InputManager_nativeInit ,简单介绍下JNI的代码风格,第一个引号括起来的函数就是我们java代码的函数原型,中间的引号中的就是代表java原型函数的参数。而最后的那个函数就是在对应的函
6、数。一般都是c+代码。Framework/base/services/jni/com_android_server_InputManager.javastatic void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz, jobject contextObj, jobject callbacksObj, jobject messageQueueObj) gNativeInputManager = new NativeInputManager(contextObj, callbacksObj, looper);在
7、JNI的代码中,又构造了一个重要的NativeInputManager类,这是个C+的本地类。已经不在是之前了那个java的InputManager类。接下来看看NativeInputManager的构造函数。Framework/base/services/jni/com_android_server_InputManager.javaNativeInputManager:NativeInputManager(jobject contextObj, jobject callbacksObj, const sp& looper) :mLooper(looper) sp eventHub = ne
8、w EventHub(); mInputManager = new InputManager(eventHub, this, this);这里new了两个类,EventHub和InputManager类。EventHub就是Input子系统的HAL层了,负责将linux的所有的input设备打开并负责轮询读取他们的上报的数据,后面会详细介绍,这里先简单介绍一下。InputManager类主要是负责管理input Event,有InputReader从EventHub读取事件,然后交给InputDispatcher进行分发。Framework/base/services/input/InputM
9、anager.cppInputManager:InputManager( const sp& reader, const sp& dispatcher) : mReader(reader), mDispatcher(dispatcher) initialize();void InputManager:initialize() mReaderThread = new InputReaderThread(mReader); mDispatcherThread = new InputDispatcherThread(mDispatcher);在InputManager中的initialize的初始化
10、了两个线程。一个是inputReaderThread,负责从EventHub中读取事件,另外一个是InputDispatcherThread线程,主要负责分发读取的事件去处理。Framework/base/services/java/com/android/server/wm/WindowManagerServer.javaprivate WindowManagerService(Context context, PowerManagerService pm, boolean haveInputMethods, boolean showBootMsgs) mInputManager.start
11、();在开始的时候,new了一个InputManager,然后在继续调用其start方法。Framework/base/services/java/com/android/server/wm/WindowManagerServer.javapublic void start() Slog.i(TAG, Starting input manager); nativeStart(); registerPointerSpeedSettingObserver(); registerShowTouchesSettingObserver(); updatePointerSpeedFromSettings(
12、); updateShowTouchesFromSettings(); NativeStart()跑到JNI的代码中去了,跟上面的方式一样。Framework/base/services/jni/com_android_server_InputManager.javastatic JNINativeMethod gInputManagerMethods = nativeStart, ()V, (void*) android_server_InputManager_nativeStart ,static void android_server_InputManager_nativeStart(J
13、NIEnv* env, jclass clazz) status_t result = gNativeInputManager-getInputManager()-start();在java代码中用了nativeStart(),然后JNI中又调用了NativeInputManager的start方法。在Native的InputManager中找到start的实现。Framework/base/services/input/InputManager.cppstatus_t InputManager:start() status_t result = mDispatcherThread-run(I
14、nputDispatcher, PRIORITY_URGENT_DISPLAY);result = mReaderThread-run(InputReader, PRIORITY_URGENT_DISPLAY);这个方法就是在前面InputManager中的构造函数initialize中的两个线程运行起来。先看InputDispatcher线程运行的情况,然后就是InputReader线程。Framework/base/services/input/InputDispatcher.cppbool InputDispatcherThread:threadLoop() mDispatcher-di
15、spatchOnce(); return true;InputDispatcher线程调用了Dispatcher的dispatchOnce的方法。同样的InputReader线程也会调用Reader的ReaderOnce的方法。Framework/base/services/input/InputDispatcher.cppvoid InputDispatcher:dispatchOnce() dispatchOnceInnerLocked(&nextWakeupTime); int timeoutMillis = toMillisecondTimeoutDelay(currentTime,
16、nextWakeupTime); mLooper-pollOnce(timeoutMillis);dispatchOnceInnerLocked是处理input输入消息,mLooper-pollOnce(timeoutMillis)是等待下次输入消息的事件。先看下消息在dispatchOnceInnerLocked函数中是如何处理的。Framework/base/services/input/InputDispatcher.cppvoid InputDispatcher:dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) case EventEnt
17、ry:TYPE_KEYdone = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);case EventEntry:TYPE_MOTION: done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);这个函数比较长,input事件在android的上层通过两个队列来保存,分别是InboundQueue和outboundQueue。当有input事件产生时候,会判断InboundQueue是否为空
18、,如果事件不为空的话,就从队列中取出这个input事件,然后根据input事件的类型来分发事件给不同的处理函数,比较常见的是KEY和Motion事件。不管是Key事件也好还是Motion事件都会调用dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false);这个函数来继续处理。Framework/base/services/input/InputDispatcher.cppvoid InputDispatcher:dispatchEventToCurrentInputTargetsLocked(nsecs_t curr
19、entTime, EventEntry* eventEntry, bool resumeWithAppendedMotionSample) prepareDispatchCycleLocked(currentTime, connection, eventEntry, & inputTarget, resumeWithAppendedMotionSample);在这个函数中会继续调用prepareDispatchCycleLocked方法来继续处理。而在prepareDispatchCycleLocked中又会继续调用startDispatchCycleLocked(currentTime, c
20、onnection)来进一步处理。Framework/base/services/input/InputDispatcher.cppvoid InputDispatcher:startDispatchCycleLocked(nsecs_t currentTime, const sp& connection) switch (eventEntry-type) case EventEntry:TYPE_KEY: status = connection-inputPublisher.publishKeyEventcase EventEntry:TYPE_MOTION: status = connec
21、tion-inputPublisher.publishMotionEventstatus = connection-inputPublisher.sendDispatchSignal();这个函数主要是根据input事件的类型来分发给不同的函数去处理,如果是KEY类型的事件就调用inputPublisher类的publishKeyEvent,如果是MOTION类的事件就会调用inputPublisher类的publishMotionEvent方法。并在最后发一个sendDispatchSignal。Framework/base/libs/ui/InputTransport.cppstatus_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Input 子系统 架构 包括 内核 框架 详解
链接地址:https://www.31ppt.com/p-2387989.html