当前位置: 首页 > news >正文

Android InputChannel连接

InputChannel是InputDispatcher 和应用程序 (InputTarget) 的通讯桥梁,InputDispatcher 通知应用程序有输入事件,通过InputChannel中的socket进行通信。

连接InputDispatcher和窗口

WinodwManagerService:addwindow: WMS 添加窗口时,会创建一对 InputChannel,其中一个保存在 WindowState 中,并注册给 IMS,它是服务端,另一个则通过传出参数 outInputChannel 交给调用者,是客户端

1.服务端连接的建立

addwindow 函数中,有以下三项工作:

通过 WindowState.setInputChannel 函数保存服务端的 InputChannel

通过 IMS.registerInputChannel 将 InputChannel 注册到 IMS

通过 InputMonitor.updateInputWindowsLw 将所有窗口的信息更新到 IMS

2.窗口端连接的建立

当窗口端通过 addwindow 函数获取 InputChannel,便会使用它创建一个 InputEventReceiver 对象,可以接收来自InputChannel 的输入事件,触发 onInputEvent 回调

InputEventRecevier 如何工作?将 InputChannel 的可读事件注册到 Looper,然后在事件到来时从 InputChannel 中读取 InputMessage,并翻译成 InputEvent,然后回调 InputEventReceiver 的 onInputEvent

详细调用栈如下: 

@frameworks/base/core/java/android/view/ViewRootImpl.java
setViewinputChannel = new InputChannel();
@frameworks/base/services/core/java/com/android/server/wm/Session.javamWindowSession.addToDisplayAsUser(mWindow, mWindowAttributes, mDisplay.getDisplayId(), userId, inputChannel
@frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java    Service.addWindow(this, window, attrs, viewVisibility, displayId, userId, requestedVisibilities, outInputChannel,    //WinodwManagerService:addwindow
@frameworks/base/services/core/java/com/android/server/wm/WindowState.javawin.openInputChannel(outInputChannel);
@frameworks/base/services/core/java/com/android/server/input/InputManagerService.java            mInputChannel = mWmService.mInputManager.createInputChannel(name);
@frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp                nativeCreateInputChannel(mPtr, name);NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);base::Result<std::unique_ptr<InputChannel>> inputChannel = im->createInputChannel(env, name);
@frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp                        mInputManager->getDispatcher()->createInputChannel(name);
@frameworks/native/libs/input/InputTransport.cppstatus_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)std::string serverChannelName = name + " (server)";outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);std::string clientChannelName = name + " (client)";outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
@frameworks/native/services/inputflinger/dispatcher/Connection.cppsp<Connection> connection = new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator);inputChannel(inputChannel),inputPublisher(inputChannel),std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, this, std::placeholders::_1, token);mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr);mInputChannel.copyTo(outInputChannel);  //复制给outInputChannelif (focusChanged) { displayContent.getInputMonitor().setInputFocusLw(displayContent.mCurrentFocus, false /*updateInputWindows*/);
@frameworks/base/services/core/java/com/android/server/wm/InputMonitor.javadisplayContent.getInputMonitor().updateInputWindowsLw(false /*force*/);scheduleUpdateInputWindows();mHandler.post(mUpdateInputWindows);run()mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);updateInputFocusRequest(mRecentsAnimationInputConsumer);requestFocus(recentsAnimationInputConsumer.mWindowHandle.token, recentsAnimationInputConsumer.mName);
@frameworks/base/core/java/android/view/SurfaceControl.javamInputTransaction.setFocusedWindow(mInputFocus, windowName, mDisplayId);     nativeSetFocusedWindow(mNativeObject, token,  windowName, null /* focusedToken */, null /* focusedWindowName */, displayId);
@frameworks/native/libs/gui/SurfaceComposerClient.cpp                                            transaction->setFocusedWindow(request);mInputWindowCommands.focusRequests.push_back(request);
@frameworks/native/services/surfaceflinger/SurfaceFlinger.cppSurfaceFlinger::handleMessageTransactionif (getTransactionFlags(eTransactionFlushNeeded)) { flushTransactionQueues();}applyTransactionState(transaction.states,  transaction.displays, transaction.inputWindowCommands,transactionFlags |= addInputWindowCommands(inputWindowCommands);
@frameworks/native/libs/gui/LayerState.cpp                                                                    bool hasChanges = mInputWindowCommands.merge(inputWindowCommands);focusRequests.insert(focusRequests.end(), std::make_move_iterator(other.focusRequests.begin()),SurfaceFlinger::onMessageInvalidateupdateInputFlinger();
@frameworks/native/services/inputflinger/InputManager.cpp                                                                                    for (const auto& focusRequest : mInputWindowCommands.focusRequests) { mInputFlinger->setFocusedWindow(focusRequest);}mDispatcher->setFocusedWindow(request);
@frameworks/base/core/jni/android_view_SurfaceControl.cpp                            mInputEventReceiver = new WindowInputEventReceiver(inputChannel, Looper.myLooper());
@frameworks/base/core/java/android/view/InputEventReceiver.javasuper(inputChannel, looper);
@frameworks/base/core/jni/android_view_InputEventReceiver.cppmReceiverPtr = nativeInit(new WeakReference<InputEventReceiver>(this), inputChannel, mMessageQueue);sp<NativeInputEventReceiver> receiver = new NativeInputEventReceiver(env, receiverWeak, inputChannel, messageQueue);status_t status = receiver->initialize();setFdEvents(ALOOPER_EVENT_INPUT);mMessageQueue->getLooper()->addFd(fd, 0, events, this, nullptr);// Set up the input pipeline.mSyntheticInputStage = new SyntheticInputStage();InputStage viewPostImeStage = new ViewPostImeInputStage(mSyntheticInputStage);InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage, "aq:native-post-ime:" + counterSuffix);InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);InputStage imeStage = new ImeInputStage(earlyPostImeStage,  "aq:ime:" + counterSuffix);InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage, "aq:native-pre-ime:" + counterSuffix);mFirstInputStage = nativePreImeStage;mFirstPostImeInputStage = earlyPostImeStage;//InputDispatcher向InputChannel使用socket写入输入事件,触发InputEventReceiver调用来接收输入事件
@frameworks/base/core/jni/android_view_InputEventReceiver.cpp
NativeInputEventReceiver::handleEventNativeInputEventReceiver::consumeEvents(env, false /*consumeBatches*/, -1, nullptr)
@frameworks/native/libs/input/InputTransport.cppmInputConsumer.consume(&mInputEventFactory, consumeBatches, frameTime, &seq, &inputEvent);status_t result = mChannel->receiveMessage(&mMsg);nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);   //从socket中读输入事件case InputMessage::Type::FOCUS: {initializeFocusEvent(focusEvent, &mMsg);case InputMessage::Type::MOTION: {initializeMotionEvent(motionEvent, &mMsg);//构造java的event事件case AINPUT_EVENT_TYPE_FOCUS: { env->CallVoidMethod(receiverObj.get(), gInputEventReceiverClassInfo.onFocusEvent, jboolean(focusEvent->getHasFocus()), jboolean(focusEvent->getInTouchMode()));
@frameworks/base/core/java/android/view/ViewRootImpl.javaonFocusEventwindowFocusChanged(hasFocus, inTouchMode);msg.what = MSG_WINDOW_FOCUS_CHANGED;mHandler.sendMessage(msg);case MSG_WINDOW_FOCUS_CHANGED: { handleWindowFocusChanged(); }                                case AINPUT_EVENT_TYPE_MOTION: {inputEventObj = android_view_MotionEvent_obtainAsCopy(env, motionEvent);if (inputEventObj) {  env->CallVoidMethod(receiverObj.get(), gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj);
@frameworks/base/core/java/android/view/InputEventReceiver.java    dispatchInputEvent
@frameworks/base/core/java/android/view/ViewRootImpl.javaonInputEvent(event);enqueueInputEvent(event, this, 0, true);if (processImmediately) { doProcessInputEvents(); }deliverInputEvent(q);  //在deliverInputEvent函数中做输入事件的实际分发stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;  //这里开始选择 责任链的入口,具体请看InputStage的处理流程stage.deliver(q);  //这里主要调用InputStage的deliver方法进行分发,InputStage代表了输入事件的处理阶段,使用责任链模式设计模式。result = onProcess(q);NativePreImeInputStage::onProcess

http://www.lryc.cn/news/386366.html

相关文章:

  • 爬虫笔记17——selenium框架的使用
  • [BUUCTF从零单排] Web方向 02.Web入门篇之『常见的搜集』解题思路(dirsearch工具详解)
  • 深度相机识别物体——实现数据集准备与数据集分割
  • STM32第十一课:ADC采集光照
  • python查找支撑数 青少年编程电子学会python编程等级考试三级真题解析2022年3月
  • 创建一个快速、高效的网络爬虫:PHP和Selenium示例
  • 两张图片怎样拼在一起?将两张图片拼在一起的几种方法介绍
  • 百日筑基第五天-关于maven
  • 【CSS in Depth 2 精译】2.2 em 和 rem + 2.2.1 使用 em 定义字号
  • C++Primer Plus 第十四章代码重用:14.4.4 数组模板示例和非类型参数
  • 短视频哪个软件好用?成都柏煜文化传媒有限公司
  • 金融科技:重塑用户体验,驱动满意度飙升
  • JavaScript——算术运算符
  • 备份SQL Server数据库并还原到另一台服务器
  • 二刷算法训练营Day45 | 动态规划(7/17)
  • 大模型项目落地时,该如何估算模型所需GPU算力资源
  • LLM应用开发-RAG系统评估与优化
  • 秋招突击——第七弹——Redis快速入门
  • 软考初级网络管理员__操作系统单选题
  • 从入门到精通:网络编程套接字(万字详解,小白友好,建议收藏)
  • dledger原理源码分析系列(一)架构,核心组件和rpc组件
  • 第七节:如何浅显易懂地理解Spring Boot中的依赖注入(自学Spring boot 3.x的第二天)
  • Postman自动化测试实战:使用脚本提升测试效率
  • CSMA/CA并不是“公平”的
  • 【漏洞复现】I doc view——任意文件读取
  • 图数据库 vs 向量数据库
  • 企业品牌出海第一站 维基百科词条创建
  • Windows下activemq集群配置(broker-network)
  • 心理辅导平台系统
  • 代理IP对SEO影响分析:提升网站排名的关键策略