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