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