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