Input系统学习-----injectInputEvent注入事件调用流程

input命令可以用来注入事件,代码在frameworks/base/cmds/input/src/com/android/commands/input/Input.java

注入点击等触碰事件时,在Android 10版本后还可以通过-d来指定displayId,这个对多屏手机具有测试参考作用。

InputManager的injectInputEvent方法最终调用到InputDispatcher::injectInputEvent

int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t displayId,

        int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,

        uint32_t policyFlags) {

enqueueInboundEventLocked(entry);

------  mInboundQueue.enqueueAtTail(entry);

这里调用到enqueueInboundEventLocked,屏幕的触碰事件也是调用到enqueueInboundEventLocked,是通过TouchInputMapper::dispatchMotion调用到的

void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,

        int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,

        const PointerProperties* properties, const PointerCoords* coords,

        const uint32_t* idToIndex, BitSet32 idBits,

        int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {

    NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,

            action, flags, metaState, buttonState, edgeFlags,

            mViewport.displayId, pointerCount, pointerProperties, pointerCoords,

            xPrecision, yPrecision, downTime);

    getListener()->notifyMotion(&args);

}

void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) {

        if (shouldSendMotionToInputFilterLocked(args)) {

            mLock.unlock();

            MotionEvent event;

            event.initialize(args->deviceId, args->source, args->action, args->flags,

                    args->edgeFlags, args->metaState, args->buttonState, 0, 0,

                    args->xPrecision, args->yPrecision,

                    args->downTime, args->eventTime,

                    args->pointerCount, args->pointerProperties, args->pointerCoords);

            policyFlags |= POLICY_FLAG_FILTERED;

            if (!mPolicy->filterInputEvent(&event, policyFlags)) {

                return; // event was consumed by the filter

            }

            mLock.lock();

        }

        // Just enqueue a new motion event.

        MotionEntry* newEntry = new MotionEntry(args->eventTime,

                args->deviceId, args->source, policyFlags,

                args->action, args->flags, args->metaState, args->buttonState,

                args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime,

                args->displayId,

                args->pointerCount, args->pointerProperties, args->pointerCoords, 0, 0);

        needWake = enqueueInboundEventLocked(newEntry);

        mLock.unlock();

    } // release lock

都有enqueueInboundEventLocked调用,

enqueueInboundEventLocked会触发dispatchOnce中的调用

loop流程

bool InputDispatcherThread::threadLoop() {

    mDispatcher->dispatchOnce();

    return true;

}

void InputDispatcher::dispatchOnce()

有指令时调用

        if (!haveCommandsLocked()) {

            dispatchOnceInnerLocked(&nextWakeupTime);

        }

bool InputDispatcher::haveCommandsLocked() const {

    return !mCommandQueue.isEmpty();

}

dispatchOnceInnerLocked

void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {

dispatchMotionLocked

event是从mPendingEvent里面获取的

bool InputDispatcher::dispatchMotionLocked(

        nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) {

findTouchedWindowTargetsLocked

获取 inputTargets

然后,dispatchEventLocked(currentTime, entry, inputTargets);

   

里面调用connect进行了分发

void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,

        EventEntry* eventEntry, const Vector& inputTargets) {

#if DEBUG_DISPATCH_CYCLE

    ALOGD("dispatchEventToCurrentInputTargets");

#endif

    ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true

    pokeUserActivityLocked(eventEntry);

    for (size_t i = 0; i < inputTargets.size(); i++) {

        const InputTarget& inputTarget = inputTargets.itemAt(i);

        ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel);

        if (connectionIndex >= 0) {

            sp connection = mConnectionsByFd.valueAt(connectionIndex);

            prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget);

        } else {

#if DEBUG_FOCUS

            ALOGD("Dropping event delivery to target with channel '%s' because it "

                    "is no longer registered with the input dispatcher.",

                    inputTarget.inputChannel->getName().string());

#endif

        }

    }

}

参考资料

InputManagerService分析一:IMS的启动与事件传递

InputManagerService分析一:IMS的启动与事件传递_李炼的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值