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