Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 23 | #define DEBUG_INBOUND_EVENT_DETAILS 0 |
| 24 | |
| 25 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 26 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 |
| 27 | |
| 28 | // Log debug messages about the dispatch cycle. |
| 29 | #define DEBUG_DISPATCH_CYCLE 0 |
| 30 | |
| 31 | // Log debug messages about registrations. |
| 32 | #define DEBUG_REGISTRATION 0 |
| 33 | |
| 34 | // Log debug messages about input event injection. |
| 35 | #define DEBUG_INJECTION 0 |
| 36 | |
| 37 | // Log debug messages about input focus tracking. |
| 38 | #define DEBUG_FOCUS 0 |
| 39 | |
| 40 | // Log debug messages about the app switch latency optimization. |
| 41 | #define DEBUG_APP_SWITCH 0 |
| 42 | |
| 43 | // Log debug messages about hover events. |
| 44 | #define DEBUG_HOVER 0 |
| 45 | |
| 46 | #include "InputDispatcher.h" |
| 47 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 | #include <errno.h> |
| 49 | #include <limits.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 50 | #include <sstream> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 51 | #include <stddef.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | #include <time.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 53 | #include <unistd.h> |
| 54 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 55 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 56 | #include <android-base/stringprintf.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 57 | #include <log/log.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 58 | #include <utils/Trace.h> |
| 59 | #include <powermanager/PowerManager.h> |
| 60 | #include <ui/Region.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 61 | |
| 62 | #define INDENT " " |
| 63 | #define INDENT2 " " |
| 64 | #define INDENT3 " " |
| 65 | #define INDENT4 " " |
| 66 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 67 | using android::base::StringPrintf; |
| 68 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | namespace android { |
| 70 | |
| 71 | // Default input dispatching timeout if there is no focused application or paused window |
| 72 | // from which to determine an appropriate dispatching timeout. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 73 | constexpr nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 74 | |
| 75 | // Amount of time to allow for all pending events to be processed when an app switch |
| 76 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 77 | // when an application takes too long to respond and the user has pressed an app switch key. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 78 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | |
| 80 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 81 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 82 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 | |
| 84 | // Amount of time to allow touch events to be streamed out to a connection before requiring |
| 85 | // that the first event be finished. This value extends the ANR timeout by the specified |
| 86 | // amount. For example, if streaming is allowed to get ahead by one second relative to the |
| 87 | // queue of waiting unfinished events, then ANRs will similarly be delayed by one second. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 88 | constexpr nsecs_t STREAM_AHEAD_EVENT_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 | |
| 90 | // Log a warning when an event takes longer than this to process, even if an ANR does not occur. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 91 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 92 | |
| 93 | // Log a warning when an interception call takes longer than this to process. |
| 94 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
| 96 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 97 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 98 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 | |
| 100 | static inline nsecs_t now() { |
| 101 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 102 | } |
| 103 | |
| 104 | static inline const char* toString(bool value) { |
| 105 | return value ? "true" : "false"; |
| 106 | } |
| 107 | |
Siarhei Vishniakou | b48188a | 2018-03-01 20:55:47 -0800 | [diff] [blame] | 108 | static std::string motionActionToString(int32_t action) { |
| 109 | // Convert MotionEvent action to string |
| 110 | switch(action & AMOTION_EVENT_ACTION_MASK) { |
| 111 | case AMOTION_EVENT_ACTION_DOWN: |
| 112 | return "DOWN"; |
| 113 | case AMOTION_EVENT_ACTION_MOVE: |
| 114 | return "MOVE"; |
| 115 | case AMOTION_EVENT_ACTION_UP: |
| 116 | return "UP"; |
| 117 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 118 | return "POINTER_DOWN"; |
| 119 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 120 | return "POINTER_UP"; |
| 121 | } |
| 122 | return StringPrintf("%" PRId32, action); |
| 123 | } |
| 124 | |
| 125 | static std::string keyActionToString(int32_t action) { |
| 126 | // Convert KeyEvent action to string |
| 127 | switch(action) { |
| 128 | case AKEY_EVENT_ACTION_DOWN: |
| 129 | return "DOWN"; |
| 130 | case AKEY_EVENT_ACTION_UP: |
| 131 | return "UP"; |
| 132 | case AKEY_EVENT_ACTION_MULTIPLE: |
| 133 | return "MULTIPLE"; |
| 134 | } |
| 135 | return StringPrintf("%" PRId32, action); |
| 136 | } |
| 137 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 138 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
| 139 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| 140 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 141 | } |
| 142 | |
| 143 | static bool isValidKeyAction(int32_t action) { |
| 144 | switch (action) { |
| 145 | case AKEY_EVENT_ACTION_DOWN: |
| 146 | case AKEY_EVENT_ACTION_UP: |
| 147 | return true; |
| 148 | default: |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static bool validateKeyEvent(int32_t action) { |
| 154 | if (! isValidKeyAction(action)) { |
| 155 | ALOGE("Key event has invalid action code 0x%x", action); |
| 156 | return false; |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 161 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 162 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 163 | case AMOTION_EVENT_ACTION_DOWN: |
| 164 | case AMOTION_EVENT_ACTION_UP: |
| 165 | case AMOTION_EVENT_ACTION_CANCEL: |
| 166 | case AMOTION_EVENT_ACTION_MOVE: |
| 167 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 168 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 169 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 170 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 171 | case AMOTION_EVENT_ACTION_SCROLL: |
| 172 | return true; |
| 173 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 174 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 175 | int32_t index = getMotionEventActionPointerIndex(action); |
Dan Albert | 1bd2fc0 | 2016-02-02 15:11:57 -0800 | [diff] [blame] | 176 | return index >= 0 && index < pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 177 | } |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 178 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 179 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 180 | return actionButton != 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | default: |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 186 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 187 | const PointerProperties* pointerProperties) { |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 188 | if (! isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 189 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 190 | return false; |
| 191 | } |
| 192 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 193 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 194 | pointerCount, MAX_POINTERS); |
| 195 | return false; |
| 196 | } |
| 197 | BitSet32 pointerIdBits; |
| 198 | for (size_t i = 0; i < pointerCount; i++) { |
| 199 | int32_t id = pointerProperties[i].id; |
| 200 | if (id < 0 || id > MAX_POINTER_ID) { |
| 201 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", |
| 202 | id, MAX_POINTER_ID); |
| 203 | return false; |
| 204 | } |
| 205 | if (pointerIdBits.hasBit(id)) { |
| 206 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 207 | return false; |
| 208 | } |
| 209 | pointerIdBits.markBit(id); |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | static bool isMainDisplay(int32_t displayId) { |
| 215 | return displayId == ADISPLAY_ID_DEFAULT || displayId == ADISPLAY_ID_NONE; |
| 216 | } |
| 217 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 218 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 219 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 220 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
| 224 | bool first = true; |
| 225 | Region::const_iterator cur = region.begin(); |
| 226 | Region::const_iterator const tail = region.end(); |
| 227 | while (cur != tail) { |
| 228 | if (first) { |
| 229 | first = false; |
| 230 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 231 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 233 | dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | cur++; |
| 235 | } |
| 236 | } |
| 237 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 238 | template<typename T, typename U> |
| 239 | static T getValueByKey(std::unordered_map<U, T>& map, U key) { |
| 240 | typename std::unordered_map<U, T>::const_iterator it = map.find(key); |
| 241 | return it != map.end() ? it->second : T{}; |
| 242 | } |
| 243 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | |
| 245 | // --- InputDispatcher --- |
| 246 | |
| 247 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) : |
| 248 | mPolicy(policy), |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 249 | mPendingEvent(nullptr), mLastDropReason(DROP_REASON_NOT_DROPPED), |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 250 | mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX), |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 251 | mNextUnblockedEvent(nullptr), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 252 | mDispatchEnabled(false), mDispatchFrozen(false), mInputFilterEnabled(false), |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 253 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) { |
| 255 | mLooper = new Looper(false); |
| 256 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 257 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 258 | |
| 259 | policy->getDispatcherConfiguration(&mConfig); |
| 260 | } |
| 261 | |
| 262 | InputDispatcher::~InputDispatcher() { |
| 263 | { // acquire lock |
| 264 | AutoMutex _l(mLock); |
| 265 | |
| 266 | resetKeyRepeatLocked(); |
| 267 | releasePendingEventLocked(); |
| 268 | drainInboundQueueLocked(); |
| 269 | } |
| 270 | |
| 271 | while (mConnectionsByFd.size() != 0) { |
| 272 | unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void InputDispatcher::dispatchOnce() { |
| 277 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 278 | { // acquire lock |
| 279 | AutoMutex _l(mLock); |
| 280 | mDispatcherIsAliveCondition.broadcast(); |
| 281 | |
| 282 | // Run a dispatch loop if there are no pending commands. |
| 283 | // The dispatch loop might enqueue commands to run afterwards. |
| 284 | if (!haveCommandsLocked()) { |
| 285 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 286 | } |
| 287 | |
| 288 | // Run all pending commands if there are any. |
| 289 | // If any commands were run then force the next poll to wake up immediately. |
| 290 | if (runCommandsLockedInterruptible()) { |
| 291 | nextWakeupTime = LONG_LONG_MIN; |
| 292 | } |
| 293 | } // release lock |
| 294 | |
| 295 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 296 | nsecs_t currentTime = now(); |
| 297 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 298 | mLooper->pollOnce(timeoutMillis); |
| 299 | } |
| 300 | |
| 301 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 302 | nsecs_t currentTime = now(); |
| 303 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 304 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 305 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 306 | // repeat if the device is just coming out of sleep. |
| 307 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 308 | resetKeyRepeatLocked(); |
| 309 | } |
| 310 | |
| 311 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 312 | if (mDispatchFrozen) { |
| 313 | #if DEBUG_FOCUS |
| 314 | ALOGD("Dispatch frozen. Waiting some more."); |
| 315 | #endif |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | // Optimize latency of app switches. |
| 320 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 321 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 322 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 323 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 324 | *nextWakeupTime = mAppSwitchDueTime; |
| 325 | } |
| 326 | |
| 327 | // Ready to start a new event. |
| 328 | // If we don't already have a pending event, go grab one. |
| 329 | if (! mPendingEvent) { |
| 330 | if (mInboundQueue.isEmpty()) { |
| 331 | if (isAppSwitchDue) { |
| 332 | // The inbound queue is empty so the app switch key we were waiting |
| 333 | // for will never arrive. Stop waiting for it. |
| 334 | resetPendingAppSwitchLocked(false); |
| 335 | isAppSwitchDue = false; |
| 336 | } |
| 337 | |
| 338 | // Synthesize a key repeat if appropriate. |
| 339 | if (mKeyRepeatState.lastKeyEntry) { |
| 340 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 341 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 342 | } else { |
| 343 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 344 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Nothing to do if there is no pending event. |
| 350 | if (!mPendingEvent) { |
| 351 | return; |
| 352 | } |
| 353 | } else { |
| 354 | // Inbound queue has at least one entry. |
| 355 | mPendingEvent = mInboundQueue.dequeueAtHead(); |
| 356 | traceInboundQueueLengthLocked(); |
| 357 | } |
| 358 | |
| 359 | // Poke user activity for this event. |
| 360 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 361 | pokeUserActivityLocked(mPendingEvent); |
| 362 | } |
| 363 | |
| 364 | // Get ready to dispatch the event. |
| 365 | resetANRTimeoutsLocked(); |
| 366 | } |
| 367 | |
| 368 | // Now we have an event to dispatch. |
| 369 | // All events are eventually dequeued and processed this way, even if we intend to drop them. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 370 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 371 | bool done = false; |
| 372 | DropReason dropReason = DROP_REASON_NOT_DROPPED; |
| 373 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 374 | dropReason = DROP_REASON_POLICY; |
| 375 | } else if (!mDispatchEnabled) { |
| 376 | dropReason = DROP_REASON_DISABLED; |
| 377 | } |
| 378 | |
| 379 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 380 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | switch (mPendingEvent->type) { |
| 384 | case EventEntry::TYPE_CONFIGURATION_CHANGED: { |
| 385 | ConfigurationChangedEntry* typedEntry = |
| 386 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 387 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
| 388 | dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | case EventEntry::TYPE_DEVICE_RESET: { |
| 393 | DeviceResetEntry* typedEntry = |
| 394 | static_cast<DeviceResetEntry*>(mPendingEvent); |
| 395 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
| 396 | dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | case EventEntry::TYPE_KEY: { |
| 401 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 402 | if (isAppSwitchDue) { |
| 403 | if (isAppSwitchKeyEventLocked(typedEntry)) { |
| 404 | resetPendingAppSwitchLocked(true); |
| 405 | isAppSwitchDue = false; |
| 406 | } else if (dropReason == DROP_REASON_NOT_DROPPED) { |
| 407 | dropReason = DROP_REASON_APP_SWITCH; |
| 408 | } |
| 409 | } |
| 410 | if (dropReason == DROP_REASON_NOT_DROPPED |
| 411 | && isStaleEventLocked(currentTime, typedEntry)) { |
| 412 | dropReason = DROP_REASON_STALE; |
| 413 | } |
| 414 | if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
| 415 | dropReason = DROP_REASON_BLOCKED; |
| 416 | } |
| 417 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | case EventEntry::TYPE_MOTION: { |
| 422 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
| 423 | if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) { |
| 424 | dropReason = DROP_REASON_APP_SWITCH; |
| 425 | } |
| 426 | if (dropReason == DROP_REASON_NOT_DROPPED |
| 427 | && isStaleEventLocked(currentTime, typedEntry)) { |
| 428 | dropReason = DROP_REASON_STALE; |
| 429 | } |
| 430 | if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
| 431 | dropReason = DROP_REASON_BLOCKED; |
| 432 | } |
| 433 | done = dispatchMotionLocked(currentTime, typedEntry, |
| 434 | &dropReason, nextWakeupTime); |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | default: |
| 439 | ALOG_ASSERT(false); |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | if (done) { |
| 444 | if (dropReason != DROP_REASON_NOT_DROPPED) { |
| 445 | dropInboundEventLocked(mPendingEvent, dropReason); |
| 446 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 447 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 448 | |
| 449 | releasePendingEventLocked(); |
| 450 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
| 455 | bool needWake = mInboundQueue.isEmpty(); |
| 456 | mInboundQueue.enqueueAtTail(entry); |
| 457 | traceInboundQueueLengthLocked(); |
| 458 | |
| 459 | switch (entry->type) { |
| 460 | case EventEntry::TYPE_KEY: { |
| 461 | // Optimize app switch latency. |
| 462 | // If the application takes too long to catch up then we drop all events preceding |
| 463 | // the app switch key. |
| 464 | KeyEntry* keyEntry = static_cast<KeyEntry*>(entry); |
| 465 | if (isAppSwitchKeyEventLocked(keyEntry)) { |
| 466 | if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) { |
| 467 | mAppSwitchSawKeyDown = true; |
| 468 | } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 469 | if (mAppSwitchSawKeyDown) { |
| 470 | #if DEBUG_APP_SWITCH |
| 471 | ALOGD("App switch is pending!"); |
| 472 | #endif |
| 473 | mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT; |
| 474 | mAppSwitchSawKeyDown = false; |
| 475 | needWake = true; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | case EventEntry::TYPE_MOTION: { |
| 483 | // Optimize case where the current application is unresponsive and the user |
| 484 | // decides to touch a window in a different application. |
| 485 | // If the application takes too long to catch up then we drop all events preceding |
| 486 | // the touch into the other window. |
| 487 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| 488 | if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN |
| 489 | && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
| 490 | && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 491 | && mInputTargetWaitApplicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 | int32_t displayId = motionEntry->displayId; |
| 493 | int32_t x = int32_t(motionEntry->pointerCoords[0]. |
| 494 | getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 495 | int32_t y = int32_t(motionEntry->pointerCoords[0]. |
| 496 | getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 497 | sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 498 | if (touchedWindowHandle != nullptr |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 499 | && touchedWindowHandle->inputApplicationHandle |
| 500 | != mInputTargetWaitApplicationHandle) { |
| 501 | // User touched a different application than the one we are waiting on. |
| 502 | // Flag the event, and start pruning the input queue. |
| 503 | mNextUnblockedEvent = motionEntry; |
| 504 | needWake = true; |
| 505 | } |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return needWake; |
| 512 | } |
| 513 | |
| 514 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 515 | entry->refCount += 1; |
| 516 | mRecentQueue.enqueueAtTail(entry); |
| 517 | if (mRecentQueue.count() > RECENT_QUEUE_MAX_SIZE) { |
| 518 | mRecentQueue.dequeueAtHead()->release(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, |
| 523 | int32_t x, int32_t y) { |
| 524 | // Traverse windows from front to back to find touched window. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 525 | const Vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 526 | size_t numWindows = windowHandles.size(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | for (size_t i = 0; i < numWindows; i++) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 528 | sp<InputWindowHandle> windowHandle = windowHandles.itemAt(i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 530 | if (windowInfo->displayId == displayId) { |
| 531 | int32_t flags = windowInfo->layoutParamsFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 | |
| 533 | if (windowInfo->visible) { |
| 534 | if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
| 535 | bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
| 536 | | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
| 537 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 538 | // Found window. |
| 539 | return windowHandle; |
| 540 | } |
| 541 | } |
| 542 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 545 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) { |
| 549 | const char* reason; |
| 550 | switch (dropReason) { |
| 551 | case DROP_REASON_POLICY: |
| 552 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 553 | ALOGD("Dropped event because policy consumed it."); |
| 554 | #endif |
| 555 | reason = "inbound event was dropped because the policy consumed it"; |
| 556 | break; |
| 557 | case DROP_REASON_DISABLED: |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 558 | if (mLastDropReason != DROP_REASON_DISABLED) { |
| 559 | ALOGI("Dropped event because input dispatch is disabled."); |
| 560 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 561 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 562 | break; |
| 563 | case DROP_REASON_APP_SWITCH: |
| 564 | ALOGI("Dropped event because of pending overdue app switch."); |
| 565 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 566 | break; |
| 567 | case DROP_REASON_BLOCKED: |
| 568 | ALOGI("Dropped event because the current application is not responding and the user " |
| 569 | "has started interacting with a different application."); |
| 570 | reason = "inbound event was dropped because the current application is not responding " |
| 571 | "and the user has started interacting with a different application"; |
| 572 | break; |
| 573 | case DROP_REASON_STALE: |
| 574 | ALOGI("Dropped event because it is stale."); |
| 575 | reason = "inbound event was dropped because it is stale"; |
| 576 | break; |
| 577 | default: |
| 578 | ALOG_ASSERT(false); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | switch (entry->type) { |
| 583 | case EventEntry::TYPE_KEY: { |
| 584 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 585 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 586 | break; |
| 587 | } |
| 588 | case EventEntry::TYPE_MOTION: { |
| 589 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| 590 | if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { |
| 591 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 592 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 593 | } else { |
| 594 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 595 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 596 | } |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) { |
| 603 | return keyCode == AKEYCODE_HOME |
| 604 | || keyCode == AKEYCODE_ENDCALL |
| 605 | || keyCode == AKEYCODE_APP_SWITCH; |
| 606 | } |
| 607 | |
| 608 | bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) { |
| 609 | return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) |
| 610 | && isAppSwitchKeyCode(keyEntry->keyCode) |
| 611 | && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED) |
| 612 | && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER); |
| 613 | } |
| 614 | |
| 615 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 616 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 617 | } |
| 618 | |
| 619 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 620 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 621 | |
| 622 | #if DEBUG_APP_SWITCH |
| 623 | if (handled) { |
| 624 | ALOGD("App switch has arrived."); |
| 625 | } else { |
| 626 | ALOGD("App switch was abandoned."); |
| 627 | } |
| 628 | #endif |
| 629 | } |
| 630 | |
| 631 | bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) { |
| 632 | return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT; |
| 633 | } |
| 634 | |
| 635 | bool InputDispatcher::haveCommandsLocked() const { |
| 636 | return !mCommandQueue.isEmpty(); |
| 637 | } |
| 638 | |
| 639 | bool InputDispatcher::runCommandsLockedInterruptible() { |
| 640 | if (mCommandQueue.isEmpty()) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | do { |
| 645 | CommandEntry* commandEntry = mCommandQueue.dequeueAtHead(); |
| 646 | |
| 647 | Command command = commandEntry->command; |
| 648 | (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible' |
| 649 | |
| 650 | commandEntry->connection.clear(); |
| 651 | delete commandEntry; |
| 652 | } while (! mCommandQueue.isEmpty()); |
| 653 | return true; |
| 654 | } |
| 655 | |
| 656 | InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) { |
| 657 | CommandEntry* commandEntry = new CommandEntry(command); |
| 658 | mCommandQueue.enqueueAtTail(commandEntry); |
| 659 | return commandEntry; |
| 660 | } |
| 661 | |
| 662 | void InputDispatcher::drainInboundQueueLocked() { |
| 663 | while (! mInboundQueue.isEmpty()) { |
| 664 | EventEntry* entry = mInboundQueue.dequeueAtHead(); |
| 665 | releaseInboundEventLocked(entry); |
| 666 | } |
| 667 | traceInboundQueueLengthLocked(); |
| 668 | } |
| 669 | |
| 670 | void InputDispatcher::releasePendingEventLocked() { |
| 671 | if (mPendingEvent) { |
| 672 | resetANRTimeoutsLocked(); |
| 673 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 674 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
| 678 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 679 | InjectionState* injectionState = entry->injectionState; |
| 680 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 681 | #if DEBUG_DISPATCH_CYCLE |
| 682 | ALOGD("Injected inbound event was dropped."); |
| 683 | #endif |
| 684 | setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED); |
| 685 | } |
| 686 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 687 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 688 | } |
| 689 | addRecentEventLocked(entry); |
| 690 | entry->release(); |
| 691 | } |
| 692 | |
| 693 | void InputDispatcher::resetKeyRepeatLocked() { |
| 694 | if (mKeyRepeatState.lastKeyEntry) { |
| 695 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 696 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 701 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 702 | |
| 703 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 704 | uint32_t policyFlags = entry->policyFlags & |
| 705 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 | if (entry->refCount == 1) { |
| 707 | entry->recycle(); |
| 708 | entry->eventTime = currentTime; |
| 709 | entry->policyFlags = policyFlags; |
| 710 | entry->repeatCount += 1; |
| 711 | } else { |
| 712 | KeyEntry* newEntry = new KeyEntry(currentTime, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 713 | entry->deviceId, entry->source, entry->displayId, policyFlags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 714 | entry->action, entry->flags, entry->keyCode, entry->scanCode, |
| 715 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
| 716 | |
| 717 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 718 | entry->release(); |
| 719 | |
| 720 | entry = newEntry; |
| 721 | } |
| 722 | entry->syntheticRepeat = true; |
| 723 | |
| 724 | // Increment reference count since we keep a reference to the event in |
| 725 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 726 | entry->refCount += 1; |
| 727 | |
| 728 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 729 | return entry; |
| 730 | } |
| 731 | |
| 732 | bool InputDispatcher::dispatchConfigurationChangedLocked( |
| 733 | nsecs_t currentTime, ConfigurationChangedEntry* entry) { |
| 734 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 735 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 736 | #endif |
| 737 | |
| 738 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 739 | resetKeyRepeatLocked(); |
| 740 | |
| 741 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. |
| 742 | CommandEntry* commandEntry = postCommandLocked( |
| 743 | & InputDispatcher::doNotifyConfigurationChangedInterruptible); |
| 744 | commandEntry->eventTime = entry->eventTime; |
| 745 | return true; |
| 746 | } |
| 747 | |
| 748 | bool InputDispatcher::dispatchDeviceResetLocked( |
| 749 | nsecs_t currentTime, DeviceResetEntry* entry) { |
| 750 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 751 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
| 752 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | #endif |
| 754 | |
| 755 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 756 | "device was reset"); |
| 757 | options.deviceId = entry->deviceId; |
| 758 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
| 763 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 764 | // Preprocessing. |
| 765 | if (! entry->dispatchInProgress) { |
| 766 | if (entry->repeatCount == 0 |
| 767 | && entry->action == AKEY_EVENT_ACTION_DOWN |
| 768 | && (entry->policyFlags & POLICY_FLAG_TRUSTED) |
| 769 | && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 770 | if (mKeyRepeatState.lastKeyEntry |
| 771 | && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
| 772 | // We have seen two identical key downs in a row which indicates that the device |
| 773 | // driver is automatically generating key repeats itself. We take note of the |
| 774 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 775 | // we will not need to synthesize key repeats ourselves. |
| 776 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 777 | resetKeyRepeatLocked(); |
| 778 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 779 | } else { |
| 780 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 781 | resetKeyRepeatLocked(); |
| 782 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 783 | } |
| 784 | mKeyRepeatState.lastKeyEntry = entry; |
| 785 | entry->refCount += 1; |
| 786 | } else if (! entry->syntheticRepeat) { |
| 787 | resetKeyRepeatLocked(); |
| 788 | } |
| 789 | |
| 790 | if (entry->repeatCount == 1) { |
| 791 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 792 | } else { |
| 793 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 794 | } |
| 795 | |
| 796 | entry->dispatchInProgress = true; |
| 797 | |
| 798 | logOutboundKeyDetailsLocked("dispatchKey - ", entry); |
| 799 | } |
| 800 | |
| 801 | // Handle case where the policy asked us to try again later last time. |
| 802 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 803 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 804 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 805 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 806 | } |
| 807 | return false; // wait until next wakeup |
| 808 | } |
| 809 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 810 | entry->interceptKeyWakeupTime = 0; |
| 811 | } |
| 812 | |
| 813 | // Give the policy a chance to intercept the key. |
| 814 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 815 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 816 | CommandEntry* commandEntry = postCommandLocked( |
| 817 | & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 818 | sp<InputWindowHandle> focusedWindowHandle = |
| 819 | getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(entry)); |
| 820 | if (focusedWindowHandle != nullptr) { |
| 821 | commandEntry->inputWindowHandle = focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | } |
| 823 | commandEntry->keyEntry = entry; |
| 824 | entry->refCount += 1; |
| 825 | return false; // wait for the command to run |
| 826 | } else { |
| 827 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 828 | } |
| 829 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
| 830 | if (*dropReason == DROP_REASON_NOT_DROPPED) { |
| 831 | *dropReason = DROP_REASON_POLICY; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // Clean up if dropping the event. |
| 836 | if (*dropReason != DROP_REASON_NOT_DROPPED) { |
| 837 | setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY |
| 838 | ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | // Identify targets. |
| 843 | Vector<InputTarget> inputTargets; |
| 844 | int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime, |
| 845 | entry, inputTargets, nextWakeupTime); |
| 846 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | setInjectionResultLocked(entry, injectionResult); |
| 851 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 852 | return true; |
| 853 | } |
| 854 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 855 | // Add monitor channels from event's or focused display. |
| 856 | addMonitoringTargetsLocked(inputTargets, getTargetDisplayId(entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 857 | |
| 858 | // Dispatch the key. |
| 859 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) { |
| 864 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 865 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 866 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 867 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 868 | prefix, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 869 | entry->eventTime, entry->deviceId, entry->source, entry->displayId, entry->policyFlags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState, |
| 871 | entry->repeatCount, entry->downTime); |
| 872 | #endif |
| 873 | } |
| 874 | |
| 875 | bool InputDispatcher::dispatchMotionLocked( |
| 876 | nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 877 | // Preprocessing. |
| 878 | if (! entry->dispatchInProgress) { |
| 879 | entry->dispatchInProgress = true; |
| 880 | |
| 881 | logOutboundMotionDetailsLocked("dispatchMotion - ", entry); |
| 882 | } |
| 883 | |
| 884 | // Clean up if dropping the event. |
| 885 | if (*dropReason != DROP_REASON_NOT_DROPPED) { |
| 886 | setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY |
| 887 | ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
| 888 | return true; |
| 889 | } |
| 890 | |
| 891 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 892 | |
| 893 | // Identify targets. |
| 894 | Vector<InputTarget> inputTargets; |
| 895 | |
| 896 | bool conflictingPointerActions = false; |
| 897 | int32_t injectionResult; |
| 898 | if (isPointerEvent) { |
| 899 | // Pointer event. (eg. touchscreen) |
| 900 | injectionResult = findTouchedWindowTargetsLocked(currentTime, |
| 901 | entry, inputTargets, nextWakeupTime, &conflictingPointerActions); |
| 902 | } else { |
| 903 | // Non touch event. (eg. trackball) |
| 904 | injectionResult = findFocusedWindowTargetsLocked(currentTime, |
| 905 | entry, inputTargets, nextWakeupTime); |
| 906 | } |
| 907 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | setInjectionResultLocked(entry, injectionResult); |
| 912 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 913 | if (injectionResult != INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| 914 | CancelationOptions::Mode mode(isPointerEvent ? |
| 915 | CancelationOptions::CANCEL_POINTER_EVENTS : |
| 916 | CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 917 | CancelationOptions options(mode, "input event injection failed"); |
| 918 | synthesizeCancelationEventsForMonitorsLocked(options); |
| 919 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 920 | return true; |
| 921 | } |
| 922 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 923 | // Add monitor channels from event's or focused display. |
| 924 | addMonitoringTargetsLocked(inputTargets, getTargetDisplayId(entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | |
| 926 | // Dispatch the motion. |
| 927 | if (conflictingPointerActions) { |
| 928 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 929 | "conflicting pointer actions"); |
| 930 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 931 | } |
| 932 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 933 | return true; |
| 934 | } |
| 935 | |
| 936 | |
| 937 | void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) { |
| 938 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 939 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 940 | ", policyFlags=0x%x, " |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 941 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 942 | "metaState=0x%x, buttonState=0x%x," |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 943 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 944 | prefix, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 945 | entry->eventTime, entry->deviceId, entry->source, entry->displayId, entry->policyFlags, |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 946 | entry->action, entry->actionButton, entry->flags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 947 | entry->metaState, entry->buttonState, |
| 948 | entry->edgeFlags, entry->xPrecision, entry->yPrecision, |
| 949 | entry->downTime); |
| 950 | |
| 951 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
| 952 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 953 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 954 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
Vladislav Kaznacheev | 78f97b3 | 2016-12-15 18:14:58 -0800 | [diff] [blame] | 955 | "orientation=%f", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | i, entry->pointerProperties[i].id, |
| 957 | entry->pointerProperties[i].toolType, |
| 958 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 959 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 960 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 961 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 962 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 963 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 964 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 965 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
Vladislav Kaznacheev | 78f97b3 | 2016-12-15 18:14:58 -0800 | [diff] [blame] | 966 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 967 | } |
| 968 | #endif |
| 969 | } |
| 970 | |
| 971 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 972 | EventEntry* eventEntry, const Vector<InputTarget>& inputTargets) { |
| 973 | #if DEBUG_DISPATCH_CYCLE |
| 974 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 975 | #endif |
| 976 | |
| 977 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 978 | |
| 979 | pokeUserActivityLocked(eventEntry); |
| 980 | |
| 981 | for (size_t i = 0; i < inputTargets.size(); i++) { |
| 982 | const InputTarget& inputTarget = inputTargets.itemAt(i); |
| 983 | |
| 984 | ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); |
| 985 | if (connectionIndex >= 0) { |
| 986 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| 987 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget); |
| 988 | } else { |
| 989 | #if DEBUG_FOCUS |
| 990 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 991 | "is no longer registered with the input dispatcher.", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 992 | inputTarget.inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 993 | #endif |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime, |
| 999 | const EventEntry* entry, |
| 1000 | const sp<InputApplicationHandle>& applicationHandle, |
| 1001 | const sp<InputWindowHandle>& windowHandle, |
| 1002 | nsecs_t* nextWakeupTime, const char* reason) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1003 | if (applicationHandle == nullptr && windowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) { |
| 1005 | #if DEBUG_FOCUS |
| 1006 | ALOGD("Waiting for system to become ready for input. Reason: %s", reason); |
| 1007 | #endif |
| 1008 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY; |
| 1009 | mInputTargetWaitStartTime = currentTime; |
| 1010 | mInputTargetWaitTimeoutTime = LONG_LONG_MAX; |
| 1011 | mInputTargetWaitTimeoutExpired = false; |
| 1012 | mInputTargetWaitApplicationHandle.clear(); |
| 1013 | } |
| 1014 | } else { |
| 1015 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| 1016 | #if DEBUG_FOCUS |
| 1017 | ALOGD("Waiting for application to become ready for input: %s. Reason: %s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1018 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).c_str(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1019 | reason); |
| 1020 | #endif |
| 1021 | nsecs_t timeout; |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1022 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1023 | timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1024 | } else if (applicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1025 | timeout = applicationHandle->getDispatchingTimeout( |
| 1026 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1027 | } else { |
| 1028 | timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
| 1029 | } |
| 1030 | |
| 1031 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY; |
| 1032 | mInputTargetWaitStartTime = currentTime; |
| 1033 | mInputTargetWaitTimeoutTime = currentTime + timeout; |
| 1034 | mInputTargetWaitTimeoutExpired = false; |
| 1035 | mInputTargetWaitApplicationHandle.clear(); |
| 1036 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1037 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | mInputTargetWaitApplicationHandle = windowHandle->inputApplicationHandle; |
| 1039 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1040 | if (mInputTargetWaitApplicationHandle == nullptr && applicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1041 | mInputTargetWaitApplicationHandle = applicationHandle; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | if (mInputTargetWaitTimeoutExpired) { |
| 1047 | return INPUT_EVENT_INJECTION_TIMED_OUT; |
| 1048 | } |
| 1049 | |
| 1050 | if (currentTime >= mInputTargetWaitTimeoutTime) { |
| 1051 | onANRLocked(currentTime, applicationHandle, windowHandle, |
| 1052 | entry->eventTime, mInputTargetWaitStartTime, reason); |
| 1053 | |
| 1054 | // Force poll loop to wake up immediately on next iteration once we get the |
| 1055 | // ANR response back from the policy. |
| 1056 | *nextWakeupTime = LONG_LONG_MIN; |
| 1057 | return INPUT_EVENT_INJECTION_PENDING; |
| 1058 | } else { |
| 1059 | // Force poll loop to wake up when timeout is due. |
| 1060 | if (mInputTargetWaitTimeoutTime < *nextWakeupTime) { |
| 1061 | *nextWakeupTime = mInputTargetWaitTimeoutTime; |
| 1062 | } |
| 1063 | return INPUT_EVENT_INJECTION_PENDING; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
| 1068 | const sp<InputChannel>& inputChannel) { |
| 1069 | if (newTimeout > 0) { |
| 1070 | // Extend the timeout. |
| 1071 | mInputTargetWaitTimeoutTime = now() + newTimeout; |
| 1072 | } else { |
| 1073 | // Give up. |
| 1074 | mInputTargetWaitTimeoutExpired = true; |
| 1075 | |
| 1076 | // Input state will not be realistic. Mark it out of sync. |
| 1077 | if (inputChannel.get()) { |
| 1078 | ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
| 1079 | if (connectionIndex >= 0) { |
| 1080 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| 1081 | sp<InputWindowHandle> windowHandle = connection->inputWindowHandle; |
| 1082 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1083 | if (windowHandle != nullptr) { |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1084 | const InputWindowInfo* info = windowHandle->getInfo(); |
| 1085 | if (info) { |
| 1086 | ssize_t stateIndex = mTouchStatesByDisplay.indexOfKey(info->displayId); |
| 1087 | if (stateIndex >= 0) { |
| 1088 | mTouchStatesByDisplay.editValueAt(stateIndex).removeWindow( |
| 1089 | windowHandle); |
| 1090 | } |
| 1091 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1095 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1096 | "application not responding"); |
| 1097 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked( |
| 1105 | nsecs_t currentTime) { |
| 1106 | if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| 1107 | return currentTime - mInputTargetWaitStartTime; |
| 1108 | } |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | void InputDispatcher::resetANRTimeoutsLocked() { |
| 1113 | #if DEBUG_FOCUS |
| 1114 | ALOGD("Resetting ANR timeouts."); |
| 1115 | #endif |
| 1116 | |
| 1117 | // Reset input target wait timeout. |
| 1118 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; |
| 1119 | mInputTargetWaitApplicationHandle.clear(); |
| 1120 | } |
| 1121 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1122 | /** |
| 1123 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1124 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1125 | * Focused display is the display that the user most recently interacted with. |
| 1126 | */ |
| 1127 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry* entry) { |
| 1128 | int32_t displayId; |
| 1129 | switch (entry->type) { |
| 1130 | case EventEntry::TYPE_KEY: { |
| 1131 | const KeyEntry* typedEntry = static_cast<const KeyEntry*>(entry); |
| 1132 | displayId = typedEntry->displayId; |
| 1133 | break; |
| 1134 | } |
| 1135 | case EventEntry::TYPE_MOTION: { |
| 1136 | const MotionEntry* typedEntry = static_cast<const MotionEntry*>(entry); |
| 1137 | displayId = typedEntry->displayId; |
| 1138 | break; |
| 1139 | } |
| 1140 | default: { |
| 1141 | ALOGE("Unsupported event type '%" PRId32 "' for target display.", entry->type); |
| 1142 | return ADISPLAY_ID_NONE; |
| 1143 | } |
| 1144 | } |
| 1145 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1146 | } |
| 1147 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
| 1149 | const EventEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) { |
| 1150 | int32_t injectionResult; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1151 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1152 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1153 | int32_t displayId = getTargetDisplayId(entry); |
| 1154 | sp<InputWindowHandle> focusedWindowHandle = |
| 1155 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 1156 | sp<InputApplicationHandle> focusedApplicationHandle = |
| 1157 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1158 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | // If there is no currently focused window and no focused application |
| 1160 | // then drop the event. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1161 | if (focusedWindowHandle == nullptr) { |
| 1162 | if (focusedApplicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1164 | focusedApplicationHandle, nullptr, nextWakeupTime, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | "Waiting because no window has focus but there is a " |
| 1166 | "focused application that may eventually add a window " |
| 1167 | "when it finishes starting up."); |
| 1168 | goto Unresponsive; |
| 1169 | } |
| 1170 | |
| 1171 | ALOGI("Dropping event because there is no focused window or focused application."); |
| 1172 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1173 | goto Failed; |
| 1174 | } |
| 1175 | |
| 1176 | // Check permissions. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1177 | if (!checkInjectionPermission(focusedWindowHandle, entry->injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1179 | goto Failed; |
| 1180 | } |
| 1181 | |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1182 | // Check whether the window is ready for more input. |
| 1183 | reason = checkWindowReadyForMoreInputLocked(currentTime, |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1184 | focusedWindowHandle, entry, "focused"); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1185 | if (!reason.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1186 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1187 | focusedApplicationHandle, focusedWindowHandle, nextWakeupTime, reason.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1188 | goto Unresponsive; |
| 1189 | } |
| 1190 | |
| 1191 | // Success! Output targets. |
| 1192 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1193 | addWindowTargetLocked(focusedWindowHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0), |
| 1195 | inputTargets); |
| 1196 | |
| 1197 | // Done. |
| 1198 | Failed: |
| 1199 | Unresponsive: |
| 1200 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
| 1201 | updateDispatchStatisticsLocked(currentTime, entry, |
| 1202 | injectionResult, timeSpentWaitingForApplication); |
| 1203 | #if DEBUG_FOCUS |
| 1204 | ALOGD("findFocusedWindow finished: injectionResult=%d, " |
| 1205 | "timeSpentWaitingForApplication=%0.1fms", |
| 1206 | injectionResult, timeSpentWaitingForApplication / 1000000.0); |
| 1207 | #endif |
| 1208 | return injectionResult; |
| 1209 | } |
| 1210 | |
| 1211 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
| 1212 | const MotionEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime, |
| 1213 | bool* outConflictingPointerActions) { |
| 1214 | enum InjectionPermission { |
| 1215 | INJECTION_PERMISSION_UNKNOWN, |
| 1216 | INJECTION_PERMISSION_GRANTED, |
| 1217 | INJECTION_PERMISSION_DENIED |
| 1218 | }; |
| 1219 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | // For security reasons, we defer updating the touch state until we are sure that |
| 1221 | // event injection will be allowed. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | int32_t displayId = entry->displayId; |
| 1223 | int32_t action = entry->action; |
| 1224 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1225 | |
| 1226 | // Update the touch state as needed based on the properties of the touch event. |
| 1227 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1228 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
| 1229 | sp<InputWindowHandle> newHoverWindowHandle; |
| 1230 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1231 | // Copy current touch state into mTempTouchState. |
| 1232 | // This state is always reset at the end of this function, so if we don't find state |
| 1233 | // for the specified display then our initial state will be empty. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1234 | const TouchState* oldState = nullptr; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1235 | ssize_t oldStateIndex = mTouchStatesByDisplay.indexOfKey(displayId); |
| 1236 | if (oldStateIndex >= 0) { |
| 1237 | oldState = &mTouchStatesByDisplay.valueAt(oldStateIndex); |
| 1238 | mTempTouchState.copyFrom(*oldState); |
| 1239 | } |
| 1240 | |
| 1241 | bool isSplit = mTempTouchState.split; |
| 1242 | bool switchedDevice = mTempTouchState.deviceId >= 0 && mTempTouchState.displayId >= 0 |
| 1243 | && (mTempTouchState.deviceId != entry->deviceId |
| 1244 | || mTempTouchState.source != entry->source |
| 1245 | || mTempTouchState.displayId != displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1246 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
| 1247 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
| 1248 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1249 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN |
| 1250 | || maskedAction == AMOTION_EVENT_ACTION_SCROLL |
| 1251 | || isHoverAction); |
| 1252 | bool wrongDevice = false; |
| 1253 | if (newGesture) { |
| 1254 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1255 | if (switchedDevice && mTempTouchState.down && !down && !isHoverAction) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | #if DEBUG_FOCUS |
| 1257 | ALOGD("Dropping event because a pointer for a different device is already down."); |
| 1258 | #endif |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1259 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1260 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1261 | switchedDevice = false; |
| 1262 | wrongDevice = true; |
| 1263 | goto Failed; |
| 1264 | } |
| 1265 | mTempTouchState.reset(); |
| 1266 | mTempTouchState.down = down; |
| 1267 | mTempTouchState.deviceId = entry->deviceId; |
| 1268 | mTempTouchState.source = entry->source; |
| 1269 | mTempTouchState.displayId = displayId; |
| 1270 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1271 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
| 1272 | #if DEBUG_FOCUS |
| 1273 | ALOGI("Dropping move event because a pointer for a different device is already active."); |
| 1274 | #endif |
| 1275 | // TODO: test multiple simultaneous input streams. |
| 1276 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1277 | switchedDevice = false; |
| 1278 | wrongDevice = true; |
| 1279 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1283 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1284 | |
| 1285 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1286 | int32_t x = int32_t(entry->pointerCoords[pointerIndex]. |
| 1287 | getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1288 | int32_t y = int32_t(entry->pointerCoords[pointerIndex]. |
| 1289 | getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 1290 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | bool isTouchModal = false; |
| 1292 | |
| 1293 | // Traverse windows from front to back to find touched window and outside targets. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1294 | const Vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 1295 | size_t numWindows = windowHandles.size(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1296 | for (size_t i = 0; i < numWindows; i++) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1297 | sp<InputWindowHandle> windowHandle = windowHandles.itemAt(i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 1299 | if (windowInfo->displayId != displayId) { |
| 1300 | continue; // wrong display |
| 1301 | } |
| 1302 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | int32_t flags = windowInfo->layoutParamsFlags; |
| 1304 | if (windowInfo->visible) { |
| 1305 | if (! (flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
| 1306 | isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
| 1307 | | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
| 1308 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 1309 | newTouchedWindowHandle = windowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 | break; // found touched window, exit window loop |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN |
| 1315 | && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1316 | mTempTouchState.addOrUpdateWindow( |
Michael Wright | 3b10610 | 2017-01-16 21:05:07 +0000 | [diff] [blame] | 1317 | windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | // Figure out whether splitting will be allowed for this window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1323 | if (newTouchedWindowHandle != nullptr |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1324 | && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1325 | // New window supports splitting. |
| 1326 | isSplit = true; |
| 1327 | } else if (isSplit) { |
| 1328 | // New window does not support splitting but we have already split events. |
| 1329 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1330 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1334 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1335 | // Try to assign the pointer to the first foreground window we find, if there is one. |
| 1336 | newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1337 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1338 | ALOGI("Dropping event because there is no touchable window at (%d, %d).", x, y); |
| 1339 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1340 | goto Failed; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | // Set target flags. |
| 1345 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1346 | if (isSplit) { |
| 1347 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1348 | } |
| 1349 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1350 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1351 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1352 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // Update hover state. |
| 1356 | if (isHoverAction) { |
| 1357 | newHoverWindowHandle = newTouchedWindowHandle; |
| 1358 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 1359 | newHoverWindowHandle = mLastHoverWindowHandle; |
| 1360 | } |
| 1361 | |
| 1362 | // Update the temporary touch state. |
| 1363 | BitSet32 pointerIds; |
| 1364 | if (isSplit) { |
| 1365 | uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
| 1366 | pointerIds.markBit(pointerId); |
| 1367 | } |
| 1368 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
| 1369 | } else { |
| 1370 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1371 | |
| 1372 | // If the pointer is not currently down, then ignore the event. |
| 1373 | if (! mTempTouchState.down) { |
| 1374 | #if DEBUG_FOCUS |
| 1375 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1376 | "dropped the pointer down event."); |
| 1377 | #endif |
| 1378 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1379 | goto Failed; |
| 1380 | } |
| 1381 | |
| 1382 | // Check whether touches should slip outside of the current foreground window. |
| 1383 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE |
| 1384 | && entry->pointerCount == 1 |
| 1385 | && mTempTouchState.isSlippery()) { |
| 1386 | int32_t x = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1387 | int32_t y = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 1388 | |
| 1389 | sp<InputWindowHandle> oldTouchedWindowHandle = |
| 1390 | mTempTouchState.getFirstForegroundWindowHandle(); |
| 1391 | sp<InputWindowHandle> newTouchedWindowHandle = |
| 1392 | findTouchedWindowAtLocked(displayId, x, y); |
| 1393 | if (oldTouchedWindowHandle != newTouchedWindowHandle |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1394 | && newTouchedWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1395 | #if DEBUG_FOCUS |
| 1396 | ALOGD("Touch is slipping out of window %s into window %s.", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1397 | oldTouchedWindowHandle->getName().c_str(), |
| 1398 | newTouchedWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1399 | #endif |
| 1400 | // Make a slippery exit from the old window. |
| 1401 | mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1402 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0)); |
| 1403 | |
| 1404 | // Make a slippery entrance into the new window. |
| 1405 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1406 | isSplit = true; |
| 1407 | } |
| 1408 | |
| 1409 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND |
| 1410 | | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 1411 | if (isSplit) { |
| 1412 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1413 | } |
| 1414 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1415 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1416 | } |
| 1417 | |
| 1418 | BitSet32 pointerIds; |
| 1419 | if (isSplit) { |
| 1420 | pointerIds.markBit(entry->pointerProperties[0].id); |
| 1421 | } |
| 1422 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
| 1428 | // Let the previous window know that the hover sequence is over. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1429 | if (mLastHoverWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1430 | #if DEBUG_HOVER |
| 1431 | ALOGD("Sending hover exit event to window %s.", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1432 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1433 | #endif |
| 1434 | mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1435 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
| 1436 | } |
| 1437 | |
| 1438 | // Let the new window know that the hover sequence is starting. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1439 | if (newHoverWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | #if DEBUG_HOVER |
| 1441 | ALOGD("Sending hover enter event to window %s.", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1442 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1443 | #endif |
| 1444 | mTempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1445 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0)); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | // Check permission to inject into all touched foreground windows and ensure there |
| 1450 | // is at least one touched foreground window. |
| 1451 | { |
| 1452 | bool haveForegroundWindow = false; |
| 1453 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1454 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| 1455 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1456 | haveForegroundWindow = true; |
| 1457 | if (! checkInjectionPermission(touchedWindow.windowHandle, |
| 1458 | entry->injectionState)) { |
| 1459 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1460 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1461 | goto Failed; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | if (! haveForegroundWindow) { |
| 1466 | #if DEBUG_FOCUS |
| 1467 | ALOGD("Dropping event because there is no touched foreground window to receive it."); |
| 1468 | #endif |
| 1469 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1470 | goto Failed; |
| 1471 | } |
| 1472 | |
| 1473 | // Permission granted to injection into all touched foreground windows. |
| 1474 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1475 | } |
| 1476 | |
| 1477 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1478 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1479 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1480 | sp<InputWindowHandle> foregroundWindowHandle = |
| 1481 | mTempTouchState.getFirstForegroundWindowHandle(); |
| 1482 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
| 1483 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1484 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| 1485 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1486 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1487 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 1488 | mTempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1489 | InputTarget::FLAG_ZERO_COORDS, BitSet32(0)); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | // Ensure all touched foreground windows are ready for new input. |
| 1496 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1497 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| 1498 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1499 | // Check whether the window is ready for more input. |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1500 | std::string reason = checkWindowReadyForMoreInputLocked(currentTime, |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1501 | touchedWindow.windowHandle, entry, "touched"); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1502 | if (!reason.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1504 | nullptr, touchedWindow.windowHandle, nextWakeupTime, reason.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1505 | goto Unresponsive; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1511 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1512 | // of the touch gesture. |
| 1513 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1514 | // engine only supports touch events. We would need to add a mechanism similar |
| 1515 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1516 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1517 | sp<InputWindowHandle> foregroundWindowHandle = |
| 1518 | mTempTouchState.getFirstForegroundWindowHandle(); |
| 1519 | if (foregroundWindowHandle->getInfo()->hasWallpaper) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1520 | const Vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 1521 | size_t numWindows = windowHandles.size(); |
| 1522 | for (size_t i = 0; i < numWindows; i++) { |
| 1523 | sp<InputWindowHandle> windowHandle = windowHandles.itemAt(i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1524 | const InputWindowInfo* info = windowHandle->getInfo(); |
| 1525 | if (info->displayId == displayId |
| 1526 | && windowHandle->getInfo()->layoutParamsType |
| 1527 | == InputWindowInfo::TYPE_WALLPAPER) { |
| 1528 | mTempTouchState.addOrUpdateWindow(windowHandle, |
| 1529 | InputTarget::FLAG_WINDOW_IS_OBSCURED |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1530 | | InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1532 | BitSet32(0)); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | // Success! Output targets. |
| 1539 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1540 | |
| 1541 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1542 | const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i); |
| 1543 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 1544 | touchedWindow.pointerIds, inputTargets); |
| 1545 | } |
| 1546 | |
| 1547 | // Drop the outside or hover touch windows since we will not care about them |
| 1548 | // in the next iteration. |
| 1549 | mTempTouchState.filterNonAsIsTouchWindows(); |
| 1550 | |
| 1551 | Failed: |
| 1552 | // Check injection permission once and for all. |
| 1553 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1554 | if (checkInjectionPermission(nullptr, entry->injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1555 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1556 | } else { |
| 1557 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | // Update final pieces of touch state if the injector had permission. |
| 1562 | if (injectionPermission == INJECTION_PERMISSION_GRANTED) { |
| 1563 | if (!wrongDevice) { |
| 1564 | if (switchedDevice) { |
| 1565 | #if DEBUG_FOCUS |
| 1566 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1567 | #endif |
| 1568 | *outConflictingPointerActions = true; |
| 1569 | } |
| 1570 | |
| 1571 | if (isHoverAction) { |
| 1572 | // Started hovering, therefore no longer down. |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1573 | if (oldState && oldState->down) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 | #if DEBUG_FOCUS |
| 1575 | ALOGD("Conflicting pointer actions: Hover received while pointer was down."); |
| 1576 | #endif |
| 1577 | *outConflictingPointerActions = true; |
| 1578 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1579 | mTempTouchState.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1580 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
| 1581 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1582 | mTempTouchState.deviceId = entry->deviceId; |
| 1583 | mTempTouchState.source = entry->source; |
| 1584 | mTempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1585 | } |
| 1586 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP |
| 1587 | || maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 1588 | // All pointers up or canceled. |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1589 | mTempTouchState.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1591 | // First pointer went down. |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1592 | if (oldState && oldState->down) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1593 | #if DEBUG_FOCUS |
| 1594 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1595 | #endif |
| 1596 | *outConflictingPointerActions = true; |
| 1597 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1598 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1599 | // One pointer went up. |
| 1600 | if (isSplit) { |
| 1601 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1602 | uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
| 1603 | |
| 1604 | for (size_t i = 0; i < mTempTouchState.windows.size(); ) { |
| 1605 | TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i); |
| 1606 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1607 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1608 | if (touchedWindow.pointerIds.isEmpty()) { |
| 1609 | mTempTouchState.windows.removeAt(i); |
| 1610 | continue; |
| 1611 | } |
| 1612 | } |
| 1613 | i += 1; |
| 1614 | } |
| 1615 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | // Save changes unless the action was scroll in which case the temporary touch |
| 1619 | // state was only valid for this one action. |
| 1620 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 1621 | if (mTempTouchState.displayId >= 0) { |
| 1622 | if (oldStateIndex >= 0) { |
| 1623 | mTouchStatesByDisplay.editValueAt(oldStateIndex).copyFrom(mTempTouchState); |
| 1624 | } else { |
| 1625 | mTouchStatesByDisplay.add(displayId, mTempTouchState); |
| 1626 | } |
| 1627 | } else if (oldStateIndex >= 0) { |
| 1628 | mTouchStatesByDisplay.removeItemsAt(oldStateIndex); |
| 1629 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | // Update hover state. |
| 1633 | mLastHoverWindowHandle = newHoverWindowHandle; |
| 1634 | } |
| 1635 | } else { |
| 1636 | #if DEBUG_FOCUS |
| 1637 | ALOGD("Not updating touch focus because injection was denied."); |
| 1638 | #endif |
| 1639 | } |
| 1640 | |
| 1641 | Unresponsive: |
| 1642 | // Reset temporary touch state to ensure we release unnecessary references to input channels. |
| 1643 | mTempTouchState.reset(); |
| 1644 | |
| 1645 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
| 1646 | updateDispatchStatisticsLocked(currentTime, entry, |
| 1647 | injectionResult, timeSpentWaitingForApplication); |
| 1648 | #if DEBUG_FOCUS |
| 1649 | ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " |
| 1650 | "timeSpentWaitingForApplication=%0.1fms", |
| 1651 | injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); |
| 1652 | #endif |
| 1653 | return injectionResult; |
| 1654 | } |
| 1655 | |
| 1656 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
| 1657 | int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets) { |
| 1658 | inputTargets.push(); |
| 1659 | |
| 1660 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 1661 | InputTarget& target = inputTargets.editTop(); |
| 1662 | target.inputChannel = windowInfo->inputChannel; |
| 1663 | target.flags = targetFlags; |
| 1664 | target.xOffset = - windowInfo->frameLeft; |
| 1665 | target.yOffset = - windowInfo->frameTop; |
| 1666 | target.scaleFactor = windowInfo->scaleFactor; |
| 1667 | target.pointerIds = pointerIds; |
| 1668 | } |
| 1669 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 1670 | void InputDispatcher::addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets, |
| 1671 | int32_t displayId) { |
| 1672 | std::unordered_map<int32_t, Vector<sp<InputChannel>>>::const_iterator it = |
| 1673 | mMonitoringChannelsByDisplay.find(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1674 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 1675 | if (it != mMonitoringChannelsByDisplay.end()) { |
| 1676 | const Vector<sp<InputChannel>>& monitoringChannels = it->second; |
| 1677 | const size_t numChannels = monitoringChannels.size(); |
| 1678 | for (size_t i = 0; i < numChannels; i++) { |
| 1679 | inputTargets.push(); |
| 1680 | |
| 1681 | InputTarget& target = inputTargets.editTop(); |
| 1682 | target.inputChannel = monitoringChannels[i]; |
| 1683 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1684 | target.xOffset = 0; |
| 1685 | target.yOffset = 0; |
| 1686 | target.pointerIds.clear(); |
| 1687 | target.scaleFactor = 1.0f; |
| 1688 | } |
| 1689 | } else { |
| 1690 | // If there is no monitor channel registered or all monitor channel unregistered, |
| 1691 | // the display can't detect the extra system gesture by a copy of input events. |
| 1692 | ALOGW("There is no monitor channel found in display=%" PRId32, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
| 1697 | const InjectionState* injectionState) { |
| 1698 | if (injectionState |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1699 | && (windowHandle == nullptr |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1700 | || windowHandle->getInfo()->ownerUid != injectionState->injectorUid) |
| 1701 | && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1702 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | ALOGW("Permission denied: injecting event from pid %d uid %d to window %s " |
| 1704 | "owned by uid %d", |
| 1705 | injectionState->injectorPid, injectionState->injectorUid, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1706 | windowHandle->getName().c_str(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1707 | windowHandle->getInfo()->ownerUid); |
| 1708 | } else { |
| 1709 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
| 1710 | injectionState->injectorPid, injectionState->injectorUid); |
| 1711 | } |
| 1712 | return false; |
| 1713 | } |
| 1714 | return true; |
| 1715 | } |
| 1716 | |
| 1717 | bool InputDispatcher::isWindowObscuredAtPointLocked( |
| 1718 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
| 1719 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1720 | const Vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 1721 | size_t numWindows = windowHandles.size(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | for (size_t i = 0; i < numWindows; i++) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1723 | sp<InputWindowHandle> otherHandle = windowHandles.itemAt(i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1724 | if (otherHandle == windowHandle) { |
| 1725 | break; |
| 1726 | } |
| 1727 | |
| 1728 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
| 1729 | if (otherInfo->displayId == displayId |
| 1730 | && otherInfo->visible && !otherInfo->isTrustedOverlay() |
| 1731 | && otherInfo->frameContainsPoint(x, y)) { |
| 1732 | return true; |
| 1733 | } |
| 1734 | } |
| 1735 | return false; |
| 1736 | } |
| 1737 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1738 | |
| 1739 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 1740 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1741 | const Vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1742 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1743 | size_t numWindows = windowHandles.size(); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1744 | for (size_t i = 0; i < numWindows; i++) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1745 | sp<InputWindowHandle> otherHandle = windowHandles.itemAt(i); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 1746 | if (otherHandle == windowHandle) { |
| 1747 | break; |
| 1748 | } |
| 1749 | |
| 1750 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
| 1751 | if (otherInfo->displayId == displayId |
| 1752 | && otherInfo->visible && !otherInfo->isTrustedOverlay() |
| 1753 | && otherInfo->overlaps(windowInfo)) { |
| 1754 | return true; |
| 1755 | } |
| 1756 | } |
| 1757 | return false; |
| 1758 | } |
| 1759 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1760 | std::string InputDispatcher::checkWindowReadyForMoreInputLocked(nsecs_t currentTime, |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1761 | const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry, |
| 1762 | const char* targetType) { |
| 1763 | // If the window is paused then keep waiting. |
| 1764 | if (windowHandle->getInfo()->paused) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1765 | return StringPrintf("Waiting because the %s window is paused.", targetType); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | // If the window's connection is not registered then keep waiting. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1769 | ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel()); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1770 | if (connectionIndex < 0) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1771 | return StringPrintf("Waiting because the %s window's input channel is not " |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1772 | "registered with the input dispatcher. The window may be in the process " |
| 1773 | "of being removed.", targetType); |
| 1774 | } |
| 1775 | |
| 1776 | // If the connection is dead then keep waiting. |
| 1777 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| 1778 | if (connection->status != Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1779 | return StringPrintf("Waiting because the %s window's input connection is %s." |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1780 | "The window may be in the process of being removed.", targetType, |
| 1781 | connection->getStatusLabel()); |
| 1782 | } |
| 1783 | |
| 1784 | // If the connection is backed up then keep waiting. |
| 1785 | if (connection->inputPublisherBlocked) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1786 | return StringPrintf("Waiting because the %s window's input channel is full. " |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1787 | "Outbound queue length: %d. Wait queue length: %d.", |
| 1788 | targetType, connection->outboundQueue.count(), connection->waitQueue.count()); |
| 1789 | } |
| 1790 | |
| 1791 | // Ensure that the dispatch queues aren't too far backed up for this event. |
| 1792 | if (eventEntry->type == EventEntry::TYPE_KEY) { |
| 1793 | // If the event is a key event, then we must wait for all previous events to |
| 1794 | // complete before delivering it because previous events may have the |
| 1795 | // side-effect of transferring focus to a different window and we want to |
| 1796 | // ensure that the following keys are sent to the new window. |
| 1797 | // |
| 1798 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1799 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1800 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1801 | // often anticipate pending UI changes when typing on a keyboard. |
| 1802 | // To obtain this behavior, we must serialize key events with respect to all |
| 1803 | // prior input events. |
| 1804 | if (!connection->outboundQueue.isEmpty() || !connection->waitQueue.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1805 | return StringPrintf("Waiting to send key event because the %s window has not " |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1806 | "finished processing all of the input events that were previously " |
| 1807 | "delivered to it. Outbound queue length: %d. Wait queue length: %d.", |
| 1808 | targetType, connection->outboundQueue.count(), connection->waitQueue.count()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | } |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1810 | } else { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | // Touch events can always be sent to a window immediately because the user intended |
| 1812 | // to touch whatever was visible at the time. Even if focus changes or a new |
| 1813 | // window appears moments later, the touch event was meant to be delivered to |
| 1814 | // whatever window happened to be on screen at the time. |
| 1815 | // |
| 1816 | // Generic motion events, such as trackball or joystick events are a little trickier. |
| 1817 | // Like key events, generic motion events are delivered to the focused window. |
| 1818 | // Unlike key events, generic motion events don't tend to transfer focus to other |
| 1819 | // windows and it is not important for them to be serialized. So we prefer to deliver |
| 1820 | // generic motion events as soon as possible to improve efficiency and reduce lag |
| 1821 | // through batching. |
| 1822 | // |
| 1823 | // The one case where we pause input event delivery is when the wait queue is piling |
| 1824 | // up with lots of events because the application is not responding. |
| 1825 | // This condition ensures that ANRs are detected reliably. |
| 1826 | if (!connection->waitQueue.isEmpty() |
| 1827 | && currentTime >= connection->waitQueue.head->deliveryTime |
| 1828 | + STREAM_AHEAD_EVENT_TIMEOUT) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1829 | return StringPrintf("Waiting to send non-key event because the %s window has not " |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1830 | "finished processing certain input events that were delivered to it over " |
| 1831 | "%0.1fms ago. Wait queue length: %d. Wait queue head age: %0.1fms.", |
| 1832 | targetType, STREAM_AHEAD_EVENT_TIMEOUT * 0.000001f, |
| 1833 | connection->waitQueue.count(), |
| 1834 | (currentTime - connection->waitQueue.head->deliveryTime) * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | } |
| 1836 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1837 | return ""; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1840 | std::string InputDispatcher::getApplicationWindowLabelLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | const sp<InputApplicationHandle>& applicationHandle, |
| 1842 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1843 | if (applicationHandle != nullptr) { |
| 1844 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1845 | std::string label(applicationHandle->getName()); |
| 1846 | label += " - "; |
| 1847 | label += windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | return label; |
| 1849 | } else { |
| 1850 | return applicationHandle->getName(); |
| 1851 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1852 | } else if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 | return windowHandle->getName(); |
| 1854 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1855 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1860 | int32_t displayId = getTargetDisplayId(eventEntry); |
| 1861 | sp<InputWindowHandle> focusedWindowHandle = |
| 1862 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 1863 | if (focusedWindowHandle != nullptr) { |
| 1864 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1865 | if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { |
| 1866 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1867 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1868 | #endif |
| 1869 | return; |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
| 1874 | switch (eventEntry->type) { |
| 1875 | case EventEntry::TYPE_MOTION: { |
| 1876 | const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry); |
| 1877 | if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 1878 | return; |
| 1879 | } |
| 1880 | |
| 1881 | if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) { |
| 1882 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 1883 | } |
| 1884 | break; |
| 1885 | } |
| 1886 | case EventEntry::TYPE_KEY: { |
| 1887 | const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry); |
| 1888 | if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) { |
| 1889 | return; |
| 1890 | } |
| 1891 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 1892 | break; |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | CommandEntry* commandEntry = postCommandLocked( |
| 1897 | & InputDispatcher::doPokeUserActivityLockedInterruptible); |
| 1898 | commandEntry->eventTime = eventEntry->eventTime; |
| 1899 | commandEntry->userActivityEventType = eventType; |
| 1900 | } |
| 1901 | |
| 1902 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
| 1903 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) { |
| 1904 | #if DEBUG_DISPATCH_CYCLE |
| 1905 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 1906 | "xOffset=%f, yOffset=%f, scaleFactor=%f, " |
| 1907 | "pointerIds=0x%x", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 1908 | connection->getInputChannelName().c_str(), inputTarget->flags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1909 | inputTarget->xOffset, inputTarget->yOffset, |
| 1910 | inputTarget->scaleFactor, inputTarget->pointerIds.value); |
| 1911 | #endif |
| 1912 | |
| 1913 | // Skip this event if the connection status is not normal. |
| 1914 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 1915 | if (connection->status != Connection::STATUS_NORMAL) { |
| 1916 | #if DEBUG_DISPATCH_CYCLE |
| 1917 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 1918 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | #endif |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | // Split a motion event if needed. |
| 1924 | if (inputTarget->flags & InputTarget::FLAG_SPLIT) { |
| 1925 | ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION); |
| 1926 | |
| 1927 | MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry); |
| 1928 | if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) { |
| 1929 | MotionEntry* splitMotionEntry = splitMotionEvent( |
| 1930 | originalMotionEntry, inputTarget->pointerIds); |
| 1931 | if (!splitMotionEntry) { |
| 1932 | return; // split event was dropped |
| 1933 | } |
| 1934 | #if DEBUG_FOCUS |
| 1935 | ALOGD("channel '%s' ~ Split motion event.", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 1936 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 | logOutboundMotionDetailsLocked(" ", splitMotionEntry); |
| 1938 | #endif |
| 1939 | enqueueDispatchEntriesLocked(currentTime, connection, |
| 1940 | splitMotionEntry, inputTarget); |
| 1941 | splitMotionEntry->release(); |
| 1942 | return; |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 1947 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 1948 | } |
| 1949 | |
| 1950 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
| 1951 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) { |
| 1952 | bool wasEmpty = connection->outboundQueue.isEmpty(); |
| 1953 | |
| 1954 | // Enqueue dispatch entries for the requested modes. |
| 1955 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 1956 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
| 1957 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 1958 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
| 1959 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 1960 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
| 1961 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 1962 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 1963 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 1964 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
| 1965 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 1966 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
| 1967 | |
| 1968 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 1969 | if (wasEmpty && !connection->outboundQueue.isEmpty()) { |
| 1970 | startDispatchCycleLocked(currentTime, connection); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | void InputDispatcher::enqueueDispatchEntryLocked( |
| 1975 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
| 1976 | int32_t dispatchMode) { |
| 1977 | int32_t inputTargetFlags = inputTarget->flags; |
| 1978 | if (!(inputTargetFlags & dispatchMode)) { |
| 1979 | return; |
| 1980 | } |
| 1981 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 1982 | |
| 1983 | // This is a new event. |
| 1984 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
| 1985 | DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref |
| 1986 | inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset, |
| 1987 | inputTarget->scaleFactor); |
| 1988 | |
| 1989 | // Apply target flags and update the connection's input state. |
| 1990 | switch (eventEntry->type) { |
| 1991 | case EventEntry::TYPE_KEY: { |
| 1992 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 1993 | dispatchEntry->resolvedAction = keyEntry->action; |
| 1994 | dispatchEntry->resolvedFlags = keyEntry->flags; |
| 1995 | |
| 1996 | if (!connection->inputState.trackKey(keyEntry, |
| 1997 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
| 1998 | #if DEBUG_DISPATCH_CYCLE |
| 1999 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2000 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | #endif |
| 2002 | delete dispatchEntry; |
| 2003 | return; // skip the inconsistent event |
| 2004 | } |
| 2005 | break; |
| 2006 | } |
| 2007 | |
| 2008 | case EventEntry::TYPE_MOTION: { |
| 2009 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
| 2010 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2011 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2012 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2013 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2014 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2015 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2016 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2017 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2018 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2019 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2020 | } else { |
| 2021 | dispatchEntry->resolvedAction = motionEntry->action; |
| 2022 | } |
| 2023 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
| 2024 | && !connection->inputState.isHovering( |
| 2025 | motionEntry->deviceId, motionEntry->source, motionEntry->displayId)) { |
| 2026 | #if DEBUG_DISPATCH_CYCLE |
| 2027 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2028 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2029 | #endif |
| 2030 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2031 | } |
| 2032 | |
| 2033 | dispatchEntry->resolvedFlags = motionEntry->flags; |
| 2034 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2035 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2036 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2037 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2038 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2039 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2040 | |
| 2041 | if (!connection->inputState.trackMotion(motionEntry, |
| 2042 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
| 2043 | #if DEBUG_DISPATCH_CYCLE |
| 2044 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2045 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2046 | #endif |
| 2047 | delete dispatchEntry; |
| 2048 | return; // skip the inconsistent event |
| 2049 | } |
| 2050 | break; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | // Remember that we are waiting for this dispatch to complete. |
| 2055 | if (dispatchEntry->hasForegroundTarget()) { |
| 2056 | incrementPendingForegroundDispatchesLocked(eventEntry); |
| 2057 | } |
| 2058 | |
| 2059 | // Enqueue the dispatch entry. |
| 2060 | connection->outboundQueue.enqueueAtTail(dispatchEntry); |
| 2061 | traceOutboundQueueLengthLocked(connection); |
| 2062 | } |
| 2063 | |
| 2064 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
| 2065 | const sp<Connection>& connection) { |
| 2066 | #if DEBUG_DISPATCH_CYCLE |
| 2067 | ALOGD("channel '%s' ~ startDispatchCycle", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2068 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2069 | #endif |
| 2070 | |
| 2071 | while (connection->status == Connection::STATUS_NORMAL |
| 2072 | && !connection->outboundQueue.isEmpty()) { |
| 2073 | DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
| 2074 | dispatchEntry->deliveryTime = currentTime; |
| 2075 | |
| 2076 | // Publish the event. |
| 2077 | status_t status; |
| 2078 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2079 | switch (eventEntry->type) { |
| 2080 | case EventEntry::TYPE_KEY: { |
| 2081 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2082 | |
| 2083 | // Publish the key event. |
| 2084 | status = connection->inputPublisher.publishKeyEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2085 | keyEntry->deviceId, keyEntry->source, keyEntry->displayId, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2086 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags, |
| 2087 | keyEntry->keyCode, keyEntry->scanCode, |
| 2088 | keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime, |
| 2089 | keyEntry->eventTime); |
| 2090 | break; |
| 2091 | } |
| 2092 | |
| 2093 | case EventEntry::TYPE_MOTION: { |
| 2094 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
| 2095 | |
| 2096 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2097 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2098 | |
| 2099 | // Set the X and Y offset depending on the input source. |
Siarhei Vishniakou | 635cb71 | 2017-11-01 16:32:14 -0700 | [diff] [blame] | 2100 | float xOffset, yOffset; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
| 2102 | && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
Siarhei Vishniakou | 635cb71 | 2017-11-01 16:32:14 -0700 | [diff] [blame] | 2103 | float scaleFactor = dispatchEntry->scaleFactor; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2104 | xOffset = dispatchEntry->xOffset * scaleFactor; |
| 2105 | yOffset = dispatchEntry->yOffset * scaleFactor; |
| 2106 | if (scaleFactor != 1.0f) { |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 2107 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2108 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
| 2109 | scaledCoords[i].scale(scaleFactor); |
| 2110 | } |
| 2111 | usingCoords = scaledCoords; |
| 2112 | } |
| 2113 | } else { |
| 2114 | xOffset = 0.0f; |
| 2115 | yOffset = 0.0f; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2116 | |
| 2117 | // We don't want the dispatch target to know. |
| 2118 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Narayan Kamath | bc6001b | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 2119 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2120 | scaledCoords[i].clear(); |
| 2121 | } |
| 2122 | usingCoords = scaledCoords; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | // Publish the motion event. |
| 2127 | status = connection->inputPublisher.publishMotionEvent(dispatchEntry->seq, |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 2128 | motionEntry->deviceId, motionEntry->source, motionEntry->displayId, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2129 | dispatchEntry->resolvedAction, motionEntry->actionButton, |
| 2130 | dispatchEntry->resolvedFlags, motionEntry->edgeFlags, |
| 2131 | motionEntry->metaState, motionEntry->buttonState, |
| 2132 | xOffset, yOffset, motionEntry->xPrecision, motionEntry->yPrecision, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2133 | motionEntry->downTime, motionEntry->eventTime, |
| 2134 | motionEntry->pointerCount, motionEntry->pointerProperties, |
| 2135 | usingCoords); |
| 2136 | break; |
| 2137 | } |
| 2138 | |
| 2139 | default: |
| 2140 | ALOG_ASSERT(false); |
| 2141 | return; |
| 2142 | } |
| 2143 | |
| 2144 | // Check the result. |
| 2145 | if (status) { |
| 2146 | if (status == WOULD_BLOCK) { |
| 2147 | if (connection->waitQueue.isEmpty()) { |
| 2148 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
| 2149 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2150 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2151 | "event to it, status=%d", connection->getInputChannelName().c_str(), |
| 2152 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2153 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2154 | } else { |
| 2155 | // Pipe is full and we are waiting for the app to finish process some events |
| 2156 | // before sending more events to it. |
| 2157 | #if DEBUG_DISPATCH_CYCLE |
| 2158 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 2159 | "waiting for the application to catch up", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2160 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2161 | #endif |
| 2162 | connection->inputPublisherBlocked = true; |
| 2163 | } |
| 2164 | } else { |
| 2165 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2166 | "status=%d", connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2167 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2168 | } |
| 2169 | return; |
| 2170 | } |
| 2171 | |
| 2172 | // Re-enqueue the event on the wait queue. |
| 2173 | connection->outboundQueue.dequeue(dispatchEntry); |
| 2174 | traceOutboundQueueLengthLocked(connection); |
| 2175 | connection->waitQueue.enqueueAtTail(dispatchEntry); |
| 2176 | traceWaitQueueLengthLocked(connection); |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
| 2181 | const sp<Connection>& connection, uint32_t seq, bool handled) { |
| 2182 | #if DEBUG_DISPATCH_CYCLE |
| 2183 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2184 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2185 | #endif |
| 2186 | |
| 2187 | connection->inputPublisherBlocked = false; |
| 2188 | |
| 2189 | if (connection->status == Connection::STATUS_BROKEN |
| 2190 | || connection->status == Connection::STATUS_ZOMBIE) { |
| 2191 | return; |
| 2192 | } |
| 2193 | |
| 2194 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2195 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2196 | } |
| 2197 | |
| 2198 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
| 2199 | const sp<Connection>& connection, bool notify) { |
| 2200 | #if DEBUG_DISPATCH_CYCLE |
| 2201 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2202 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | #endif |
| 2204 | |
| 2205 | // Clear the dispatch queues. |
| 2206 | drainDispatchQueueLocked(&connection->outboundQueue); |
| 2207 | traceOutboundQueueLengthLocked(connection); |
| 2208 | drainDispatchQueueLocked(&connection->waitQueue); |
| 2209 | traceWaitQueueLengthLocked(connection); |
| 2210 | |
| 2211 | // The connection appears to be unrecoverably broken. |
| 2212 | // Ignore already broken or zombie connections. |
| 2213 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2214 | connection->status = Connection::STATUS_BROKEN; |
| 2215 | |
| 2216 | if (notify) { |
| 2217 | // Notify other system components. |
| 2218 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | void InputDispatcher::drainDispatchQueueLocked(Queue<DispatchEntry>* queue) { |
| 2224 | while (!queue->isEmpty()) { |
| 2225 | DispatchEntry* dispatchEntry = queue->dequeueAtHead(); |
| 2226 | releaseDispatchEntryLocked(dispatchEntry); |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | void InputDispatcher::releaseDispatchEntryLocked(DispatchEntry* dispatchEntry) { |
| 2231 | if (dispatchEntry->hasForegroundTarget()) { |
| 2232 | decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry); |
| 2233 | } |
| 2234 | delete dispatchEntry; |
| 2235 | } |
| 2236 | |
| 2237 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2238 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2239 | |
| 2240 | { // acquire lock |
| 2241 | AutoMutex _l(d->mLock); |
| 2242 | |
| 2243 | ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd); |
| 2244 | if (connectionIndex < 0) { |
| 2245 | ALOGE("Received spurious receive callback for unknown input channel. " |
| 2246 | "fd=%d, events=0x%x", fd, events); |
| 2247 | return 0; // remove the callback |
| 2248 | } |
| 2249 | |
| 2250 | bool notify; |
| 2251 | sp<Connection> connection = d->mConnectionsByFd.valueAt(connectionIndex); |
| 2252 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2253 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2254 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2255 | "events=0x%x", connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | return 1; |
| 2257 | } |
| 2258 | |
| 2259 | nsecs_t currentTime = now(); |
| 2260 | bool gotOne = false; |
| 2261 | status_t status; |
| 2262 | for (;;) { |
| 2263 | uint32_t seq; |
| 2264 | bool handled; |
| 2265 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2266 | if (status) { |
| 2267 | break; |
| 2268 | } |
| 2269 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2270 | gotOne = true; |
| 2271 | } |
| 2272 | if (gotOne) { |
| 2273 | d->runCommandsLockedInterruptible(); |
| 2274 | if (status == WOULD_BLOCK) { |
| 2275 | return 1; |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2280 | if (notify) { |
| 2281 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2282 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } else { |
| 2285 | // Monitor channels are never explicitly unregistered. |
| 2286 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2287 | // about them. |
| 2288 | notify = !connection->monitor; |
| 2289 | if (notify) { |
| 2290 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2291 | "events=0x%x", connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | // Unregister the channel. |
| 2296 | d->unregisterInputChannelLocked(connection->inputChannel, notify); |
| 2297 | return 0; // remove the callback |
| 2298 | } // release lock |
| 2299 | } |
| 2300 | |
| 2301 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
| 2302 | const CancelationOptions& options) { |
| 2303 | for (size_t i = 0; i < mConnectionsByFd.size(); i++) { |
| 2304 | synthesizeCancelationEventsForConnectionLocked( |
| 2305 | mConnectionsByFd.valueAt(i), options); |
| 2306 | } |
| 2307 | } |
| 2308 | |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2309 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2310 | const CancelationOptions& options) { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 2311 | for (auto& it : mMonitoringChannelsByDisplay) { |
| 2312 | const Vector<sp<InputChannel>>& monitoringChannels = it.second; |
| 2313 | const size_t numChannels = monitoringChannels.size(); |
| 2314 | for (size_t i = 0; i < numChannels; i++) { |
| 2315 | synthesizeCancelationEventsForInputChannelLocked(monitoringChannels[i], options); |
| 2316 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2320 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
| 2321 | const sp<InputChannel>& channel, const CancelationOptions& options) { |
| 2322 | ssize_t index = getConnectionIndexLocked(channel); |
| 2323 | if (index >= 0) { |
| 2324 | synthesizeCancelationEventsForConnectionLocked( |
| 2325 | mConnectionsByFd.valueAt(index), options); |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2330 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2331 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2332 | return; |
| 2333 | } |
| 2334 | |
| 2335 | nsecs_t currentTime = now(); |
| 2336 | |
| 2337 | Vector<EventEntry*> cancelationEvents; |
| 2338 | connection->inputState.synthesizeCancelationEvents(currentTime, |
| 2339 | cancelationEvents, options); |
| 2340 | |
| 2341 | if (!cancelationEvents.isEmpty()) { |
| 2342 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2343 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | "with reality: %s, mode=%d.", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 2345 | connection->getInputChannelName().c_str(), cancelationEvents.size(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | options.reason, options.mode); |
| 2347 | #endif |
| 2348 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2349 | EventEntry* cancelationEventEntry = cancelationEvents.itemAt(i); |
| 2350 | switch (cancelationEventEntry->type) { |
| 2351 | case EventEntry::TYPE_KEY: |
| 2352 | logOutboundKeyDetailsLocked("cancel - ", |
| 2353 | static_cast<KeyEntry*>(cancelationEventEntry)); |
| 2354 | break; |
| 2355 | case EventEntry::TYPE_MOTION: |
| 2356 | logOutboundMotionDetailsLocked("cancel - ", |
| 2357 | static_cast<MotionEntry*>(cancelationEventEntry)); |
| 2358 | break; |
| 2359 | } |
| 2360 | |
| 2361 | InputTarget target; |
| 2362 | sp<InputWindowHandle> windowHandle = getWindowHandleLocked(connection->inputChannel); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2363 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2365 | target.xOffset = -windowInfo->frameLeft; |
| 2366 | target.yOffset = -windowInfo->frameTop; |
| 2367 | target.scaleFactor = windowInfo->scaleFactor; |
| 2368 | } else { |
| 2369 | target.xOffset = 0; |
| 2370 | target.yOffset = 0; |
| 2371 | target.scaleFactor = 1.0f; |
| 2372 | } |
| 2373 | target.inputChannel = connection->inputChannel; |
| 2374 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2375 | |
| 2376 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2377 | &target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2378 | |
| 2379 | cancelationEventEntry->release(); |
| 2380 | } |
| 2381 | |
| 2382 | startDispatchCycleLocked(currentTime, connection); |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | InputDispatcher::MotionEntry* |
| 2387 | InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) { |
| 2388 | ALOG_ASSERT(pointerIds.value != 0); |
| 2389 | |
| 2390 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 2391 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 2392 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 2393 | |
| 2394 | uint32_t originalPointerCount = originalMotionEntry->pointerCount; |
| 2395 | uint32_t splitPointerCount = 0; |
| 2396 | |
| 2397 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
| 2398 | originalPointerIndex++) { |
| 2399 | const PointerProperties& pointerProperties = |
| 2400 | originalMotionEntry->pointerProperties[originalPointerIndex]; |
| 2401 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 2402 | if (pointerIds.hasBit(pointerId)) { |
| 2403 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 2404 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 2405 | splitPointerCoords[splitPointerCount].copyFrom( |
| 2406 | originalMotionEntry->pointerCoords[originalPointerIndex]); |
| 2407 | splitPointerCount += 1; |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | if (splitPointerCount != pointerIds.count()) { |
| 2412 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 2413 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 2414 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 2415 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 2416 | // in this way. |
| 2417 | ALOGW("Dropping split motion event because the pointer count is %d but " |
| 2418 | "we expected there to be %d pointers. This probably means we received " |
| 2419 | "a broken sequence of pointer ids from the input device.", |
| 2420 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2421 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | int32_t action = originalMotionEntry->action; |
| 2425 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 2426 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
| 2427 | || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2428 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 2429 | const PointerProperties& pointerProperties = |
| 2430 | originalMotionEntry->pointerProperties[originalPointerIndex]; |
| 2431 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 2432 | if (pointerIds.hasBit(pointerId)) { |
| 2433 | if (pointerIds.count() == 1) { |
| 2434 | // The first/last pointer went down/up. |
| 2435 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
| 2436 | ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2437 | } else { |
| 2438 | // A secondary pointer went down/up. |
| 2439 | uint32_t splitPointerIndex = 0; |
| 2440 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 2441 | splitPointerIndex += 1; |
| 2442 | } |
| 2443 | action = maskedAction | (splitPointerIndex |
| 2444 | << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2445 | } |
| 2446 | } else { |
| 2447 | // An unrelated pointer changed. |
| 2448 | action = AMOTION_EVENT_ACTION_MOVE; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | MotionEntry* splitMotionEntry = new MotionEntry( |
| 2453 | originalMotionEntry->eventTime, |
| 2454 | originalMotionEntry->deviceId, |
| 2455 | originalMotionEntry->source, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2456 | originalMotionEntry->displayId, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2457 | originalMotionEntry->policyFlags, |
| 2458 | action, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2459 | originalMotionEntry->actionButton, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2460 | originalMotionEntry->flags, |
| 2461 | originalMotionEntry->metaState, |
| 2462 | originalMotionEntry->buttonState, |
| 2463 | originalMotionEntry->edgeFlags, |
| 2464 | originalMotionEntry->xPrecision, |
| 2465 | originalMotionEntry->yPrecision, |
| 2466 | originalMotionEntry->downTime, |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2467 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2468 | |
| 2469 | if (originalMotionEntry->injectionState) { |
| 2470 | splitMotionEntry->injectionState = originalMotionEntry->injectionState; |
| 2471 | splitMotionEntry->injectionState->refCount += 1; |
| 2472 | } |
| 2473 | |
| 2474 | return splitMotionEntry; |
| 2475 | } |
| 2476 | |
| 2477 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 2478 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2479 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2480 | #endif |
| 2481 | |
| 2482 | bool needWake; |
| 2483 | { // acquire lock |
| 2484 | AutoMutex _l(mLock); |
| 2485 | |
| 2486 | ConfigurationChangedEntry* newEntry = new ConfigurationChangedEntry(args->eventTime); |
| 2487 | needWake = enqueueInboundEventLocked(newEntry); |
| 2488 | } // release lock |
| 2489 | |
| 2490 | if (needWake) { |
| 2491 | mLooper->wake(); |
| 2492 | } |
| 2493 | } |
| 2494 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2495 | /** |
| 2496 | * If one of the meta shortcuts is detected, process them here: |
| 2497 | * Meta + Backspace -> generate BACK |
| 2498 | * Meta + Enter -> generate HOME |
| 2499 | * This will potentially overwrite keyCode and metaState. |
| 2500 | */ |
| 2501 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
| 2502 | int32_t& keyCode, int32_t& metaState) { |
| 2503 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 2504 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 2505 | if (keyCode == AKEYCODE_DEL) { |
| 2506 | newKeyCode = AKEYCODE_BACK; |
| 2507 | } else if (keyCode == AKEYCODE_ENTER) { |
| 2508 | newKeyCode = AKEYCODE_HOME; |
| 2509 | } |
| 2510 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
| 2511 | AutoMutex _l(mLock); |
| 2512 | struct KeyReplacement replacement = {keyCode, deviceId}; |
| 2513 | mReplacedKeys.add(replacement, newKeyCode); |
| 2514 | keyCode = newKeyCode; |
| 2515 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 2516 | } |
| 2517 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 2518 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 2519 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 2520 | // even if the modifier was released between the down and the up events. |
| 2521 | AutoMutex _l(mLock); |
| 2522 | struct KeyReplacement replacement = {keyCode, deviceId}; |
| 2523 | ssize_t index = mReplacedKeys.indexOfKey(replacement); |
| 2524 | if (index >= 0) { |
| 2525 | keyCode = mReplacedKeys.valueAt(index); |
| 2526 | mReplacedKeys.removeItemsAt(index); |
| 2527 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2532 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 2533 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2534 | ALOGD("notifyKey - eventTime=%" PRId64 |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2535 | ", deviceId=%d, source=0x%x, displayId=%" PRId32 "policyFlags=0x%x, action=0x%x, " |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2536 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2537 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2538 | args->action, args->flags, args->keyCode, args->scanCode, |
| 2539 | args->metaState, args->downTime); |
| 2540 | #endif |
| 2541 | if (!validateKeyEvent(args->action)) { |
| 2542 | return; |
| 2543 | } |
| 2544 | |
| 2545 | uint32_t policyFlags = args->policyFlags; |
| 2546 | int32_t flags = args->flags; |
| 2547 | int32_t metaState = args->metaState; |
| 2548 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 2549 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 2550 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2551 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2552 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 2553 | metaState |= AMETA_FUNCTION_ON; |
| 2554 | } |
| 2555 | |
| 2556 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 2557 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 2558 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2559 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 2560 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2561 | KeyEvent event; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2562 | event.initialize(args->deviceId, args->source, args->displayId, args->action, |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 2563 | flags, keyCode, args->scanCode, metaState, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2564 | args->downTime, args->eventTime); |
| 2565 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2566 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2567 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2568 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 2569 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 2570 | std::to_string(t.duration().count()).c_str()); |
| 2571 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2572 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2573 | bool needWake; |
| 2574 | { // acquire lock |
| 2575 | mLock.lock(); |
| 2576 | |
| 2577 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 2578 | mLock.unlock(); |
| 2579 | |
| 2580 | policyFlags |= POLICY_FLAG_FILTERED; |
| 2581 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 2582 | return; // event was consumed by the filter |
| 2583 | } |
| 2584 | |
| 2585 | mLock.lock(); |
| 2586 | } |
| 2587 | |
| 2588 | int32_t repeatCount = 0; |
| 2589 | KeyEntry* newEntry = new KeyEntry(args->eventTime, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2590 | args->deviceId, args->source, args->displayId, policyFlags, |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 2591 | args->action, flags, keyCode, args->scanCode, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2592 | metaState, repeatCount, args->downTime); |
| 2593 | |
| 2594 | needWake = enqueueInboundEventLocked(newEntry); |
| 2595 | mLock.unlock(); |
| 2596 | } // release lock |
| 2597 | |
| 2598 | if (needWake) { |
| 2599 | mLooper->wake(); |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 2604 | return mInputFilterEnabled; |
| 2605 | } |
| 2606 | |
| 2607 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 2608 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2609 | ALOGD("notifyMotion - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 2610 | ", policyFlags=0x%x, " |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2611 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x," |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2612 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2613 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2614 | args->action, args->actionButton, args->flags, args->metaState, args->buttonState, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2615 | args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime); |
| 2616 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 2617 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 2618 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2619 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2620 | "orientation=%f", |
| 2621 | i, args->pointerProperties[i].id, |
| 2622 | args->pointerProperties[i].toolType, |
| 2623 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2624 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2625 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2626 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2627 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2628 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2629 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2630 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2631 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2632 | } |
| 2633 | #endif |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2634 | if (!validateMotionEvent(args->action, args->actionButton, |
| 2635 | args->pointerCount, args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2636 | return; |
| 2637 | } |
| 2638 | |
| 2639 | uint32_t policyFlags = args->policyFlags; |
| 2640 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2641 | |
| 2642 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2643 | mPolicy->interceptMotionBeforeQueueing(args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2644 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 2645 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 2646 | std::to_string(t.duration().count()).c_str()); |
| 2647 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2648 | |
| 2649 | bool needWake; |
| 2650 | { // acquire lock |
| 2651 | mLock.lock(); |
| 2652 | |
| 2653 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 2654 | mLock.unlock(); |
| 2655 | |
| 2656 | MotionEvent event; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2657 | event.initialize(args->deviceId, args->source, args->displayId, |
| 2658 | args->action, args->actionButton, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2659 | args->flags, args->edgeFlags, args->metaState, args->buttonState, |
| 2660 | 0, 0, args->xPrecision, args->yPrecision, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2661 | args->downTime, args->eventTime, |
| 2662 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
| 2663 | |
| 2664 | policyFlags |= POLICY_FLAG_FILTERED; |
| 2665 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 2666 | return; // event was consumed by the filter |
| 2667 | } |
| 2668 | |
| 2669 | mLock.lock(); |
| 2670 | } |
| 2671 | |
| 2672 | // Just enqueue a new motion event. |
| 2673 | MotionEntry* newEntry = new MotionEntry(args->eventTime, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2674 | args->deviceId, args->source, args->displayId, policyFlags, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2675 | args->action, args->actionButton, args->flags, |
| 2676 | args->metaState, args->buttonState, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2677 | args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime, |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2678 | args->pointerCount, args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2679 | |
| 2680 | needWake = enqueueInboundEventLocked(newEntry); |
| 2681 | mLock.unlock(); |
| 2682 | } // release lock |
| 2683 | |
| 2684 | if (needWake) { |
| 2685 | mLooper->wake(); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
| 2690 | // TODO: support sending secondary display events to input filter |
| 2691 | return mInputFilterEnabled && isMainDisplay(args->displayId); |
| 2692 | } |
| 2693 | |
| 2694 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 2695 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2696 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 2697 | "switchMask=0x%08x", |
| 2698 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2699 | #endif |
| 2700 | |
| 2701 | uint32_t policyFlags = args->policyFlags; |
| 2702 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 2703 | mPolicy->notifySwitch(args->eventTime, |
| 2704 | args->switchValues, args->switchMask, policyFlags); |
| 2705 | } |
| 2706 | |
| 2707 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 2708 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 2709 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 | args->eventTime, args->deviceId); |
| 2711 | #endif |
| 2712 | |
| 2713 | bool needWake; |
| 2714 | { // acquire lock |
| 2715 | AutoMutex _l(mLock); |
| 2716 | |
| 2717 | DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId); |
| 2718 | needWake = enqueueInboundEventLocked(newEntry); |
| 2719 | } // release lock |
| 2720 | |
| 2721 | if (needWake) { |
| 2722 | mLooper->wake(); |
| 2723 | } |
| 2724 | } |
| 2725 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2726 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| 2728 | uint32_t policyFlags) { |
| 2729 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 2730 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2731 | "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x", |
| 2732 | event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2733 | #endif |
| 2734 | |
| 2735 | nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis); |
| 2736 | |
| 2737 | policyFlags |= POLICY_FLAG_INJECTED; |
| 2738 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 2739 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 2740 | } |
| 2741 | |
| 2742 | EventEntry* firstInjectedEntry; |
| 2743 | EventEntry* lastInjectedEntry; |
| 2744 | switch (event->getType()) { |
| 2745 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2746 | KeyEvent keyEvent; |
| 2747 | keyEvent.initialize(*static_cast<const KeyEvent*>(event)); |
| 2748 | int32_t action = keyEvent.getAction(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2749 | if (! validateKeyEvent(action)) { |
| 2750 | return INPUT_EVENT_INJECTION_FAILED; |
| 2751 | } |
| 2752 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2753 | int32_t flags = keyEvent.getFlags(); |
| 2754 | int32_t keyCode = keyEvent.getKeyCode(); |
| 2755 | int32_t metaState = keyEvent.getMetaState(); |
| 2756 | accelerateMetaShortcuts(keyEvent.getDeviceId(), action, |
| 2757 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2758 | keyEvent.initialize(keyEvent.getDeviceId(), keyEvent.getSource(), keyEvent.getDisplayId(), |
| 2759 | action, flags, keyCode, keyEvent.getScanCode(), metaState, 0, |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2760 | keyEvent.getDownTime(), keyEvent.getEventTime()); |
| 2761 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 2763 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 2764 | } |
| 2765 | |
| 2766 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2767 | android::base::Timer t; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2768 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2769 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 2770 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 2771 | std::to_string(t.duration().count()).c_str()); |
| 2772 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2773 | } |
| 2774 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2775 | mLock.lock(); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2776 | firstInjectedEntry = new KeyEntry(keyEvent.getEventTime(), |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 2777 | keyEvent.getDeviceId(), keyEvent.getSource(), keyEvent.getDisplayId(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 | policyFlags, action, flags, |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 2779 | keyEvent.getKeyCode(), keyEvent.getScanCode(), keyEvent.getMetaState(), |
| 2780 | keyEvent.getRepeatCount(), keyEvent.getDownTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2781 | lastInjectedEntry = firstInjectedEntry; |
| 2782 | break; |
| 2783 | } |
| 2784 | |
| 2785 | case AINPUT_EVENT_TYPE_MOTION: { |
| 2786 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2787 | int32_t action = motionEvent->getAction(); |
| 2788 | size_t pointerCount = motionEvent->getPointerCount(); |
| 2789 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2790 | int32_t actionButton = motionEvent->getActionButton(); |
| 2791 | if (! validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | return INPUT_EVENT_INJECTION_FAILED; |
| 2793 | } |
| 2794 | |
| 2795 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 2796 | nsecs_t eventTime = motionEvent->getEventTime(); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2797 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2798 | mPolicy->interceptMotionBeforeQueueing(eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 2799 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 2800 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 2801 | std::to_string(t.duration().count()).c_str()); |
| 2802 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | mLock.lock(); |
| 2806 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 2807 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 2808 | firstInjectedEntry = new MotionEntry(*sampleEventTimes, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2809 | motionEvent->getDeviceId(), motionEvent->getSource(), motionEvent->getDisplayId(), |
| 2810 | policyFlags, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2811 | action, actionButton, motionEvent->getFlags(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2812 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 2813 | motionEvent->getEdgeFlags(), |
| 2814 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2815 | motionEvent->getDownTime(), |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2816 | uint32_t(pointerCount), pointerProperties, samplePointerCoords, |
| 2817 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2818 | lastInjectedEntry = firstInjectedEntry; |
| 2819 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 2820 | sampleEventTimes += 1; |
| 2821 | samplePointerCoords += pointerCount; |
| 2822 | MotionEntry* nextInjectedEntry = new MotionEntry(*sampleEventTimes, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2823 | motionEvent->getDeviceId(), motionEvent->getSource(), |
| 2824 | motionEvent->getDisplayId(), policyFlags, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 2825 | action, actionButton, motionEvent->getFlags(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 2827 | motionEvent->getEdgeFlags(), |
| 2828 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 2829 | motionEvent->getDownTime(), |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2830 | uint32_t(pointerCount), pointerProperties, samplePointerCoords, |
| 2831 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2832 | lastInjectedEntry->next = nextInjectedEntry; |
| 2833 | lastInjectedEntry = nextInjectedEntry; |
| 2834 | } |
| 2835 | break; |
| 2836 | } |
| 2837 | |
| 2838 | default: |
| 2839 | ALOGW("Cannot inject event of type %d", event->getType()); |
| 2840 | return INPUT_EVENT_INJECTION_FAILED; |
| 2841 | } |
| 2842 | |
| 2843 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 2844 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 2845 | injectionState->injectionIsAsync = true; |
| 2846 | } |
| 2847 | |
| 2848 | injectionState->refCount += 1; |
| 2849 | lastInjectedEntry->injectionState = injectionState; |
| 2850 | |
| 2851 | bool needWake = false; |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2852 | for (EventEntry* entry = firstInjectedEntry; entry != nullptr; ) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2853 | EventEntry* nextEntry = entry->next; |
| 2854 | needWake |= enqueueInboundEventLocked(entry); |
| 2855 | entry = nextEntry; |
| 2856 | } |
| 2857 | |
| 2858 | mLock.unlock(); |
| 2859 | |
| 2860 | if (needWake) { |
| 2861 | mLooper->wake(); |
| 2862 | } |
| 2863 | |
| 2864 | int32_t injectionResult; |
| 2865 | { // acquire lock |
| 2866 | AutoMutex _l(mLock); |
| 2867 | |
| 2868 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 2869 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 2870 | } else { |
| 2871 | for (;;) { |
| 2872 | injectionResult = injectionState->injectionResult; |
| 2873 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 2874 | break; |
| 2875 | } |
| 2876 | |
| 2877 | nsecs_t remainingTimeout = endTime - now(); |
| 2878 | if (remainingTimeout <= 0) { |
| 2879 | #if DEBUG_INJECTION |
| 2880 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 2881 | "to become available."); |
| 2882 | #endif |
| 2883 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 2884 | break; |
| 2885 | } |
| 2886 | |
| 2887 | mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout); |
| 2888 | } |
| 2889 | |
| 2890 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED |
| 2891 | && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
| 2892 | while (injectionState->pendingForegroundDispatches != 0) { |
| 2893 | #if DEBUG_INJECTION |
| 2894 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 2895 | injectionState->pendingForegroundDispatches); |
| 2896 | #endif |
| 2897 | nsecs_t remainingTimeout = endTime - now(); |
| 2898 | if (remainingTimeout <= 0) { |
| 2899 | #if DEBUG_INJECTION |
| 2900 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 2901 | "dispatches to finish."); |
| 2902 | #endif |
| 2903 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 2904 | break; |
| 2905 | } |
| 2906 | |
| 2907 | mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout); |
| 2908 | } |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | injectionState->release(); |
| 2913 | } // release lock |
| 2914 | |
| 2915 | #if DEBUG_INJECTION |
| 2916 | ALOGD("injectInputEvent - Finished with result %d. " |
| 2917 | "injectorPid=%d, injectorUid=%d", |
| 2918 | injectionResult, injectorPid, injectorUid); |
| 2919 | #endif |
| 2920 | |
| 2921 | return injectionResult; |
| 2922 | } |
| 2923 | |
| 2924 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
| 2925 | return injectorUid == 0 |
| 2926 | || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
| 2927 | } |
| 2928 | |
| 2929 | void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) { |
| 2930 | InjectionState* injectionState = entry->injectionState; |
| 2931 | if (injectionState) { |
| 2932 | #if DEBUG_INJECTION |
| 2933 | ALOGD("Setting input event injection result to %d. " |
| 2934 | "injectorPid=%d, injectorUid=%d", |
| 2935 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 2936 | #endif |
| 2937 | |
| 2938 | if (injectionState->injectionIsAsync |
| 2939 | && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
| 2940 | // Log the outcome since the injector did not wait for the injection result. |
| 2941 | switch (injectionResult) { |
| 2942 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 2943 | ALOGV("Asynchronous input event injection succeeded."); |
| 2944 | break; |
| 2945 | case INPUT_EVENT_INJECTION_FAILED: |
| 2946 | ALOGW("Asynchronous input event injection failed."); |
| 2947 | break; |
| 2948 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 2949 | ALOGW("Asynchronous input event injection permission denied."); |
| 2950 | break; |
| 2951 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 2952 | ALOGW("Asynchronous input event injection timed out."); |
| 2953 | break; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | injectionState->injectionResult = injectionResult; |
| 2958 | mInjectionResultAvailableCondition.broadcast(); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) { |
| 2963 | InjectionState* injectionState = entry->injectionState; |
| 2964 | if (injectionState) { |
| 2965 | injectionState->pendingForegroundDispatches += 1; |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) { |
| 2970 | InjectionState* injectionState = entry->injectionState; |
| 2971 | if (injectionState) { |
| 2972 | injectionState->pendingForegroundDispatches -= 1; |
| 2973 | |
| 2974 | if (injectionState->pendingForegroundDispatches == 0) { |
| 2975 | mInjectionSyncFinishedCondition.broadcast(); |
| 2976 | } |
| 2977 | } |
| 2978 | } |
| 2979 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2980 | Vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked(int32_t displayId) const { |
| 2981 | std::unordered_map<int32_t, Vector<sp<InputWindowHandle>>>::const_iterator it = |
| 2982 | mWindowHandlesByDisplay.find(displayId); |
| 2983 | if(it != mWindowHandlesByDisplay.end()) { |
| 2984 | return it->second; |
| 2985 | } |
| 2986 | |
| 2987 | // Return an empty one if nothing found. |
| 2988 | return Vector<sp<InputWindowHandle>>(); |
| 2989 | } |
| 2990 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2991 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
| 2992 | const sp<InputChannel>& inputChannel) const { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2993 | for (auto& it : mWindowHandlesByDisplay) { |
| 2994 | const Vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 2995 | size_t numWindows = windowHandles.size(); |
| 2996 | for (size_t i = 0; i < numWindows; i++) { |
| 2997 | const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i); |
| 2998 | if (windowHandle->getInputChannel() == inputChannel) { |
| 2999 | return windowHandle; |
| 3000 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3001 | } |
| 3002 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3003 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | bool InputDispatcher::hasWindowHandleLocked( |
Siarhei Vishniakou | 9224fba | 2018-08-13 18:55:08 +0000 | [diff] [blame] | 3007 | const sp<InputWindowHandle>& windowHandle) const { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3008 | for (auto& it : mWindowHandlesByDisplay) { |
| 3009 | const Vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3010 | size_t numWindows = windowHandles.size(); |
| 3011 | for (size_t i = 0; i < numWindows; i++) { |
| 3012 | if (windowHandles.itemAt(i) == windowHandle) { |
| 3013 | if (windowHandle->getInfo()->displayId != it.first) { |
| 3014 | ALOGE("Found window %s in display %d, but it should belong to display %d", |
| 3015 | windowHandle->getName().c_str(), it.first, |
| 3016 | windowHandle->getInfo()->displayId); |
| 3017 | } |
| 3018 | return true; |
| 3019 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | return false; |
| 3023 | } |
| 3024 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3025 | /** |
| 3026 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3027 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3028 | * If set an empty list, remove all handles from the specific display. |
| 3029 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3030 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3031 | */ |
| 3032 | void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle>>& inputWindowHandles, |
| 3033 | int32_t displayId) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3034 | #if DEBUG_FOCUS |
| 3035 | ALOGD("setInputWindows"); |
| 3036 | #endif |
| 3037 | { // acquire lock |
| 3038 | AutoMutex _l(mLock); |
| 3039 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3040 | // Copy old handles for release if they are no longer present. |
| 3041 | const Vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3043 | sp<InputWindowHandle> newFocusedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3044 | bool foundHoveredWindow = false; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3045 | |
| 3046 | if (inputWindowHandles.isEmpty()) { |
| 3047 | // Remove all handles on a display if there are no windows left. |
| 3048 | mWindowHandlesByDisplay.erase(displayId); |
| 3049 | } else { |
| 3050 | size_t numWindows = inputWindowHandles.size(); |
| 3051 | for (size_t i = 0; i < numWindows; i++) { |
| 3052 | const sp<InputWindowHandle>& windowHandle = inputWindowHandles.itemAt(i); |
| 3053 | if (!windowHandle->updateInfo() || windowHandle->getInputChannel() == nullptr) { |
| 3054 | continue; |
| 3055 | } |
| 3056 | |
| 3057 | if (windowHandle->getInfo()->displayId != displayId) { |
| 3058 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3059 | windowHandle->getName().c_str(), displayId, |
| 3060 | windowHandle->getInfo()->displayId); |
| 3061 | continue; |
| 3062 | } |
| 3063 | |
| 3064 | if (windowHandle->getInfo()->hasFocus) { |
| 3065 | newFocusedWindowHandle = windowHandle; |
| 3066 | } |
| 3067 | if (windowHandle == mLastHoverWindowHandle) { |
| 3068 | foundHoveredWindow = true; |
| 3069 | } |
Siarhei Vishniakou | 9224fba | 2018-08-13 18:55:08 +0000 | [diff] [blame] | 3070 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3071 | |
| 3072 | // Insert or replace |
| 3073 | mWindowHandlesByDisplay[displayId] = inputWindowHandles; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | if (!foundHoveredWindow) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3077 | mLastHoverWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3080 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3081 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 3082 | |
| 3083 | if (oldFocusedWindowHandle != newFocusedWindowHandle) { |
| 3084 | if (oldFocusedWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3085 | #if DEBUG_FOCUS |
| 3086 | ALOGD("Focus left window: %s", |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3087 | oldFocusedWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3088 | #endif |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3089 | sp<InputChannel> focusedInputChannel = oldFocusedWindowHandle->getInputChannel(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3090 | if (focusedInputChannel != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3091 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3092 | "focus left window"); |
| 3093 | synthesizeCancelationEventsForInputChannelLocked( |
| 3094 | focusedInputChannel, options); |
| 3095 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3096 | mFocusedWindowHandlesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3097 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3098 | if (newFocusedWindowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 | #if DEBUG_FOCUS |
| 3100 | ALOGD("Focus entered window: %s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3101 | newFocusedWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3102 | #endif |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3103 | mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3104 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3105 | } |
| 3106 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3107 | ssize_t stateIndex = mTouchStatesByDisplay.indexOfKey(displayId); |
| 3108 | if (stateIndex >= 0) { |
| 3109 | TouchState& state = mTouchStatesByDisplay.editValueAt(stateIndex); |
Ivan Lozano | 96f1299 | 2017-11-09 14:45:38 -0800 | [diff] [blame] | 3110 | for (size_t i = 0; i < state.windows.size(); ) { |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3111 | TouchedWindow& touchedWindow = state.windows.editItemAt(i); |
Siarhei Vishniakou | 9224fba | 2018-08-13 18:55:08 +0000 | [diff] [blame] | 3112 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3113 | #if DEBUG_FOCUS |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3114 | ALOGD("Touched window was removed: %s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3115 | touchedWindow.windowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3116 | #endif |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3117 | sp<InputChannel> touchedInputChannel = |
| 3118 | touchedWindow.windowHandle->getInputChannel(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3119 | if (touchedInputChannel != nullptr) { |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3120 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3121 | "touched window was removed"); |
| 3122 | synthesizeCancelationEventsForInputChannelLocked( |
| 3123 | touchedInputChannel, options); |
| 3124 | } |
Ivan Lozano | 96f1299 | 2017-11-09 14:45:38 -0800 | [diff] [blame] | 3125 | state.windows.removeAt(i); |
| 3126 | } else { |
| 3127 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3128 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | // Release information for windows that are no longer present. |
| 3133 | // This ensures that unused input channels are released promptly. |
| 3134 | // Otherwise, they might stick around until the window handle is destroyed |
| 3135 | // which might not happen until the next GC. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3136 | size_t numWindows = oldWindowHandles.size(); |
| 3137 | for (size_t i = 0; i < numWindows; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3138 | const sp<InputWindowHandle>& oldWindowHandle = oldWindowHandles.itemAt(i); |
Siarhei Vishniakou | 9224fba | 2018-08-13 18:55:08 +0000 | [diff] [blame] | 3139 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3140 | #if DEBUG_FOCUS |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3141 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3142 | #endif |
| 3143 | oldWindowHandle->releaseInfo(); |
| 3144 | } |
| 3145 | } |
| 3146 | } // release lock |
| 3147 | |
| 3148 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3149 | mLooper->wake(); |
| 3150 | } |
| 3151 | |
| 3152 | void InputDispatcher::setFocusedApplication( |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3153 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3154 | #if DEBUG_FOCUS |
| 3155 | ALOGD("setFocusedApplication"); |
| 3156 | #endif |
| 3157 | { // acquire lock |
| 3158 | AutoMutex _l(mLock); |
| 3159 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3160 | sp<InputApplicationHandle> oldFocusedApplicationHandle = |
| 3161 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3162 | if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3163 | if (oldFocusedApplicationHandle != inputApplicationHandle) { |
| 3164 | if (oldFocusedApplicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3165 | resetANRTimeoutsLocked(); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3166 | oldFocusedApplicationHandle->releaseInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3167 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3168 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3169 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3170 | } else if (oldFocusedApplicationHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | resetANRTimeoutsLocked(); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3172 | oldFocusedApplicationHandle->releaseInfo(); |
| 3173 | oldFocusedApplicationHandle.clear(); |
| 3174 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | #if DEBUG_FOCUS |
| 3178 | //logDispatchStateLocked(); |
| 3179 | #endif |
| 3180 | } // release lock |
| 3181 | |
| 3182 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3183 | mLooper->wake(); |
| 3184 | } |
| 3185 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3186 | /** |
| 3187 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3188 | * the display not specified. |
| 3189 | * |
| 3190 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3191 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3192 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3193 | * display. The display-specified events won't be affected. |
| 3194 | */ |
| 3195 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
| 3196 | #if DEBUG_FOCUS |
| 3197 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3198 | #endif |
| 3199 | { // acquire lock |
| 3200 | AutoMutex _l(mLock); |
| 3201 | |
| 3202 | if (mFocusedDisplayId != displayId) { |
| 3203 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3204 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
| 3205 | if (oldFocusedWindowHandle != nullptr) { |
| 3206 | sp<InputChannel> inputChannel = oldFocusedWindowHandle->getInputChannel(); |
| 3207 | if (inputChannel != nullptr) { |
| 3208 | CancelationOptions options( |
| 3209 | CancelationOptions::CANCEL_DISPLAY_UNSPECIFIED_EVENTS, |
| 3210 | "The display which contains this window no longer has focus."); |
| 3211 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3212 | } |
| 3213 | } |
| 3214 | mFocusedDisplayId = displayId; |
| 3215 | |
| 3216 | // Sanity check |
| 3217 | sp<InputWindowHandle> newFocusedWindowHandle = |
| 3218 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 3219 | if (newFocusedWindowHandle == nullptr) { |
| 3220 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
| 3221 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 3222 | ALOGE("But another display has a focused window:"); |
| 3223 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 3224 | const int32_t displayId = it.first; |
| 3225 | const sp<InputWindowHandle>& windowHandle = it.second; |
| 3226 | ALOGE("Display #%" PRId32 " has focused window: '%s'\n", |
| 3227 | displayId, windowHandle->getName().c_str()); |
| 3228 | } |
| 3229 | } |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | #if DEBUG_FOCUS |
| 3234 | logDispatchStateLocked(); |
| 3235 | #endif |
| 3236 | } // release lock |
| 3237 | |
| 3238 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3239 | mLooper->wake(); |
| 3240 | } |
| 3241 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3242 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
| 3243 | #if DEBUG_FOCUS |
| 3244 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3245 | #endif |
| 3246 | |
| 3247 | bool changed; |
| 3248 | { // acquire lock |
| 3249 | AutoMutex _l(mLock); |
| 3250 | |
| 3251 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 3252 | if (mDispatchFrozen && !frozen) { |
| 3253 | resetANRTimeoutsLocked(); |
| 3254 | } |
| 3255 | |
| 3256 | if (mDispatchEnabled && !enabled) { |
| 3257 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3258 | } |
| 3259 | |
| 3260 | mDispatchEnabled = enabled; |
| 3261 | mDispatchFrozen = frozen; |
| 3262 | changed = true; |
| 3263 | } else { |
| 3264 | changed = false; |
| 3265 | } |
| 3266 | |
| 3267 | #if DEBUG_FOCUS |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3268 | logDispatchStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3269 | #endif |
| 3270 | } // release lock |
| 3271 | |
| 3272 | if (changed) { |
| 3273 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3274 | mLooper->wake(); |
| 3275 | } |
| 3276 | } |
| 3277 | |
| 3278 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
| 3279 | #if DEBUG_FOCUS |
| 3280 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 3281 | #endif |
| 3282 | |
| 3283 | { // acquire lock |
| 3284 | AutoMutex _l(mLock); |
| 3285 | |
| 3286 | if (mInputFilterEnabled == enabled) { |
| 3287 | return; |
| 3288 | } |
| 3289 | |
| 3290 | mInputFilterEnabled = enabled; |
| 3291 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 3292 | } // release lock |
| 3293 | |
| 3294 | // Wake up poll loop since there might be work to do to drop everything. |
| 3295 | mLooper->wake(); |
| 3296 | } |
| 3297 | |
| 3298 | bool InputDispatcher::transferTouchFocus(const sp<InputChannel>& fromChannel, |
| 3299 | const sp<InputChannel>& toChannel) { |
| 3300 | #if DEBUG_FOCUS |
| 3301 | ALOGD("transferTouchFocus: fromChannel=%s, toChannel=%s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3302 | fromChannel->getName().c_str(), toChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3303 | #endif |
| 3304 | { // acquire lock |
| 3305 | AutoMutex _l(mLock); |
| 3306 | |
| 3307 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromChannel); |
| 3308 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toChannel); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3309 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3310 | #if DEBUG_FOCUS |
| 3311 | ALOGD("Cannot transfer focus because from or to window not found."); |
| 3312 | #endif |
| 3313 | return false; |
| 3314 | } |
| 3315 | if (fromWindowHandle == toWindowHandle) { |
| 3316 | #if DEBUG_FOCUS |
| 3317 | ALOGD("Trivial transfer to same window."); |
| 3318 | #endif |
| 3319 | return true; |
| 3320 | } |
| 3321 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
| 3322 | #if DEBUG_FOCUS |
| 3323 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 3324 | #endif |
| 3325 | return false; |
| 3326 | } |
| 3327 | |
| 3328 | bool found = false; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3329 | for (size_t d = 0; d < mTouchStatesByDisplay.size(); d++) { |
| 3330 | TouchState& state = mTouchStatesByDisplay.editValueAt(d); |
| 3331 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 3332 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 3333 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 3334 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 3335 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3336 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3337 | state.windows.removeAt(i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3338 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3339 | int32_t newTargetFlags = oldTargetFlags |
| 3340 | & (InputTarget::FLAG_FOREGROUND |
| 3341 | | InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 3342 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3344 | found = true; |
| 3345 | goto Found; |
| 3346 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | } |
| 3348 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3349 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3350 | |
| 3351 | if (! found) { |
| 3352 | #if DEBUG_FOCUS |
| 3353 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 3354 | #endif |
| 3355 | return false; |
| 3356 | } |
| 3357 | |
| 3358 | ssize_t fromConnectionIndex = getConnectionIndexLocked(fromChannel); |
| 3359 | ssize_t toConnectionIndex = getConnectionIndexLocked(toChannel); |
| 3360 | if (fromConnectionIndex >= 0 && toConnectionIndex >= 0) { |
| 3361 | sp<Connection> fromConnection = mConnectionsByFd.valueAt(fromConnectionIndex); |
| 3362 | sp<Connection> toConnection = mConnectionsByFd.valueAt(toConnectionIndex); |
| 3363 | |
| 3364 | fromConnection->inputState.copyPointerStateTo(toConnection->inputState); |
| 3365 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3366 | "transferring touch focus from this window to another window"); |
| 3367 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
| 3368 | } |
| 3369 | |
| 3370 | #if DEBUG_FOCUS |
| 3371 | logDispatchStateLocked(); |
| 3372 | #endif |
| 3373 | } // release lock |
| 3374 | |
| 3375 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3376 | mLooper->wake(); |
| 3377 | return true; |
| 3378 | } |
| 3379 | |
| 3380 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
| 3381 | #if DEBUG_FOCUS |
| 3382 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 3383 | #endif |
| 3384 | |
| 3385 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 3386 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 3387 | |
| 3388 | resetKeyRepeatLocked(); |
| 3389 | releasePendingEventLocked(); |
| 3390 | drainInboundQueueLocked(); |
| 3391 | resetANRTimeoutsLocked(); |
| 3392 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3393 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3394 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3395 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3399 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3400 | dumpDispatchStateLocked(dump); |
| 3401 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3402 | std::istringstream stream(dump); |
| 3403 | std::string line; |
| 3404 | |
| 3405 | while (std::getline(stream, line, '\n')) { |
| 3406 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3407 | } |
| 3408 | } |
| 3409 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3410 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
| 3411 | dump += StringPrintf(INDENT "DispatchEnabled: %d\n", mDispatchEnabled); |
| 3412 | dump += StringPrintf(INDENT "DispatchFrozen: %d\n", mDispatchFrozen); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3413 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3414 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3415 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 3416 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 3417 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 3418 | const int32_t displayId = it.first; |
| 3419 | const sp<InputApplicationHandle>& applicationHandle = it.second; |
| 3420 | dump += StringPrintf( |
| 3421 | INDENT2 "displayId=%" PRId32 ", name='%s', dispatchingTimeout=%0.3fms\n", |
| 3422 | displayId, |
| 3423 | applicationHandle->getName().c_str(), |
| 3424 | applicationHandle->getDispatchingTimeout( |
| 3425 | DEFAULT_INPUT_DISPATCHING_TIMEOUT) / 1000000.0); |
| 3426 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3427 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3428 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3429 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3430 | |
| 3431 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 3432 | dump += StringPrintf(INDENT "FocusedWindows:\n"); |
| 3433 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 3434 | const int32_t displayId = it.first; |
| 3435 | const sp<InputWindowHandle>& windowHandle = it.second; |
| 3436 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", |
| 3437 | displayId, windowHandle->getName().c_str()); |
| 3438 | } |
| 3439 | } else { |
| 3440 | dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); |
| 3441 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3442 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3443 | if (!mTouchStatesByDisplay.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3444 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3445 | for (size_t i = 0; i < mTouchStatesByDisplay.size(); i++) { |
| 3446 | const TouchState& state = mTouchStatesByDisplay.valueAt(i); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3447 | dump += StringPrintf(INDENT2 "%d: down=%s, split=%s, deviceId=%d, source=0x%08x\n", |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3448 | state.displayId, toString(state.down), toString(state.split), |
| 3449 | state.deviceId, state.source); |
| 3450 | if (!state.windows.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3451 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3452 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 3453 | const TouchedWindow& touchedWindow = state.windows[i]; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3454 | dump += StringPrintf(INDENT4 "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 3455 | i, touchedWindow.windowHandle->getName().c_str(), |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3456 | touchedWindow.pointerIds.value, |
| 3457 | touchedWindow.targetFlags); |
| 3458 | } |
| 3459 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3460 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3461 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | } |
| 3463 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3464 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3465 | } |
| 3466 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3467 | if (!mWindowHandlesByDisplay.empty()) { |
| 3468 | for (auto& it : mWindowHandlesByDisplay) { |
| 3469 | const Vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3470 | dump += StringPrintf(INDENT "Display: %d\n", it.first); |
| 3471 | if (!windowHandles.isEmpty()) { |
| 3472 | dump += INDENT2 "Windows:\n"; |
| 3473 | for (size_t i = 0; i < windowHandles.size(); i++) { |
| 3474 | const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i); |
| 3475 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3476 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3477 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
| 3478 | "paused=%s, hasFocus=%s, hasWallpaper=%s, " |
| 3479 | "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, " |
| 3480 | "frame=[%d,%d][%d,%d], scale=%f, " |
| 3481 | "touchableRegion=", |
| 3482 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 3483 | toString(windowInfo->paused), |
| 3484 | toString(windowInfo->hasFocus), |
| 3485 | toString(windowInfo->hasWallpaper), |
| 3486 | toString(windowInfo->visible), |
| 3487 | toString(windowInfo->canReceiveKeys), |
| 3488 | windowInfo->layoutParamsFlags, windowInfo->layoutParamsType, |
| 3489 | windowInfo->layer, |
| 3490 | windowInfo->frameLeft, windowInfo->frameTop, |
| 3491 | windowInfo->frameRight, windowInfo->frameBottom, |
| 3492 | windowInfo->scaleFactor); |
| 3493 | dumpRegion(dump, windowInfo->touchableRegion); |
| 3494 | dump += StringPrintf(", inputFeatures=0x%08x", windowInfo->inputFeatures); |
| 3495 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n", |
| 3496 | windowInfo->ownerPid, windowInfo->ownerUid, |
| 3497 | windowInfo->dispatchingTimeout / 1000000.0); |
| 3498 | } |
| 3499 | } else { |
| 3500 | dump += INDENT2 "Windows: <none>\n"; |
| 3501 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } |
| 3503 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3504 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | } |
| 3506 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3507 | if (!mMonitoringChannelsByDisplay.empty()) { |
| 3508 | for (auto& it : mMonitoringChannelsByDisplay) { |
| 3509 | const Vector<sp<InputChannel>>& monitoringChannels = it.second; |
| 3510 | dump += INDENT "MonitoringChannels in Display %d:\n"; |
| 3511 | const size_t numChannels = monitoringChannels.size(); |
| 3512 | for (size_t i = 0; i < numChannels; i++) { |
| 3513 | const sp<InputChannel>& channel = monitoringChannels[i]; |
| 3514 | dump += StringPrintf(INDENT2 "%zu: '%s'\n", i, channel->getName().c_str()); |
| 3515 | } |
| 3516 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3517 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3518 | dump += INDENT "MonitoringChannels: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | nsecs_t currentTime = now(); |
| 3522 | |
| 3523 | // Dump recently dispatched or dropped events from oldest to newest. |
| 3524 | if (!mRecentQueue.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3525 | dump += StringPrintf(INDENT "RecentQueue: length=%u\n", mRecentQueue.count()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | for (EventEntry* entry = mRecentQueue.head; entry; entry = entry->next) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3527 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | entry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3529 | dump += StringPrintf(", age=%0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | (currentTime - entry->eventTime) * 0.000001f); |
| 3531 | } |
| 3532 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3533 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | // Dump event currently being dispatched. |
| 3537 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3538 | dump += INDENT "PendingEvent:\n"; |
| 3539 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3541 | dump += StringPrintf(", age=%0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | (currentTime - mPendingEvent->eventTime) * 0.000001f); |
| 3543 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3544 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | // Dump inbound events from oldest to newest. |
| 3548 | if (!mInboundQueue.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3549 | dump += StringPrintf(INDENT "InboundQueue: length=%u\n", mInboundQueue.count()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3550 | for (EventEntry* entry = mInboundQueue.head; entry; entry = entry->next) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3551 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3552 | entry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3553 | dump += StringPrintf(", age=%0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 | (currentTime - entry->eventTime) * 0.000001f); |
| 3555 | } |
| 3556 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3557 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3558 | } |
| 3559 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3560 | if (!mReplacedKeys.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3561 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3562 | for (size_t i = 0; i < mReplacedKeys.size(); i++) { |
| 3563 | const KeyReplacement& replacement = mReplacedKeys.keyAt(i); |
| 3564 | int32_t newKeyCode = mReplacedKeys.valueAt(i); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3565 | dump += StringPrintf(INDENT2 "%zu: originalKeyCode=%d, deviceId=%d, newKeyCode=%d\n", |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3566 | i, replacement.keyCode, replacement.deviceId, newKeyCode); |
| 3567 | } |
| 3568 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3569 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3570 | } |
| 3571 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3572 | if (!mConnectionsByFd.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3573 | dump += INDENT "Connections:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3574 | for (size_t i = 0; i < mConnectionsByFd.size(); i++) { |
| 3575 | const sp<Connection>& connection = mConnectionsByFd.valueAt(i); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3576 | dump += StringPrintf(INDENT2 "%zu: channelName='%s', windowName='%s', " |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3577 | "status=%s, monitor=%s, inputPublisherBlocked=%s\n", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 3578 | i, connection->getInputChannelName().c_str(), |
| 3579 | connection->getWindowName().c_str(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3580 | connection->getStatusLabel(), toString(connection->monitor), |
| 3581 | toString(connection->inputPublisherBlocked)); |
| 3582 | |
| 3583 | if (!connection->outboundQueue.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3584 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%u\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3585 | connection->outboundQueue.count()); |
| 3586 | for (DispatchEntry* entry = connection->outboundQueue.head; entry; |
| 3587 | entry = entry->next) { |
| 3588 | dump.append(INDENT4); |
| 3589 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3590 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | entry->targetFlags, entry->resolvedAction, |
| 3592 | (currentTime - entry->eventEntry->eventTime) * 0.000001f); |
| 3593 | } |
| 3594 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3595 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | if (!connection->waitQueue.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3599 | dump += StringPrintf(INDENT3 "WaitQueue: length=%u\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3600 | connection->waitQueue.count()); |
| 3601 | for (DispatchEntry* entry = connection->waitQueue.head; entry; |
| 3602 | entry = entry->next) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3603 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3605 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | "age=%0.1fms, wait=%0.1fms\n", |
| 3607 | entry->targetFlags, entry->resolvedAction, |
| 3608 | (currentTime - entry->eventEntry->eventTime) * 0.000001f, |
| 3609 | (currentTime - entry->deliveryTime) * 0.000001f); |
| 3610 | } |
| 3611 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3612 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3616 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3620 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3621 | (mAppSwitchDueTime - now()) / 1000000.0); |
| 3622 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3623 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 | } |
| 3625 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3626 | dump += INDENT "Configuration:\n"; |
| 3627 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3628 | mConfig.keyRepeatDelay * 0.000001f); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3629 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %0.1fms\n", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3630 | mConfig.keyRepeatTimeout * 0.000001f); |
| 3631 | } |
| 3632 | |
| 3633 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel, |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3634 | const sp<InputWindowHandle>& inputWindowHandle, int32_t displayId) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | #if DEBUG_REGISTRATION |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3636 | ALOGD("channel '%s' ~ registerInputChannel - displayId=%" PRId32, |
| 3637 | inputChannel->getName().c_str(), displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | #endif |
| 3639 | |
| 3640 | { // acquire lock |
| 3641 | AutoMutex _l(mLock); |
| 3642 | |
| 3643 | if (getConnectionIndexLocked(inputChannel) >= 0) { |
| 3644 | ALOGW("Attempted to register already registered input channel '%s'", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3645 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | return BAD_VALUE; |
| 3647 | } |
| 3648 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3649 | // If InputWindowHandle is null and displayId is not ADISPLAY_ID_NONE, |
| 3650 | // treat inputChannel as monitor channel for displayId. |
| 3651 | bool monitor = inputWindowHandle == nullptr && displayId != ADISPLAY_ID_NONE; |
| 3652 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3653 | sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor); |
| 3654 | |
| 3655 | int fd = inputChannel->getFd(); |
| 3656 | mConnectionsByFd.add(fd, connection); |
| 3657 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3658 | // Store monitor channel by displayId. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3659 | if (monitor) { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3660 | Vector<sp<InputChannel>>& monitoringChannels = |
| 3661 | mMonitoringChannelsByDisplay[displayId]; |
| 3662 | monitoringChannels.push(inputChannel); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 3666 | } // release lock |
| 3667 | |
| 3668 | // Wake the looper because some connections have changed. |
| 3669 | mLooper->wake(); |
| 3670 | return OK; |
| 3671 | } |
| 3672 | |
| 3673 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
| 3674 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3675 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3676 | #endif |
| 3677 | |
| 3678 | { // acquire lock |
| 3679 | AutoMutex _l(mLock); |
| 3680 | |
| 3681 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
| 3682 | if (status) { |
| 3683 | return status; |
| 3684 | } |
| 3685 | } // release lock |
| 3686 | |
| 3687 | // Wake the poll loop because removing the connection may have changed the current |
| 3688 | // synchronization state. |
| 3689 | mLooper->wake(); |
| 3690 | return OK; |
| 3691 | } |
| 3692 | |
| 3693 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, |
| 3694 | bool notify) { |
| 3695 | ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
| 3696 | if (connectionIndex < 0) { |
| 3697 | ALOGW("Attempted to unregister already unregistered input channel '%s'", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3698 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 | return BAD_VALUE; |
| 3700 | } |
| 3701 | |
| 3702 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| 3703 | mConnectionsByFd.removeItemsAt(connectionIndex); |
| 3704 | |
| 3705 | if (connection->monitor) { |
| 3706 | removeMonitorChannelLocked(inputChannel); |
| 3707 | } |
| 3708 | |
| 3709 | mLooper->removeFd(inputChannel->getFd()); |
| 3710 | |
| 3711 | nsecs_t currentTime = now(); |
| 3712 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 3713 | |
| 3714 | connection->status = Connection::STATUS_ZOMBIE; |
| 3715 | return OK; |
| 3716 | } |
| 3717 | |
| 3718 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame^] | 3719 | for (auto it = mMonitoringChannelsByDisplay.begin(); |
| 3720 | it != mMonitoringChannelsByDisplay.end(); ) { |
| 3721 | Vector<sp<InputChannel>>& monitoringChannels = it->second; |
| 3722 | const size_t numChannels = monitoringChannels.size(); |
| 3723 | for (size_t i = 0; i < numChannels; i++) { |
| 3724 | if (monitoringChannels[i] == inputChannel) { |
| 3725 | monitoringChannels.removeAt(i); |
| 3726 | break; |
| 3727 | } |
| 3728 | } |
| 3729 | if (monitoringChannels.empty()) { |
| 3730 | it = mMonitoringChannelsByDisplay.erase(it); |
| 3731 | } else { |
| 3732 | ++it; |
| 3733 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) { |
| 3738 | ssize_t connectionIndex = mConnectionsByFd.indexOfKey(inputChannel->getFd()); |
| 3739 | if (connectionIndex >= 0) { |
| 3740 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
| 3741 | if (connection->inputChannel.get() == inputChannel.get()) { |
| 3742 | return connectionIndex; |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | return -1; |
| 3747 | } |
| 3748 | |
| 3749 | void InputDispatcher::onDispatchCycleFinishedLocked( |
| 3750 | nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled) { |
| 3751 | CommandEntry* commandEntry = postCommandLocked( |
| 3752 | & InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
| 3753 | commandEntry->connection = connection; |
| 3754 | commandEntry->eventTime = currentTime; |
| 3755 | commandEntry->seq = seq; |
| 3756 | commandEntry->handled = handled; |
| 3757 | } |
| 3758 | |
| 3759 | void InputDispatcher::onDispatchCycleBrokenLocked( |
| 3760 | nsecs_t currentTime, const sp<Connection>& connection) { |
| 3761 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 3762 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | |
| 3764 | CommandEntry* commandEntry = postCommandLocked( |
| 3765 | & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
| 3766 | commandEntry->connection = connection; |
| 3767 | } |
| 3768 | |
| 3769 | void InputDispatcher::onANRLocked( |
| 3770 | nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle, |
| 3771 | const sp<InputWindowHandle>& windowHandle, |
| 3772 | nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) { |
| 3773 | float dispatchLatency = (currentTime - eventTime) * 0.000001f; |
| 3774 | float waitDuration = (currentTime - waitStartTime) * 0.000001f; |
| 3775 | ALOGI("Application is not responding: %s. " |
| 3776 | "It has been %0.1fms since event, %0.1fms since wait started. Reason: %s", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3777 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).c_str(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3778 | dispatchLatency, waitDuration, reason); |
| 3779 | |
| 3780 | // Capture a record of the InputDispatcher state at the time of the ANR. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3781 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3782 | struct tm tm; |
| 3783 | localtime_r(&t, &tm); |
| 3784 | char timestr[64]; |
| 3785 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
| 3786 | mLastANRState.clear(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3787 | mLastANRState += INDENT "ANR:\n"; |
| 3788 | mLastANRState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
| 3789 | mLastANRState += StringPrintf(INDENT2 "Window: %s\n", |
| 3790 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).c_str()); |
| 3791 | mLastANRState += StringPrintf(INDENT2 "DispatchLatency: %0.1fms\n", dispatchLatency); |
| 3792 | mLastANRState += StringPrintf(INDENT2 "WaitDuration: %0.1fms\n", waitDuration); |
| 3793 | mLastANRState += StringPrintf(INDENT2 "Reason: %s\n", reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3794 | dumpDispatchStateLocked(mLastANRState); |
| 3795 | |
| 3796 | CommandEntry* commandEntry = postCommandLocked( |
| 3797 | & InputDispatcher::doNotifyANRLockedInterruptible); |
| 3798 | commandEntry->inputApplicationHandle = applicationHandle; |
| 3799 | commandEntry->inputWindowHandle = windowHandle; |
| 3800 | commandEntry->reason = reason; |
| 3801 | } |
| 3802 | |
| 3803 | void InputDispatcher::doNotifyConfigurationChangedInterruptible( |
| 3804 | CommandEntry* commandEntry) { |
| 3805 | mLock.unlock(); |
| 3806 | |
| 3807 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 3808 | |
| 3809 | mLock.lock(); |
| 3810 | } |
| 3811 | |
| 3812 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible( |
| 3813 | CommandEntry* commandEntry) { |
| 3814 | sp<Connection> connection = commandEntry->connection; |
| 3815 | |
| 3816 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 3817 | mLock.unlock(); |
| 3818 | |
| 3819 | mPolicy->notifyInputChannelBroken(connection->inputWindowHandle); |
| 3820 | |
| 3821 | mLock.lock(); |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | void InputDispatcher::doNotifyANRLockedInterruptible( |
| 3826 | CommandEntry* commandEntry) { |
| 3827 | mLock.unlock(); |
| 3828 | |
| 3829 | nsecs_t newTimeout = mPolicy->notifyANR( |
| 3830 | commandEntry->inputApplicationHandle, commandEntry->inputWindowHandle, |
| 3831 | commandEntry->reason); |
| 3832 | |
| 3833 | mLock.lock(); |
| 3834 | |
| 3835 | resumeAfterTargetsNotReadyTimeoutLocked(newTimeout, |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3836 | commandEntry->inputWindowHandle != nullptr |
| 3837 | ? commandEntry->inputWindowHandle->getInputChannel() : nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 3841 | CommandEntry* commandEntry) { |
| 3842 | KeyEntry* entry = commandEntry->keyEntry; |
| 3843 | |
| 3844 | KeyEvent event; |
| 3845 | initializeKeyEvent(&event, entry); |
| 3846 | |
| 3847 | mLock.unlock(); |
| 3848 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3849 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3850 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle, |
| 3851 | &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3852 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3853 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 3854 | std::to_string(t.duration().count()).c_str()); |
| 3855 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3856 | |
| 3857 | mLock.lock(); |
| 3858 | |
| 3859 | if (delay < 0) { |
| 3860 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 3861 | } else if (!delay) { |
| 3862 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 3863 | } else { |
| 3864 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 3865 | entry->interceptKeyWakeupTime = now() + delay; |
| 3866 | } |
| 3867 | entry->release(); |
| 3868 | } |
| 3869 | |
| 3870 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible( |
| 3871 | CommandEntry* commandEntry) { |
| 3872 | sp<Connection> connection = commandEntry->connection; |
| 3873 | nsecs_t finishTime = commandEntry->eventTime; |
| 3874 | uint32_t seq = commandEntry->seq; |
| 3875 | bool handled = commandEntry->handled; |
| 3876 | |
| 3877 | // Handle post-event policy actions. |
| 3878 | DispatchEntry* dispatchEntry = connection->findWaitQueueEntry(seq); |
| 3879 | if (dispatchEntry) { |
| 3880 | nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 3881 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3882 | std::string msg = |
| 3883 | StringPrintf("Window '%s' spent %0.1fms processing the last input event: ", |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 3884 | connection->getWindowName().c_str(), eventDuration * 0.000001f); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | dispatchEntry->eventEntry->appendDescription(msg); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3886 | ALOGI("%s", msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | bool restartEvent; |
| 3890 | if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) { |
| 3891 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 3892 | restartEvent = afterKeyEventLockedInterruptible(connection, |
| 3893 | dispatchEntry, keyEntry, handled); |
| 3894 | } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) { |
| 3895 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 3896 | restartEvent = afterMotionEventLockedInterruptible(connection, |
| 3897 | dispatchEntry, motionEntry, handled); |
| 3898 | } else { |
| 3899 | restartEvent = false; |
| 3900 | } |
| 3901 | |
| 3902 | // Dequeue the event and start the next cycle. |
| 3903 | // Note that because the lock might have been released, it is possible that the |
| 3904 | // contents of the wait queue to have been drained, so we need to double-check |
| 3905 | // a few things. |
| 3906 | if (dispatchEntry == connection->findWaitQueueEntry(seq)) { |
| 3907 | connection->waitQueue.dequeue(dispatchEntry); |
| 3908 | traceWaitQueueLengthLocked(connection); |
| 3909 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
| 3910 | connection->outboundQueue.enqueueAtHead(dispatchEntry); |
| 3911 | traceOutboundQueueLengthLocked(connection); |
| 3912 | } else { |
| 3913 | releaseDispatchEntryLocked(dispatchEntry); |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | // Start the next dispatch cycle for this connection. |
| 3918 | startDispatchCycleLocked(now(), connection); |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
| 3923 | DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) { |
| 3924 | if (!(keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK)) { |
| 3925 | // Get the fallback key state. |
| 3926 | // Clear it out after dispatching the UP. |
| 3927 | int32_t originalKeyCode = keyEntry->keyCode; |
| 3928 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 3929 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 3930 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 3931 | } |
| 3932 | |
| 3933 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 3934 | // If the application handles the original key for which we previously |
| 3935 | // generated a fallback or if the window is not a foreground window, |
| 3936 | // then cancel the associated fallback key, if any. |
| 3937 | if (fallbackKeyCode != -1) { |
| 3938 | // Dispatch the unhandled key to the policy with the cancel flag. |
| 3939 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3940 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 3941 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 3942 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 3943 | keyEntry->policyFlags); |
| 3944 | #endif |
| 3945 | KeyEvent event; |
| 3946 | initializeKeyEvent(&event, keyEntry); |
| 3947 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
| 3948 | |
| 3949 | mLock.unlock(); |
| 3950 | |
| 3951 | mPolicy->dispatchUnhandledKey(connection->inputWindowHandle, |
| 3952 | &event, keyEntry->policyFlags, &event); |
| 3953 | |
| 3954 | mLock.lock(); |
| 3955 | |
| 3956 | // Cancel the fallback key. |
| 3957 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
| 3958 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 3959 | "application handled the original non-fallback key " |
| 3960 | "or is no longer a foreground target, " |
| 3961 | "canceling previously dispatched fallback key"); |
| 3962 | options.keyCode = fallbackKeyCode; |
| 3963 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 3964 | } |
| 3965 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 3966 | } |
| 3967 | } else { |
| 3968 | // If the application did not handle a non-fallback key, first check |
| 3969 | // that we are in a good state to perform unhandled key event processing |
| 3970 | // Then ask the policy what to do with it. |
| 3971 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN |
| 3972 | && keyEntry->repeatCount == 0; |
| 3973 | if (fallbackKeyCode == -1 && !initialDown) { |
| 3974 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3975 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 3976 | "since this is not an initial down. " |
| 3977 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 3978 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, |
| 3979 | keyEntry->policyFlags); |
| 3980 | #endif |
| 3981 | return false; |
| 3982 | } |
| 3983 | |
| 3984 | // Dispatch the unhandled key to the policy. |
| 3985 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3986 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 3987 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 3988 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 3989 | keyEntry->policyFlags); |
| 3990 | #endif |
| 3991 | KeyEvent event; |
| 3992 | initializeKeyEvent(&event, keyEntry); |
| 3993 | |
| 3994 | mLock.unlock(); |
| 3995 | |
| 3996 | bool fallback = mPolicy->dispatchUnhandledKey(connection->inputWindowHandle, |
| 3997 | &event, keyEntry->policyFlags, &event); |
| 3998 | |
| 3999 | mLock.lock(); |
| 4000 | |
| 4001 | if (connection->status != Connection::STATUS_NORMAL) { |
| 4002 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4003 | return false; |
| 4004 | } |
| 4005 | |
| 4006 | // Latch the fallback keycode for this key on an initial down. |
| 4007 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 4008 | if (initialDown) { |
| 4009 | if (fallback) { |
| 4010 | fallbackKeyCode = event.getKeyCode(); |
| 4011 | } else { |
| 4012 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4013 | } |
| 4014 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 4015 | } |
| 4016 | |
| 4017 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 4018 | |
| 4019 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 4020 | // We will continue to dispatch the key to the policy but we will no |
| 4021 | // longer dispatch a fallback key to the application. |
| 4022 | if (fallbackKeyCode != AKEYCODE_UNKNOWN |
| 4023 | && (!fallback || fallbackKeyCode != event.getKeyCode())) { |
| 4024 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4025 | if (fallback) { |
| 4026 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 4027 | "as a fallback for %d, but on the DOWN it had requested " |
| 4028 | "to send %d instead. Fallback canceled.", |
| 4029 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 4030 | } else { |
| 4031 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 4032 | "but on the DOWN it had requested to send %d. " |
| 4033 | "Fallback canceled.", |
| 4034 | originalKeyCode, fallbackKeyCode); |
| 4035 | } |
| 4036 | #endif |
| 4037 | |
| 4038 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 4039 | "canceling fallback, policy no longer desires it"); |
| 4040 | options.keyCode = fallbackKeyCode; |
| 4041 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 4042 | |
| 4043 | fallback = false; |
| 4044 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4045 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
| 4046 | connection->inputState.setFallbackKey(originalKeyCode, |
| 4047 | fallbackKeyCode); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4052 | { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4053 | std::string msg; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4054 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 4055 | connection->inputState.getFallbackKeys(); |
| 4056 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4057 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4058 | fallbackKeys.valueAt(i)); |
| 4059 | } |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 4060 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4061 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4062 | } |
| 4063 | #endif |
| 4064 | |
| 4065 | if (fallback) { |
| 4066 | // Restart the dispatch cycle using the fallback key. |
| 4067 | keyEntry->eventTime = event.getEventTime(); |
| 4068 | keyEntry->deviceId = event.getDeviceId(); |
| 4069 | keyEntry->source = event.getSource(); |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4070 | keyEntry->displayId = event.getDisplayId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 4072 | keyEntry->keyCode = fallbackKeyCode; |
| 4073 | keyEntry->scanCode = event.getScanCode(); |
| 4074 | keyEntry->metaState = event.getMetaState(); |
| 4075 | keyEntry->repeatCount = event.getRepeatCount(); |
| 4076 | keyEntry->downTime = event.getDownTime(); |
| 4077 | keyEntry->syntheticRepeat = false; |
| 4078 | |
| 4079 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4080 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 4081 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 4082 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
| 4083 | #endif |
| 4084 | return true; // restart the event |
| 4085 | } else { |
| 4086 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4087 | ALOGD("Unhandled key event: No fallback key."); |
| 4088 | #endif |
| 4089 | } |
| 4090 | } |
| 4091 | } |
| 4092 | return false; |
| 4093 | } |
| 4094 | |
| 4095 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
| 4096 | DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) { |
| 4097 | return false; |
| 4098 | } |
| 4099 | |
| 4100 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 4101 | mLock.unlock(); |
| 4102 | |
| 4103 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 4104 | |
| 4105 | mLock.lock(); |
| 4106 | } |
| 4107 | |
| 4108 | void InputDispatcher::initializeKeyEvent(KeyEvent* event, const KeyEntry* entry) { |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4109 | event->initialize(entry->deviceId, entry->source, entry->displayId, entry->action, entry->flags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount, |
| 4111 | entry->downTime, entry->eventTime); |
| 4112 | } |
| 4113 | |
| 4114 | void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 4115 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) { |
| 4116 | // TODO Write some statistics about how long we spend waiting. |
| 4117 | } |
| 4118 | |
| 4119 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 4120 | if (ATRACE_ENABLED()) { |
| 4121 | ATRACE_INT("iq", mInboundQueue.count()); |
| 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | void InputDispatcher::traceOutboundQueueLengthLocked(const sp<Connection>& connection) { |
| 4126 | if (ATRACE_ENABLED()) { |
| 4127 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 4128 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4129 | ATRACE_INT(counterName, connection->outboundQueue.count()); |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | void InputDispatcher::traceWaitQueueLengthLocked(const sp<Connection>& connection) { |
| 4134 | if (ATRACE_ENABLED()) { |
| 4135 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 4136 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4137 | ATRACE_INT(counterName, connection->waitQueue.count()); |
| 4138 | } |
| 4139 | } |
| 4140 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4141 | void InputDispatcher::dump(std::string& dump) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4142 | AutoMutex _l(mLock); |
| 4143 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4144 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | dumpDispatchStateLocked(dump); |
| 4146 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4147 | if (!mLastANRState.empty()) { |
| 4148 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
| 4149 | dump += mLastANRState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4150 | } |
| 4151 | } |
| 4152 | |
| 4153 | void InputDispatcher::monitor() { |
| 4154 | // Acquire and release the lock to ensure that the dispatcher has not deadlocked. |
| 4155 | mLock.lock(); |
| 4156 | mLooper->wake(); |
| 4157 | mDispatcherIsAliveCondition.wait(mLock); |
| 4158 | mLock.unlock(); |
| 4159 | } |
| 4160 | |
| 4161 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | // --- InputDispatcher::InjectionState --- |
| 4163 | |
| 4164 | InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) : |
| 4165 | refCount(1), |
| 4166 | injectorPid(injectorPid), injectorUid(injectorUid), |
| 4167 | injectionResult(INPUT_EVENT_INJECTION_PENDING), injectionIsAsync(false), |
| 4168 | pendingForegroundDispatches(0) { |
| 4169 | } |
| 4170 | |
| 4171 | InputDispatcher::InjectionState::~InjectionState() { |
| 4172 | } |
| 4173 | |
| 4174 | void InputDispatcher::InjectionState::release() { |
| 4175 | refCount -= 1; |
| 4176 | if (refCount == 0) { |
| 4177 | delete this; |
| 4178 | } else { |
| 4179 | ALOG_ASSERT(refCount > 0); |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | |
| 4184 | // --- InputDispatcher::EventEntry --- |
| 4185 | |
| 4186 | InputDispatcher::EventEntry::EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags) : |
| 4187 | refCount(1), type(type), eventTime(eventTime), policyFlags(policyFlags), |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4188 | injectionState(nullptr), dispatchInProgress(false) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | InputDispatcher::EventEntry::~EventEntry() { |
| 4192 | releaseInjectionState(); |
| 4193 | } |
| 4194 | |
| 4195 | void InputDispatcher::EventEntry::release() { |
| 4196 | refCount -= 1; |
| 4197 | if (refCount == 0) { |
| 4198 | delete this; |
| 4199 | } else { |
| 4200 | ALOG_ASSERT(refCount > 0); |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | void InputDispatcher::EventEntry::releaseInjectionState() { |
| 4205 | if (injectionState) { |
| 4206 | injectionState->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4207 | injectionState = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | |
| 4212 | // --- InputDispatcher::ConfigurationChangedEntry --- |
| 4213 | |
| 4214 | InputDispatcher::ConfigurationChangedEntry::ConfigurationChangedEntry(nsecs_t eventTime) : |
| 4215 | EventEntry(TYPE_CONFIGURATION_CHANGED, eventTime, 0) { |
| 4216 | } |
| 4217 | |
| 4218 | InputDispatcher::ConfigurationChangedEntry::~ConfigurationChangedEntry() { |
| 4219 | } |
| 4220 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4221 | void InputDispatcher::ConfigurationChangedEntry::appendDescription(std::string& msg) const { |
| 4222 | msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | |
| 4226 | // --- InputDispatcher::DeviceResetEntry --- |
| 4227 | |
| 4228 | InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) : |
| 4229 | EventEntry(TYPE_DEVICE_RESET, eventTime, 0), |
| 4230 | deviceId(deviceId) { |
| 4231 | } |
| 4232 | |
| 4233 | InputDispatcher::DeviceResetEntry::~DeviceResetEntry() { |
| 4234 | } |
| 4235 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4236 | void InputDispatcher::DeviceResetEntry::appendDescription(std::string& msg) const { |
| 4237 | msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | deviceId, policyFlags); |
| 4239 | } |
| 4240 | |
| 4241 | |
| 4242 | // --- InputDispatcher::KeyEntry --- |
| 4243 | |
| 4244 | InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4245 | int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags, int32_t action, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
| 4247 | int32_t repeatCount, nsecs_t downTime) : |
| 4248 | EventEntry(TYPE_KEY, eventTime, policyFlags), |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4249 | deviceId(deviceId), source(source), displayId(displayId), action(action), flags(flags), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4250 | keyCode(keyCode), scanCode(scanCode), metaState(metaState), |
| 4251 | repeatCount(repeatCount), downTime(downTime), |
| 4252 | syntheticRepeat(false), interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN), |
| 4253 | interceptKeyWakeupTime(0) { |
| 4254 | } |
| 4255 | |
| 4256 | InputDispatcher::KeyEntry::~KeyEntry() { |
| 4257 | } |
| 4258 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4259 | void InputDispatcher::KeyEntry::appendDescription(std::string& msg) const { |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4260 | msg += StringPrintf("KeyEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, " |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4261 | "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, " |
| 4262 | "repeatCount=%d), policyFlags=0x%08x", |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4263 | deviceId, source, displayId, keyActionToString(action).c_str(), flags, keyCode, |
Siarhei Vishniakou | b48188a | 2018-03-01 20:55:47 -0800 | [diff] [blame] | 4264 | scanCode, metaState, repeatCount, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | void InputDispatcher::KeyEntry::recycle() { |
| 4268 | releaseInjectionState(); |
| 4269 | |
| 4270 | dispatchInProgress = false; |
| 4271 | syntheticRepeat = false; |
| 4272 | interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 4273 | interceptKeyWakeupTime = 0; |
| 4274 | } |
| 4275 | |
| 4276 | |
| 4277 | // --- InputDispatcher::MotionEntry --- |
| 4278 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 4279 | InputDispatcher::MotionEntry::MotionEntry(nsecs_t eventTime, int32_t deviceId, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4280 | uint32_t source, int32_t displayId, uint32_t policyFlags, int32_t action, |
| 4281 | int32_t actionButton, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 4282 | int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 4283 | float xPrecision, float yPrecision, nsecs_t downTime, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4284 | uint32_t pointerCount, |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4285 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords, |
| 4286 | float xOffset, float yOffset) : |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | EventEntry(TYPE_MOTION, eventTime, policyFlags), |
| 4288 | eventTime(eventTime), |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4289 | deviceId(deviceId), source(source), displayId(displayId), action(action), |
| 4290 | actionButton(actionButton), flags(flags), metaState(metaState), buttonState(buttonState), |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 4291 | edgeFlags(edgeFlags), xPrecision(xPrecision), yPrecision(yPrecision), |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4292 | downTime(downTime), pointerCount(pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4293 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 4294 | this->pointerProperties[i].copyFrom(pointerProperties[i]); |
| 4295 | this->pointerCoords[i].copyFrom(pointerCoords[i]); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4296 | if (xOffset || yOffset) { |
| 4297 | this->pointerCoords[i].applyOffset(xOffset, yOffset); |
| 4298 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4299 | } |
| 4300 | } |
| 4301 | |
| 4302 | InputDispatcher::MotionEntry::~MotionEntry() { |
| 4303 | } |
| 4304 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4305 | void InputDispatcher::MotionEntry::appendDescription(std::string& msg) const { |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4306 | msg += StringPrintf("MotionEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 |
Siarhei Vishniakou | b48188a | 2018-03-01 20:55:47 -0800 | [diff] [blame] | 4307 | ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, buttonState=0x%08x, " |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4308 | "edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, pointers=[", |
Siarhei Vishniakou | b48188a | 2018-03-01 20:55:47 -0800 | [diff] [blame] | 4309 | deviceId, source, displayId, motionActionToString(action).c_str(), actionButton, flags, |
| 4310 | metaState, buttonState, edgeFlags, xPrecision, yPrecision); |
| 4311 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 4313 | if (i) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4314 | msg += ", "; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4315 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4316 | msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | pointerCoords[i].getX(), pointerCoords[i].getY()); |
| 4318 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4319 | msg += StringPrintf("]), policyFlags=0x%08x", policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | |
| 4323 | // --- InputDispatcher::DispatchEntry --- |
| 4324 | |
| 4325 | volatile int32_t InputDispatcher::DispatchEntry::sNextSeqAtomic; |
| 4326 | |
| 4327 | InputDispatcher::DispatchEntry::DispatchEntry(EventEntry* eventEntry, |
| 4328 | int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) : |
| 4329 | seq(nextSeq()), |
| 4330 | eventEntry(eventEntry), targetFlags(targetFlags), |
| 4331 | xOffset(xOffset), yOffset(yOffset), scaleFactor(scaleFactor), |
| 4332 | deliveryTime(0), resolvedAction(0), resolvedFlags(0) { |
| 4333 | eventEntry->refCount += 1; |
| 4334 | } |
| 4335 | |
| 4336 | InputDispatcher::DispatchEntry::~DispatchEntry() { |
| 4337 | eventEntry->release(); |
| 4338 | } |
| 4339 | |
| 4340 | uint32_t InputDispatcher::DispatchEntry::nextSeq() { |
| 4341 | // Sequence number 0 is reserved and will never be returned. |
| 4342 | uint32_t seq; |
| 4343 | do { |
| 4344 | seq = android_atomic_inc(&sNextSeqAtomic); |
| 4345 | } while (!seq); |
| 4346 | return seq; |
| 4347 | } |
| 4348 | |
| 4349 | |
| 4350 | // --- InputDispatcher::InputState --- |
| 4351 | |
| 4352 | InputDispatcher::InputState::InputState() { |
| 4353 | } |
| 4354 | |
| 4355 | InputDispatcher::InputState::~InputState() { |
| 4356 | } |
| 4357 | |
| 4358 | bool InputDispatcher::InputState::isNeutral() const { |
| 4359 | return mKeyMementos.isEmpty() && mMotionMementos.isEmpty(); |
| 4360 | } |
| 4361 | |
| 4362 | bool InputDispatcher::InputState::isHovering(int32_t deviceId, uint32_t source, |
| 4363 | int32_t displayId) const { |
| 4364 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 4365 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
| 4366 | if (memento.deviceId == deviceId |
| 4367 | && memento.source == source |
| 4368 | && memento.displayId == displayId |
| 4369 | && memento.hovering) { |
| 4370 | return true; |
| 4371 | } |
| 4372 | } |
| 4373 | return false; |
| 4374 | } |
| 4375 | |
| 4376 | bool InputDispatcher::InputState::trackKey(const KeyEntry* entry, |
| 4377 | int32_t action, int32_t flags) { |
| 4378 | switch (action) { |
| 4379 | case AKEY_EVENT_ACTION_UP: { |
| 4380 | if (entry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
| 4381 | for (size_t i = 0; i < mFallbackKeys.size(); ) { |
| 4382 | if (mFallbackKeys.valueAt(i) == entry->keyCode) { |
| 4383 | mFallbackKeys.removeItemsAt(i); |
| 4384 | } else { |
| 4385 | i += 1; |
| 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | ssize_t index = findKeyMemento(entry); |
| 4390 | if (index >= 0) { |
| 4391 | mKeyMementos.removeAt(index); |
| 4392 | return true; |
| 4393 | } |
| 4394 | /* FIXME: We can't just drop the key up event because that prevents creating |
| 4395 | * popup windows that are automatically shown when a key is held and then |
| 4396 | * dismissed when the key is released. The problem is that the popup will |
| 4397 | * not have received the original key down, so the key up will be considered |
| 4398 | * to be inconsistent with its observed state. We could perhaps handle this |
| 4399 | * by synthesizing a key down but that will cause other problems. |
| 4400 | * |
| 4401 | * So for now, allow inconsistent key up events to be dispatched. |
| 4402 | * |
| 4403 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4404 | ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, " |
| 4405 | "keyCode=%d, scanCode=%d", |
| 4406 | entry->deviceId, entry->source, entry->keyCode, entry->scanCode); |
| 4407 | #endif |
| 4408 | return false; |
| 4409 | */ |
| 4410 | return true; |
| 4411 | } |
| 4412 | |
| 4413 | case AKEY_EVENT_ACTION_DOWN: { |
| 4414 | ssize_t index = findKeyMemento(entry); |
| 4415 | if (index >= 0) { |
| 4416 | mKeyMementos.removeAt(index); |
| 4417 | } |
| 4418 | addKeyMemento(entry, flags); |
| 4419 | return true; |
| 4420 | } |
| 4421 | |
| 4422 | default: |
| 4423 | return true; |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | bool InputDispatcher::InputState::trackMotion(const MotionEntry* entry, |
| 4428 | int32_t action, int32_t flags) { |
| 4429 | int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK; |
| 4430 | switch (actionMasked) { |
| 4431 | case AMOTION_EVENT_ACTION_UP: |
| 4432 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 4433 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 4434 | if (index >= 0) { |
| 4435 | mMotionMementos.removeAt(index); |
| 4436 | return true; |
| 4437 | } |
| 4438 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4439 | ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, " |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4440 | "displayId=%" PRId32 ", actionMasked=%d", |
| 4441 | entry->deviceId, entry->source, entry->displayId, actionMasked); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4442 | #endif |
| 4443 | return false; |
| 4444 | } |
| 4445 | |
| 4446 | case AMOTION_EVENT_ACTION_DOWN: { |
| 4447 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 4448 | if (index >= 0) { |
| 4449 | mMotionMementos.removeAt(index); |
| 4450 | } |
| 4451 | addMotionMemento(entry, flags, false /*hovering*/); |
| 4452 | return true; |
| 4453 | } |
| 4454 | |
| 4455 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 4456 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 4457 | case AMOTION_EVENT_ACTION_MOVE: { |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 4458 | if (entry->source & AINPUT_SOURCE_CLASS_NAVIGATION) { |
| 4459 | // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need to |
| 4460 | // generate cancellation events for these since they're based in relative rather than |
| 4461 | // absolute units. |
| 4462 | return true; |
| 4463 | } |
| 4464 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4465 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 4466 | |
| 4467 | if (entry->source & AINPUT_SOURCE_CLASS_JOYSTICK) { |
| 4468 | // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all |
| 4469 | // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral. Any |
| 4470 | // other value and we need to track the motion so we can send cancellation events for |
| 4471 | // anything generating fallback events (e.g. DPad keys for joystick movements). |
| 4472 | if (index >= 0) { |
| 4473 | if (entry->pointerCoords[0].isEmpty()) { |
| 4474 | mMotionMementos.removeAt(index); |
| 4475 | } else { |
| 4476 | MotionMemento& memento = mMotionMementos.editItemAt(index); |
| 4477 | memento.setPointers(entry); |
| 4478 | } |
| 4479 | } else if (!entry->pointerCoords[0].isEmpty()) { |
| 4480 | addMotionMemento(entry, flags, false /*hovering*/); |
| 4481 | } |
| 4482 | |
| 4483 | // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP. |
| 4484 | return true; |
| 4485 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4486 | if (index >= 0) { |
| 4487 | MotionMemento& memento = mMotionMementos.editItemAt(index); |
| 4488 | memento.setPointers(entry); |
| 4489 | return true; |
| 4490 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4491 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4492 | ALOGD("Dropping inconsistent motion pointer up/down or move event: " |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4493 | "deviceId=%d, source=%08x, displayId=%" PRId32 ", actionMasked=%d", |
| 4494 | entry->deviceId, entry->source, entry->displayId, actionMasked); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4495 | #endif |
| 4496 | return false; |
| 4497 | } |
| 4498 | |
| 4499 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 4500 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
| 4501 | if (index >= 0) { |
| 4502 | mMotionMementos.removeAt(index); |
| 4503 | return true; |
| 4504 | } |
| 4505 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4506 | ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, " |
| 4507 | "displayId=%" PRId32, |
| 4508 | entry->deviceId, entry->source, entry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4509 | #endif |
| 4510 | return false; |
| 4511 | } |
| 4512 | |
| 4513 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 4514 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
| 4515 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
| 4516 | if (index >= 0) { |
| 4517 | mMotionMementos.removeAt(index); |
| 4518 | } |
| 4519 | addMotionMemento(entry, flags, true /*hovering*/); |
| 4520 | return true; |
| 4521 | } |
| 4522 | |
| 4523 | default: |
| 4524 | return true; |
| 4525 | } |
| 4526 | } |
| 4527 | |
| 4528 | ssize_t InputDispatcher::InputState::findKeyMemento(const KeyEntry* entry) const { |
| 4529 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
| 4530 | const KeyMemento& memento = mKeyMementos.itemAt(i); |
| 4531 | if (memento.deviceId == entry->deviceId |
| 4532 | && memento.source == entry->source |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4533 | && memento.displayId == entry->displayId |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | && memento.keyCode == entry->keyCode |
| 4535 | && memento.scanCode == entry->scanCode) { |
| 4536 | return i; |
| 4537 | } |
| 4538 | } |
| 4539 | return -1; |
| 4540 | } |
| 4541 | |
| 4542 | ssize_t InputDispatcher::InputState::findMotionMemento(const MotionEntry* entry, |
| 4543 | bool hovering) const { |
| 4544 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 4545 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
| 4546 | if (memento.deviceId == entry->deviceId |
| 4547 | && memento.source == entry->source |
| 4548 | && memento.displayId == entry->displayId |
| 4549 | && memento.hovering == hovering) { |
| 4550 | return i; |
| 4551 | } |
| 4552 | } |
| 4553 | return -1; |
| 4554 | } |
| 4555 | |
| 4556 | void InputDispatcher::InputState::addKeyMemento(const KeyEntry* entry, int32_t flags) { |
| 4557 | mKeyMementos.push(); |
| 4558 | KeyMemento& memento = mKeyMementos.editTop(); |
| 4559 | memento.deviceId = entry->deviceId; |
| 4560 | memento.source = entry->source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4561 | memento.displayId = entry->displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4562 | memento.keyCode = entry->keyCode; |
| 4563 | memento.scanCode = entry->scanCode; |
| 4564 | memento.metaState = entry->metaState; |
| 4565 | memento.flags = flags; |
| 4566 | memento.downTime = entry->downTime; |
| 4567 | memento.policyFlags = entry->policyFlags; |
| 4568 | } |
| 4569 | |
| 4570 | void InputDispatcher::InputState::addMotionMemento(const MotionEntry* entry, |
| 4571 | int32_t flags, bool hovering) { |
| 4572 | mMotionMementos.push(); |
| 4573 | MotionMemento& memento = mMotionMementos.editTop(); |
| 4574 | memento.deviceId = entry->deviceId; |
| 4575 | memento.source = entry->source; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4576 | memento.displayId = entry->displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4577 | memento.flags = flags; |
| 4578 | memento.xPrecision = entry->xPrecision; |
| 4579 | memento.yPrecision = entry->yPrecision; |
| 4580 | memento.downTime = entry->downTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4581 | memento.setPointers(entry); |
| 4582 | memento.hovering = hovering; |
| 4583 | memento.policyFlags = entry->policyFlags; |
| 4584 | } |
| 4585 | |
| 4586 | void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) { |
| 4587 | pointerCount = entry->pointerCount; |
| 4588 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
| 4589 | pointerProperties[i].copyFrom(entry->pointerProperties[i]); |
| 4590 | pointerCoords[i].copyFrom(entry->pointerCoords[i]); |
| 4591 | } |
| 4592 | } |
| 4593 | |
| 4594 | void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime, |
| 4595 | Vector<EventEntry*>& outEvents, const CancelationOptions& options) { |
| 4596 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
| 4597 | const KeyMemento& memento = mKeyMementos.itemAt(i); |
| 4598 | if (shouldCancelKey(memento, options)) { |
| 4599 | outEvents.push(new KeyEntry(currentTime, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4600 | memento.deviceId, memento.source, memento.displayId, memento.policyFlags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 | AKEY_EVENT_ACTION_UP, memento.flags | AKEY_EVENT_FLAG_CANCELED, |
| 4602 | memento.keyCode, memento.scanCode, memento.metaState, 0, memento.downTime)); |
| 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 4607 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
| 4608 | if (shouldCancelMotion(memento, options)) { |
| 4609 | outEvents.push(new MotionEntry(currentTime, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 4610 | memento.deviceId, memento.source, memento.displayId, memento.policyFlags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4611 | memento.hovering |
| 4612 | ? AMOTION_EVENT_ACTION_HOVER_EXIT |
| 4613 | : AMOTION_EVENT_ACTION_CANCEL, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 4614 | memento.flags, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4615 | memento.xPrecision, memento.yPrecision, memento.downTime, |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4616 | memento.pointerCount, memento.pointerProperties, memento.pointerCoords, |
| 4617 | 0, 0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4618 | } |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | void InputDispatcher::InputState::clear() { |
| 4623 | mKeyMementos.clear(); |
| 4624 | mMotionMementos.clear(); |
| 4625 | mFallbackKeys.clear(); |
| 4626 | } |
| 4627 | |
| 4628 | void InputDispatcher::InputState::copyPointerStateTo(InputState& other) const { |
| 4629 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 4630 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
| 4631 | if (memento.source & AINPUT_SOURCE_CLASS_POINTER) { |
| 4632 | for (size_t j = 0; j < other.mMotionMementos.size(); ) { |
| 4633 | const MotionMemento& otherMemento = other.mMotionMementos.itemAt(j); |
| 4634 | if (memento.deviceId == otherMemento.deviceId |
| 4635 | && memento.source == otherMemento.source |
| 4636 | && memento.displayId == otherMemento.displayId) { |
| 4637 | other.mMotionMementos.removeAt(j); |
| 4638 | } else { |
| 4639 | j += 1; |
| 4640 | } |
| 4641 | } |
| 4642 | other.mMotionMementos.push(memento); |
| 4643 | } |
| 4644 | } |
| 4645 | } |
| 4646 | |
| 4647 | int32_t InputDispatcher::InputState::getFallbackKey(int32_t originalKeyCode) { |
| 4648 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 4649 | return index >= 0 ? mFallbackKeys.valueAt(index) : -1; |
| 4650 | } |
| 4651 | |
| 4652 | void InputDispatcher::InputState::setFallbackKey(int32_t originalKeyCode, |
| 4653 | int32_t fallbackKeyCode) { |
| 4654 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 4655 | if (index >= 0) { |
| 4656 | mFallbackKeys.replaceValueAt(index, fallbackKeyCode); |
| 4657 | } else { |
| 4658 | mFallbackKeys.add(originalKeyCode, fallbackKeyCode); |
| 4659 | } |
| 4660 | } |
| 4661 | |
| 4662 | void InputDispatcher::InputState::removeFallbackKey(int32_t originalKeyCode) { |
| 4663 | mFallbackKeys.removeItem(originalKeyCode); |
| 4664 | } |
| 4665 | |
| 4666 | bool InputDispatcher::InputState::shouldCancelKey(const KeyMemento& memento, |
| 4667 | const CancelationOptions& options) { |
| 4668 | if (options.keyCode != -1 && memento.keyCode != options.keyCode) { |
| 4669 | return false; |
| 4670 | } |
| 4671 | |
| 4672 | if (options.deviceId != -1 && memento.deviceId != options.deviceId) { |
| 4673 | return false; |
| 4674 | } |
| 4675 | |
| 4676 | switch (options.mode) { |
| 4677 | case CancelationOptions::CANCEL_ALL_EVENTS: |
| 4678 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
| 4679 | return true; |
| 4680 | case CancelationOptions::CANCEL_FALLBACK_EVENTS: |
| 4681 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4682 | case CancelationOptions::CANCEL_DISPLAY_UNSPECIFIED_EVENTS: |
| 4683 | return memento.displayId == ADISPLAY_ID_NONE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4684 | default: |
| 4685 | return false; |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento, |
| 4690 | const CancelationOptions& options) { |
| 4691 | if (options.deviceId != -1 && memento.deviceId != options.deviceId) { |
| 4692 | return false; |
| 4693 | } |
| 4694 | |
| 4695 | switch (options.mode) { |
| 4696 | case CancelationOptions::CANCEL_ALL_EVENTS: |
| 4697 | return true; |
| 4698 | case CancelationOptions::CANCEL_POINTER_EVENTS: |
| 4699 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
| 4700 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
| 4701 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4702 | case CancelationOptions::CANCEL_DISPLAY_UNSPECIFIED_EVENTS: |
| 4703 | return memento.displayId == ADISPLAY_ID_NONE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4704 | default: |
| 4705 | return false; |
| 4706 | } |
| 4707 | } |
| 4708 | |
| 4709 | |
| 4710 | // --- InputDispatcher::Connection --- |
| 4711 | |
| 4712 | InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel, |
| 4713 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) : |
| 4714 | status(STATUS_NORMAL), inputChannel(inputChannel), inputWindowHandle(inputWindowHandle), |
| 4715 | monitor(monitor), |
| 4716 | inputPublisher(inputChannel), inputPublisherBlocked(false) { |
| 4717 | } |
| 4718 | |
| 4719 | InputDispatcher::Connection::~Connection() { |
| 4720 | } |
| 4721 | |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 4722 | const std::string InputDispatcher::Connection::getWindowName() const { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4723 | if (inputWindowHandle != nullptr) { |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 4724 | return inputWindowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4725 | } |
| 4726 | if (monitor) { |
| 4727 | return "monitor"; |
| 4728 | } |
| 4729 | return "?"; |
| 4730 | } |
| 4731 | |
| 4732 | const char* InputDispatcher::Connection::getStatusLabel() const { |
| 4733 | switch (status) { |
| 4734 | case STATUS_NORMAL: |
| 4735 | return "NORMAL"; |
| 4736 | |
| 4737 | case STATUS_BROKEN: |
| 4738 | return "BROKEN"; |
| 4739 | |
| 4740 | case STATUS_ZOMBIE: |
| 4741 | return "ZOMBIE"; |
| 4742 | |
| 4743 | default: |
| 4744 | return "UNKNOWN"; |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | InputDispatcher::DispatchEntry* InputDispatcher::Connection::findWaitQueueEntry(uint32_t seq) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4749 | for (DispatchEntry* entry = waitQueue.head; entry != nullptr; entry = entry->next) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4750 | if (entry->seq == seq) { |
| 4751 | return entry; |
| 4752 | } |
| 4753 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4754 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | |
| 4758 | // --- InputDispatcher::CommandEntry --- |
| 4759 | |
| 4760 | InputDispatcher::CommandEntry::CommandEntry(Command command) : |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4761 | command(command), eventTime(0), keyEntry(nullptr), userActivityEventType(0), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4762 | seq(0), handled(false) { |
| 4763 | } |
| 4764 | |
| 4765 | InputDispatcher::CommandEntry::~CommandEntry() { |
| 4766 | } |
| 4767 | |
| 4768 | |
| 4769 | // --- InputDispatcher::TouchState --- |
| 4770 | |
| 4771 | InputDispatcher::TouchState::TouchState() : |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4772 | down(false), split(false), deviceId(-1), source(0), displayId(ADISPLAY_ID_NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | InputDispatcher::TouchState::~TouchState() { |
| 4776 | } |
| 4777 | |
| 4778 | void InputDispatcher::TouchState::reset() { |
| 4779 | down = false; |
| 4780 | split = false; |
| 4781 | deviceId = -1; |
| 4782 | source = 0; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 4783 | displayId = ADISPLAY_ID_NONE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4784 | windows.clear(); |
| 4785 | } |
| 4786 | |
| 4787 | void InputDispatcher::TouchState::copyFrom(const TouchState& other) { |
| 4788 | down = other.down; |
| 4789 | split = other.split; |
| 4790 | deviceId = other.deviceId; |
| 4791 | source = other.source; |
| 4792 | displayId = other.displayId; |
| 4793 | windows = other.windows; |
| 4794 | } |
| 4795 | |
| 4796 | void InputDispatcher::TouchState::addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle, |
| 4797 | int32_t targetFlags, BitSet32 pointerIds) { |
| 4798 | if (targetFlags & InputTarget::FLAG_SPLIT) { |
| 4799 | split = true; |
| 4800 | } |
| 4801 | |
| 4802 | for (size_t i = 0; i < windows.size(); i++) { |
| 4803 | TouchedWindow& touchedWindow = windows.editItemAt(i); |
| 4804 | if (touchedWindow.windowHandle == windowHandle) { |
| 4805 | touchedWindow.targetFlags |= targetFlags; |
| 4806 | if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 4807 | touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS; |
| 4808 | } |
| 4809 | touchedWindow.pointerIds.value |= pointerIds.value; |
| 4810 | return; |
| 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | windows.push(); |
| 4815 | |
| 4816 | TouchedWindow& touchedWindow = windows.editTop(); |
| 4817 | touchedWindow.windowHandle = windowHandle; |
| 4818 | touchedWindow.targetFlags = targetFlags; |
| 4819 | touchedWindow.pointerIds = pointerIds; |
| 4820 | } |
| 4821 | |
| 4822 | void InputDispatcher::TouchState::removeWindow(const sp<InputWindowHandle>& windowHandle) { |
| 4823 | for (size_t i = 0; i < windows.size(); i++) { |
| 4824 | if (windows.itemAt(i).windowHandle == windowHandle) { |
| 4825 | windows.removeAt(i); |
| 4826 | return; |
| 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | void InputDispatcher::TouchState::filterNonAsIsTouchWindows() { |
| 4832 | for (size_t i = 0 ; i < windows.size(); ) { |
| 4833 | TouchedWindow& window = windows.editItemAt(i); |
| 4834 | if (window.targetFlags & (InputTarget::FLAG_DISPATCH_AS_IS |
| 4835 | | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) { |
| 4836 | window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK; |
| 4837 | window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS; |
| 4838 | i += 1; |
| 4839 | } else { |
| 4840 | windows.removeAt(i); |
| 4841 | } |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | sp<InputWindowHandle> InputDispatcher::TouchState::getFirstForegroundWindowHandle() const { |
| 4846 | for (size_t i = 0; i < windows.size(); i++) { |
| 4847 | const TouchedWindow& window = windows.itemAt(i); |
| 4848 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 4849 | return window.windowHandle; |
| 4850 | } |
| 4851 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4852 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | bool InputDispatcher::TouchState::isSlippery() const { |
| 4856 | // Must have exactly one foreground window. |
| 4857 | bool haveSlipperyForegroundWindow = false; |
| 4858 | for (size_t i = 0; i < windows.size(); i++) { |
| 4859 | const TouchedWindow& window = windows.itemAt(i); |
| 4860 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 4861 | if (haveSlipperyForegroundWindow |
| 4862 | || !(window.windowHandle->getInfo()->layoutParamsFlags |
| 4863 | & InputWindowInfo::FLAG_SLIPPERY)) { |
| 4864 | return false; |
| 4865 | } |
| 4866 | haveSlipperyForegroundWindow = true; |
| 4867 | } |
| 4868 | } |
| 4869 | return haveSlipperyForegroundWindow; |
| 4870 | } |
| 4871 | |
| 4872 | |
| 4873 | // --- InputDispatcherThread --- |
| 4874 | |
| 4875 | InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) : |
| 4876 | Thread(/*canCallJava*/ true), mDispatcher(dispatcher) { |
| 4877 | } |
| 4878 | |
| 4879 | InputDispatcherThread::~InputDispatcherThread() { |
| 4880 | } |
| 4881 | |
| 4882 | bool InputDispatcherThread::threadLoop() { |
| 4883 | mDispatcher->dispatchOnce(); |
| 4884 | return true; |
| 4885 | } |
| 4886 | |
| 4887 | } // namespace android |