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