Android InputDispatcher分发输入事件

派发循环是指 InputDispatcher 不断地派发队列取出事件,寻找合适的窗口并进行发送的过程,是 InputDispatcher 线程的主要工作

事件发送循环是 InputDispatcher 通过 Connection 对象将事件发送给窗口,并接受其反馈的过程

InputDispatcher —> dispatchEventLocked:dispatchEventLocked 根据 InputTarget 中的 InputChannel 找到对应的Connection


 

@frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp
//1. InputReader将事件注入派发队列
InputDispatcher::notifyMotion 
    mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags);
        needWake = enqueueInboundEventLocked(std::move(newEntry));
            mInboundQueue.push_back(std::move(newEntry));

//2.派发线程的线程循环
InputDispatcher::dispatchOnce
//3. 为事件寻找合适的窗口,窗口分为普通窗口和监听窗口,普通通过按点和焦点查找,监听窗口则无条件监听所有输入事件
    dispatchOnceInnerLocked(&nextWakeupTime);
        mPendingEvent = mInboundQueue.front();
        case EventEntry::Type::FOCUS:
            dispatchFocusLocked(currentTime, typedEntry);
                //dispatchFocusLocked: message=Focus entering 76ac74c com/com.MainActivity (server), reason=reason=Window became focusable. Previous reason: NOT_FOCUSABLE
                //dispatchFocusLocked: message=Focus leaving f81ce53 org.mozilla.firefox/org.mozilla.fenix.HomeActivity (server), reason=reason=NOT_FOCUSABLE
                //dispatchFocusLocked: message=Focus leaving 76ac74c com/com.MainActivity (server), reason=reason=NOT_FOCUSABLE
                //dispatchFocusLocked: message=Focus entering f81ce53 org.mozilla.firefox/org.mozilla.fenix.HomeActivity (server), reason=reason=Window became focusable. Previous reason: NOT_FOCUSABLE            
                std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken);
                    auto connectionIt = mConnectionsByToken.find(token);
                    return connectionIt->second->inputChannel;
                InputTarget target;
                target.inputChannel = channel;                
                dispatchEventLocked(currentTime, entry, {target});
        case EventEntry::Type::MOTION: 
            done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime);
                //寻找事件接受窗口的inputTargets
                std::vector<InputTarget> inputTargets;
                if (isPointerEvent) { injectionResult = findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, &conflictingPointerActions); }
                else { injectionResult = findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); }                
                dispatchEventLocked(currentTime, entry, inputTargets);
                    for (const InputTarget& inputTarget : inputTargets) {
                         sp<Connection> connection = getConnectionLocked(inputTarget.inputChannel->getConnectionToken());
                         prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget);
                             enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget);
                                 //输入事件被 InputPublisher 以 InputMessage 的形式写入 InputChannel,然后将事件转存到 waitQueue 中等待窗口的反馈
                                 startDispatchCycleLocked(currentTime, connection); 
                                     case EventEntry::Type::FOCUS: {
                                         status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, focusEntry.id, focusEntry.hasFocus, mInTouchMode);
                                     case EventEntry::Type::MOTION: {
                                         status = connection->inputPublisher.publishMotionEvent(motionEntry.deviceId, motionEntry.source, motionEntry.displayId,  
@frameworks/native/libs/input/InputTransport.cpp
                                             mChannel->sendMessage(&msg);
                                                 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);    //向socket写入输入事件                            

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛文旺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值