| /* |
| * Copyright (C) 2010 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #define LOG_TAG "InputDispatcher" |
| #define ATRACE_TAG ATRACE_TAG_INPUT |
| |
| #define LOG_NDEBUG 0 |
| |
| // Log detailed debug messages about each inbound event notification to the dispatcher. |
| #define DEBUG_INBOUND_EVENT_DETAILS 0 |
| |
| // Log detailed debug messages about each outbound event processed by the dispatcher. |
| #define DEBUG_OUTBOUND_EVENT_DETAILS 0 |
| |
| // Log debug messages about the dispatch cycle. |
| #define DEBUG_DISPATCH_CYCLE 0 |
| |
| // Log debug messages about registrations. |
| #define DEBUG_REGISTRATION 0 |
| |
| // Log debug messages about input event injection. |
| #define DEBUG_INJECTION 0 |
| |
| // Log debug messages about input focus tracking. |
| #define DEBUG_FOCUS 0 |
| |
| // Log debug messages about the app switch latency optimization. |
| #define DEBUG_APP_SWITCH 0 |
| |
| // Log debug messages about hover events. |
| #define DEBUG_HOVER 0 |
| |
| #include "InputDispatcher.h" |
| |
| #include <errno.h> |
| #include <inttypes.h> |
| #include <limits.h> |
| #include <sstream> |
| #include <stddef.h> |
| #include <time.h> |
| #include <unistd.h> |
| |
| #include <android-base/chrono_utils.h> |
| #include <android-base/stringprintf.h> |
| #include <log/log.h> |
| #include <utils/Trace.h> |
| #include <powermanager/PowerManager.h> |
| #include <binder/Binder.h> |
| |
| #define INDENT " " |
| #define INDENT2 " " |
| #define INDENT3 " " |
| #define INDENT4 " " |
| |
| using android::base::StringPrintf; |
| |
| namespace android { |
| |
| // Default input dispatching timeout if there is no focused application or paused window |
| // from which to determine an appropriate dispatching timeout. |
| constexpr nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec |
| |
| // Amount of time to allow for all pending events to be processed when an app switch |
| // key is on the way. This is used to preempt input dispatch and drop input events |
| // when an application takes too long to respond and the user has pressed an app switch key. |
| constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
| |
| // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| // before considering it stale and dropping it. |
| constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
| |
| // Amount of time to allow touch events to be streamed out to a connection before requiring |
| // that the first event be finished. This value extends the ANR timeout by the specified |
| // amount. For example, if streaming is allowed to get ahead by one second relative to the |
| // queue of waiting unfinished events, then ANRs will similarly be delayed by one second. |
| constexpr nsecs_t STREAM_AHEAD_EVENT_TIMEOUT = 500 * 1000000LL; // 0.5sec |
| |
| // Log a warning when an event takes longer than this to process, even if an ANR does not occur. |
| constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| |
| // Log a warning when an interception call takes longer than this to process. |
| constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
| |
| // Number of recent events to keep for debugging purposes. |
| constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| |
| // Sequence number for synthesized or injected events. |
| constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0; |
| |
| |
| static inline nsecs_t now() { |
| return systemTime(SYSTEM_TIME_MONOTONIC); |
| } |
| |
| static inline const char* toString(bool value) { |
| return value ? "true" : "false"; |
| } |
| |
| static std::string dispatchModeToString(int32_t dispatchMode) { |
| switch (dispatchMode) { |
| case InputTarget::FLAG_DISPATCH_AS_IS: |
| return "DISPATCH_AS_IS"; |
| case InputTarget::FLAG_DISPATCH_AS_OUTSIDE: |
| return "DISPATCH_AS_OUTSIDE"; |
| case InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER: |
| return "DISPATCH_AS_HOVER_ENTER"; |
| case InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT: |
| return "DISPATCH_AS_HOVER_EXIT"; |
| case InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT: |
| return "DISPATCH_AS_SLIPPERY_EXIT"; |
| case InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER: |
| return "DISPATCH_AS_SLIPPERY_ENTER"; |
| } |
| return StringPrintf("%" PRId32, dispatchMode); |
| } |
| |
| static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
| return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| } |
| |
| static bool isValidKeyAction(int32_t action) { |
| switch (action) { |
| case AKEY_EVENT_ACTION_DOWN: |
| case AKEY_EVENT_ACTION_UP: |
| return true; |
| default: |
| return false; |
| } |
| } |
| |
| static bool validateKeyEvent(int32_t action) { |
| if (! isValidKeyAction(action)) { |
| ALOGE("Key event has invalid action code 0x%x", action); |
| return false; |
| } |
| return true; |
| } |
| |
| static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
| switch (action & AMOTION_EVENT_ACTION_MASK) { |
| case AMOTION_EVENT_ACTION_DOWN: |
| case AMOTION_EVENT_ACTION_UP: |
| case AMOTION_EVENT_ACTION_CANCEL: |
| case AMOTION_EVENT_ACTION_MOVE: |
| case AMOTION_EVENT_ACTION_OUTSIDE: |
| case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| case AMOTION_EVENT_ACTION_SCROLL: |
| return true; |
| case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| case AMOTION_EVENT_ACTION_POINTER_UP: { |
| int32_t index = getMotionEventActionPointerIndex(action); |
| return index >= 0 && index < pointerCount; |
| } |
| case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| return actionButton != 0; |
| default: |
| return false; |
| } |
| } |
| |
| static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| const PointerProperties* pointerProperties) { |
| if (! isValidMotionAction(action, actionButton, pointerCount)) { |
| ALOGE("Motion event has invalid action code 0x%x", action); |
| return false; |
| } |
| if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
| ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.", |
| pointerCount, MAX_POINTERS); |
| return false; |
| } |
| BitSet32 pointerIdBits; |
| for (size_t i = 0; i < pointerCount; i++) { |
| int32_t id = pointerProperties[i].id; |
| if (id < 0 || id > MAX_POINTER_ID) { |
| ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", |
| id, MAX_POINTER_ID); |
| return false; |
| } |
| if (pointerIdBits.hasBit(id)) { |
| ALOGE("Motion event has duplicate pointer id %d", id); |
| return false; |
| } |
| pointerIdBits.markBit(id); |
| } |
| return true; |
| } |
| |
| static void dumpRegion(std::string& dump, const Region& region) { |
| if (region.isEmpty()) { |
| dump += "<empty>"; |
| return; |
| } |
| |
| bool first = true; |
| Region::const_iterator cur = region.begin(); |
| Region::const_iterator const tail = region.end(); |
| while (cur != tail) { |
| if (first) { |
| first = false; |
| } else { |
| dump += "|"; |
| } |
| dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom); |
| cur++; |
| } |
| } |
| |
| template<typename T, typename U> |
| static T getValueByKey(std::unordered_map<U, T>& map, U key) { |
| typename std::unordered_map<U, T>::const_iterator it = map.find(key); |
| return it != map.end() ? it->second : T{}; |
| } |
| |
| |
| // --- InputDispatcher --- |
| |
| InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) : |
| mPolicy(policy), |
| mPendingEvent(nullptr), mLastDropReason(DROP_REASON_NOT_DROPPED), |
| mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX), |
| mNextUnblockedEvent(nullptr), |
| mDispatchEnabled(false), mDispatchFrozen(false), mInputFilterEnabled(false), |
| mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
| mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) { |
| mLooper = new Looper(false); |
| mReporter = createInputReporter(); |
| |
| mKeyRepeatState.lastKeyEntry = nullptr; |
| |
| policy->getDispatcherConfiguration(&mConfig); |
| } |
| |
| InputDispatcher::~InputDispatcher() { |
| { // acquire lock |
| std::scoped_lock _l(mLock); |
| |
| resetKeyRepeatLocked(); |
| releasePendingEventLocked(); |
| drainInboundQueueLocked(); |
| } |
| |
| while (mConnectionsByFd.size() != 0) { |
| unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel); |
| } |
| } |
| |
| void InputDispatcher::dispatchOnce() { |
| nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| { // acquire lock |
| std::scoped_lock _l(mLock); |
| mDispatcherIsAlive.notify_all(); |
| |
| // Run a dispatch loop if there are no pending commands. |
| // The dispatch loop might enqueue commands to run afterwards. |
| if (!haveCommandsLocked()) { |
| dispatchOnceInnerLocked(&nextWakeupTime); |
| } |
| |
| // Run all pending commands if there are any. |
| // If any commands were run then force the next poll to wake up immediately. |
| if (runCommandsLockedInterruptible()) { |
| nextWakeupTime = LONG_LONG_MIN; |
| } |
| } // release lock |
| |
| // Wait for callback or timeout or wake. (make sure we round up, not down) |
| nsecs_t currentTime = now(); |
| int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| mLooper->pollOnce(timeoutMillis); |
| } |
| |
| void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| nsecs_t currentTime = now(); |
| |
| // Reset the key repeat timer whenever normal dispatch is suspended while the |
| // device is in a non-interactive state. This is to ensure that we abort a key |
| // repeat if the device is just coming out of sleep. |
| if (!mDispatchEnabled) { |
| resetKeyRepeatLocked(); |
| } |
| |
| // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| if (mDispatchFrozen) { |
| #if DEBUG_FOCUS |
| ALOGD("Dispatch frozen. Waiting some more."); |
| #endif |
| return; |
| } |
| |
| // Optimize latency of app switches. |
| // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| if (mAppSwitchDueTime < *nextWakeupTime) { |
| *nextWakeupTime = mAppSwitchDueTime; |
| } |
| |
| // Ready to start a new event. |
| // If we don't already have a pending event, go grab one. |
| if (! mPendingEvent) { |
| if (mInboundQueue.isEmpty()) { |
| if (isAppSwitchDue) { |
| // The inbound queue is empty so the app switch key we were waiting |
| // for will never arrive. Stop waiting for it. |
| resetPendingAppSwitchLocked(false); |
| isAppSwitchDue = false; |
| } |
| |
| // Synthesize a key repeat if appropriate. |
| if (mKeyRepeatState.lastKeyEntry) { |
| if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| } else { |
| if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| } |
| } |
| } |
| |
| // Nothing to do if there is no pending event. |
| if (!mPendingEvent) { |
| return; |
| } |
| } else { |
| // Inbound queue has at least one entry. |
| mPendingEvent = mInboundQueue.dequeueAtHead(); |
| traceInboundQueueLengthLocked(); |
| } |
| |
| // Poke user activity for this event. |
| if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| pokeUserActivityLocked(mPendingEvent); |
| } |
| |
| // Get ready to dispatch the event. |
| resetANRTimeoutsLocked(); |
| } |
| |
| // Now we have an event to dispatch. |
| // All events are eventually dequeued and processed this way, even if we intend to drop them. |
| ALOG_ASSERT(mPendingEvent != nullptr); |
| bool done = false; |
| DropReason dropReason = DROP_REASON_NOT_DROPPED; |
| if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| dropReason = DROP_REASON_POLICY; |
| } else if (!mDispatchEnabled) { |
| dropReason = DROP_REASON_DISABLED; |
| } |
| |
| if (mNextUnblockedEvent == mPendingEvent) { |
| mNextUnblockedEvent = nullptr; |
| } |
| |
| switch (mPendingEvent->type) { |
| case EventEntry::TYPE_CONFIGURATION_CHANGED: { |
| ConfigurationChangedEntry* typedEntry = |
| static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
| dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped |
| break; |
| } |
| |
| case EventEntry::TYPE_DEVICE_RESET: { |
| DeviceResetEntry* typedEntry = |
| static_cast<DeviceResetEntry*>(mPendingEvent); |
| done = dispatchDeviceResetLocked(currentTime, typedEntry); |
| dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped |
| break; |
| } |
| |
| case EventEntry::TYPE_KEY: { |
| KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| if (isAppSwitchDue) { |
| if (isAppSwitchKeyEvent(typedEntry)) { |
| resetPendingAppSwitchLocked(true); |
| isAppSwitchDue = false; |
| } else if (dropReason == DROP_REASON_NOT_DROPPED) { |
| dropReason = DROP_REASON_APP_SWITCH; |
| } |
| } |
| if (dropReason == DROP_REASON_NOT_DROPPED |
| && isStaleEvent(currentTime, typedEntry)) { |
| dropReason = DROP_REASON_STALE; |
| } |
| if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
| dropReason = DROP_REASON_BLOCKED; |
| } |
| done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| break; |
| } |
| |
| case EventEntry::TYPE_MOTION: { |
| MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
| if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) { |
| dropReason = DROP_REASON_APP_SWITCH; |
| } |
| if (dropReason == DROP_REASON_NOT_DROPPED |
| && isStaleEvent(currentTime, typedEntry)) { |
| dropReason = DROP_REASON_STALE; |
| } |
| if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
| dropReason = DROP_REASON_BLOCKED; |
| } |
| done = dispatchMotionLocked(currentTime, typedEntry, |
| &dropReason, nextWakeupTime); |
| break; |
| } |
| |
| default: |
| ALOG_ASSERT(false); |
| break; |
| } |
| |
| if (done) { |
| if (dropReason != DROP_REASON_NOT_DROPPED) { |
| dropInboundEventLocked(mPendingEvent, dropReason); |
| } |
| mLastDropReason = dropReason; |
| |
| releasePendingEventLocked(); |
| *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
| } |
| } |
| |
| bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
| bool needWake = mInboundQueue.isEmpty(); |
| mInboundQueue.enqueueAtTail(entry); |
| traceInboundQueueLengthLocked(); |
| |
| switch (entry->type) { |
| case EventEntry::TYPE_KEY: { |
| // Optimize app switch latency. |
| // If the application takes too long to catch up then we drop all events preceding |
| // the app switch key. |
| KeyEntry* keyEntry = static_cast<KeyEntry*>(entry); |
| if (isAppSwitchKeyEvent(keyEntry)) { |
| if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) { |
| mAppSwitchSawKeyDown = true; |
| } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| if (mAppSwitchSawKeyDown) { |
| #if DEBUG_APP_SWITCH |
| ALOGD("App switch is pending!"); |
| #endif |
| mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT; |
| mAppSwitchSawKeyDown = false; |
| needWake = true; |
| } |
| } |
| } |
| break; |
| } |
| |
| case EventEntry::TYPE_MOTION: { |
| // Optimize case where the current application is unresponsive and the user |
| // decides to touch a window in a different application. |
| // If the application takes too long to catch up then we drop all events preceding |
| // the touch into the other window. |
| MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN |
| && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
| && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY |
| && mInputTargetWaitApplicationToken != nullptr) { |
| int32_t displayId = motionEntry->displayId; |
| int32_t x = int32_t(motionEntry->pointerCoords[0]. |
| getAxisValue(AMOTION_EVENT_AXIS_X)); |
| int32_t y = int32_t(motionEntry->pointerCoords[0]. |
| getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y); |
| if (touchedWindowHandle != nullptr |
| && touchedWindowHandle->getApplicationToken() |
| != mInputTargetWaitApplicationToken) { |
| // User touched a different application than the one we are waiting on. |
| // Flag the event, and start pruning the input queue. |
| mNextUnblockedEvent = motionEntry; |
| needWake = true; |
| } |
| } |
| break; |
| } |
| } |
| |
| return needWake; |
| } |
| |
| void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| entry->refCount += 1; |
| mRecentQueue.enqueueAtTail(entry); |
| if (mRecentQueue.count() > RECENT_QUEUE_MAX_SIZE) { |
| mRecentQueue.dequeueAtHead()->release(); |
| } |
| } |
| |
| sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, |
| int32_t x, int32_t y, bool addOutsideTargets, bool addPortalWindows) { |
| // Traverse windows from front to back to find touched window. |
| const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
| const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| if (windowInfo->displayId == displayId) { |
| int32_t flags = windowInfo->layoutParamsFlags; |
| |
| if (windowInfo->visible) { |
| if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
| bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
| | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
| if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
| if (portalToDisplayId != ADISPLAY_ID_NONE |
| && portalToDisplayId != displayId) { |
| if (addPortalWindows) { |
| // For the monitoring channels of the display. |
| mTempTouchState.addPortalWindow(windowHandle); |
| } |
| return findTouchedWindowAtLocked( |
| portalToDisplayId, x, y, addOutsideTargets, addPortalWindows); |
| } |
| // Found window. |
| return windowHandle; |
| } |
| } |
| |
| if (addOutsideTargets && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { |
| mTempTouchState.addOrUpdateWindow( |
| windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, BitSet32(0)); |
| } |
| } |
| } |
| } |
| return nullptr; |
| } |
| |
| std::vector<InputDispatcher::TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
| int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) { |
| std::vector<TouchedMonitor> touchedMonitors; |
| |
| std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| addGestureMonitors(monitors, touchedMonitors); |
| for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
| addGestureMonitors(monitors, touchedMonitors, |
| -windowInfo->frameLeft, -windowInfo->frameTop); |
| } |
| return touchedMonitors; |
| } |
| |
| void InputDispatcher::addGestureMonitors(const std::vector<Monitor>& monitors, |
| std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset, float yOffset) { |
| if (monitors.empty()) { |
| return; |
| } |
| outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| for (const Monitor& monitor : monitors) { |
| outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| } |
| } |
| |
| void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) { |
| const char* reason; |
| switch (dropReason) { |
| case DROP_REASON_POLICY: |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("Dropped event because policy consumed it."); |
| #endif |
| reason = "inbound event was dropped because the policy consumed it"; |
| break; |
| case DROP_REASON_DISABLED: |
| if (mLastDropReason != DROP_REASON_DISABLED) { |
| ALOGI("Dropped event because input dispatch is disabled."); |
| } |
| reason = "inbound event was dropped because input dispatch is disabled"; |
| break; |
| case DROP_REASON_APP_SWITCH: |
| ALOGI("Dropped event because of pending overdue app switch."); |
| reason = "inbound event was dropped because of pending overdue app switch"; |
| break; |
| case DROP_REASON_BLOCKED: |
| ALOGI("Dropped event because the current application is not responding and the user " |
| "has started interacting with a different application."); |
| reason = "inbound event was dropped because the current application is not responding " |
| "and the user has started interacting with a different application"; |
| break; |
| case DROP_REASON_STALE: |
| ALOGI("Dropped event because it is stale."); |
| reason = "inbound event was dropped because it is stale"; |
| break; |
| default: |
| ALOG_ASSERT(false); |
| return; |
| } |
| |
| switch (entry->type) { |
| case EventEntry::TYPE_KEY: { |
| CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| synthesizeCancelationEventsForAllConnectionsLocked(options); |
| break; |
| } |
| case EventEntry::TYPE_MOTION: { |
| MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { |
| CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| synthesizeCancelationEventsForAllConnectionsLocked(options); |
| } else { |
| CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| synthesizeCancelationEventsForAllConnectionsLocked(options); |
| } |
| break; |
| } |
| } |
| } |
| |
| static bool isAppSwitchKeyCode(int32_t keyCode) { |
| return keyCode == AKEYCODE_HOME |
| || keyCode == AKEYCODE_ENDCALL |
| || keyCode == AKEYCODE_APP_SWITCH; |
| } |
| |
| bool InputDispatcher::isAppSwitchKeyEvent(KeyEntry* keyEntry) { |
| return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) |
| && isAppSwitchKeyCode(keyEntry->keyCode) |
| && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED) |
| && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER); |
| } |
| |
| bool InputDispatcher::isAppSwitchPendingLocked() { |
| return mAppSwitchDueTime != LONG_LONG_MAX; |
| } |
| |
| void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| mAppSwitchDueTime = LONG_LONG_MAX; |
| |
| #if DEBUG_APP_SWITCH |
| if (handled) { |
| ALOGD("App switch has arrived."); |
| } else { |
| ALOGD("App switch was abandoned."); |
| } |
| #endif |
| } |
| |
| bool InputDispatcher::isStaleEvent(nsecs_t currentTime, EventEntry* entry) { |
| return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT; |
| } |
| |
| bool InputDispatcher::haveCommandsLocked() const { |
| return !mCommandQueue.isEmpty(); |
| } |
| |
| bool InputDispatcher::runCommandsLockedInterruptible() { |
| if (mCommandQueue.isEmpty()) { |
| return false; |
| } |
| |
| do { |
| CommandEntry* commandEntry = mCommandQueue.dequeueAtHead(); |
| |
| Command command = commandEntry->command; |
| (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible' |
| |
| commandEntry->connection.clear(); |
| delete commandEntry; |
| } while (! mCommandQueue.isEmpty()); |
| return true; |
| } |
| |
| InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) { |
| CommandEntry* commandEntry = new CommandEntry(command); |
| mCommandQueue.enqueueAtTail(commandEntry); |
| return commandEntry; |
| } |
| |
| void InputDispatcher::drainInboundQueueLocked() { |
| while (! mInboundQueue.isEmpty()) { |
| EventEntry* entry = mInboundQueue.dequeueAtHead(); |
| releaseInboundEventLocked(entry); |
| } |
| traceInboundQueueLengthLocked(); |
| } |
| |
| void InputDispatcher::releasePendingEventLocked() { |
| if (mPendingEvent) { |
| resetANRTimeoutsLocked(); |
| releaseInboundEventLocked(mPendingEvent); |
| mPendingEvent = nullptr; |
| } |
| } |
| |
| void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| InjectionState* injectionState = entry->injectionState; |
| if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("Injected inbound event was dropped."); |
| #endif |
| setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); |
| } |
| if (entry == mNextUnblockedEvent) { |
| mNextUnblockedEvent = nullptr; |
| } |
| addRecentEventLocked(entry); |
| entry->release(); |
| } |
| |
| void InputDispatcher::resetKeyRepeatLocked() { |
| if (mKeyRepeatState.lastKeyEntry) { |
| mKeyRepeatState.lastKeyEntry->release(); |
| mKeyRepeatState.lastKeyEntry = nullptr; |
| } |
| } |
| |
| InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| |
| // Reuse the repeated key entry if it is otherwise unreferenced. |
| uint32_t policyFlags = entry->policyFlags & |
| (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
| if (entry->refCount == 1) { |
| entry->recycle(); |
| entry->eventTime = currentTime; |
| entry->policyFlags = policyFlags; |
| entry->repeatCount += 1; |
| } else { |
| KeyEntry* newEntry = new KeyEntry(SYNTHESIZED_EVENT_SEQUENCE_NUM, currentTime, |
| entry->deviceId, entry->source, entry->displayId, policyFlags, |
| entry->action, entry->flags, entry->keyCode, entry->scanCode, |
| entry->metaState, entry->repeatCount + 1, entry->downTime); |
| |
| mKeyRepeatState.lastKeyEntry = newEntry; |
| entry->release(); |
| |
| entry = newEntry; |
| } |
| entry->syntheticRepeat = true; |
| |
| // Increment reference count since we keep a reference to the event in |
| // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| entry->refCount += 1; |
| |
| mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| return entry; |
| } |
| |
| bool InputDispatcher::dispatchConfigurationChangedLocked( |
| nsecs_t currentTime, ConfigurationChangedEntry* entry) { |
| #if DEBUG_OUTBOUND_EVENT_DETAILS |
| ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
| #endif |
| |
| // Reset key repeating in case a keyboard device was added or removed or something. |
| resetKeyRepeatLocked(); |
| |
| // Enqueue a command to run outside the lock to tell the policy that the configuration changed. |
| CommandEntry* commandEntry = postCommandLocked( |
| & InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
| commandEntry->eventTime = entry->eventTime; |
| return true; |
| } |
| |
| bool InputDispatcher::dispatchDeviceResetLocked( |
| nsecs_t currentTime, DeviceResetEntry* entry) { |
| #if DEBUG_OUTBOUND_EVENT_DETAILS |
| ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
| entry->deviceId); |
| #endif |
| |
| CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| "device was reset"); |
| options.deviceId = entry->deviceId; |
| synthesizeCancelationEventsForAllConnectionsLocked(options); |
| return true; |
| } |
| |
| bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
| DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| // Preprocessing. |
| if (! entry->dispatchInProgress) { |
| if (entry->repeatCount == 0 |
| && entry->action == AKEY_EVENT_ACTION_DOWN |
| && (entry->policyFlags & POLICY_FLAG_TRUSTED) |
| && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| if (mKeyRepeatState.lastKeyEntry |
| && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
| // We have seen two identical key downs in a row which indicates that the device |
| // driver is automatically generating key repeats itself. We take note of the |
| // repeat here, but we disable our own next key repeat timer since it is clear that |
| // we will not need to synthesize key repeats ourselves. |
| entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| resetKeyRepeatLocked(); |
| mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| } else { |
| // Not a repeat. Save key down state in case we do see a repeat later. |
| resetKeyRepeatLocked(); |
| mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| } |
| mKeyRepeatState.lastKeyEntry = entry; |
| entry->refCount += 1; |
| } else if (! entry->syntheticRepeat) { |
| resetKeyRepeatLocked(); |
| } |
| |
| if (entry->repeatCount == 1) { |
| entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| } else { |
| entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| } |
| |
| entry->dispatchInProgress = true; |
| |
| logOutboundKeyDetails("dispatchKey - ", entry); |
| } |
| |
| // Handle case where the policy asked us to try again later last time. |
| if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| if (currentTime < entry->interceptKeyWakeupTime) { |
| if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| *nextWakeupTime = entry->interceptKeyWakeupTime; |
| } |
| return false; // wait until next wakeup |
| } |
| entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| entry->interceptKeyWakeupTime = 0; |
| } |
| |
| // Give the policy a chance to intercept the key. |
| if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| CommandEntry* commandEntry = postCommandLocked( |
| & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
| sp<InputWindowHandle> focusedWindowHandle = |
| getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(entry)); |
| if (focusedWindowHandle != nullptr) { |
| commandEntry->inputChannel = |
| getInputChannelLocked(focusedWindowHandle->getToken()); |
| } |
| commandEntry->keyEntry = entry; |
| entry->refCount += 1; |
| return false; // wait for the command to run |
| } else { |
| entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| } |
| } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
| if (*dropReason == DROP_REASON_NOT_DROPPED) { |
| *dropReason = DROP_REASON_POLICY; |
| } |
| } |
| |
| // Clean up if dropping the event. |
| if (*dropReason != DROP_REASON_NOT_DROPPED) { |
| setInjectionResult(entry, *dropReason == DROP_REASON_POLICY |
| ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
| mReporter->reportDroppedKey(entry->sequenceNum); |
| return true; |
| } |
| |
| // Identify targets. |
| std::vector<InputTarget> inputTargets; |
| int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime, |
| entry, inputTargets, nextWakeupTime); |
| if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| return false; |
| } |
| |
| setInjectionResult(entry, injectionResult); |
| if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| return true; |
| } |
| |
| // Add monitor channels from event's or focused display. |
| addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(entry)); |
| |
| // Dispatch the key. |
| dispatchEventLocked(currentTime, entry, inputTargets); |
| return true; |
| } |
| |
| void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry* entry) { |
| #if DEBUG_OUTBOUND_EVENT_DETAILS |
| ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| prefix, |
| entry->eventTime, entry->deviceId, entry->source, entry->displayId, entry->policyFlags, |
| entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState, |
| entry->repeatCount, entry->downTime); |
| #endif |
| } |
| |
| bool InputDispatcher::dispatchMotionLocked( |
| nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| ATRACE_CALL(); |
| // Preprocessing. |
| if (! entry->dispatchInProgress) { |
| entry->dispatchInProgress = true; |
| |
| logOutboundMotionDetails("dispatchMotion - ", entry); |
| } |
| |
| // Clean up if dropping the event. |
| if (*dropReason != DROP_REASON_NOT_DROPPED) { |
| setInjectionResult(entry, *dropReason == DROP_REASON_POLICY |
| ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
| return true; |
| } |
| |
| bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| |
| // Identify targets. |
| std::vector<InputTarget> inputTargets; |
| |
| bool conflictingPointerActions = false; |
| int32_t injectionResult; |
| if (isPointerEvent) { |
| // Pointer event. (eg. touchscreen) |
| injectionResult = findTouchedWindowTargetsLocked(currentTime, |
| entry, inputTargets, nextWakeupTime, &conflictingPointerActions); |
| } else { |
| // Non touch event. (eg. trackball) |
| injectionResult = findFocusedWindowTargetsLocked(currentTime, |
| entry, inputTargets, nextWakeupTime); |
| } |
| if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| return false; |
| } |
| |
| setInjectionResult(entry, injectionResult); |
| if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| if (injectionResult != INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| CancelationOptions::Mode mode(isPointerEvent ? |
| CancelationOptions::CANCEL_POINTER_EVENTS : |
| CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| CancelationOptions options(mode, "input event injection failed"); |
| synthesizeCancelationEventsForMonitorsLocked(options); |
| } |
| return true; |
| } |
| |
| // Add monitor channels from event's or focused display. |
| addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(entry)); |
| |
| if (isPointerEvent) { |
| ssize_t stateIndex = mTouchStatesByDisplay.indexOfKey(entry->displayId); |
| if (stateIndex >= 0) { |
| const TouchState& state = mTouchStatesByDisplay.valueAt(stateIndex); |
| if (!state.portalWindows.empty()) { |
| // The event has gone through these portal windows, so we add monitoring targets of |
| // the corresponding displays as well. |
| for (size_t i = 0; i < state.portalWindows.size(); i++) { |
| const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
| addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
| -windowInfo->frameLeft, -windowInfo->frameTop); |
| } |
| } |
| } |
| } |
| |
| // Dispatch the motion. |
| if (conflictingPointerActions) { |
| CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| "conflicting pointer actions"); |
| synthesizeCancelationEventsForAllConnectionsLocked(options); |
| } |
| dispatchEventLocked(currentTime, entry, inputTargets); |
| return true; |
| } |
| |
| |
| void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry* entry) { |
| #if DEBUG_OUTBOUND_EVENT_DETAILS |
| ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| ", policyFlags=0x%x, " |
| "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| "metaState=0x%x, buttonState=0x%x," |
| "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| prefix, |
| entry->eventTime, entry->deviceId, entry->source, entry->displayId, entry->policyFlags, |
| entry->action, entry->actionButton, entry->flags, |
| entry->metaState, entry->buttonState, |
| entry->edgeFlags, entry->xPrecision, entry->yPrecision, |
| entry->downTime); |
| |
| for (uint32_t i = 0; i < entry->pointerCount; i++) { |
| ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| "x=%f, y=%f, pressure=%f, size=%f, " |
| "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| "orientation=%f", |
| i, entry->pointerProperties[i].id, |
| entry->pointerProperties[i].toolType, |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| } |
| #endif |
| } |
| |
| void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| EventEntry* eventEntry, const std::vector<InputTarget>& inputTargets) { |
| ATRACE_CALL(); |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("dispatchEventToCurrentInputTargets"); |
| #endif |
| |
| ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| |
| pokeUserActivityLocked(eventEntry); |
| |
| for (const InputTarget& inputTarget : inputTargets) { |
| ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); |
| if (connectionIndex >= 0) { |
| sp<Connection> 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().c_str()); |
| #endif |
| } |
| } |
| } |
| |
| int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime, |
| const EventEntry* entry, |
| const sp<InputApplicationHandle>& applicationHandle, |
| const sp<InputWindowHandle>& windowHandle, |
| nsecs_t* nextWakeupTime, const char* reason) { |
| if (applicationHandle == nullptr && windowHandle == nullptr) { |
| if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) { |
| #if DEBUG_FOCUS |
| ALOGD("Waiting for system to become ready for input. Reason: %s", reason); |
| #endif |
| mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY; |
| mInputTargetWaitStartTime = currentTime; |
| mInputTargetWaitTimeoutTime = LONG_LONG_MAX; |
| mInputTargetWaitTimeoutExpired = false; |
| mInputTargetWaitApplicationToken.clear(); |
| } |
| } else { |
| if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| #if DEBUG_FOCUS |
| ALOGD("Waiting for application to become ready for input: %s. Reason: %s", |
| getApplicationWindowLabel(applicationHandle, windowHandle).c_str(), |
| reason); |
| #endif |
| nsecs_t timeout; |
| if (windowHandle != nullptr) { |
| timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| } else if (applicationHandle != nullptr) { |
| timeout = applicationHandle->getDispatchingTimeout( |
| DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| } else { |
| timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
| } |
| |
| mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY; |
| mInputTargetWaitStartTime = currentTime; |
| mInputTargetWaitTimeoutTime = currentTime + timeout; |
| mInputTargetWaitTimeoutExpired = false; |
| mInputTargetWaitApplicationToken.clear(); |
| |
| if (windowHandle != nullptr) { |
| mInputTargetWaitApplicationToken = windowHandle->getApplicationToken(); |
| } |
| if (mInputTargetWaitApplicationToken == nullptr && applicationHandle != nullptr) { |
| mInputTargetWaitApplicationToken = applicationHandle->getApplicationToken(); |
| } |
| } |
| } |
| |
| if (mInputTargetWaitTimeoutExpired) { |
| return INPUT_EVENT_INJECTION_TIMED_OUT; |
| } |
| |
| if (currentTime >= mInputTargetWaitTimeoutTime) { |
| onANRLocked(currentTime, applicationHandle, windowHandle, |
| entry->eventTime, mInputTargetWaitStartTime, reason); |
| |
| // Force poll loop to wake up immediately on next iteration once we get the |
| // ANR response back from the policy. |
| *nextWakeupTime = LONG_LONG_MIN; |
| return INPUT_EVENT_INJECTION_PENDING; |
| } else { |
| // Force poll loop to wake up when timeout is due. |
| if (mInputTargetWaitTimeoutTime < *nextWakeupTime) { |
| *nextWakeupTime = mInputTargetWaitTimeoutTime; |
| } |
| return INPUT_EVENT_INJECTION_PENDING; |
| } |
| } |
| |
| void InputDispatcher::removeWindowByTokenLocked(const sp<IBinder>& token) { |
| for (size_t d = 0; d < mTouchStatesByDisplay.size(); d++) { |
| TouchState& state = mTouchStatesByDisplay.editValueAt(d); |
| state.removeWindowByToken(token); |
| } |
| } |
| |
| void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
| const sp<InputChannel>& inputChannel) { |
| if (newTimeout > 0) { |
| // Extend the timeout. |
| mInputTargetWaitTimeoutTime = now() + newTimeout; |
| } else { |
| // Give up. |
| mInputTargetWaitTimeoutExpired = true; |
| |
| // Input state will not be realistic. Mark it out of sync. |
| if (inputChannel.get()) { |
| ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
| if (connectionIndex >= 0) { |
| sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| sp<IBinder> token = connection->inputChannel->getToken(); |
| |
| if (token != nullptr) { |
| removeWindowByTokenLocked(token); |
| } |
| |
| if (connection->status == Connection::STATUS_NORMAL) { |
| CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| "application not responding"); |
| synthesizeCancelationEventsForConnectionLocked(connection, options); |
| } |
| } |
| } |
| } |
| } |
| |
| nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked( |
| nsecs_t currentTime) { |
| if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| return currentTime - mInputTargetWaitStartTime; |
| } |
| return 0; |
| } |
| |
| void InputDispatcher::resetANRTimeoutsLocked() { |
| #if DEBUG_FOCUS |
| ALOGD("Resetting ANR timeouts."); |
| #endif |
| |
| // Reset input target wait timeout. |
| mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; |
| mInputTargetWaitApplicationToken.clear(); |
| } |
| |
| /** |
| * Get the display id that the given event should go to. If this event specifies a valid display id, |
| * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| * Focused display is the display that the user most recently interacted with. |
| */ |
| int32_t InputDispatcher::getTargetDisplayId(const EventEntry* entry) { |
| int32_t displayId; |
| switch (entry->type) { |
| case EventEntry::TYPE_KEY: { |
| const KeyEntry* typedEntry = static_cast<const KeyEntry*>(entry); |
| displayId = typedEntry->displayId; |
| break; |
| } |
| case EventEntry::TYPE_MOTION: { |
| const MotionEntry* typedEntry = static_cast<const MotionEntry*>(entry); |
| displayId = typedEntry->displayId; |
| break; |
| } |
| default: { |
| ALOGE("Unsupported event type '%" PRId32 "' for target display.", entry->type); |
| return ADISPLAY_ID_NONE; |
| } |
| } |
| return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| } |
| |
| int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
| const EventEntry* entry, std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) { |
| int32_t injectionResult; |
| std::string reason; |
| |
| int32_t displayId = getTargetDisplayId(entry); |
| sp<InputWindowHandle> focusedWindowHandle = |
| getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| sp<InputApplicationHandle> focusedApplicationHandle = |
| getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| |
| // If there is no currently focused window and no focused application |
| // then drop the event. |
| if (focusedWindowHandle == nullptr) { |
| if (focusedApplicationHandle != nullptr) { |
| injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
| focusedApplicationHandle, nullptr, nextWakeupTime, |
| "Waiting because no window has focus but there is a " |
| "focused application that may eventually add a window " |
| "when it finishes starting up."); |
| goto Unresponsive; |
| } |
| |
| ALOGI("Dropping event because there is no focused window or focused application in display " |
| "%" PRId32 ".", displayId); |
| injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| goto Failed; |
| } |
| |
| // Check permissions. |
| if (!checkInjectionPermission(focusedWindowHandle, entry->injectionState)) { |
| injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| goto Failed; |
| } |
| |
| // Check whether the window is ready for more input. |
| reason = checkWindowReadyForMoreInputLocked(currentTime, |
| focusedWindowHandle, entry, "focused"); |
| if (!reason.empty()) { |
| injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
| focusedApplicationHandle, focusedWindowHandle, nextWakeupTime, reason.c_str()); |
| goto Unresponsive; |
| } |
| |
| // Success! Output targets. |
| injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| addWindowTargetLocked(focusedWindowHandle, |
| InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0), |
| inputTargets); |
| |
| // Done. |
| Failed: |
| Unresponsive: |
| nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
| updateDispatchStatistics(currentTime, entry, injectionResult, timeSpentWaitingForApplication); |
| #if DEBUG_FOCUS |
| ALOGD("findFocusedWindow finished: injectionResult=%d, " |
| "timeSpentWaitingForApplication=%0.1fms", |
| injectionResult, timeSpentWaitingForApplication / 1000000.0); |
| #endif |
| return injectionResult; |
| } |
| |
| int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
| const MotionEntry* entry, std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime, |
| bool* outConflictingPointerActions) { |
| ATRACE_CALL(); |
| enum InjectionPermission { |
| INJECTION_PERMISSION_UNKNOWN, |
| INJECTION_PERMISSION_GRANTED, |
| INJECTION_PERMISSION_DENIED |
| }; |
| |
| // For security reasons, we defer updating the touch state until we are sure that |
| // event injection will be allowed. |
| int32_t displayId = entry->displayId; |
| int32_t action = entry->action; |
| int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| |
| // Update the touch state as needed based on the properties of the touch event. |
| int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
| sp<InputWindowHandle> newHoverWindowHandle; |
| |
| // Copy current touch state into mTempTouchState. |
| // This state is always reset at the end of this function, so if we don't find state |
| // for the specified display then our initial state will be empty. |
| const TouchState* oldState = nullptr; |
| ssize_t oldStateIndex = mTouchStatesByDisplay.indexOfKey(displayId); |
| if (oldStateIndex >= 0) { |
| oldState = &mTouchStatesByDisplay.valueAt(oldStateIndex); |
| mTempTouchState.copyFrom(*oldState); |
| } |
| |
| bool isSplit = mTempTouchState.split; |
| bool switchedDevice = mTempTouchState.deviceId >= 0 && mTempTouchState.displayId >= 0 |
| && (mTempTouchState.deviceId != entry->deviceId |
| || mTempTouchState.source != entry->source |
| || mTempTouchState.displayId != displayId); |
| bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
| || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
| || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN |
| || maskedAction == AMOTION_EVENT_ACTION_SCROLL |
| || isHoverAction); |
| bool wrongDevice = false; |
| if (newGesture) { |
| bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
| if (switchedDevice && mTempTouchState.down && !down && !isHoverAction) { |
| #if DEBUG_FOCUS |
| ALOGD("Dropping event because a pointer for a different device is already down " |
| "in display %" PRId32, displayId); |
| #endif |
| // TODO: test multiple simultaneous input streams. |
| injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| switchedDevice = false; |
| wrongDevice = true; |
| goto Failed; |
| } |
| mTempTouchState.reset(); |
| mTempTouchState.down = down; |
| mTempTouchState.deviceId = entry->deviceId; |
| mTempTouchState.source = entry->source; |
| mTempTouchState.displayId = displayId; |
| isSplit = false; |
| } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
| #if DEBUG_FOCUS |
| ALOGI("Dropping move event because a pointer for a different device is already active " |
| "in display %" PRId32, displayId); |
| #endif |
| // TODO: test multiple simultaneous input streams. |
| injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| switchedDevice = false; |
| wrongDevice = true; |
| goto Failed; |
| } |
| |
| if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| |
| int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| int32_t x = int32_t(entry->pointerCoords[pointerIndex]. |
| getAxisValue(AMOTION_EVENT_AXIS_X)); |
| int32_t y = int32_t(entry->pointerCoords[pointerIndex]. |
| getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
| sp<InputWindowHandle> newTouchedWindowHandle = findTouchedWindowAtLocked( |
| displayId, x, y, isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
| |
| std::vector<TouchedMonitor> newGestureMonitors = isDown |
| ? findTouchedGestureMonitorsLocked(displayId, mTempTouchState.portalWindows) |
| : std::vector<TouchedMonitor>{}; |
| |
| // Figure out whether splitting will be allowed for this window. |
| if (newTouchedWindowHandle != nullptr |
| && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| // New window supports splitting. |
| isSplit = true; |
| } else if (isSplit) { |
| // New window does not support splitting but we have already split events. |
| // Ignore the new window. |
| newTouchedWindowHandle = nullptr; |
| } |
| |
| // Handle the case where we did not find a window. |
| if (newTouchedWindowHandle == nullptr) { |
| // Try to assign the pointer to the first foreground window we find, if there is one. |
| newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle(); |
| } |
| |
| if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| ALOGI("Dropping event because there is no touchable window or gesture monitor at " |
| "(%d, %d) in display %" PRId32 ".", x, y, displayId); |
| injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| goto Failed; |
| } |
| |
| if (newTouchedWindowHandle != nullptr) { |
| // Set target flags. |
| int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| if (isSplit) { |
| targetFlags |= InputTarget::FLAG_SPLIT; |
| } |
| if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| } |
| |
| // Update hover state. |
| if (isHoverAction) { |
| newHoverWindowHandle = newTouchedWindowHandle; |
| } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| newHoverWindowHandle = mLastHoverWindowHandle; |
| } |
| |
| // Update the temporary touch state. |
| BitSet32 pointerIds; |
| if (isSplit) { |
| uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
| pointerIds.markBit(pointerId); |
| } |
| mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
| } |
| |
| mTempTouchState.addGestureMonitors(newGestureMonitors); |
| } else { |
| /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| |
| // If the pointer is not currently down, then ignore the event. |
| if (! mTempTouchState.down) { |
| #if DEBUG_FOCUS |
| ALOGD("Dropping event because the pointer is not down or we previously " |
| "dropped the pointer down event in display %" PRId32, displayId); |
| #endif |
| injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| goto Failed; |
| } |
| |
| // Check whether touches should slip outside of the current foreground window. |
| if (maskedAction == AMOTION_EVENT_ACTION_MOVE |
| && entry->pointerCount == 1 |
| && mTempTouchState.isSlippery()) { |
| int32_t x = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| int32_t y = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| |
| sp<InputWindowHandle> oldTouchedWindowHandle = |
| mTempTouchState.getFirstForegroundWindowHandle(); |
| sp<InputWindowHandle> newTouchedWindowHandle = |
| findTouchedWindowAtLocked(displayId, x, y); |
| if (oldTouchedWindowHandle != newTouchedWindowHandle |
| && oldTouchedWindowHandle != nullptr |
| && newTouchedWindowHandle != nullptr) { |
| #if DEBUG_FOCUS |
| ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| oldTouchedWindowHandle->getName().c_str(), |
| newTouchedWindowHandle->getName().c_str(), |
| displayId); |
| #endif |
| // Make a slippery exit from the old window. |
| mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0)); |
| |
| // Make a slippery entrance into the new window. |
| if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| isSplit = true; |
| } |
| |
| int32_t targetFlags = InputTarget::FLAG_FOREGROUND |
| | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| if (isSplit) { |
| targetFlags |= InputTarget::FLAG_SPLIT; |
| } |
| if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| } |
| |
| BitSet32 pointerIds; |
| if (isSplit) { |
| pointerIds.markBit(entry->pointerProperties[0].id); |
| } |
| mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
| } |
| } |
| } |
| |
| if (newHoverWindowHandle != mLastHoverWindowHandle) { |
| // Let the previous window know that the hover sequence is over. |
| if (mLastHoverWindowHandle != nullptr) { |
| #if DEBUG_HOVER |
| ALOGD("Sending hover exit event to window %s.", |
| mLastHoverWindowHandle->getName().c_str()); |
| #endif |
| mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
| } |
| |
| // Let the new window know that the hover sequence is starting. |
| if (newHoverWindowHandle != nullptr) { |
| #if DEBUG_HOVER |
| ALOGD("Sending hover enter event to window %s.", |
| newHoverWindowHandle->getName().c_str()); |
| #endif |
| mTempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0)); |
| } |
| } |
| |
| // Check permission to inject into all touched foreground windows and ensure there |
| // is at least one touched foreground window. |
| { |
| bool haveForegroundWindow = false; |
| for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
| if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| haveForegroundWindow = true; |
| if (! checkInjectionPermission(touchedWindow.windowHandle, |
| entry->injectionState)) { |
| injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| injectionPermission = INJECTION_PERMISSION_DENIED; |
| goto Failed; |
| } |
| } |
| } |
| bool hasGestureMonitor = !mTempTouchState.gestureMonitors.empty(); |
| if (!haveForegroundWindow && !hasGestureMonitor) { |
| #if DEBUG_FOCUS |
| ALOGD("Dropping event because there is no touched foreground window in display %" |
| PRId32 " or gesture monitor to receive it.", displayId); |
| #endif |
| injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| goto Failed; |
| } |
| |
| // Permission granted to injection into all touched foreground windows. |
| injectionPermission = INJECTION_PERMISSION_GRANTED; |
| } |
| |
| // Check whether windows listening for outside touches are owned by the same UID. If it is |
| // set the policy flag that we will not reveal coordinate information to this window. |
| if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| sp<InputWindowHandle> foregroundWindowHandle = |
| mTempTouchState.getFirstForegroundWindowHandle(); |
| if (foregroundWindowHandle) { |
| const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
| for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
| if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| mTempTouchState.addOrUpdateWindow(inputWindowHandle, |
| InputTarget::FLAG_ZERO_COORDS, BitSet32(0)); |
| } |
| } |
| } |
| } |
| } |
| |
| // Ensure all touched foreground windows are ready for new input. |
| for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
| if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| // Check whether the window is ready for more input. |
| std::string reason = checkWindowReadyForMoreInputLocked(currentTime, |
| touchedWindow.windowHandle, entry, "touched"); |
| if (!reason.empty()) { |
| injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
| nullptr, touchedWindow.windowHandle, nextWakeupTime, reason.c_str()); |
| goto Unresponsive; |
| } |
| } |
| } |
| |
| // If this is the first pointer going down and the touched window has a wallpaper |
| // then also add the touched wallpaper windows so they are locked in for the duration |
| // of the touch gesture. |
| // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| // engine only supports touch events. We would need to add a mechanism similar |
| // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| sp<InputWindowHandle> foregroundWindowHandle = |
| mTempTouchState.getFirstForegroundWindowHandle(); |
| if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
| const std::vector<sp<InputWindowHandle>> windowHandles = |
| getWindowHandlesLocked(displayId); |
| for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
| const InputWindowInfo* info = windowHandle->getInfo(); |
| if (info->displayId == displayId |
| && windowHandle->getInfo()->layoutParamsType |
| == InputWindowInfo::TYPE_WALLPAPER) { |
| mTempTouchState.addOrUpdateWindow(windowHandle, |
| InputTarget::FLAG_WINDOW_IS_OBSCURED |
| | InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED |
| | InputTarget::FLAG_DISPATCH_AS_IS, |
| BitSet32(0)); |
| } |
| } |
| } |
| } |
| |
| // Success! Output targets. |
| injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| |
| for (const TouchedWindow& touchedWindow : mTempTouchState.windows) { |
| addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| touchedWindow.pointerIds, inputTargets); |
| } |
| |
| for (const TouchedMonitor& touchedMonitor : mTempTouchState.gestureMonitors) { |
| addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
| touchedMonitor.yOffset, inputTargets); |
| } |
| |
| // Drop the outside or hover touch windows since we will not care about them |
| // in the next iteration. |
| mTempTouchState.filterNonAsIsTouchWindows(); |
| |
| Failed: |
| // Check injection permission once and for all. |
| if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
| if (checkInjectionPermission(nullptr, entry->injectionState)) { |
| injectionPermission = INJECTION_PERMISSION_GRANTED; |
| } else { |
| injectionPermission = INJECTION_PERMISSION_DENIED; |
| } |
| } |
| |
| // Update final pieces of touch state if the injector had permission. |
| if (injectionPermission == INJECTION_PERMISSION_GRANTED) { |
| if (!wrongDevice) { |
| if (switchedDevice) { |
| #if DEBUG_FOCUS |
| ALOGD("Conflicting pointer actions: Switched to a different device."); |
| #endif |
| *outConflictingPointerActions = true; |
| } |
| |
| if (isHoverAction) { |
| // Started hovering, therefore no longer down. |
| if (oldState && oldState->down) { |
| #if DEBUG_FOCUS |
| ALOGD("Conflicting pointer actions: Hover received while pointer was down."); |
| #endif |
| *outConflictingPointerActions = true; |
| } |
| mTempTouchState.reset(); |
| if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
| || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| mTempTouchState.deviceId = entry->deviceId; |
| mTempTouchState.source = entry->source; |
| mTempTouchState.displayId = displayId; |
| } |
| } else if (maskedAction == AMOTION_EVENT_ACTION_UP |
| || maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| // All pointers up or canceled. |
| mTempTouchState.reset(); |
| } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| // First pointer went down. |
| if (oldState && oldState->down) { |
| #if DEBUG_FOCUS |
| ALOGD("Conflicting pointer actions: Down received while already down."); |
| #endif |
| *outConflictingPointerActions = true; |
| } |
| } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| // One pointer went up. |
| if (isSplit) { |
| int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
| |
| for (size_t i = 0; i < mTempTouchState.windows.size(); ) { |
| TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| touchedWindow.pointerIds.clearBit(pointerId); |
| if (touchedWindow.pointerIds.isEmpty()) { |
| mTempTouchState.windows.erase(mTempTouchState.windows.begin() + i); |
| continue; |
| } |
| } |
| i += 1; |
| } |
| } |
| } |
| |
| // Save changes unless the action was scroll in which case the temporary touch |
| // state was only valid for this one action. |
| if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| if (mTempTouchState.displayId >= 0) { |
| if (oldStateIndex >= 0) { |
| mTouchStatesByDisplay.editValueAt(oldStateIndex).copyFrom(mTempTouchState); |
| } else { |
| mTouchStatesByDisplay.add(displayId, mTempTouchState); |
| } |
| } else if (oldStateIndex >= 0) { |
| mTouchStatesByDisplay.removeItemsAt(oldStateIndex); |
| } |
| } |
| |
| // Update hover state. |
| mLastHoverWindowHandle = newHoverWindowHandle; |
| } |
| } else { |
| #if DEBUG_FOCUS |
| ALOGD("Not updating touch focus because injection was denied."); |
| #endif |
| } |
| |
| Unresponsive: |
| // Reset temporary touch state to ensure we release unnecessary references to input channels. |
| mTempTouchState.reset(); |
| |
| nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
| updateDispatchStatistics(currentTime, entry, injectionResult, timeSpentWaitingForApplication); |
| #if DEBUG_FOCUS |
| ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " |
| "timeSpentWaitingForApplication=%0.1fms", |
| injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); |
| #endif |
| return injectionResult; |
| } |
| |
| void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
| int32_t targetFlags, BitSet32 pointerIds, std::vector<InputTarget>& inputTargets) { |
| sp<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| if (inputChannel == nullptr) { |
| ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| return; |
| } |
| |
| const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| InputTarget target; |
| target.inputChannel = inputChannel; |
| target.flags = targetFlags; |
| target.xOffset = - windowInfo->frameLeft; |
| target.yOffset = - windowInfo->frameTop; |
| target.globalScaleFactor = windowInfo->globalScaleFactor; |
| target.windowXScale = windowInfo->windowXScale; |
| target.windowYScale = windowInfo->windowYScale; |
| target.pointerIds = pointerIds; |
| inputTargets.push_back(target); |
| } |
| |
| void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
| int32_t displayId, float xOffset, float yOffset) { |
| |
| std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| mGlobalMonitorsByDisplay.find(displayId); |
| |
| if (it != mGlobalMonitorsByDisplay.end()) { |
| const std::vector<Monitor>& monitors = it->second; |
| for (const Monitor& monitor : monitors) { |
| addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
| } |
| } |
| } |
| |
| void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, |
| float xOffset, float yOffset, std::vector<InputTarget>& inputTargets) { |
| InputTarget target; |
| target.inputChannel = monitor.inputChannel; |
| target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| target.xOffset = xOffset; |
| target.yOffset = yOffset; |
| target.pointerIds.clear(); |
| target.globalScaleFactor = 1.0f; |
| inputTargets.push_back(target); |
| } |
| |
| bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
| const InjectionState* injectionState) { |
| if (injectionState |
| && (windowHandle == nullptr |
| || windowHandle->getInfo()->ownerUid != injectionState->injectorUid) |
| && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
| if (windowHandle != nullptr) { |
| ALOGW("Permission denied: injecting event from pid %d uid %d to window %s " |
| "owned by uid %d", |
| injectionState->injectorPid, injectionState->injectorUid, |
| windowHandle->getName().c_str(), |
| windowHandle->getInfo()->ownerUid); |
| } else { |
| ALOGW("Permission denied: injecting event from pid %d uid %d", |
| injectionState->injectorPid, injectionState->injectorUid); |
| } |
| return false; |
| } |
| return true; |
| } |
| |
| bool InputDispatcher::isWindowObscuredAtPointLocked( |
| const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
| int32_t displayId = windowHandle->getInfo()->displayId; |
| const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| if (otherHandle == windowHandle) { |
| break; |
| } |
| |
| const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
| if (otherInfo->displayId == displayId |
| && otherInfo->visible && !otherInfo->isTrustedOverlay() |
| && otherInfo->frameContainsPoint(x, y)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| |
| bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| int32_t displayId = windowHandle->getInfo()->displayId; |
| const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| if (otherHandle == windowHandle) { |
| break; |
| } |
| |
| const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
| if (otherInfo->displayId == displayId |
| && otherInfo->visible && !otherInfo->isTrustedOverlay() |
| && otherInfo->overlaps(windowInfo)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| std::string InputDispatcher::checkWindowReadyForMoreInputLocked(nsecs_t currentTime, |
| const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry, |
| const char* targetType) { |
| // If the window is paused then keep waiting. |
| if (windowHandle->getInfo()->paused) { |
| return StringPrintf("Waiting because the %s window is paused.", targetType); |
| } |
| |
| // If the window's connection is not registered then keep waiting. |
| ssize_t connectionIndex = getConnectionIndexLocked( |
| getInputChannelLocked(windowHandle->getToken())); |
| if (connectionIndex < 0) { |
| return StringPrintf("Waiting because the %s window's input channel is not " |
| "registered with the input dispatcher. The window may be in the process " |
| "of being removed.", targetType); |
| } |
| |
| // If the connection is dead then keep waiting. |
| sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| if (connection->status != Connection::STATUS_NORMAL) { |
| return StringPrintf("Waiting because the %s window's input connection is %s." |
| "The window may be in the process of being removed.", targetType, |
| connection->getStatusLabel()); |
| } |
| |
| // If the connection is backed up then keep waiting. |
| if (connection->inputPublisherBlocked) { |
| return StringPrintf("Waiting because the %s window's input channel is full. " |
| "Outbound queue length: %d. Wait queue length: %d.", |
| targetType, connection->outboundQueue.count(), connection->waitQueue.count()); |
| } |
| |
| // Ensure that the dispatch queues aren't too far backed up for this event. |
| if (eventEntry->type == EventEntry::TYPE_KEY) { |
| // If the event is a key event, then we must wait for all previous events to |
| // complete before delivering it because previous events may have the |
| // side-effect of transferring focus to a different window and we want to |
| // ensure that the following keys are sent to the new window. |
| // |
| // Suppose the user touches a button in a window then immediately presses "A". |
| // If the button causes a pop-up window to appear then we want to ensure that |
| // the "A" key is delivered to the new pop-up window. This is because users |
| // often anticipate pending UI changes when typing on a keyboard. |
| // To obtain this behavior, we must serialize key events with respect to all |
| // prior input events. |
| if (!connection->outboundQueue.isEmpty() || !connection->waitQueue.isEmpty()) { |
| return StringPrintf("Waiting to send key event because the %s window has not " |
| "finished processing all of the input events that were previously " |
| "delivered to it. Outbound queue length: %d. Wait queue length: %d.", |
| targetType, connection->outboundQueue.count(), connection->waitQueue.count()); |
| } |
| } else { |
| // Touch events can always be sent to a window immediately because the user intended |
| // to touch whatever was visible at the time. Even if focus changes or a new |
| // window appears moments later, the touch event was meant to be delivered to |
| // whatever window happened to be on screen at the time. |
| // |
| // Generic motion events, such as trackball or joystick events are a little trickier. |
| // Like key events, generic motion events are delivered to the focused window. |
| // Unlike key events, generic motion events don't tend to transfer focus to other |
| // windows and it is not important for them to be serialized. So we prefer to deliver |
| // generic motion events as soon as possible to improve efficiency and reduce lag |
| // through batching. |
| // |
| // The one case where we pause input event delivery is when the wait queue is piling |
| // up with lots of events because the application is not responding. |
| // This condition ensures that ANRs are detected reliably. |
| if (!connection->waitQueue.isEmpty() |
| && currentTime >= connection->waitQueue.head->deliveryTime |
| + STREAM_AHEAD_EVENT_TIMEOUT) { |
| return StringPrintf("Waiting to send non-key event because the %s window has not " |
| "finished processing certain input events that were delivered to it over " |
| "%0.1fms ago. Wait queue length: %d. Wait queue head age: %0.1fms.", |
| targetType, STREAM_AHEAD_EVENT_TIMEOUT * 0.000001f, |
| connection->waitQueue.count(), |
| (currentTime - connection->waitQueue.head->deliveryTime) * 0.000001f); |
| } |
| } |
| return ""; |
| } |
| |
| std::string InputDispatcher::getApplicationWindowLabel( |
| const sp<InputApplicationHandle>& applicationHandle, |
| const sp<InputWindowHandle>& windowHandle) { |
| if (applicationHandle != nullptr) { |
| if (windowHandle != nullptr) { |
| std::string label(applicationHandle->getName()); |
| label += " - "; |
| label += windowHandle->getName(); |
| return label; |
| } else { |
| return applicationHandle->getName(); |
| } |
| } else if (windowHandle != nullptr) { |
| return windowHandle->getName(); |
| } else { |
| return "<unknown application or window>"; |
| } |
| } |
| |
| void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) { |
| int32_t displayId = getTargetDisplayId(eventEntry); |
| sp<InputWindowHandle> focusedWindowHandle = |
| getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| if (focusedWindowHandle != nullptr) { |
| const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
| if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| #endif |
| return; |
| } |
| } |
| |
| int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
| switch (eventEntry->type) { |
| case EventEntry::TYPE_MOTION: { |
| const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry); |
| if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| return; |
| } |
| |
| if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) { |
| eventType = USER_ACTIVITY_EVENT_TOUCH; |
| } |
| break; |
| } |
| case EventEntry::TYPE_KEY: { |
| const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry); |
| if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) { |
| return; |
| } |
| eventType = USER_ACTIVITY_EVENT_BUTTON; |
| break; |
| } |
| } |
| |
| CommandEntry* commandEntry = postCommandLocked( |
| & InputDispatcher::doPokeUserActivityLockedInterruptible); |
| commandEntry->eventTime = eventEntry->eventTime; |
| commandEntry->userActivityEventType = eventType; |
| } |
| |
| void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
| const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) { |
| if (ATRACE_ENABLED()) { |
| std::string message = StringPrintf( |
| "prepareDispatchCycleLocked(inputChannel=%s, sequenceNum=%" PRIu32 ")", |
| connection->getInputChannelName().c_str(), eventEntry->sequenceNum); |
| ATRACE_NAME(message.c_str()); |
| } |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| "xOffset=%f, yOffset=%f, globalScaleFactor=%f, " |
| "windowScaleFactor=(%f, %f), pointerIds=0x%x", |
| connection->getInputChannelName().c_str(), inputTarget->flags, |
| inputTarget->xOffset, inputTarget->yOffset, |
| inputTarget->globalScaleFactor, |
| inputTarget->windowXScale, inputTarget->windowYScale, |
| inputTarget->pointerIds.value); |
| #endif |
| |
| // Skip this event if the connection status is not normal. |
| // We don't want to enqueue additional outbound events if the connection is broken. |
| if (connection->status != Connection::STATUS_NORMAL) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
| connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
| #endif |
| return; |
| } |
| |
| // Split a motion event if needed. |
| if (inputTarget->flags & InputTarget::FLAG_SPLIT) { |
| ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION); |
| |
| MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry); |
| if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) { |
| MotionEntry* splitMotionEntry = splitMotionEvent( |
| originalMotionEntry, inputTarget->pointerIds); |
| if (!splitMotionEntry) { |
| return; // split event was dropped |
| } |
| #if DEBUG_FOCUS |
| ALOGD("channel '%s' ~ Split motion event.", |
| connection->getInputChannelName().c_str()); |
| logOutboundMotionDetails(" ", splitMotionEntry); |
| #endif |
| enqueueDispatchEntriesLocked(currentTime, connection, |
| splitMotionEntry, inputTarget); |
| splitMotionEntry->release(); |
| return; |
| } |
| } |
| |
| // Not splitting. Enqueue dispatch entries for the event as is. |
| enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| } |
| |
| void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
| const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) { |
| if (ATRACE_ENABLED()) { |
| std::string message = StringPrintf( |
| "enqueueDispatchEntriesLocked(inputChannel=%s, sequenceNum=%" PRIu32 ")", |
| connection->getInputChannelName().c_str(), eventEntry->sequenceNum); |
| ATRACE_NAME(message.c_str()); |
| } |
| |
| bool wasEmpty = connection->outboundQueue.isEmpty(); |
| |
| // Enqueue dispatch entries for the requested modes. |
| enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
| enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
| enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
| enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| InputTarget::FLAG_DISPATCH_AS_IS); |
| enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
| enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
| |
| // If the outbound queue was previously empty, start the dispatch cycle going. |
| if (wasEmpty && !connection->outboundQueue.isEmpty()) { |
| startDispatchCycleLocked(currentTime, connection); |
| } |
| } |
| |
| void InputDispatcher::enqueueDispatchEntryLocked( |
| const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
| int32_t dispatchMode) { |
| if (ATRACE_ENABLED()) { |
| std::string message = StringPrintf( |
| "enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| connection->getInputChannelName().c_str(), |
| dispatchModeToString(dispatchMode).c_str()); |
| ATRACE_NAME(message.c_str()); |
| } |
| int32_t inputTargetFlags = inputTarget->flags; |
| if (!(inputTargetFlags & dispatchMode)) { |
| return; |
| } |
| inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| |
| // This is a new event. |
| // Enqueue a new dispatch entry onto the outbound queue for this connection. |
| DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref |
| inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset, |
| inputTarget->globalScaleFactor, inputTarget->windowXScale, |
| inputTarget->windowYScale); |
| |
| // Apply target flags and update the connection's input state. |
| switch (eventEntry->type) { |
| case EventEntry::TYPE_KEY: { |
| KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| dispatchEntry->resolvedAction = keyEntry->action; |
| dispatchEntry->resolvedFlags = keyEntry->flags; |
| |
| if (!connection->inputState.trackKey(keyEntry, |
| dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| connection->getInputChannelName().c_str()); |
| #endif |
| delete dispatchEntry; |
| return; // skip the inconsistent event |
| } |
| break; |
| } |
| |
| case EventEntry::TYPE_MOTION: { |
| MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
| if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| } else { |
| dispatchEntry->resolvedAction = motionEntry->action; |
| } |
| if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
| && !connection->inputState.isHovering( |
| motionEntry->deviceId, motionEntry->source, motionEntry->displayId)) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event", |
| connection->getInputChannelName().c_str()); |
| #endif |
| dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| } |
| |
| dispatchEntry->resolvedFlags = motionEntry->flags; |
| if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| } |
| if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| } |
| |
| if (!connection->inputState.trackMotion(motionEntry, |
| dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event", |
| connection->getInputChannelName().c_str()); |
| #endif |
| delete dispatchEntry; |
| return; // skip the inconsistent event |
| } |
| |
| dispatchPointerDownOutsideFocus(motionEntry->source, |
| dispatchEntry->resolvedAction, inputTarget->inputChannel->getToken()); |
| |
| break; |
| } |
| } |
| |
| // Remember that we are waiting for this dispatch to complete. |
| if (dispatchEntry->hasForegroundTarget()) { |
| incrementPendingForegroundDispatches(eventEntry); |
| } |
| |
| // Enqueue the dispatch entry. |
| connection->outboundQueue.enqueueAtTail(dispatchEntry); |
| traceOutboundQueueLength(connection); |
| |
| } |
| |
| void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
| const sp<IBinder>& newToken) { |
| int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
| return; |
| } |
| |
| sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); |
| if (inputWindowHandle == nullptr) { |
| return; |
| } |
| |
| sp<InputWindowHandle> focusedWindowHandle = |
| getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
| |
| bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; |
| |
| if (!hasFocusChanged) { |
| return; |
| } |
| |
| CommandEntry* commandEntry = postCommandLocked( |
| & InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
| commandEntry->newToken = newToken; |
| } |
| |
| void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
| const sp<Connection>& connection) { |
| if (ATRACE_ENABLED()) { |
| std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| connection->getInputChannelName().c_str()); |
| ATRACE_NAME(message.c_str()); |
| } |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ startDispatchCycle", |
| connection->getInputChannelName().c_str()); |
| #endif |
| |
| while (connection->status == Connection::STATUS_NORMAL |
| && !connection->outboundQueue.isEmpty()) { |
| DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
| dispatchEntry->deliveryTime = currentTime; |
| |
| // Publish the event. |
| status_t status; |
| EventEntry* eventEntry = dispatchEntry->eventEntry; |
| switch (eventEntry->type) { |
| case EventEntry::TYPE_KEY: { |
| KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| |
| // Publish the key event. |
| status = connection->inputPublisher.publishKeyEvent(dispatchEntry->seq, |
| keyEntry->deviceId, keyEntry->source, keyEntry->displayId, |
| dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags, |
| keyEntry->keyCode, keyEntry->scanCode, |
| keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime, |
| keyEntry->eventTime); |
| break; |
| } |
| |
| case EventEntry::TYPE_MOTION: { |
| MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
| |
| PointerCoords scaledCoords[MAX_POINTERS]; |
| const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| |
| // Set the X and Y offset depending on the input source. |
| float xOffset, yOffset; |
| if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
| && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| float globalScaleFactor = dispatchEntry->globalScaleFactor; |
| float wxs = dispatchEntry->windowXScale; |
| float wys = dispatchEntry->windowYScale; |
| xOffset = dispatchEntry->xOffset * wxs; |
| yOffset = dispatchEntry->yOffset * wys; |
| if (wxs != 1.0f || wys != 1.0f || globalScaleFactor != 1.0f) { |
| for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| scaledCoords[i] = motionEntry->pointerCoords[i]; |
| scaledCoords[i].scale(globalScaleFactor, wxs, wys); |
| } |
| usingCoords = scaledCoords; |
| } |
| } else { |
| xOffset = 0.0f; |
| yOffset = 0.0f; |
| |
| // We don't want the dispatch target to know. |
| if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| scaledCoords[i].clear(); |
| } |
| usingCoords = scaledCoords; |
| } |
| } |
| |
| // Publish the motion event. |
| status = connection->inputPublisher.publishMotionEvent(dispatchEntry->seq, |
| motionEntry->deviceId, motionEntry->source, motionEntry->displayId, |
| dispatchEntry->resolvedAction, motionEntry->actionButton, |
| dispatchEntry->resolvedFlags, motionEntry->edgeFlags, |
| motionEntry->metaState, motionEntry->buttonState, motionEntry->classification, |
| xOffset, yOffset, motionEntry->xPrecision, motionEntry->yPrecision, |
| motionEntry->downTime, motionEntry->eventTime, |
| motionEntry->pointerCount, motionEntry->pointerProperties, |
| usingCoords); |
| break; |
| } |
| |
| default: |
| ALOG_ASSERT(false); |
| return; |
| } |
| |
| // Check the result. |
| if (status) { |
| if (status == WOULD_BLOCK) { |
| if (connection->waitQueue.isEmpty()) { |
| ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
| "This is unexpected because the wait queue is empty, so the pipe " |
| "should be empty and we shouldn't have any problems writing an " |
| "event to it, status=%d", connection->getInputChannelName().c_str(), |
| status); |
| abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| } else { |
| // Pipe is full and we are waiting for the app to finish process some events |
| // before sending more events to it. |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| "waiting for the application to catch up", |
| connection->getInputChannelName().c_str()); |
| #endif |
| connection->inputPublisherBlocked = true; |
| } |
| } else { |
| ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
| "status=%d", connection->getInputChannelName().c_str(), status); |
| abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| } |
| return; |
| } |
| |
| // Re-enqueue the event on the wait queue. |
| connection->outboundQueue.dequeue(dispatchEntry); |
| traceOutboundQueueLength(connection); |
| connection->waitQueue.enqueueAtTail(dispatchEntry); |
| traceWaitQueueLength(connection); |
| } |
| } |
| |
| void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
| const sp<Connection>& connection, uint32_t seq, bool handled) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| connection->getInputChannelName().c_str(), seq, toString(handled)); |
| #endif |
| |
| connection->inputPublisherBlocked = false; |
| |
| if (connection->status == Connection::STATUS_BROKEN |
| || connection->status == Connection::STATUS_ZOMBIE) { |
| return; |
| } |
| |
| // Notify other system components and prepare to start the next dispatch cycle. |
| onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| } |
| |
| void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
| const sp<Connection>& connection, bool notify) { |
| #if DEBUG_DISPATCH_CYCLE |
| ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| connection->getInputChannelName().c_str(), toString(notify)); |
| #endif |
| |
| // Clear the dispatch queues. |
| drainDispatchQueue(&connection->outboundQueue); |
| traceOutboundQueueLength(connection); |
| drainDispatchQueue(&connection->waitQueue); |
| traceWaitQueueLength(connection); |
| |
| // The connection appears to be unrecoverably broken. |
| // Ignore already broken or zombie connections. |
| if (connection->status == Connection::STATUS_NORMAL) { |
| connection->status = Connection::STATUS_BROKEN; |
| |
| if (notify) { |
| // Notify other system components. |
| onDispatchCycleBrokenLocked(currentTime, connection); |
| } |
| } |
| } |
| |
| void InputDispatcher::drainDispatchQueue(Queue<DispatchEntry>* queue) { |
| while (!queue->isEmpty()) { |
| DispatchEntry* dispatchEntry = queue->dequeueAtHead(); |
| releaseDispatchEntry(dispatchEntry); |
| } |
| } |
| |
| void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
| if (dispatchEntry->hasForegroundTarget()) { |
| decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
| } |
| delete dispatchEntry; |
| } |
| |
| int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| |
| { // acquire lock |
| std::scoped_lock _l(d->mLock); |
| |
| ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd); |
| if (connectionIndex < 0) { |
| ALOGE("Received spurious receive callback for unknown input channel. " |
| "fd=%d, events=0x%x", fd, events); |
| return 0; // remove the callback |
| } |
| |
| bool notify; |
| sp<Connection> connection = d->mConnectionsByFd.valueAt(connectionIndex); |
| if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| if (!(events & ALOOPER_EVENT_INPUT)) { |
| ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| "events=0x%x", connection->getInputChannelName().c_str(), events); |
| return 1; |
| } |
| |
| nsecs_t currentTime = now(); |
| bool gotOne = false; |
| status_t status; |
| for (;;) { |
| uint32_t seq; |
| bool handled; |
| status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| if (status) { |
| break; |
| } |
| d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| gotOne = true; |
| } |
| if (gotOne) { |
| d->runCommandsLockedInterruptible(); |
| if (status == WOULD_BLOCK) { |
| return 1; |
| } |
| } |
| |
| notify = status != DEAD_OBJECT || !connection->monitor; |
| if (notify) { |
| ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
| connection->getInputChannelName().c_str(), status); |
| } |
| } else { |
| // Monitor channels are never explicitly unregistered. |
| // We do it automatically when the remote endpoint is closed so don't warn |
| // about them. |
| notify = !connection->monitor; |
| if (notify) { |
| ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
| "events=0x%x", connection->getInputChannelName().c_str(), events); |
| } |
| } |
| |
| // Unregister the channel. |
| d->unregisterInputChannelLocked(connection->inputChannel, notify); |
| return 0; // remove the callback |
| } // release lock |
| } |
| |
| void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked ( |
| const CancelationOptions& options) { |
| for (size_t i = 0; i < mConnectionsByFd.size(); i++) { |
| synthesizeCancelationEventsForConnectionLocked( |
| mConnectionsByFd.valueAt(i), options); |
| } |
| } |
| |
| void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked ( |
| const CancelationOptions& options) { |
| synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| } |
| |
| void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| const CancelationOptions& options, |
| std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| for (const auto& it : monitorsByDisplay) { |
| const std::vector<Monitor>& monitors = it.second; |
| for (const Monitor& monitor : monitors) { |
| synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
| } |
| } |
| } |
| |
| void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
| const sp<InputChannel>& channel, const CancelationOptions& options) { |
| ssize_t index = getConnectionIndexLocked(channel); |
| if (index >= 0) { |
| synthesizeCancelationEventsForConnectionLocked( |
| mConnectionsByFd.valueAt(index), options); |
| } |
| } |
| |
| void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| const sp<Connection>& connection, const CancelationOptions& options) { |
| if (connection->status == Connection::STATUS_BROKEN) { |
| return; |
| } |
| |
| nsecs_t currentTime = now(); |
| |
| std::vector<EventEntry*> cancelationEvents; |
| connection->inputState.synthesizeCancelationEvents(currentTime, |
| cancelationEvents, options); |
| |
| if (!cancelationEvents.empty()) { |
| #if DEBUG_OUTBOUND_EVENT_DETAILS |
| ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| "with reality: %s, mode=%d.", |
| connection->getInputChannelName().c_str(), cancelationEvents.size(), |
| options.reason, options.mode); |
| #endif |
| for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| switch (cancelationEventEntry->type) { |
| case EventEntry::TYPE_KEY: |
| logOutboundKeyDetails("cancel - ", |
| static_cast<KeyEntry*>(cancelationEventEntry)); |
| break; |
| case EventEntry::TYPE_MOTION: |
| logOutboundMotionDetails("cancel - ", |
| static_cast<MotionEntry*>(cancelationEventEntry)); |
| break; |
| } |
| |
| InputTarget target; |
| sp<InputWindowHandle> windowHandle = getWindowHandleLocked( |
| connection->inputChannel->getToken()); |
| if (windowHandle != nullptr) { |
| const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| target.xOffset = -windowInfo->frameLeft; |
| target.yOffset = -windowInfo->frameTop; |
| target.globalScaleFactor = windowInfo->globalScaleFactor; |
| target.windowXScale = windowInfo->windowXScale; |
| target.windowYScale = windowInfo->windowYScale; |
| } else { |
| target.xOffset = 0; |
| target.yOffset = 0; |
| target.globalScaleFactor = 1.0f; |
| } |
| target.inputChannel = connection->inputChannel; |
| target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| |
| enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| &target, InputTarget::FLAG_DISPATCH_AS_IS); |
| |
| cancelationEventEntry->release(); |
| } |
| |
| startDispatchCycleLocked(currentTime, connection); |
| } |
| } |
| |
| InputDispatcher::MotionEntry* |
| InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) { |
| ALOG_ASSERT(pointerIds.value != 0); |
| |
| uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| PointerProperties splitPointerProperties[MAX_POINTERS]; |
| PointerCoords splitPointerCoords[MAX_POINTERS]; |
| |
| uint32_t originalPointerCount = originalMotionEntry->pointerCount; |
| uint32_t splitPointerCount = 0; |
| |
| for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
| originalPointerIndex++) { |
| const PointerProperties& pointerProperties = |
| originalMotionEntry->pointerProperties[originalPointerIndex]; |
| uint32_t pointerId = uint32_t(pointerProperties.id); |
| if (pointerIds.hasBit(pointerId)) { |
| splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| splitPointerCoords[splitPointerCount].copyFrom( |
| originalMotionEntry->pointerCoords[originalPointerIndex]); |
| splitPointerCount += 1; |
| } |
| } |
| |
| if (splitPointerCount != pointerIds.count()) { |
| // This is bad. We are missing some of the pointers that we expected to deliver. |
| // Most likely this indicates that we received an ACTION_MOVE events that has |
| // different pointer ids than we expected based on the previous ACTION_DOWN |
| // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| // in this way. |
| ALOGW("Dropping split motion event because the pointer count is %d but " |
| "we expected there to be %d pointers. This probably means we received " |
| "a broken sequence of pointer ids from the input device.", |
| splitPointerCount, pointerIds.count()); |
| return nullptr; |
| } |
| |
| int32_t action = originalMotionEntry->action; |
| int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
| || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| const PointerProperties& pointerProperties = |
| originalMotionEntry->pointerProperties[originalPointerIndex]; |
| uint32_t pointerId = uint32_t(pointerProperties.id); |
| if (pointerIds.hasBit(pointerId)) { |
| if (pointerIds.count() == 1) { |
| // The first/last pointer went down/up. |
| action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
| ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| } else { |
| // A secondary pointer went down/up. |
| uint32_t splitPointerIndex = 0; |
| while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| splitPointerIndex += 1; |
| } |
| action = maskedAction | (splitPointerIndex |
| << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| } |
| } else { |
| // An unrelated pointer changed. |
| action = AMOTION_EVENT_ACTION_MOVE; |
| } |
| } |
| |
| MotionEntry* splitMotionEntry = new MotionEntry( |
| originalMotionEntry->sequenceNum, |
| originalMotionEntry->eventTime, |
| originalMotionEntry->deviceId, |
| originalMotionEntry->source, |
| originalMotionEntry->displayId, |
| originalMotionEntry->policyFlags, |
| action, |
| originalMotionEntry->actionButton, |
| originalMotionEntry->flags, |
| originalMotionEntry->metaState, |
| originalMotionEntry->buttonState, |
| originalMotionEntry->classification, |
| originalMotionEntry->edgeFlags, |
| originalMotionEntry->xPrecision, |
| originalMotionEntry->yPrecision, |
| originalMotionEntry->downTime, |
| splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
| |
| if (originalMotionEntry->injectionState) { |
| splitMotionEntry->injectionState = originalMotionEntry->injectionState; |
| splitMotionEntry->injectionState->refCount += 1; |
| } |
| |
| return splitMotionEntry; |
| } |
| |
| void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| #endif |
| |
| bool needWake; |
| { // acquire lock |
| std::scoped_lock _l(mLock); |
| |
| ConfigurationChangedEntry* newEntry = |
| new ConfigurationChangedEntry(args->sequenceNum, args->eventTime); |
| needWake = enqueueInboundEventLocked(newEntry); |
| } // release lock |
| |
| if (needWake) { |
| mLooper->wake(); |
| } |
| } |
| |
| /** |
| * If one of the meta shortcuts is detected, process them here: |
| * Meta + Backspace -> generate BACK |
| * Meta + Enter -> generate HOME |
| * This will potentially overwrite keyCode and metaState. |
| */ |
| void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
| int32_t& keyCode, int32_t& metaState) { |
| if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| if (keyCode == AKEYCODE_DEL) { |
| newKeyCode = AKEYCODE_BACK; |
| } else if (keyCode == AKEYCODE_ENTER) { |
| newKeyCode = AKEYCODE_HOME; |
| } |
| if (newKeyCode != AKEYCODE_UNKNOWN) { |
| std::scoped_lock _l(mLock); |
| struct KeyReplacement replacement = {keyCode, deviceId}; |
| mReplacedKeys.add(replacement, newKeyCode); |
| keyCode = newKeyCode; |
| metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| } |
| } else if (action == AKEY_EVENT_ACTION_UP) { |
| // In order to maintain a consistent stream of up and down events, check to see if the key |
| // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| // even if the modifier was released between the down and the up events. |
| std::scoped_lock _l(mLock); |
| struct KeyReplacement replacement = {keyCode, deviceId}; |
| ssize_t index = mReplacedKeys.indexOfKey(replacement); |
| if (index >= 0) { |
| keyCode = mReplacedKeys.valueAt(index); |
| mReplacedKeys.removeItemsAt(index); |
| metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| } |
| } |
| } |
| |
| void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("notifyKey - eventTime=%" PRId64 |
| ", deviceId=%d, source=0x%x, displayId=%" PRId32 "policyFlags=0x%x, action=0x%x, " |
| "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| args->action, args->flags, args->keyCode, args->scanCode, |
| args->metaState, args->downTime); |
| #endif |
| if (!validateKeyEvent(args->action)) { |
| return; |
| } |
| |
| uint32_t policyFlags = args->policyFlags; |
| int32_t flags = args->flags; |
| int32_t metaState = args->metaState; |
| // InputDispatcher tracks and generates key repeats on behalf of |
| // whatever notifies it, so repeatCount should always be set to 0 |
| constexpr int32_t repeatCount = 0; |
| if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| policyFlags |= POLICY_FLAG_VIRTUAL; |
| flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| } |
| if (policyFlags & POLICY_FLAG_FUNCTION) { |
| metaState |= AMETA_FUNCTION_ON; |
| } |
| |
| policyFlags |= POLICY_FLAG_TRUSTED; |
| |
| int32_t keyCode = args->keyCode; |
| accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
| |
| KeyEvent event; |
| event.initialize(args->deviceId, args->source, args->displayId, args->action, |
| flags, keyCode, args->scanCode, metaState, repeatCount, |
| args->downTime, args->eventTime); |
| |
| android::base::Timer t; |
| mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
| if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| std::to_string(t.duration().count()).c_str()); |
| } |
| |
| bool needWake; |
| { // acquire lock |
| mLock.lock(); |
| |
| if (shouldSendKeyToInputFilterLocked(args)) { |
| mLock.unlock(); |
| |
| policyFlags |= POLICY_FLAG_FILTERED; |
| if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| return; // event was consumed by the filter |
| } |
| |
| mLock.lock(); |
| } |
| |
| KeyEntry* newEntry = new KeyEntry(args->sequenceNum, args->eventTime, |
| args->deviceId, args->source, args->displayId, policyFlags, |
| args->action, flags, keyCode, args->scanCode, |
| metaState, repeatCount, args->downTime); |
| |
| needWake = enqueueInboundEventLocked(newEntry); |
| mLock.unlock(); |
| } // release lock |
| |
| if (needWake) { |
| mLooper->wake(); |
| } |
| } |
| |
| bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| return mInputFilterEnabled; |
| } |
| |
| void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("notifyMotion - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| ", policyFlags=0x%x, " |
| "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x," |
| "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| args->action, args->actionButton, args->flags, args->metaState, args->buttonState, |
| args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime); |
| for (uint32_t i = 0; i < args->pointerCount; i++) { |
| ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| "x=%f, y=%f, pressure=%f, size=%f, " |
| "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| "orientation=%f", |
| i, args->pointerProperties[i].id, |
| args->pointerProperties[i].toolType, |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| } |
| #endif |
| if (!validateMotionEvent(args->action, args->actionButton, |
| args->pointerCount, args->pointerProperties)) { |
| return; |
| } |
| |
| uint32_t policyFlags = args->policyFlags; |
| policyFlags |= POLICY_FLAG_TRUSTED; |
| |
| android::base::Timer t; |
| mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
| if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| std::to_string(t.duration().count()).c_str()); |
| } |
| |
| bool needWake; |
| { // acquire lock |
| mLock.lock(); |
| |
| if (shouldSendMotionToInputFilterLocked(args)) { |
| mLock.unlock(); |
| |
| MotionEvent event; |
| event.initialize(args->deviceId, args->source, args->displayId, |
| args->action, args->actionButton, |
| args->flags, args->edgeFlags, args->metaState, args->buttonState, |
| args->classification, 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->sequenceNum, args->eventTime, |
| args->deviceId, args->source, args->displayId, policyFlags, |
| args->action, args->actionButton, args->flags, |
| args->metaState, args->buttonState, args->classification, |
| args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime, |
| args->pointerCount, args->pointerProperties, args->pointerCoords, 0, 0); |
| |
| needWake = enqueueInboundEventLocked(newEntry); |
| mLock.unlock(); |
| } // release lock |
| |
| if (needWake) { |
| mLooper->wake(); |
| } |
| } |
| |
| bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
| return mInputFilterEnabled; |
| } |
| |
| void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| "switchMask=0x%08x", |
| args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| #endif |
| |
| uint32_t policyFlags = args->policyFlags; |
| policyFlags |= POLICY_FLAG_TRUSTED; |
| mPolicy->notifySwitch(args->eventTime, |
| args->switchValues, args->switchMask, policyFlags); |
| } |
| |
| void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", |
| args->eventTime, args->deviceId); |
| #endif |
| |
| bool needWake; |
| { // acquire lock |
| std::scoped_lock _l(mLock); |
| |
| DeviceResetEntry* newEntry = |
| new DeviceResetEntry(args->sequenceNum, args->eventTime, args->deviceId); |
| needWake = enqueueInboundEventLocked(newEntry); |
| } // release lock |
| |
| if (needWake) { |
| mLooper->wake(); |
| } |
| } |
| |
| int32_t InputDispatcher::injectInputEvent(const InputEvent* event, |
| int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| uint32_t policyFlags) { |
| #if DEBUG_INBOUND_EVENT_DETAILS |
| ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x", |
| event->getType(), injectorPid |