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