android inputmanager中事件的传递流程

涉及文件路径:

frameworks\base\services\core\java\com\android\server\input\InputManagerService.java
frameworks\base\core\java\android\view\InputChannel.java
frameworks\base\core\jni\android_view_InputChannel.cpp
frameworks\base\core\java\android\hardware\input\InputManager.java
frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
frameworks\native\services\inputflinger\InputManager.h
frameworks\native\services\inputflinger\InputManager.cpp
frameworks\native\services\inputflinger\InputReader.h
frameworks\native\services\inputflinger\InputReader.cpp
frameworks\native\services\inputflinger\InputListener.cpp
frameworks\native\services\inputflinger\InputDispatcher.h
system\core\libutils\include\utils\Looper.h
system\core\libutils\Looper.cpp
frameworks\native\services\inputflinger\InputDispatcher.cpp
frameworks\native\libs\input\InputTransport.cpp
frameworks\native\include\input\InputTransport.h
frameworks\base\core\java\android\view\ViewRootImpl.java
frameworks\native\services\inputflinger\EventHub.h
frameworks\native\include\input\InputDevice.h
frameworks\native\services\inputflinger\EventHub.cpp
frameworks\base\services\core\java\com\android\server\wm\WindowManagerService.java
frameworks\base\services\core\java\com\android\server\wm\Session.java
frameworks\base\core\java\android\view\InputEventReceiver.java
frameworks\base\core\jni\android_view_InputEventReceiver.cpp

概述:

android系统输入管理系统inputmanager主要功能就是监听android输入设备,及时将屏幕点击、按键事件、滚动球、鼠标等输入设备产生的输入事件通过转换成java层可使用的事件,比如motionevent,keyevent等将其从底层设备获取出来传递至android系统或者app,然后由系统或者app来做出相应的处理,比如在一个activity中点击了一个控件,屏幕的点击事件会通过inputmanager系统转化成一次触摸事件,然后将其传递至activity,然后activity继续将其传递至该控件,由该控件来做相应处理。

inputmanager系统主要类关系图如下:
在这里插入图片描述
主要从五个流程上来分析:
1、inputmanager系统的初始化
2、InputReader读取事件的流程
3、InputDispatcher事件分发的流程
4、java层事件监听器的注册流程
5、事件如何从C层传递至Java层

inputmanager系统的初始化

inputmanagerservice服务在systemserver中启动:

            traceBeginAndSlog("StartInputManagerService");
            inputManager = new InputManagerService(context);
            traceEnd();
            ......
            
            ......
            traceBeginAndSlog("StartInputManager");
            inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
            inputManager.start();
            traceEnd();

InputManagerService服务启动后的流程如下图:
在这里插入图片描述
inputmanagerservice初始化方法如下:

    public InputManagerService(Context context) {
        this.mContext = context;
        this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());

        mUseDevInputEventForAudioJack =
                context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
        //调用native方法的初始化,这个是重点
        mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());

        ......
    }
    ......
    
        public void start() {
        //开始扫描设备发送事件
        nativeStart(mPtr);

       ......
    }

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;
    }
    //初始化NativeInputManager
    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
            messageQueue->getLooper());
    im->incStrong(0);
    return reinterpret_cast<jlong>(im);
}

static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
    //开始事件的扫描和派发
    status_t result = im->getInputManager()->start();
    if (result) {
        jniThrowRuntimeException(env, "Input manager could not be started.");
    }
}

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;
        mLocked.pointerCapture = false;
    }
    mInteractive = true;
    //初始化EventHub和InputManager
    sp<EventHub> eventHub = new EventHub();
    mInputManager = new InputManager(eventHub, this, this);
}

EventHub.cpp

EventHub::EventHub(void) :
        mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
        mOpeningDevices(0), mClosingDevices(0),
        mNeedToSendFinishedDeviceScan(false),
        mNeedToReopenDevices(false), mNeedToScanDevices(true),
        mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
    acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
    //新建epoll对象
    mEpollFd = epoll_create(EPOLL_SIZE_HINT);
    //新建notify对象
    mINotifyFd = inotify_init();
    //将存储设备点的路径dev/input作为监听对象添加到inotify对象中
    int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
    //将mINotifyFd作为epoll的一个监控对象
    struct epoll_event eventItem;
    memset(&eventItem, 0, sizeof(eventItem));
    eventItem.events = EPOLLIN;
    eventItem.data.u32 = EPOLL_ID_INOTIFY;
    //将对mINotifyFd的监听注册到epoll对象中
    result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);

    int wakeFds[2];
    result = pipe(wakeFds);
    LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe.  errno=%d", errno);

    mWakeReadPipeFd = wakeFds[0];
    mWakeWritePipeFd = wakeFds[1];

    result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
    

    result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
    

    eventItem.data.u32 = EPOLL_ID_WAKE;
    result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
    

    int major, minor;
    getLinuxRelease(&major, &minor);
    
    mUsingEpollWakeup = major > 3 || (major == 3 && minor >= 5);
}

在EventHub的构造函数中,它通过INotify与Epoll机制建立起对设备点增删事件及可读状态的监听。INotify是Linux内核所提供的一种文件系统变化通知机制。它可以为应用程序监控文件系统的变化,如文件的新建、删除、读写等等。它有两个基本对象,inotify对象对应一个队列,应用程序可以向inotify对象添加多个监听,当被监听的事件发生时,可以通过read()函数从inotify对象中将事件信息读取出来;而watch对象则用来描述文件系统的变化事件的监听,它是一个二元组,包括监听目标和事件掩码两个元素,监听目标是文件系统的一个路径,可以是文件也可以是文件夹。Epoll可以使用一次等待监听多个描述符的可读/可写状态。

InputManager.cpp

InputManager::InputManager(
        const sp<EventHubInterface>& eventHub,
        const sp<InputReaderPolicyInterface>& readerPolicy,
        const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
    mDispatcher = new InputDispatcher(dispatcherPolicy);
    //reader的初始化中将mDispatcher作为参数传入
    mReader = new InputReader(eventHub, readerPolicy, mDispatcher);
    initialize();
}

从类关系图可知,InputDispatcher继承自InputListenerInterface

void InputManager::initialize() {
    //初始化两个线程
    mReaderThread = new InputReaderThread(mReader);
    mDispatcherThread = new InputDispatcherThread(mDispatcher);
}
status_t InputManager::start() {
    //启动线程开始派发事件
    status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
    if (result) {
        ALOGE("Could not start InputDispatcher thread due to error %d.", result);
        return result;
    }
    //启动线程开始读取设备输入事件
    result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
    if (result) {
        ALOGE("Could not start InputReader thread due to error %d.", result);

        mDispatcherThread->requestExit();
        return result;
    }

    return OK;
}
InputReader读取事件的流程

事件读取流程如下图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值