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