10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch

12. 输入系统_APP跟输入系统建立联系_InputChannel和Connection
核心: socketpair // 第9课第3节_输入系统_必备Linux编程知识_任意进程双向通信(scoketpair+binder)

对于每个APP在WindowManagerService中都有个WindowState与之对应,当新建一个APP的时候通过binder系统调用addToDisplay把自己告诉给WindowManagerService,同时会导致addWindow被调用,其会创建WindowState来表示这个应用程序,并创建一个socketpair得到两个文件句柄fd0和fd1,同时把fd1返回给应用程序,把fd0封装成Inputchannel,这个Inputchannel会放入到WindowState中,也会通过registerInputchannel注册给InputDispatcher,在InputDispatcher中会根据Inputchannel来构造一个Connection,把这个创建出来的Connection放入KeyedVector(KeyedVector<int,sp<Connection>>  mConnectionsByFd),这个InputDispatcher得到数据后放入某个Connection.Inputchannel.fd0中,APP端从fd1取得数据;APP在收到返回的fd1后,把其封装成Inputchannel,在把其封装成WindowInputEventReceiver,最后把fd1放入Looper中,使用epoll来查询等待。

对于每个能接受输入事件的应用程序,其在InputDispatcher中都有对应的connection,InputDispatcher需要把数据发送给应用程序的时候,需要找出当前在屏幕最前面的是哪个应用程序,然后找到他的connection,把数据输入他的fd就可以

InputReader线程、InputDispatcher线程、WindowManagerService线程都属于SystemServer进程中,线程可以直接通讯,不需要binder

应用程序调用:

handleResumeActivity

  ViewManager wm =a.getWindowManager()//这个WindowManager和上面说的WindowManagerService不是一个东西

  wm.addView(decor,l);

    mGlobal.addView()

      root = new ViewRootlmpl()

      root.setView()

        mWindowSession.addToDisplay(...,mInputChannel)//该函数是分界线,下面的内容是在WindowManagerService线程中执行,前面的都是在应用程序中执行的,mWindowSession是一个远程服务的引用,mInputChannel包含有返回的文件句柄fd1

          mRemote.transact(Stub.TRANSACTION_addToDisplay,...)

            onTransact

              addToDisplay  

                addWindow

                  InputChannel[] inputChannels = InputChannel.openInputChannelPair(name)//得到fd0和fd1

                    nativeOpenInputChannelPair

                      android_view_InputChannel_nativeOpenInputChannelPair

                        openInputChannelPair

                          socketpair

                  win.setInputChannel(inputChannels[0])

                  inputChannels[1].transferTo(outInputChannel)

                  mInputManager.registerInputChannel //把inputChannels[1]告诉InputDispatcher,WindowManagerService与InputDispatcher同属一个进程,所有可以直接调用 

                    nativeRegisterInputChannel

                      im->registerInputChannel

                        registerInputChannel

                          connection = new Connection(inputChannel,inputWindowHandle,monitor)

                            int fd = inputChannel->getFd()

                            mConnectionByFd.add(fd,connection);

                            mLooker->addFd(fd,0,ALOOPER_EVENT_INPUT,....)

              _arg6.writeToParcel

                parcel->writeDupFileDescriptor(inputChannel->getFd())//把fd1写入binder驱动

          outInputChannel.readFromParcel(_reply)

            int rawFd = parcel->readFileDescriptor();//binder驱动中读到fd1
            int dupFd = dup(rawFd);
            InputChannel* inputChannel = new InputChannel(name, dupFd);
            NativeInputChannel* nativeInputChannel = new NativeInputChannel(inputChannel);
            android_view_InputChannel_setNativeInputChannel(env, obj, nativeInputChannel);

        mInputEventReceiver = new WindowInputEventReceiver()

          InputEventReceiver  

            nativeInit

              receiver = new NativeInputEventReceiver

              receiver->initialize()

                setFdEvents

                  mMessageQueue->getLooper()->addFd(fd,0,events,this,NULL)                  


13. 输入系统_Dispatcher线程_分发dispatch

dispatch线程中有mConnectionsByFd,其中有一些列的connection,connection中有inputchannel.fd0,通过socketpair与APP的fd1通讯

分发的步骤(具体情景分析见uml图中的dispatch_input_event)

(1)查找目标

  向WindowManagerService查询当前Window获得对应的Connection

(2)把输入事件放入Connection的队列outboundQueue

(3)从队列中取出事件构造为InputMessage发送

原文地址:https://www.cnblogs.com/liusiluandzhangkun/p/9167066.html