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 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 48 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 49 | #include <android-base/stringprintf.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 50 | #include <binder/Binder.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 51 | #include <input/InputDevice.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 52 | #include <input/InputWindow.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 53 | #include <log/log.h> |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 54 | #include <openssl/hmac.h> |
| 55 | #include <openssl/rand.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 56 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 57 | #include <statslog.h> |
| 58 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 60 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 61 | #include <cerrno> |
| 62 | #include <cinttypes> |
| 63 | #include <climits> |
| 64 | #include <cstddef> |
| 65 | #include <ctime> |
| 66 | #include <queue> |
| 67 | #include <sstream> |
| 68 | |
| 69 | #include "Connection.h" |
| 70 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | #define INDENT " " |
| 72 | #define INDENT2 " " |
| 73 | #define INDENT3 " " |
| 74 | #define INDENT4 " " |
| 75 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 76 | using android::base::StringPrintf; |
| 77 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | |
| 80 | // Default input dispatching timeout if there is no focused application or paused window |
| 81 | // from which to determine an appropriate dispatching timeout. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 82 | constexpr std::chrono::nanoseconds DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5s; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 | |
| 84 | // Amount of time to allow for all pending events to be processed when an app switch |
| 85 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 86 | // 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] | 87 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 | |
| 89 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 90 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 91 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | // 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] | 94 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 95 | |
| 96 | // Log a warning when an interception call takes longer than this to process. |
| 97 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 99 | // Additional key latency in case a connection is still processing some motion events. |
| 100 | // This will help with the case when a user touched a button that opens a new window, |
| 101 | // and gives us the chance to dispatch the key to this new window. |
| 102 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 103 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 104 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 105 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | static inline nsecs_t now() { |
| 108 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 109 | } |
| 110 | |
| 111 | static inline const char* toString(bool value) { |
| 112 | return value ? "true" : "false"; |
| 113 | } |
| 114 | |
| 115 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 116 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 117 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static bool isValidKeyAction(int32_t action) { |
| 121 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 122 | case AKEY_EVENT_ACTION_DOWN: |
| 123 | case AKEY_EVENT_ACTION_UP: |
| 124 | return true; |
| 125 | default: |
| 126 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 131 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 132 | ALOGE("Key event has invalid action code 0x%x", action); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 138 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 140 | case AMOTION_EVENT_ACTION_DOWN: |
| 141 | case AMOTION_EVENT_ACTION_UP: |
| 142 | case AMOTION_EVENT_ACTION_CANCEL: |
| 143 | case AMOTION_EVENT_ACTION_MOVE: |
| 144 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 145 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 146 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 147 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 148 | case AMOTION_EVENT_ACTION_SCROLL: |
| 149 | return true; |
| 150 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 151 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 152 | int32_t index = getMotionEventActionPointerIndex(action); |
| 153 | return index >= 0 && index < pointerCount; |
| 154 | } |
| 155 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 156 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 157 | return actionButton != 0; |
| 158 | default: |
| 159 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 163 | static int64_t millis(std::chrono::nanoseconds t) { |
| 164 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 165 | } |
| 166 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 167 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 168 | const PointerProperties* pointerProperties) { |
| 169 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 171 | return false; |
| 172 | } |
| 173 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 174 | 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] | 175 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | BitSet32 pointerIdBits; |
| 179 | for (size_t i = 0; i < pointerCount; i++) { |
| 180 | int32_t id = pointerProperties[i].id; |
| 181 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 182 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 183 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | if (pointerIdBits.hasBit(id)) { |
| 187 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 188 | return false; |
| 189 | } |
| 190 | pointerIdBits.markBit(id); |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 195 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 197 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
| 201 | bool first = true; |
| 202 | Region::const_iterator cur = region.begin(); |
| 203 | Region::const_iterator const tail = region.end(); |
| 204 | while (cur != tail) { |
| 205 | if (first) { |
| 206 | first = false; |
| 207 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 208 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 210 | 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] | 211 | cur++; |
| 212 | } |
| 213 | } |
| 214 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Find the entry in std::unordered_map by key, and return it. |
| 217 | * If the entry is not found, return a default constructed entry. |
| 218 | * |
| 219 | * Useful when the entries are vectors, since an empty vector will be returned |
| 220 | * if the entry is not found. |
| 221 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 222 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 223 | template <typename K, typename V> |
| 224 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 225 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 226 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Find the entry in std::unordered_map by value, and remove it. |
| 231 | * If more than one entry has the same value, then all matching |
| 232 | * key-value pairs will be removed. |
| 233 | * |
| 234 | * Return true if at least one value has been removed. |
| 235 | */ |
| 236 | template <typename K, typename V> |
| 237 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 238 | bool removed = false; |
| 239 | for (auto it = map.begin(); it != map.end();) { |
| 240 | if (it->second == value) { |
| 241 | it = map.erase(it); |
| 242 | removed = true; |
| 243 | } else { |
| 244 | it++; |
| 245 | } |
| 246 | } |
| 247 | return removed; |
| 248 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 250 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 251 | if (first == second) { |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | if (first == nullptr || second == nullptr) { |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return first->getToken() == second->getToken(); |
| 260 | } |
| 261 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 262 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 263 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 264 | } |
| 265 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 266 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 267 | EventEntry* eventEntry, |
| 268 | int32_t inputTargetFlags) { |
| 269 | if (inputTarget.useDefaultPointerInfo()) { |
| 270 | const PointerInfo& pointerInfo = inputTarget.getDefaultPointerInfo(); |
| 271 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref |
| 272 | inputTargetFlags, pointerInfo.xOffset, |
| 273 | pointerInfo.yOffset, inputTarget.globalScaleFactor, |
| 274 | pointerInfo.windowXScale, pointerInfo.windowYScale); |
| 275 | } |
| 276 | |
| 277 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 278 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 279 | |
| 280 | PointerCoords pointerCoords[motionEntry.pointerCount]; |
| 281 | |
| 282 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 283 | // as long as all other pointers are normalized to the same value and the final DispatchEntry |
| 284 | // uses the offset and scale for the normalized pointer. |
| 285 | const PointerInfo& firstPointerInfo = |
| 286 | inputTarget.pointerInfos[inputTarget.pointerIds.firstMarkedBit()]; |
| 287 | |
| 288 | // Iterate through all pointers in the event to normalize against the first. |
| 289 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 290 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 291 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 292 | const PointerInfo& currPointerInfo = inputTarget.pointerInfos[pointerId]; |
| 293 | |
| 294 | // The scale factor is the ratio of the current pointers scale to the normalized scale. |
| 295 | float scaleXDiff = currPointerInfo.windowXScale / firstPointerInfo.windowXScale; |
| 296 | float scaleYDiff = currPointerInfo.windowYScale / firstPointerInfo.windowYScale; |
| 297 | |
| 298 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
| 299 | // First apply the current pointers offset to set the window at 0,0 |
| 300 | pointerCoords[pointerIndex].applyOffset(currPointerInfo.xOffset, currPointerInfo.yOffset); |
| 301 | // Next scale the coordinates. |
| 302 | pointerCoords[pointerIndex].scale(1, scaleXDiff, scaleYDiff); |
| 303 | // Lastly, offset the coordinates so they're in the normalized pointer's frame. |
| 304 | pointerCoords[pointerIndex].applyOffset(-firstPointerInfo.xOffset, |
| 305 | -firstPointerInfo.yOffset); |
| 306 | } |
| 307 | |
| 308 | MotionEntry* combinedMotionEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 309 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 310 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, |
| 311 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, |
| 312 | motionEntry.metaState, motionEntry.buttonState, |
| 313 | motionEntry.classification, motionEntry.edgeFlags, |
| 314 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 315 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 316 | motionEntry.downTime, motionEntry.pointerCount, |
| 317 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, |
| 318 | 0 /* yOffset */); |
| 319 | |
| 320 | if (motionEntry.injectionState) { |
| 321 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 322 | combinedMotionEntry->injectionState->refCount += 1; |
| 323 | } |
| 324 | |
| 325 | std::unique_ptr<DispatchEntry> dispatchEntry = |
| 326 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref |
| 327 | inputTargetFlags, firstPointerInfo.xOffset, |
| 328 | firstPointerInfo.yOffset, inputTarget.globalScaleFactor, |
| 329 | firstPointerInfo.windowXScale, |
| 330 | firstPointerInfo.windowYScale); |
| 331 | combinedMotionEntry->release(); |
| 332 | return dispatchEntry; |
| 333 | } |
| 334 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 335 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 336 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 337 | float yOffset = 0) { |
| 338 | if (monitors.empty()) { |
| 339 | return; |
| 340 | } |
| 341 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 342 | for (const Monitor& monitor : monitors) { |
| 343 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 344 | } |
| 345 | } |
| 346 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 347 | static std::array<uint8_t, 128> getRandomKey() { |
| 348 | std::array<uint8_t, 128> key; |
| 349 | if (RAND_bytes(key.data(), key.size()) != 1) { |
| 350 | LOG_ALWAYS_FATAL("Can't generate HMAC key"); |
| 351 | } |
| 352 | return key; |
| 353 | } |
| 354 | |
| 355 | // --- HmacKeyManager --- |
| 356 | |
| 357 | HmacKeyManager::HmacKeyManager() : mHmacKey(getRandomKey()) {} |
| 358 | |
| 359 | std::array<uint8_t, 32> HmacKeyManager::sign(const VerifiedInputEvent& event) const { |
| 360 | size_t size; |
| 361 | switch (event.type) { |
| 362 | case VerifiedInputEvent::Type::KEY: { |
| 363 | size = sizeof(VerifiedKeyEvent); |
| 364 | break; |
| 365 | } |
| 366 | case VerifiedInputEvent::Type::MOTION: { |
| 367 | size = sizeof(VerifiedMotionEvent); |
| 368 | break; |
| 369 | } |
| 370 | } |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 371 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 372 | return sign(start, size); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 375 | std::array<uint8_t, 32> HmacKeyManager::sign(const uint8_t* data, size_t size) const { |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 376 | // SHA256 always generates 32-bytes result |
| 377 | std::array<uint8_t, 32> hash; |
| 378 | unsigned int hashLen = 0; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 379 | uint8_t* result = |
| 380 | HMAC(EVP_sha256(), mHmacKey.data(), mHmacKey.size(), data, size, hash.data(), &hashLen); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 381 | if (result == nullptr) { |
| 382 | ALOGE("Could not sign the data using HMAC"); |
| 383 | return INVALID_HMAC; |
| 384 | } |
| 385 | |
| 386 | if (hashLen != hash.size()) { |
| 387 | ALOGE("HMAC-SHA256 has unexpected length"); |
| 388 | return INVALID_HMAC; |
| 389 | } |
| 390 | |
| 391 | return hash; |
| 392 | } |
| 393 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 394 | // --- InputDispatcher --- |
| 395 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 396 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 397 | : mPolicy(policy), |
| 398 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 399 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 400 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 401 | mAppSwitchSawKeyDown(false), |
| 402 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 403 | mNextUnblockedEvent(nullptr), |
| 404 | mDispatchEnabled(false), |
| 405 | mDispatchFrozen(false), |
| 406 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 407 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 408 | // To avoid leaking stack in case that call never comes, and for tests, |
| 409 | // initialize it here anyways. |
| 410 | mInTouchMode(true), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 411 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 412 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 413 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 414 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 415 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 416 | |
| 417 | policy->getDispatcherConfiguration(&mConfig); |
| 418 | } |
| 419 | |
| 420 | InputDispatcher::~InputDispatcher() { |
| 421 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 422 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 423 | |
| 424 | resetKeyRepeatLocked(); |
| 425 | releasePendingEventLocked(); |
| 426 | drainInboundQueueLocked(); |
| 427 | } |
| 428 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 429 | while (!mConnectionsByFd.empty()) { |
| 430 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
| 431 | unregisterInputChannel(connection->inputChannel); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 435 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 436 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 437 | return ALREADY_EXISTS; |
| 438 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 439 | mThread = std::make_unique<InputThread>( |
| 440 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 441 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 445 | if (mThread && mThread->isCallingThread()) { |
| 446 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 447 | return INVALID_OPERATION; |
| 448 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 449 | mThread.reset(); |
| 450 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | void InputDispatcher::dispatchOnce() { |
| 454 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 455 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 456 | std::scoped_lock _l(mLock); |
| 457 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | |
| 459 | // Run a dispatch loop if there are no pending commands. |
| 460 | // The dispatch loop might enqueue commands to run afterwards. |
| 461 | if (!haveCommandsLocked()) { |
| 462 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 463 | } |
| 464 | |
| 465 | // Run all pending commands if there are any. |
| 466 | // If any commands were run then force the next poll to wake up immediately. |
| 467 | if (runCommandsLockedInterruptible()) { |
| 468 | nextWakeupTime = LONG_LONG_MIN; |
| 469 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 470 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 471 | // If we are still waiting for ack on some events, |
| 472 | // we might have to wake up earlier to check if an app is anr'ing. |
| 473 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 474 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 475 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 476 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 477 | // pending or queued events |
| 478 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 479 | mDispatcherEnteredIdle.notify_all(); |
| 480 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 481 | } // release lock |
| 482 | |
| 483 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 484 | nsecs_t currentTime = now(); |
| 485 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 486 | mLooper->pollOnce(timeoutMillis); |
| 487 | } |
| 488 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Check if any of the connections' wait queues have events that are too old. |
| 491 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 492 | * Return the time at which we should wake up next. |
| 493 | */ |
| 494 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 495 | const nsecs_t currentTime = now(); |
| 496 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 497 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 498 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 499 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
| 500 | onAnrLocked(mAwaitedFocusedApplication); |
| 501 | mAwaitedFocusedApplication.clear(); |
| 502 | return LONG_LONG_MIN; |
| 503 | } else { |
| 504 | // Keep waiting |
| 505 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); |
| 506 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); |
| 507 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Check if any connection ANRs are due |
| 512 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 513 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 514 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 515 | } |
| 516 | |
| 517 | // If we reached here, we have an unresponsive connection. |
| 518 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 519 | if (connection == nullptr) { |
| 520 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 521 | return nextAnrCheck; |
| 522 | } |
| 523 | connection->responsive = false; |
| 524 | // Stop waking up for this unresponsive connection |
| 525 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
| 526 | onAnrLocked(connection); |
| 527 | return LONG_LONG_MIN; |
| 528 | } |
| 529 | |
| 530 | nsecs_t InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
| 531 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 532 | if (window != nullptr) { |
| 533 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT).count(); |
| 534 | } |
| 535 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT.count(); |
| 536 | } |
| 537 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 538 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 539 | nsecs_t currentTime = now(); |
| 540 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 541 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 542 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 543 | // repeat if the device is just coming out of sleep. |
| 544 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 545 | resetKeyRepeatLocked(); |
| 546 | } |
| 547 | |
| 548 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 549 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 550 | if (DEBUG_FOCUS) { |
| 551 | ALOGD("Dispatch frozen. Waiting some more."); |
| 552 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
| 556 | // Optimize latency of app switches. |
| 557 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 558 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 559 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 560 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 561 | *nextWakeupTime = mAppSwitchDueTime; |
| 562 | } |
| 563 | |
| 564 | // Ready to start a new event. |
| 565 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 566 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 567 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | if (isAppSwitchDue) { |
| 569 | // The inbound queue is empty so the app switch key we were waiting |
| 570 | // for will never arrive. Stop waiting for it. |
| 571 | resetPendingAppSwitchLocked(false); |
| 572 | isAppSwitchDue = false; |
| 573 | } |
| 574 | |
| 575 | // Synthesize a key repeat if appropriate. |
| 576 | if (mKeyRepeatState.lastKeyEntry) { |
| 577 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 578 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 579 | } else { |
| 580 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 581 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Nothing to do if there is no pending event. |
| 587 | if (!mPendingEvent) { |
| 588 | return; |
| 589 | } |
| 590 | } else { |
| 591 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 592 | mPendingEvent = mInboundQueue.front(); |
| 593 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | traceInboundQueueLengthLocked(); |
| 595 | } |
| 596 | |
| 597 | // Poke user activity for this event. |
| 598 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 599 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Now we have an event to dispatch. |
| 604 | // 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] | 605 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 607 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 609 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 611 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 615 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 619 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 620 | ConfigurationChangedEntry* typedEntry = |
| 621 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 622 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 623 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 624 | break; |
| 625 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 627 | case EventEntry::Type::DEVICE_RESET: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 628 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); |
| 629 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 630 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 631 | break; |
| 632 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 633 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 634 | case EventEntry::Type::FOCUS: { |
| 635 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); |
| 636 | dispatchFocusLocked(currentTime, typedEntry); |
| 637 | done = true; |
| 638 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 639 | break; |
| 640 | } |
| 641 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 642 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 643 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 644 | if (isAppSwitchDue) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 645 | if (isAppSwitchKeyEvent(*typedEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 646 | resetPendingAppSwitchLocked(true); |
| 647 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 648 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 649 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 652 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 653 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 654 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 655 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 656 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 657 | } |
| 658 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 659 | break; |
| 660 | } |
| 661 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 662 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 663 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 664 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 665 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 667 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 668 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 669 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 670 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 671 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 672 | } |
| 673 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 674 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 679 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 680 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 682 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | |
| 684 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 685 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 689 | /** |
| 690 | * Return true if the events preceding this incoming motion event should be dropped |
| 691 | * Return false otherwise (the default behaviour) |
| 692 | */ |
| 693 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 694 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 695 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 696 | |
| 697 | // Optimize case where the current application is unresponsive and the user |
| 698 | // decides to touch a window in a different application. |
| 699 | // If the application takes too long to catch up then we drop all events preceding |
| 700 | // the touch into the other window. |
| 701 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 702 | int32_t displayId = motionEntry.displayId; |
| 703 | int32_t x = static_cast<int32_t>( |
| 704 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 705 | int32_t y = static_cast<int32_t>( |
| 706 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 707 | sp<InputWindowHandle> touchedWindowHandle = |
| 708 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 709 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 710 | touchedWindowHandle->getApplicationToken() != |
| 711 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 712 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 713 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 714 | "for %s", |
| 715 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 716 | return true; |
| 717 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 718 | |
| 719 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 720 | std::vector<TouchedMonitor> gestureMonitors = |
| 721 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 722 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 723 | sp<Connection> connection = |
| 724 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 725 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 726 | // This monitor could take more input. Drop all events preceding this |
| 727 | // event, so that gesture monitor could get a chance to receive the stream |
| 728 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 729 | "responsive gesture monitor that may handle the event", |
| 730 | mAwaitedFocusedApplication->getName().c_str()); |
| 731 | return true; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 737 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 738 | // events to be processed before dispatching the key event. This is because these motion events |
| 739 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 740 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 741 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 742 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 743 | "just send the pending key event to the focused window."); |
| 744 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 745 | } |
| 746 | return false; |
| 747 | } |
| 748 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 750 | bool needWake = mInboundQueue.empty(); |
| 751 | mInboundQueue.push_back(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | traceInboundQueueLengthLocked(); |
| 753 | |
| 754 | switch (entry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 755 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 756 | // Optimize app switch latency. |
| 757 | // If the application takes too long to catch up then we drop all events preceding |
| 758 | // the app switch key. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 759 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 761 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 762 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 763 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 764 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 766 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 768 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 769 | mAppSwitchSawKeyDown = false; |
| 770 | needWake = true; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | break; |
| 775 | } |
| 776 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 777 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 778 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { |
| 779 | mNextUnblockedEvent = entry; |
| 780 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 782 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 784 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 785 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 786 | break; |
| 787 | } |
| 788 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 789 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 790 | // nothing to do |
| 791 | break; |
| 792 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return needWake; |
| 796 | } |
| 797 | |
| 798 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 799 | entry->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 800 | mRecentQueue.push_back(entry); |
| 801 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
| 802 | mRecentQueue.front()->release(); |
| 803 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 807 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 808 | int32_t y, TouchState* touchState, |
| 809 | bool addOutsideTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 810 | bool addPortalWindows) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 811 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 812 | LOG_ALWAYS_FATAL( |
| 813 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 814 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | // Traverse windows from front to back to find touched window. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 816 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 817 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 818 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 819 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 820 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | |
| 822 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 823 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 824 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 825 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 826 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 827 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 828 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 829 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 830 | if (addPortalWindows) { |
| 831 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 832 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 833 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 834 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 835 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 837 | // Found window. |
| 838 | return windowHandle; |
| 839 | } |
| 840 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 841 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 842 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 843 | touchState->addOrUpdateWindow(windowHandle, |
| 844 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 845 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 846 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | } |
| 849 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 850 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 853 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 854 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 855 | std::vector<TouchedMonitor> touchedMonitors; |
| 856 | |
| 857 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 858 | addGestureMonitors(monitors, touchedMonitors); |
| 859 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 860 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 861 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 862 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 863 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 864 | } |
| 865 | return touchedMonitors; |
| 866 | } |
| 867 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 868 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 869 | const char* reason; |
| 870 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 873 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 874 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | reason = "inbound event was dropped because the policy consumed it"; |
| 876 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | case DropReason::DISABLED: |
| 878 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 879 | ALOGI("Dropped event because input dispatch is disabled."); |
| 880 | } |
| 881 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 882 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 883 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 884 | ALOGI("Dropped event because of pending overdue app switch."); |
| 885 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 886 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 887 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | ALOGI("Dropped event because the current application is not responding and the user " |
| 889 | "has started interacting with a different application."); |
| 890 | reason = "inbound event was dropped because the current application is not responding " |
| 891 | "and the user has started interacting with a different application"; |
| 892 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 893 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 894 | ALOGI("Dropped event because it is stale."); |
| 895 | reason = "inbound event was dropped because it is stale"; |
| 896 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 897 | case DropReason::NOT_DROPPED: { |
| 898 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 899 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 903 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 904 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 906 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 907 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 909 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 910 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 911 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 912 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 913 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 914 | } else { |
| 915 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 916 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 917 | } |
| 918 | break; |
| 919 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 920 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 921 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 922 | case EventEntry::Type::DEVICE_RESET: { |
| 923 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); |
| 924 | break; |
| 925 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 929 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 930 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 931 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 934 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 935 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 936 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 937 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 941 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 942 | } |
| 943 | |
| 944 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 945 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 946 | |
| 947 | #if DEBUG_APP_SWITCH |
| 948 | if (handled) { |
| 949 | ALOGD("App switch has arrived."); |
| 950 | } else { |
| 951 | ALOGD("App switch was abandoned."); |
| 952 | } |
| 953 | #endif |
| 954 | } |
| 955 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 957 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 961 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
| 965 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 966 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 967 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 968 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 969 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 970 | |
| 971 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 972 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 976 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 977 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 981 | while (!mInboundQueue.empty()) { |
| 982 | EventEntry* entry = mInboundQueue.front(); |
| 983 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 984 | releaseInboundEventLocked(entry); |
| 985 | } |
| 986 | traceInboundQueueLengthLocked(); |
| 987 | } |
| 988 | |
| 989 | void InputDispatcher::releasePendingEventLocked() { |
| 990 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 991 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 992 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
| 996 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 997 | InjectionState* injectionState = entry->injectionState; |
| 998 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 999 | #if DEBUG_DISPATCH_CYCLE |
| 1000 | ALOGD("Injected inbound event was dropped."); |
| 1001 | #endif |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1002 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | } |
| 1004 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1005 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1006 | } |
| 1007 | addRecentEventLocked(entry); |
| 1008 | entry->release(); |
| 1009 | } |
| 1010 | |
| 1011 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1012 | if (mKeyRepeatState.lastKeyEntry) { |
| 1013 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1014 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1018 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1019 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 1020 | |
| 1021 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1022 | uint32_t policyFlags = entry->policyFlags & |
| 1023 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1024 | if (entry->refCount == 1) { |
| 1025 | entry->recycle(); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1026 | entry->id = mIdGenerator.nextId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1027 | entry->eventTime = currentTime; |
| 1028 | entry->policyFlags = policyFlags; |
| 1029 | entry->repeatCount += 1; |
| 1030 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1031 | KeyEntry* newEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1032 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1033 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1034 | entry->keyCode, entry->scanCode, entry->metaState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1035 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1036 | |
| 1037 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 1038 | entry->release(); |
| 1039 | |
| 1040 | entry = newEntry; |
| 1041 | } |
| 1042 | entry->syntheticRepeat = true; |
| 1043 | |
| 1044 | // Increment reference count since we keep a reference to the event in |
| 1045 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 1046 | entry->refCount += 1; |
| 1047 | |
| 1048 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 1049 | return entry; |
| 1050 | } |
| 1051 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1052 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
| 1053 | ConfigurationChangedEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1054 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1055 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | #endif |
| 1057 | |
| 1058 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1059 | resetKeyRepeatLocked(); |
| 1060 | |
| 1061 | // 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] | 1062 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1063 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | commandEntry->eventTime = entry->eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1065 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
| 1068 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1069 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1070 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1071 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1072 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1073 | #endif |
| 1074 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1075 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1076 | options.deviceId = entry->deviceId; |
| 1077 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1081 | void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1082 | if (mPendingEvent != nullptr) { |
| 1083 | // Move the pending event to the front of the queue. This will give the chance |
| 1084 | // for the pending event to get dispatched to the newly focused window |
| 1085 | mInboundQueue.push_front(mPendingEvent); |
| 1086 | mPendingEvent = nullptr; |
| 1087 | } |
| 1088 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1089 | FocusEntry* focusEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1090 | new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1091 | |
| 1092 | // This event should go to the front of the queue, but behind all other focus events |
| 1093 | // Find the last focus event, and insert right after it |
| 1094 | std::deque<EventEntry*>::reverse_iterator it = |
| 1095 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1096 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); |
| 1097 | |
| 1098 | // Maintain the order of focus events. Insert the entry after all other focus events. |
| 1099 | mInboundQueue.insert(it.base(), focusEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
| 1103 | sp<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1104 | if (channel == nullptr) { |
| 1105 | return; // Window has gone away |
| 1106 | } |
| 1107 | InputTarget target; |
| 1108 | target.inputChannel = channel; |
| 1109 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1110 | entry->dispatchInProgress = true; |
Selim Cinek | 3d989c2 | 2020-06-17 21:42:12 +0000 | [diff] [blame] | 1111 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1112 | dispatchEventLocked(currentTime, entry, {target}); |
| 1113 | } |
| 1114 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1115 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1116 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1117 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1118 | if (!entry->dispatchInProgress) { |
| 1119 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1120 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1121 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1122 | if (mKeyRepeatState.lastKeyEntry && |
| 1123 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1124 | // We have seen two identical key downs in a row which indicates that the device |
| 1125 | // driver is automatically generating key repeats itself. We take note of the |
| 1126 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1127 | // we will not need to synthesize key repeats ourselves. |
| 1128 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1129 | resetKeyRepeatLocked(); |
| 1130 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1131 | } else { |
| 1132 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1133 | resetKeyRepeatLocked(); |
| 1134 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1135 | } |
| 1136 | mKeyRepeatState.lastKeyEntry = entry; |
| 1137 | entry->refCount += 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1138 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | resetKeyRepeatLocked(); |
| 1140 | } |
| 1141 | |
| 1142 | if (entry->repeatCount == 1) { |
| 1143 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1144 | } else { |
| 1145 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1146 | } |
| 1147 | |
| 1148 | entry->dispatchInProgress = true; |
| 1149 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1150 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | // Handle case where the policy asked us to try again later last time. |
| 1154 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1155 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1156 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1157 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1158 | } |
| 1159 | return false; // wait until next wakeup |
| 1160 | } |
| 1161 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1162 | entry->interceptKeyWakeupTime = 0; |
| 1163 | } |
| 1164 | |
| 1165 | // Give the policy a chance to intercept the key. |
| 1166 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1167 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1168 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1169 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1170 | sp<InputWindowHandle> focusedWindowHandle = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1171 | getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(*entry)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1172 | if (focusedWindowHandle != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1173 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowHandle->getToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | } |
| 1175 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1176 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | entry->refCount += 1; |
| 1178 | return false; // wait for the command to run |
| 1179 | } else { |
| 1180 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1181 | } |
| 1182 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1183 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1184 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1189 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1190 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1191 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1192 | : INPUT_EVENT_INJECTION_FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1193 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1198 | std::vector<InputTarget> inputTargets; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1199 | int32_t injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1200 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1201 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1205 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1210 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1211 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | |
| 1213 | // Dispatch the key. |
| 1214 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1218 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1220 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1221 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1222 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1223 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1224 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1225 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | #endif |
| 1227 | } |
| 1228 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1229 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1230 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1231 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1233 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1234 | entry->dispatchInProgress = true; |
| 1235 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1236 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1240 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1241 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1242 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1243 | : INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | return true; |
| 1245 | } |
| 1246 | |
| 1247 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1248 | |
| 1249 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1250 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1251 | |
| 1252 | bool conflictingPointerActions = false; |
| 1253 | int32_t injectionResult; |
| 1254 | if (isPointerEvent) { |
| 1255 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1256 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1257 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1258 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | } else { |
| 1260 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1261 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1262 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | } |
| 1264 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1268 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1269 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| 1270 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1271 | return true; |
| 1272 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1273 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1274 | CancelationOptions::Mode mode(isPointerEvent |
| 1275 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1276 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1277 | CancelationOptions options(mode, "input event injection failed"); |
| 1278 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1282 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1283 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1285 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1286 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1287 | mTouchStatesByDisplay.find(entry->displayId); |
| 1288 | if (it != mTouchStatesByDisplay.end()) { |
| 1289 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1290 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1291 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1292 | // the corresponding displays as well. |
| 1293 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1294 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1295 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1296 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | // Dispatch the motion. |
| 1303 | if (conflictingPointerActions) { |
| 1304 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1305 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1306 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1307 | } |
| 1308 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1312 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1313 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1314 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1315 | ", policyFlags=0x%x, " |
| 1316 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1317 | "metaState=0x%x, buttonState=0x%x," |
| 1318 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1319 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1320 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1321 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1323 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1324 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1325 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1326 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1327 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1328 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1329 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1330 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1331 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1332 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1333 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1334 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1335 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1336 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1337 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1338 | } |
| 1339 | #endif |
| 1340 | } |
| 1341 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1342 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1343 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1344 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1345 | #if DEBUG_DISPATCH_CYCLE |
| 1346 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1347 | #endif |
| 1348 | |
| 1349 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1350 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1351 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1353 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1354 | sp<Connection> connection = |
| 1355 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1356 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1357 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1358 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1359 | if (DEBUG_FOCUS) { |
| 1360 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1361 | "is no longer registered with the input dispatcher.", |
| 1362 | inputTarget.inputChannel->getName().c_str()); |
| 1363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1368 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1369 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1370 | // If the policy decides to close the app, we will get a channel removal event via |
| 1371 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1372 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1373 | // pile up. |
| 1374 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1375 | connection->inputChannel->getName().c_str()); |
| 1376 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1377 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1378 | "application not responding"); |
| 1379 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1383 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1384 | if (DEBUG_FOCUS) { |
| 1385 | ALOGD("Resetting ANR timeouts."); |
| 1386 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1387 | |
| 1388 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1389 | mNoFocusedWindowTimeoutTime = std::nullopt; |
| 1390 | mAwaitedFocusedApplication.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1393 | /** |
| 1394 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1395 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1396 | * Focused display is the display that the user most recently interacted with. |
| 1397 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1398 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1399 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1400 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1401 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1402 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1403 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1406 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1407 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1408 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1409 | break; |
| 1410 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1411 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1412 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1413 | case EventEntry::Type::DEVICE_RESET: { |
| 1414 | 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] | 1415 | return ADISPLAY_ID_NONE; |
| 1416 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1417 | } |
| 1418 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1419 | } |
| 1420 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1421 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1422 | const char* focusedWindowName) { |
| 1423 | if (mAnrTracker.empty()) { |
| 1424 | // already processed all events that we waited for |
| 1425 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1430 | // Start the timer |
| 1431 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " |
| 1432 | "focus to change", |
| 1433 | focusedWindowName); |
| 1434 | mKeyIsWaitingForEventsTimeout = currentTime + KEY_WAITING_FOR_EVENTS_TIMEOUT.count(); |
| 1435 | return true; |
| 1436 | } |
| 1437 | |
| 1438 | // We still have pending events, and already started the timer |
| 1439 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1440 | return true; // Still waiting |
| 1441 | } |
| 1442 | |
| 1443 | // Waited too long, and some connection still hasn't processed all motions |
| 1444 | // Just send the key to the focused window |
| 1445 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1446 | focusedWindowName); |
| 1447 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1451 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1452 | const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1453 | std::vector<InputTarget>& inputTargets, |
| 1454 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1455 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1456 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1457 | int32_t displayId = getTargetDisplayId(entry); |
| 1458 | sp<InputWindowHandle> focusedWindowHandle = |
| 1459 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 1460 | sp<InputApplicationHandle> focusedApplicationHandle = |
| 1461 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1462 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | // If there is no currently focused window and no focused application |
| 1464 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1465 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1466 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1467 | "display %" PRId32 ".", |
| 1468 | EventEntry::typeToString(entry.type), displayId); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1469 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1472 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1473 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1474 | // start interacting with another application via touch (app switch). This code can be removed |
| 1475 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1476 | // an app is expected to have a focused window. |
| 1477 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1478 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1479 | // We just discovered that there's no focused window. Start the ANR timer |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1480 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1481 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1482 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1483 | mAwaitedFocusedApplication = focusedApplicationHandle; |
| 1484 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1485 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1486 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1487 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
| 1488 | return INPUT_EVENT_INJECTION_PENDING; |
| 1489 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1490 | // Already raised ANR. Drop the event |
| 1491 | ALOGE("Dropping %s event because there is no focused window", |
| 1492 | EventEntry::typeToString(entry.type)); |
| 1493 | return INPUT_EVENT_INJECTION_FAILED; |
| 1494 | } else { |
| 1495 | // Still waiting for the focused window |
| 1496 | return INPUT_EVENT_INJECTION_PENDING; |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | // we have a valid, non-null focused window |
| 1501 | resetNoFocusedWindowTimeoutLocked(); |
| 1502 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1504 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1505 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1508 | if (focusedWindowHandle->getInfo()->paused) { |
| 1509 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
| 1510 | return INPUT_EVENT_INJECTION_PENDING; |
| 1511 | } |
| 1512 | |
| 1513 | // If the event is a key event, then we must wait for all previous events to |
| 1514 | // complete before delivering it because previous events may have the |
| 1515 | // side-effect of transferring focus to a different window and we want to |
| 1516 | // ensure that the following keys are sent to the new window. |
| 1517 | // |
| 1518 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1519 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1520 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1521 | // often anticipate pending UI changes when typing on a keyboard. |
| 1522 | // To obtain this behavior, we must serialize key events with respect to all |
| 1523 | // prior input events. |
| 1524 | if (entry.type == EventEntry::Type::KEY) { |
| 1525 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1526 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
| 1527 | return INPUT_EVENT_INJECTION_PENDING; |
| 1528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1532 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1533 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1534 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1535 | |
| 1536 | // Done. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1537 | return INPUT_EVENT_INJECTION_SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1540 | /** |
| 1541 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1542 | * that are currently unresponsive. |
| 1543 | */ |
| 1544 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1545 | const std::vector<TouchedMonitor>& monitors) const { |
| 1546 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1547 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1548 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1549 | sp<Connection> connection = getConnectionLocked( |
| 1550 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1551 | if (connection == nullptr) { |
| 1552 | ALOGE("Could not find connection for monitor %s", |
| 1553 | monitor.monitor.inputChannel->getName().c_str()); |
| 1554 | return false; |
| 1555 | } |
| 1556 | if (!connection->responsive) { |
| 1557 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1558 | connection->inputChannel->getName().c_str()); |
| 1559 | return false; |
| 1560 | } |
| 1561 | return true; |
| 1562 | }); |
| 1563 | return responsiveMonitors; |
| 1564 | } |
| 1565 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1566 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1567 | const MotionEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1568 | std::vector<InputTarget>& inputTargets, |
| 1569 | nsecs_t* nextWakeupTime, |
| 1570 | bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1571 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1572 | enum InjectionPermission { |
| 1573 | INJECTION_PERMISSION_UNKNOWN, |
| 1574 | INJECTION_PERMISSION_GRANTED, |
| 1575 | INJECTION_PERMISSION_DENIED |
| 1576 | }; |
| 1577 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 | // For security reasons, we defer updating the touch state until we are sure that |
| 1579 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1580 | int32_t displayId = entry.displayId; |
| 1581 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1582 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1583 | |
| 1584 | // Update the touch state as needed based on the properties of the touch event. |
| 1585 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1586 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1587 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1588 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1589 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1590 | // Copy current touch state into tempTouchState. |
| 1591 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1592 | // If no state for the specified display exists, then our initial state will be empty. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1593 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1594 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1595 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1596 | mTouchStatesByDisplay.find(displayId); |
| 1597 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1598 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1599 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1600 | } |
| 1601 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1602 | bool isSplit = tempTouchState.split; |
| 1603 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1604 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1605 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1606 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1607 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1608 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1609 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1610 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1611 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1612 | bool wrongDevice = false; |
| 1613 | if (newGesture) { |
| 1614 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1615 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1616 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1617 | "in display %" PRId32, |
| 1618 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1619 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1620 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1621 | switchedDevice = false; |
| 1622 | wrongDevice = true; |
| 1623 | goto Failed; |
| 1624 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1625 | tempTouchState.reset(); |
| 1626 | tempTouchState.down = down; |
| 1627 | tempTouchState.deviceId = entry.deviceId; |
| 1628 | tempTouchState.source = entry.source; |
| 1629 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1631 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1632 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1633 | "in display %" PRId32, |
| 1634 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1635 | // TODO: test multiple simultaneous input streams. |
| 1636 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1637 | switchedDevice = false; |
| 1638 | wrongDevice = true; |
| 1639 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1643 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1644 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1645 | int32_t x; |
| 1646 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1647 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1648 | // Always dispatch mouse events to cursor position. |
| 1649 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1650 | x = int32_t(entry.xCursorPosition); |
| 1651 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1652 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1653 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1654 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1655 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1656 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1657 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1658 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1659 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1660 | |
| 1661 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1662 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1663 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1665 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1666 | if (newTouchedWindowHandle != nullptr && |
| 1667 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1668 | // New window supports splitting, but we should never split mouse events. |
| 1669 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1670 | } else if (isSplit) { |
| 1671 | // New window does not support splitting but we have already split events. |
| 1672 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1673 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1677 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | // Try to assign the pointer to the first foreground window we find, if there is one. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1679 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1682 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1683 | ALOGI("Not sending touch event to %s because it is paused", |
| 1684 | newTouchedWindowHandle->getName().c_str()); |
| 1685 | newTouchedWindowHandle = nullptr; |
| 1686 | } |
| 1687 | |
| 1688 | if (newTouchedWindowHandle != nullptr) { |
| 1689 | sp<Connection> connection = getConnectionLocked(newTouchedWindowHandle->getToken()); |
| 1690 | if (connection == nullptr) { |
| 1691 | ALOGI("Could not find connection for %s", |
| 1692 | newTouchedWindowHandle->getName().c_str()); |
| 1693 | newTouchedWindowHandle = nullptr; |
| 1694 | } else if (!connection->responsive) { |
| 1695 | // don't send the new touch to an unresponsive window |
| 1696 | ALOGW("Unresponsive window %s will not get the new gesture at %" PRIu64, |
| 1697 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1698 | newTouchedWindowHandle = nullptr; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1703 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1704 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1705 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1706 | 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] | 1707 | "(%d, %d) in display %" PRId32 ".", |
| 1708 | x, y, displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1709 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1710 | goto Failed; |
| 1711 | } |
| 1712 | |
| 1713 | if (newTouchedWindowHandle != nullptr) { |
| 1714 | // Set target flags. |
| 1715 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1716 | if (isSplit) { |
| 1717 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1718 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1719 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1720 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1721 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1722 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1723 | } |
| 1724 | |
| 1725 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1726 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 1727 | newHoverWindowHandle = nullptr; |
| 1728 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1729 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | // Update the temporary touch state. |
| 1733 | BitSet32 pointerIds; |
| 1734 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1735 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1736 | pointerIds.markBit(pointerId); |
| 1737 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1738 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1741 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1742 | } else { |
| 1743 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1744 | |
| 1745 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1746 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1747 | if (DEBUG_FOCUS) { |
| 1748 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1749 | "dropped the pointer down event in display %" PRId32, |
| 1750 | displayId); |
| 1751 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1753 | goto Failed; |
| 1754 | } |
| 1755 | |
| 1756 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1757 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1758 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1759 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1760 | 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] | 1761 | |
| 1762 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1763 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1764 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1765 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1766 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1767 | if (DEBUG_FOCUS) { |
| 1768 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1769 | oldTouchedWindowHandle->getName().c_str(), |
| 1770 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1771 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1772 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1773 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1774 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1775 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1776 | |
| 1777 | // Make a slippery entrance into the new window. |
| 1778 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1779 | isSplit = true; |
| 1780 | } |
| 1781 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1782 | int32_t targetFlags = |
| 1783 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1784 | if (isSplit) { |
| 1785 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1786 | } |
| 1787 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1788 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1789 | } |
| 1790 | |
| 1791 | BitSet32 pointerIds; |
| 1792 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1793 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1795 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1801 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 1802 | // when dispatching it as is to newTouchedWindowHandle. |
| 1803 | if (mLastHoverWindowHandle != nullptr && |
| 1804 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 1805 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | #if DEBUG_HOVER |
| 1807 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1808 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1810 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1811 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1814 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 1815 | // when dispatching it as is to newTouchedWindowHandle. |
| 1816 | if (newHoverWindowHandle != nullptr && |
| 1817 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1818 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | #if DEBUG_HOVER |
| 1820 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1821 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1822 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1823 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1824 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1825 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | // Check permission to inject into all touched foreground windows and ensure there |
| 1830 | // is at least one touched foreground window. |
| 1831 | { |
| 1832 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1833 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1835 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1836 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1837 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1838 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1839 | goto Failed; |
| 1840 | } |
| 1841 | } |
| 1842 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1843 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1844 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1845 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1846 | "%" PRId32 " or gesture monitor to receive it.", |
| 1847 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1849 | goto Failed; |
| 1850 | } |
| 1851 | |
| 1852 | // Permission granted to injection into all touched foreground windows. |
| 1853 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1854 | } |
| 1855 | |
| 1856 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1857 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1858 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1859 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1860 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1861 | if (foregroundWindowHandle) { |
| 1862 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1863 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1864 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1865 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1866 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1867 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1868 | InputTarget::FLAG_ZERO_COORDS, |
| 1869 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1870 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1876 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1877 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1878 | // of the touch gesture. |
| 1879 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1880 | // engine only supports touch events. We would need to add a mechanism similar |
| 1881 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1882 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1883 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1884 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1885 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1886 | const std::vector<sp<InputWindowHandle>> windowHandles = |
| 1887 | getWindowHandlesLocked(displayId); |
| 1888 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1889 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1890 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 1891 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1892 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1893 | .addOrUpdateWindow(windowHandle, |
| 1894 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1895 | InputTarget:: |
| 1896 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1897 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1898 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | // Success! Output targets. |
| 1905 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1906 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1907 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1909 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1910 | } |
| 1911 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1912 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1913 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1914 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1917 | // Drop the outside or hover touch windows since we will not care about them |
| 1918 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1919 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1920 | |
| 1921 | Failed: |
| 1922 | // Check injection permission once and for all. |
| 1923 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1924 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1925 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1926 | } else { |
| 1927 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1928 | } |
| 1929 | } |
| 1930 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1931 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 1932 | return injectionResult; |
| 1933 | } |
| 1934 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1936 | if (!wrongDevice) { |
| 1937 | if (switchedDevice) { |
| 1938 | if (DEBUG_FOCUS) { |
| 1939 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1940 | } |
| 1941 | *outConflictingPointerActions = true; |
| 1942 | } |
| 1943 | |
| 1944 | if (isHoverAction) { |
| 1945 | // Started hovering, therefore no longer down. |
| 1946 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1947 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1948 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 1949 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1950 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1951 | *outConflictingPointerActions = true; |
| 1952 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1953 | tempTouchState.reset(); |
| 1954 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1955 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 1956 | tempTouchState.deviceId = entry.deviceId; |
| 1957 | tempTouchState.source = entry.source; |
| 1958 | tempTouchState.displayId = displayId; |
| 1959 | } |
| 1960 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 1961 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 1962 | // All pointers up or canceled. |
| 1963 | tempTouchState.reset(); |
| 1964 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1965 | // First pointer went down. |
| 1966 | if (oldState && oldState->down) { |
| 1967 | if (DEBUG_FOCUS) { |
| 1968 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1969 | } |
| 1970 | *outConflictingPointerActions = true; |
| 1971 | } |
| 1972 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1973 | // One pointer went up. |
| 1974 | if (isSplit) { |
| 1975 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1976 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1977 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1978 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 1979 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 1980 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1981 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1982 | if (touchedWindow.pointerIds.isEmpty()) { |
| 1983 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 1984 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1986 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1987 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1988 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1989 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1990 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1991 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1992 | // Save changes unless the action was scroll in which case the temporary touch |
| 1993 | // state was only valid for this one action. |
| 1994 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 1995 | if (tempTouchState.displayId >= 0) { |
| 1996 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 1997 | } else { |
| 1998 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2000 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2002 | // Update hover state. |
| 2003 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2006 | return injectionResult; |
| 2007 | } |
| 2008 | |
| 2009 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2010 | int32_t targetFlags, BitSet32 pointerIds, |
| 2011 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2012 | std::vector<InputTarget>::iterator it = |
| 2013 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2014 | [&windowHandle](const InputTarget& inputTarget) { |
| 2015 | return inputTarget.inputChannel->getConnectionToken() == |
| 2016 | windowHandle->getToken(); |
| 2017 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2018 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2019 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2020 | |
| 2021 | if (it == inputTargets.end()) { |
| 2022 | InputTarget inputTarget; |
| 2023 | sp<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2024 | if (inputChannel == nullptr) { |
| 2025 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2026 | return; |
| 2027 | } |
| 2028 | inputTarget.inputChannel = inputChannel; |
| 2029 | inputTarget.flags = targetFlags; |
| 2030 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2031 | inputTargets.push_back(inputTarget); |
| 2032 | it = inputTargets.end() - 1; |
| 2033 | } |
| 2034 | |
| 2035 | ALOG_ASSERT(it->flags == targetFlags); |
| 2036 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2037 | |
| 2038 | it->addPointers(pointerIds, -windowInfo->frameLeft, -windowInfo->frameTop, |
| 2039 | windowInfo->windowXScale, windowInfo->windowYScale); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2040 | } |
| 2041 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2042 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2043 | int32_t displayId, float xOffset, |
| 2044 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2045 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2046 | mGlobalMonitorsByDisplay.find(displayId); |
| 2047 | |
| 2048 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2049 | const std::vector<Monitor>& monitors = it->second; |
| 2050 | for (const Monitor& monitor : monitors) { |
| 2051 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2052 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2056 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2057 | float yOffset, |
| 2058 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2059 | InputTarget target; |
| 2060 | target.inputChannel = monitor.inputChannel; |
| 2061 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2062 | target.setDefaultPointerInfo(xOffset, yOffset, 1 /* windowXScale */, 1 /* windowYScale */); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2063 | inputTargets.push_back(target); |
| 2064 | } |
| 2065 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2066 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2067 | const InjectionState* injectionState) { |
| 2068 | if (injectionState && |
| 2069 | (windowHandle == nullptr || |
| 2070 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2071 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2072 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2073 | 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] | 2074 | "owned by uid %d", |
| 2075 | injectionState->injectorPid, injectionState->injectorUid, |
| 2076 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | } else { |
| 2078 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2079 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2080 | } |
| 2081 | return false; |
| 2082 | } |
| 2083 | return true; |
| 2084 | } |
| 2085 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2086 | /** |
| 2087 | * Indicate whether one window handle should be considered as obscuring |
| 2088 | * another window handle. We only check a few preconditions. Actually |
| 2089 | * checking the bounds is left to the caller. |
| 2090 | */ |
| 2091 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2092 | const sp<InputWindowHandle>& otherHandle) { |
| 2093 | // Compare by token so cloned layers aren't counted |
| 2094 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2095 | return false; |
| 2096 | } |
| 2097 | auto info = windowHandle->getInfo(); |
| 2098 | auto otherInfo = otherHandle->getInfo(); |
| 2099 | if (!otherInfo->visible) { |
| 2100 | return false; |
Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2101 | } else if (info->ownerPid == otherInfo->ownerPid) { |
| 2102 | // If ownerPid is the same we don't generate occlusion events as there |
| 2103 | // is no in-process security boundary. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2104 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2105 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2106 | return false; |
| 2107 | } else if (otherInfo->displayId != info->displayId) { |
| 2108 | return false; |
| 2109 | } |
| 2110 | return true; |
| 2111 | } |
| 2112 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2113 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2114 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2115 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2116 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 2117 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2118 | if (windowHandle == otherHandle) { |
| 2119 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2121 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2122 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2123 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | return true; |
| 2125 | } |
| 2126 | } |
| 2127 | return false; |
| 2128 | } |
| 2129 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2130 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2131 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2132 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2133 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2134 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2135 | if (windowHandle == otherHandle) { |
| 2136 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2137 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2138 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2139 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2140 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2141 | return true; |
| 2142 | } |
| 2143 | } |
| 2144 | return false; |
| 2145 | } |
| 2146 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2147 | std::string InputDispatcher::getApplicationWindowLabel( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2148 | const sp<InputApplicationHandle>& applicationHandle, |
| 2149 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2150 | if (applicationHandle != nullptr) { |
| 2151 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2152 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2153 | } else { |
| 2154 | return applicationHandle->getName(); |
| 2155 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2156 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2157 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2158 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2159 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2163 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2164 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2165 | // Focus events are passed to apps, but do not represent user activity. |
| 2166 | return; |
| 2167 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2168 | int32_t displayId = getTargetDisplayId(eventEntry); |
| 2169 | sp<InputWindowHandle> focusedWindowHandle = |
| 2170 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 2171 | if (focusedWindowHandle != nullptr) { |
| 2172 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 2173 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2174 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2175 | 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] | 2176 | #endif |
| 2177 | return; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2182 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2183 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2184 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2185 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2186 | return; |
| 2187 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2189 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2190 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2191 | } |
| 2192 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2194 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2195 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2196 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2197 | return; |
| 2198 | } |
| 2199 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2200 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2202 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2203 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2204 | case EventEntry::Type::DEVICE_RESET: { |
| 2205 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2206 | EventEntry::typeToString(eventEntry.type)); |
| 2207 | break; |
| 2208 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2209 | } |
| 2210 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2211 | std::unique_ptr<CommandEntry> commandEntry = |
| 2212 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2213 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2214 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2215 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2219 | const sp<Connection>& connection, |
| 2220 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2221 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2222 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2223 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2224 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2225 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2226 | ATRACE_NAME(message.c_str()); |
| 2227 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2228 | #if DEBUG_DISPATCH_CYCLE |
| 2229 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2230 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2231 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2232 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2233 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | #endif |
| 2235 | |
| 2236 | // Skip this event if the connection status is not normal. |
| 2237 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2238 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2239 | #if DEBUG_DISPATCH_CYCLE |
| 2240 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2241 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2242 | #endif |
| 2243 | return; |
| 2244 | } |
| 2245 | |
| 2246 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2247 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2248 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2249 | "Entry type %s should not have FLAG_SPLIT", |
| 2250 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2251 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2252 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2253 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2254 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2255 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | if (!splitMotionEntry) { |
| 2257 | return; // split event was dropped |
| 2258 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2259 | if (DEBUG_FOCUS) { |
| 2260 | ALOGD("channel '%s' ~ Split motion event.", |
| 2261 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2262 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2263 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2264 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2265 | splitMotionEntry->release(); |
| 2266 | return; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2271 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2272 | } |
| 2273 | |
| 2274 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2275 | const sp<Connection>& connection, |
| 2276 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2277 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2278 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2279 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2280 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2281 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2282 | ATRACE_NAME(message.c_str()); |
| 2283 | } |
| 2284 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2285 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2286 | |
| 2287 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2288 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2289 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2290 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2291 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2292 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2293 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2294 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2295 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2296 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2297 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2298 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2299 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2300 | |
| 2301 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2302 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | startDispatchCycleLocked(currentTime, connection); |
| 2304 | } |
| 2305 | } |
| 2306 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2307 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2308 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2309 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2310 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2311 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2312 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2313 | connection->getInputChannelName().c_str(), |
| 2314 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2315 | ATRACE_NAME(message.c_str()); |
| 2316 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2317 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2318 | if (!(inputTargetFlags & dispatchMode)) { |
| 2319 | return; |
| 2320 | } |
| 2321 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2322 | |
| 2323 | // This is a new event. |
| 2324 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2325 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2326 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2328 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2329 | // different EventEntry than what was passed in. |
| 2330 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2331 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2332 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2333 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2334 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2335 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2336 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2337 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2339 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2340 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2341 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2342 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2343 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2345 | return; // skip the inconsistent event |
| 2346 | } |
| 2347 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2348 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2349 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2350 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2351 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2352 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2353 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2354 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2355 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2356 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2357 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2358 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2359 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2360 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2361 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2362 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2363 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2364 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2365 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2366 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2367 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2368 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2369 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2370 | } |
| 2371 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2372 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2373 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2374 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2375 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2376 | "event", |
| 2377 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2378 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2379 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2380 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2381 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2382 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2383 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2384 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2385 | } |
| 2386 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2387 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2388 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2389 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2390 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2391 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2392 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2393 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2394 | "event", |
| 2395 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2396 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2397 | return; // skip the inconsistent event |
| 2398 | } |
| 2399 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2400 | dispatchEntry->resolvedEventId = |
| 2401 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2402 | ? mIdGenerator.nextId() |
| 2403 | : motionEntry.id; |
| 2404 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2405 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2406 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2407 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2408 | ATRACE_NAME(message.c_str()); |
| 2409 | } |
| 2410 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2411 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2412 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2413 | |
| 2414 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2415 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2416 | case EventEntry::Type::FOCUS: { |
| 2417 | break; |
| 2418 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2419 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2420 | case EventEntry::Type::DEVICE_RESET: { |
| 2421 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2422 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2423 | break; |
| 2424 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | // Remember that we are waiting for this dispatch to complete. |
| 2428 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2429 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2433 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2434 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2437 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2438 | const sp<IBinder>& newToken) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2439 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2440 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2441 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2442 | return; |
| 2443 | } |
| 2444 | |
| 2445 | sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); |
| 2446 | if (inputWindowHandle == nullptr) { |
| 2447 | return; |
| 2448 | } |
| 2449 | |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2450 | sp<InputWindowHandle> focusedWindowHandle = |
Tiger Huang | 0683fe7 | 2019-06-03 21:50:55 +0800 | [diff] [blame] | 2451 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2452 | |
| 2453 | bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; |
| 2454 | |
| 2455 | if (!hasFocusChanged) { |
| 2456 | return; |
| 2457 | } |
| 2458 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2459 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2460 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2461 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2462 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2466 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2467 | if (ATRACE_ENABLED()) { |
| 2468 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2469 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2470 | ATRACE_NAME(message.c_str()); |
| 2471 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2472 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2473 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2474 | #endif |
| 2475 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2476 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2477 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2478 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2479 | const nsecs_t timeout = |
| 2480 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
| 2481 | dispatchEntry->timeoutTime = currentTime + timeout; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2482 | |
| 2483 | // Publish the event. |
| 2484 | status_t status; |
| 2485 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2486 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2487 | case EventEntry::Type::KEY: { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2488 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2489 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2490 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2491 | // Publish the key event. |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2492 | status = |
| 2493 | connection->inputPublisher |
| 2494 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2495 | keyEntry->deviceId, keyEntry->source, |
| 2496 | keyEntry->displayId, std::move(hmac), |
| 2497 | dispatchEntry->resolvedAction, |
| 2498 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2499 | keyEntry->scanCode, keyEntry->metaState, |
| 2500 | keyEntry->repeatCount, keyEntry->downTime, |
| 2501 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2502 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2505 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2506 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2507 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2508 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2509 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2510 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2511 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 2512 | float xOffset = 0.0f, yOffset = 0.0f; |
| 2513 | float xScale = 1.0f, yScale = 1.0f; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2514 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2515 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2516 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2517 | xScale = dispatchEntry->windowXScale; |
| 2518 | yScale = dispatchEntry->windowYScale; |
| 2519 | xOffset = dispatchEntry->xOffset * xScale; |
| 2520 | yOffset = dispatchEntry->yOffset * yScale; |
| 2521 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2522 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2523 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2524 | // Don't apply window scale here since we don't want scale to affect raw |
| 2525 | // coordinates. The scale will be sent back to the client and applied |
| 2526 | // later when requesting relative coordinates. |
| 2527 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2528 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2529 | } |
| 2530 | usingCoords = scaledCoords; |
| 2531 | } |
| 2532 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2533 | // We don't want the dispatch target to know. |
| 2534 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2535 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2536 | scaledCoords[i].clear(); |
| 2537 | } |
| 2538 | usingCoords = scaledCoords; |
| 2539 | } |
| 2540 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2541 | |
| 2542 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2543 | |
| 2544 | // Publish the motion event. |
| 2545 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2546 | .publishMotionEvent(dispatchEntry->seq, |
| 2547 | dispatchEntry->resolvedEventId, |
| 2548 | motionEntry->deviceId, motionEntry->source, |
| 2549 | motionEntry->displayId, std::move(hmac), |
| 2550 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2551 | motionEntry->actionButton, |
| 2552 | dispatchEntry->resolvedFlags, |
| 2553 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2554 | motionEntry->buttonState, |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2555 | motionEntry->classification, xScale, yScale, |
| 2556 | xOffset, yOffset, motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2557 | motionEntry->yPrecision, |
| 2558 | motionEntry->xCursorPosition, |
| 2559 | motionEntry->yCursorPosition, |
| 2560 | motionEntry->downTime, motionEntry->eventTime, |
| 2561 | motionEntry->pointerCount, |
| 2562 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2563 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2564 | break; |
| 2565 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2566 | case EventEntry::Type::FOCUS: { |
| 2567 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2568 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2569 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2570 | focusEntry->hasFocus, |
| 2571 | mInTouchMode); |
| 2572 | break; |
| 2573 | } |
| 2574 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2575 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2576 | case EventEntry::Type::DEVICE_RESET: { |
| 2577 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2578 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2579 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2580 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | // Check the result. |
| 2584 | if (status) { |
| 2585 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2586 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2587 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2588 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2589 | "should be empty and we shouldn't have any problems writing an " |
| 2590 | "event to it, status=%d", |
| 2591 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2592 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2593 | } else { |
| 2594 | // Pipe is full and we are waiting for the app to finish process some events |
| 2595 | // before sending more events to it. |
| 2596 | #if DEBUG_DISPATCH_CYCLE |
| 2597 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2598 | "waiting for the application to catch up", |
| 2599 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2600 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2601 | } |
| 2602 | } else { |
| 2603 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2604 | "status=%d", |
| 2605 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2606 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2607 | } |
| 2608 | return; |
| 2609 | } |
| 2610 | |
| 2611 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2612 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2613 | connection->outboundQueue.end(), |
| 2614 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2615 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2616 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2617 | if (connection->responsive) { |
| 2618 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 2619 | connection->inputChannel->getConnectionToken()); |
| 2620 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2621 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2625 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2626 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 2627 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2628 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 2629 | // Only sign events up and down events as the purely move events |
| 2630 | // are tied to their up/down counterparts so signing would be redundant. |
| 2631 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 2632 | verifiedEvent.actionMasked = actionMasked; |
| 2633 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 2634 | return mHmacKeyManager.sign(verifiedEvent); |
| 2635 | } |
| 2636 | return INVALID_HMAC; |
| 2637 | } |
| 2638 | |
| 2639 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2640 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 2641 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 2642 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2643 | verifiedEvent.action = dispatchEntry.resolvedAction; |
| 2644 | return mHmacKeyManager.sign(verifiedEvent); |
| 2645 | } |
| 2646 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2647 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2648 | const sp<Connection>& connection, uint32_t seq, |
| 2649 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2650 | #if DEBUG_DISPATCH_CYCLE |
| 2651 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2652 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2653 | #endif |
| 2654 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2655 | if (connection->status == Connection::STATUS_BROKEN || |
| 2656 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2657 | return; |
| 2658 | } |
| 2659 | |
| 2660 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2661 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2662 | } |
| 2663 | |
| 2664 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2665 | const sp<Connection>& connection, |
| 2666 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2667 | #if DEBUG_DISPATCH_CYCLE |
| 2668 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2669 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2670 | #endif |
| 2671 | |
| 2672 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2673 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2674 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2675 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2676 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2677 | |
| 2678 | // The connection appears to be unrecoverably broken. |
| 2679 | // Ignore already broken or zombie connections. |
| 2680 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2681 | connection->status = Connection::STATUS_BROKEN; |
| 2682 | |
| 2683 | if (notify) { |
| 2684 | // Notify other system components. |
| 2685 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2686 | } |
| 2687 | } |
| 2688 | } |
| 2689 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2690 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2691 | while (!queue.empty()) { |
| 2692 | DispatchEntry* dispatchEntry = queue.front(); |
| 2693 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2694 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2695 | } |
| 2696 | } |
| 2697 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2698 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2699 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2700 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2701 | } |
| 2702 | delete dispatchEntry; |
| 2703 | } |
| 2704 | |
| 2705 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2706 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2707 | |
| 2708 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2709 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2711 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2712 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2713 | "fd=%d, events=0x%x", |
| 2714 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2715 | return 0; // remove the callback |
| 2716 | } |
| 2717 | |
| 2718 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2719 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2720 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2721 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2722 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2723 | "events=0x%x", |
| 2724 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2725 | return 1; |
| 2726 | } |
| 2727 | |
| 2728 | nsecs_t currentTime = now(); |
| 2729 | bool gotOne = false; |
| 2730 | status_t status; |
| 2731 | for (;;) { |
| 2732 | uint32_t seq; |
| 2733 | bool handled; |
| 2734 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2735 | if (status) { |
| 2736 | break; |
| 2737 | } |
| 2738 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2739 | gotOne = true; |
| 2740 | } |
| 2741 | if (gotOne) { |
| 2742 | d->runCommandsLockedInterruptible(); |
| 2743 | if (status == WOULD_BLOCK) { |
| 2744 | return 1; |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2749 | if (notify) { |
| 2750 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2751 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2752 | } |
| 2753 | } else { |
| 2754 | // Monitor channels are never explicitly unregistered. |
| 2755 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2756 | // about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2757 | const bool stillHaveWindowHandle = |
| 2758 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 2759 | nullptr; |
| 2760 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2761 | if (notify) { |
| 2762 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2763 | "events=0x%x", |
| 2764 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | // Unregister the channel. |
| 2769 | d->unregisterInputChannelLocked(connection->inputChannel, notify); |
| 2770 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2771 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | } |
| 2773 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2774 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2775 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2776 | for (const auto& pair : mConnectionsByFd) { |
| 2777 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2781 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2782 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2783 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 2784 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 2785 | } |
| 2786 | |
| 2787 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2788 | const CancelationOptions& options, |
| 2789 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 2790 | for (const auto& it : monitorsByDisplay) { |
| 2791 | const std::vector<Monitor>& monitors = it.second; |
| 2792 | for (const Monitor& monitor : monitors) { |
| 2793 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2794 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2795 | } |
| 2796 | } |
| 2797 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2798 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
| 2799 | const sp<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2800 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2801 | if (connection == nullptr) { |
| 2802 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2803 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2804 | |
| 2805 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2809 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2810 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2811 | return; |
| 2812 | } |
| 2813 | |
| 2814 | nsecs_t currentTime = now(); |
| 2815 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2816 | std::vector<EventEntry*> cancelationEvents = |
| 2817 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2818 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2819 | if (cancelationEvents.empty()) { |
| 2820 | return; |
| 2821 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2822 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2823 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 2824 | "with reality: %s, mode=%d.", |
| 2825 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 2826 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2827 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2828 | |
| 2829 | InputTarget target; |
| 2830 | sp<InputWindowHandle> windowHandle = |
| 2831 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2832 | if (windowHandle != nullptr) { |
| 2833 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2834 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, |
| 2835 | windowInfo->windowXScale, windowInfo->windowYScale); |
| 2836 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2837 | } |
| 2838 | target.inputChannel = connection->inputChannel; |
| 2839 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2840 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2841 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2842 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 2843 | switch (cancelationEventEntry->type) { |
| 2844 | case EventEntry::Type::KEY: { |
| 2845 | logOutboundKeyDetails("cancel - ", |
| 2846 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 2847 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2848 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2849 | case EventEntry::Type::MOTION: { |
| 2850 | logOutboundMotionDetails("cancel - ", |
| 2851 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 2852 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2853 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2854 | case EventEntry::Type::FOCUS: { |
| 2855 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 2856 | break; |
| 2857 | } |
| 2858 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2859 | case EventEntry::Type::DEVICE_RESET: { |
| 2860 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2861 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 2862 | break; |
| 2863 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2864 | } |
| 2865 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2866 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2867 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2868 | |
| 2869 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2870 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2871 | |
| 2872 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2875 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 2876 | const sp<Connection>& connection) { |
| 2877 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2878 | return; |
| 2879 | } |
| 2880 | |
| 2881 | nsecs_t currentTime = now(); |
| 2882 | |
| 2883 | std::vector<EventEntry*> downEvents = |
| 2884 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 2885 | |
| 2886 | if (downEvents.empty()) { |
| 2887 | return; |
| 2888 | } |
| 2889 | |
| 2890 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2891 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 2892 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 2893 | #endif |
| 2894 | |
| 2895 | InputTarget target; |
| 2896 | sp<InputWindowHandle> windowHandle = |
| 2897 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2898 | if (windowHandle != nullptr) { |
| 2899 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2900 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, |
| 2901 | windowInfo->windowXScale, windowInfo->windowYScale); |
| 2902 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2903 | } |
| 2904 | target.inputChannel = connection->inputChannel; |
| 2905 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2906 | |
| 2907 | for (EventEntry* downEventEntry : downEvents) { |
| 2908 | switch (downEventEntry->type) { |
| 2909 | case EventEntry::Type::MOTION: { |
| 2910 | logOutboundMotionDetails("down - ", |
| 2911 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 2912 | break; |
| 2913 | } |
| 2914 | |
| 2915 | case EventEntry::Type::KEY: |
| 2916 | case EventEntry::Type::FOCUS: |
| 2917 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2918 | case EventEntry::Type::DEVICE_RESET: { |
| 2919 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2920 | EventEntry::typeToString(downEventEntry->type)); |
| 2921 | break; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 2926 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2927 | |
| 2928 | downEventEntry->release(); |
| 2929 | } |
| 2930 | |
| 2931 | startDispatchCycleLocked(currentTime, connection); |
| 2932 | } |
| 2933 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2934 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2935 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2936 | ALOG_ASSERT(pointerIds.value != 0); |
| 2937 | |
| 2938 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 2939 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 2940 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 2941 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2942 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2943 | uint32_t splitPointerCount = 0; |
| 2944 | |
| 2945 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2946 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2947 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2948 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2949 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 2950 | if (pointerIds.hasBit(pointerId)) { |
| 2951 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 2952 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 2953 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2954 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2955 | splitPointerCount += 1; |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | if (splitPointerCount != pointerIds.count()) { |
| 2960 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 2961 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 2962 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 2963 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 2964 | // in this way. |
| 2965 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2966 | "we expected there to be %d pointers. This probably means we received " |
| 2967 | "a broken sequence of pointer ids from the input device.", |
| 2968 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2969 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2970 | } |
| 2971 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2972 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2974 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 2975 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2976 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 2977 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2978 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2979 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 2980 | if (pointerIds.hasBit(pointerId)) { |
| 2981 | if (pointerIds.count() == 1) { |
| 2982 | // The first/last pointer went down/up. |
| 2983 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2984 | ? AMOTION_EVENT_ACTION_DOWN |
| 2985 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2986 | } else { |
| 2987 | // A secondary pointer went down/up. |
| 2988 | uint32_t splitPointerIndex = 0; |
| 2989 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 2990 | splitPointerIndex += 1; |
| 2991 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2992 | action = maskedAction | |
| 2993 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2994 | } |
| 2995 | } else { |
| 2996 | // An unrelated pointer changed. |
| 2997 | action = AMOTION_EVENT_ACTION_MOVE; |
| 2998 | } |
| 2999 | } |
| 3000 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3001 | int32_t newId = mIdGenerator.nextId(); |
| 3002 | if (ATRACE_ENABLED()) { |
| 3003 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3004 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3005 | originalMotionEntry.id, newId); |
| 3006 | ATRACE_NAME(message.c_str()); |
| 3007 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3008 | MotionEntry* splitMotionEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3009 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 3010 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 3011 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3012 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 3013 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 3014 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 3015 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 3016 | originalMotionEntry.xCursorPosition, |
| 3017 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3018 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3020 | if (originalMotionEntry.injectionState) { |
| 3021 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3022 | splitMotionEntry->injectionState->refCount += 1; |
| 3023 | } |
| 3024 | |
| 3025 | return splitMotionEntry; |
| 3026 | } |
| 3027 | |
| 3028 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3029 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3030 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3031 | #endif |
| 3032 | |
| 3033 | bool needWake; |
| 3034 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3035 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3036 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3037 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3038 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3039 | needWake = enqueueInboundEventLocked(newEntry); |
| 3040 | } // release lock |
| 3041 | |
| 3042 | if (needWake) { |
| 3043 | mLooper->wake(); |
| 3044 | } |
| 3045 | } |
| 3046 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3047 | /** |
| 3048 | * If one of the meta shortcuts is detected, process them here: |
| 3049 | * Meta + Backspace -> generate BACK |
| 3050 | * Meta + Enter -> generate HOME |
| 3051 | * This will potentially overwrite keyCode and metaState. |
| 3052 | */ |
| 3053 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3054 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3055 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3056 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3057 | if (keyCode == AKEYCODE_DEL) { |
| 3058 | newKeyCode = AKEYCODE_BACK; |
| 3059 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3060 | newKeyCode = AKEYCODE_HOME; |
| 3061 | } |
| 3062 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3063 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3064 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3065 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3066 | keyCode = newKeyCode; |
| 3067 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3068 | } |
| 3069 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3070 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3071 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3072 | // 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] | 3073 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3074 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3075 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3076 | if (replacementIt != mReplacedKeys.end()) { |
| 3077 | keyCode = replacementIt->second; |
| 3078 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3079 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3084 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3085 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3086 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3087 | "policyFlags=0x%x, action=0x%x, " |
| 3088 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3089 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3090 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3091 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3092 | #endif |
| 3093 | if (!validateKeyEvent(args->action)) { |
| 3094 | return; |
| 3095 | } |
| 3096 | |
| 3097 | uint32_t policyFlags = args->policyFlags; |
| 3098 | int32_t flags = args->flags; |
| 3099 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3100 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3101 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3102 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3103 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3104 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3105 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3106 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3108 | metaState |= AMETA_FUNCTION_ON; |
| 3109 | } |
| 3110 | |
| 3111 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3112 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3113 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3114 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3115 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3116 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3117 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3118 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3119 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3120 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3121 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3122 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3123 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3124 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3125 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3127 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3128 | bool needWake; |
| 3129 | { // acquire lock |
| 3130 | mLock.lock(); |
| 3131 | |
| 3132 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3133 | mLock.unlock(); |
| 3134 | |
| 3135 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3136 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3137 | return; // event was consumed by the filter |
| 3138 | } |
| 3139 | |
| 3140 | mLock.lock(); |
| 3141 | } |
| 3142 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3143 | KeyEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3144 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3145 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3146 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3147 | |
| 3148 | needWake = enqueueInboundEventLocked(newEntry); |
| 3149 | mLock.unlock(); |
| 3150 | } // release lock |
| 3151 | |
| 3152 | if (needWake) { |
| 3153 | mLooper->wake(); |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3158 | return mInputFilterEnabled; |
| 3159 | } |
| 3160 | |
| 3161 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3162 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3163 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3164 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3165 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3166 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3167 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3168 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3169 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3170 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3171 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3172 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3173 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3174 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3175 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3176 | "orientation=%f", |
| 3177 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3178 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3179 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3180 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3181 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3182 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3183 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3184 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3185 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3186 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3187 | } |
| 3188 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3189 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3190 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3191 | return; |
| 3192 | } |
| 3193 | |
| 3194 | uint32_t policyFlags = args->policyFlags; |
| 3195 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3196 | |
| 3197 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3198 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3199 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3200 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3201 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3203 | |
| 3204 | bool needWake; |
| 3205 | { // acquire lock |
| 3206 | mLock.lock(); |
| 3207 | |
| 3208 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3209 | mLock.unlock(); |
| 3210 | |
| 3211 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3212 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3213 | args->action, args->actionButton, args->flags, args->edgeFlags, |
| 3214 | args->metaState, args->buttonState, args->classification, 1 /*xScale*/, |
| 3215 | 1 /*yScale*/, 0 /* xOffset */, 0 /* yOffset */, args->xPrecision, |
| 3216 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3217 | args->downTime, args->eventTime, args->pointerCount, |
| 3218 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3219 | |
| 3220 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3221 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3222 | return; // event was consumed by the filter |
| 3223 | } |
| 3224 | |
| 3225 | mLock.lock(); |
| 3226 | } |
| 3227 | |
| 3228 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3229 | MotionEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3230 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3231 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3232 | args->flags, args->metaState, args->buttonState, |
| 3233 | args->classification, args->edgeFlags, args->xPrecision, |
| 3234 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3235 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3236 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3237 | |
| 3238 | needWake = enqueueInboundEventLocked(newEntry); |
| 3239 | mLock.unlock(); |
| 3240 | } // release lock |
| 3241 | |
| 3242 | if (needWake) { |
| 3243 | mLooper->wake(); |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3248 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3252 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3253 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3254 | "switchMask=0x%08x", |
| 3255 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | #endif |
| 3257 | |
| 3258 | uint32_t policyFlags = args->policyFlags; |
| 3259 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3260 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3264 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3265 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3266 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3267 | #endif |
| 3268 | |
| 3269 | bool needWake; |
| 3270 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3271 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3272 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3273 | DeviceResetEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3274 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3275 | needWake = enqueueInboundEventLocked(newEntry); |
| 3276 | } // release lock |
| 3277 | |
| 3278 | if (needWake) { |
| 3279 | mLooper->wake(); |
| 3280 | } |
| 3281 | } |
| 3282 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3283 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 3284 | int32_t injectorUid, int32_t syncMode, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3285 | std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3286 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3287 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3288 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3289 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | #endif |
| 3291 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3292 | nsecs_t endTime = now() + std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3293 | |
| 3294 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3295 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3296 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3297 | } |
| 3298 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3299 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3300 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3301 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3302 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3303 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3304 | if (!validateKeyEvent(action)) { |
| 3305 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3306 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3307 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3308 | int32_t flags = incomingKey.getFlags(); |
| 3309 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3310 | int32_t metaState = incomingKey.getMetaState(); |
| 3311 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3312 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3313 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3314 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3315 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3316 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3317 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3318 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3319 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3320 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3321 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3322 | |
| 3323 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3324 | android::base::Timer t; |
| 3325 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3326 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3327 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3328 | std::to_string(t.duration().count()).c_str()); |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | mLock.lock(); |
| 3333 | KeyEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3334 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3335 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3336 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, |
| 3337 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3338 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3339 | injectedEntries.push(injectedEntry); |
| 3340 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3341 | } |
| 3342 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3343 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3344 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3345 | int32_t action = motionEvent->getAction(); |
| 3346 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3347 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3348 | int32_t actionButton = motionEvent->getActionButton(); |
| 3349 | int32_t displayId = motionEvent->getDisplayId(); |
| 3350 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
| 3351 | return INPUT_EVENT_INJECTION_FAILED; |
| 3352 | } |
| 3353 | |
| 3354 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3355 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3356 | android::base::Timer t; |
| 3357 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3358 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3359 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3360 | std::to_string(t.duration().count()).c_str()); |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | mLock.lock(); |
| 3365 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3366 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3367 | MotionEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3368 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3369 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3370 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3371 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3372 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3373 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3374 | motionEvent->getRawXCursorPosition(), |
| 3375 | motionEvent->getRawYCursorPosition(), |
| 3376 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3377 | pointerProperties, samplePointerCoords, |
| 3378 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3379 | injectedEntries.push(injectedEntry); |
| 3380 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3381 | sampleEventTimes += 1; |
| 3382 | samplePointerCoords += pointerCount; |
| 3383 | MotionEntry* nextInjectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3384 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3385 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3386 | motionEvent->getDisplayId(), policyFlags, action, |
| 3387 | actionButton, motionEvent->getFlags(), |
| 3388 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3389 | motionEvent->getClassification(), |
| 3390 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3391 | motionEvent->getYPrecision(), |
| 3392 | motionEvent->getRawXCursorPosition(), |
| 3393 | motionEvent->getRawYCursorPosition(), |
| 3394 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3395 | pointerProperties, samplePointerCoords, |
| 3396 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3397 | injectedEntries.push(nextInjectedEntry); |
| 3398 | } |
| 3399 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3400 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3401 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3402 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3403 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3404 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 3408 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3409 | injectionState->injectionIsAsync = true; |
| 3410 | } |
| 3411 | |
| 3412 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3413 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3414 | |
| 3415 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3416 | while (!injectedEntries.empty()) { |
| 3417 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3418 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | mLock.unlock(); |
| 3422 | |
| 3423 | if (needWake) { |
| 3424 | mLooper->wake(); |
| 3425 | } |
| 3426 | |
| 3427 | int32_t injectionResult; |
| 3428 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3429 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3430 | |
| 3431 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3432 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3433 | } else { |
| 3434 | for (;;) { |
| 3435 | injectionResult = injectionState->injectionResult; |
| 3436 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3437 | break; |
| 3438 | } |
| 3439 | |
| 3440 | nsecs_t remainingTimeout = endTime - now(); |
| 3441 | if (remainingTimeout <= 0) { |
| 3442 | #if DEBUG_INJECTION |
| 3443 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3444 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3445 | #endif |
| 3446 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3447 | break; |
| 3448 | } |
| 3449 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3450 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 | } |
| 3452 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3453 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && |
| 3454 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3455 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3456 | #if DEBUG_INJECTION |
| 3457 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3458 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3459 | #endif |
| 3460 | nsecs_t remainingTimeout = endTime - now(); |
| 3461 | if (remainingTimeout <= 0) { |
| 3462 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3463 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3464 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3465 | #endif |
| 3466 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3467 | break; |
| 3468 | } |
| 3469 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3470 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | injectionState->release(); |
| 3476 | } // release lock |
| 3477 | |
| 3478 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3479 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3480 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3481 | #endif |
| 3482 | |
| 3483 | return injectionResult; |
| 3484 | } |
| 3485 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3486 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3487 | std::array<uint8_t, 32> calculatedHmac; |
| 3488 | std::unique_ptr<VerifiedInputEvent> result; |
| 3489 | switch (event.getType()) { |
| 3490 | case AINPUT_EVENT_TYPE_KEY: { |
| 3491 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3492 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3493 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
| 3494 | calculatedHmac = mHmacKeyManager.sign(verifiedKeyEvent); |
| 3495 | break; |
| 3496 | } |
| 3497 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3498 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3499 | VerifiedMotionEvent verifiedMotionEvent = |
| 3500 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3501 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
| 3502 | calculatedHmac = mHmacKeyManager.sign(verifiedMotionEvent); |
| 3503 | break; |
| 3504 | } |
| 3505 | default: { |
| 3506 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3507 | return nullptr; |
| 3508 | } |
| 3509 | } |
| 3510 | if (calculatedHmac == INVALID_HMAC) { |
| 3511 | return nullptr; |
| 3512 | } |
| 3513 | if (calculatedHmac != event.getHmac()) { |
| 3514 | return nullptr; |
| 3515 | } |
| 3516 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3517 | } |
| 3518 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3519 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3520 | return injectorUid == 0 || |
| 3521 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | } |
| 3523 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3524 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3525 | InjectionState* injectionState = entry->injectionState; |
| 3526 | if (injectionState) { |
| 3527 | #if DEBUG_INJECTION |
| 3528 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3529 | "injectorPid=%d, injectorUid=%d", |
| 3530 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3531 | #endif |
| 3532 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3533 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | // Log the outcome since the injector did not wait for the injection result. |
| 3535 | switch (injectionResult) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3536 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3537 | ALOGV("Asynchronous input event injection succeeded."); |
| 3538 | break; |
| 3539 | case INPUT_EVENT_INJECTION_FAILED: |
| 3540 | ALOGW("Asynchronous input event injection failed."); |
| 3541 | break; |
| 3542 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3543 | ALOGW("Asynchronous input event injection permission denied."); |
| 3544 | break; |
| 3545 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3546 | ALOGW("Asynchronous input event injection timed out."); |
| 3547 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3552 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3556 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3557 | InjectionState* injectionState = entry->injectionState; |
| 3558 | if (injectionState) { |
| 3559 | injectionState->pendingForegroundDispatches += 1; |
| 3560 | } |
| 3561 | } |
| 3562 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3563 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3564 | InjectionState* injectionState = entry->injectionState; |
| 3565 | if (injectionState) { |
| 3566 | injectionState->pendingForegroundDispatches -= 1; |
| 3567 | |
| 3568 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3569 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3570 | } |
| 3571 | } |
| 3572 | } |
| 3573 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3574 | std::vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked( |
| 3575 | int32_t displayId) const { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3576 | return getValueByKey(mWindowHandlesByDisplay, displayId); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3577 | } |
| 3578 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3579 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3580 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3581 | if (windowHandleToken == nullptr) { |
| 3582 | return nullptr; |
| 3583 | } |
| 3584 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3585 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3586 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3587 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3588 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3589 | return windowHandle; |
| 3590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | } |
| 3592 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3593 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3594 | } |
| 3595 | |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3596 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 3597 | for (auto& it : mWindowHandlesByDisplay) { |
| 3598 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3599 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3600 | if (handle->getId() == windowHandle->getId() && |
| 3601 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3602 | if (windowHandle->getInfo()->displayId != it.first) { |
| 3603 | ALOGE("Found window %s in display %" PRId32 |
| 3604 | ", but it should belong to display %" PRId32, |
| 3605 | windowHandle->getName().c_str(), it.first, |
| 3606 | windowHandle->getInfo()->displayId); |
| 3607 | } |
| 3608 | return true; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3609 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | return false; |
| 3613 | } |
| 3614 | |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3615 | sp<InputChannel> InputDispatcher::getInputChannelLocked(const sp<IBinder>& token) const { |
| 3616 | size_t count = mInputChannelsByToken.count(token); |
| 3617 | if (count == 0) { |
| 3618 | return nullptr; |
| 3619 | } |
| 3620 | return mInputChannelsByToken.at(token); |
| 3621 | } |
| 3622 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3623 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3624 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3625 | if (inputWindowHandles.empty()) { |
| 3626 | // Remove all handles on a display if there are no windows left. |
| 3627 | mWindowHandlesByDisplay.erase(displayId); |
| 3628 | return; |
| 3629 | } |
| 3630 | |
| 3631 | // Since we compare the pointer of input window handles across window updates, we need |
| 3632 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3633 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3634 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3635 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3636 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3640 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3641 | if (!handle->updateInfo()) { |
| 3642 | // handle no longer valid |
| 3643 | continue; |
| 3644 | } |
| 3645 | |
| 3646 | const InputWindowInfo* info = handle->getInfo(); |
| 3647 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3648 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3649 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 3650 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3651 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 3652 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3653 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3654 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3655 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3656 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3657 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | if (info->displayId != displayId) { |
| 3661 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3662 | handle->getName().c_str(), displayId, info->displayId); |
| 3663 | continue; |
| 3664 | } |
| 3665 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3666 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 3667 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3668 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3669 | oldHandle->updateFrom(handle); |
| 3670 | newHandles.push_back(oldHandle); |
| 3671 | } else { |
| 3672 | newHandles.push_back(handle); |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | // Insert or replace |
| 3677 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3678 | } |
| 3679 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3680 | void InputDispatcher::setInputWindows( |
| 3681 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3682 | { // acquire lock |
| 3683 | std::scoped_lock _l(mLock); |
| 3684 | for (auto const& i : handlesPerDisplay) { |
| 3685 | setInputWindowsLocked(i.second, i.first); |
| 3686 | } |
| 3687 | } |
| 3688 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3689 | mLooper->wake(); |
| 3690 | } |
| 3691 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3692 | /** |
| 3693 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3694 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3695 | * If set an empty list, remove all handles from the specific display. |
| 3696 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3697 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3698 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3699 | void InputDispatcher::setInputWindowsLocked( |
| 3700 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3701 | if (DEBUG_FOCUS) { |
| 3702 | std::string windowList; |
| 3703 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3704 | windowList += iwh->getName() + " "; |
| 3705 | } |
| 3706 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3707 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3709 | // Copy old handles for release if they are no longer present. |
| 3710 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3711 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3712 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3713 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3714 | sp<InputWindowHandle> newFocusedWindowHandle = nullptr; |
| 3715 | bool foundHoveredWindow = false; |
| 3716 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3717 | // Set newFocusedWindowHandle to the top most focused window instead of the last one |
| 3718 | if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus && |
| 3719 | windowHandle->getInfo()->visible) { |
| 3720 | newFocusedWindowHandle = windowHandle; |
| 3721 | } |
| 3722 | if (windowHandle == mLastHoverWindowHandle) { |
| 3723 | foundHoveredWindow = true; |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | if (!foundHoveredWindow) { |
| 3728 | mLastHoverWindowHandle = nullptr; |
| 3729 | } |
| 3730 | |
| 3731 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3732 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 3733 | |
| 3734 | if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) { |
| 3735 | if (oldFocusedWindowHandle != nullptr) { |
| 3736 | if (DEBUG_FOCUS) { |
| 3737 | ALOGD("Focus left window: %s in display %" PRId32, |
| 3738 | oldFocusedWindowHandle->getName().c_str(), displayId); |
Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3739 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3740 | sp<InputChannel> focusedInputChannel = |
| 3741 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
| 3742 | if (focusedInputChannel != nullptr) { |
| 3743 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3744 | "focus left window"); |
| 3745 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
| 3746 | enqueueFocusEventLocked(*oldFocusedWindowHandle, false /*hasFocus*/); |
Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3747 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3748 | mFocusedWindowHandlesByDisplay.erase(displayId); |
| 3749 | } |
| 3750 | if (newFocusedWindowHandle != nullptr) { |
| 3751 | if (DEBUG_FOCUS) { |
| 3752 | ALOGD("Focus entered window: %s in display %" PRId32, |
| 3753 | newFocusedWindowHandle->getName().c_str(), displayId); |
| 3754 | } |
| 3755 | mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; |
| 3756 | enqueueFocusEventLocked(*newFocusedWindowHandle, true /*hasFocus*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3757 | } |
| 3758 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3759 | if (mFocusedDisplayId == displayId) { |
| 3760 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3761 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3762 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3764 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 3765 | mTouchStatesByDisplay.find(displayId); |
| 3766 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 3767 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3768 | for (size_t i = 0; i < state.windows.size();) { |
| 3769 | TouchedWindow& touchedWindow = state.windows[i]; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3770 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3771 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3772 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 3773 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3774 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3775 | sp<InputChannel> touchedInputChannel = |
| 3776 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 3777 | if (touchedInputChannel != nullptr) { |
| 3778 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3779 | "touched window was removed"); |
| 3780 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3781 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3782 | state.windows.erase(state.windows.begin() + i); |
| 3783 | } else { |
| 3784 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3785 | } |
| 3786 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3787 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3788 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3789 | // Release information for windows that are no longer present. |
| 3790 | // This ensures that unused input channels are released promptly. |
| 3791 | // Otherwise, they might stick around until the window handle is destroyed |
| 3792 | // which might not happen until the next GC. |
| 3793 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3794 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3795 | if (DEBUG_FOCUS) { |
| 3796 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3797 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3798 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3799 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3800 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | void InputDispatcher::setFocusedApplication( |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3804 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3805 | if (DEBUG_FOCUS) { |
| 3806 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 3807 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 3808 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3809 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3810 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3811 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3812 | sp<InputApplicationHandle> oldFocusedApplicationHandle = |
| 3813 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3814 | |
| 3815 | if (oldFocusedApplicationHandle == mAwaitedFocusedApplication && |
| 3816 | inputApplicationHandle != oldFocusedApplicationHandle) { |
| 3817 | resetNoFocusedWindowTimeoutLocked(); |
| 3818 | } |
| 3819 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3820 | if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3821 | if (oldFocusedApplicationHandle != inputApplicationHandle) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3822 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3823 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3824 | } else if (oldFocusedApplicationHandle != nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3825 | oldFocusedApplicationHandle.clear(); |
| 3826 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3827 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 | } // release lock |
| 3829 | |
| 3830 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3831 | mLooper->wake(); |
| 3832 | } |
| 3833 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3834 | /** |
| 3835 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3836 | * the display not specified. |
| 3837 | * |
| 3838 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3839 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3840 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3841 | * display. The display-specified events won't be affected. |
| 3842 | */ |
| 3843 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3844 | if (DEBUG_FOCUS) { |
| 3845 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3846 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3847 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3848 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3849 | |
| 3850 | if (mFocusedDisplayId != displayId) { |
| 3851 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3852 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
| 3853 | if (oldFocusedWindowHandle != nullptr) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3854 | sp<InputChannel> inputChannel = |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3855 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3856 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3857 | CancelationOptions |
| 3858 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3859 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3860 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3861 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3862 | } |
| 3863 | } |
| 3864 | mFocusedDisplayId = displayId; |
| 3865 | |
| 3866 | // Sanity check |
| 3867 | sp<InputWindowHandle> newFocusedWindowHandle = |
| 3868 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 3869 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3870 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3871 | if (newFocusedWindowHandle == nullptr) { |
| 3872 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
| 3873 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 3874 | ALOGE("But another display has a focused window:"); |
| 3875 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3876 | const sp<InputWindowHandle>& windowHandle = it.second; |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 3877 | ALOGE("Display #%" PRId32 " has focused window: '%s'\n", it.first, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3878 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3884 | if (DEBUG_FOCUS) { |
| 3885 | logDispatchStateLocked(); |
| 3886 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3887 | } // release lock |
| 3888 | |
| 3889 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3890 | mLooper->wake(); |
| 3891 | } |
| 3892 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3894 | if (DEBUG_FOCUS) { |
| 3895 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3896 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3897 | |
| 3898 | bool changed; |
| 3899 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3900 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | |
| 3902 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 3903 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3904 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | if (mDispatchEnabled && !enabled) { |
| 3908 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3909 | } |
| 3910 | |
| 3911 | mDispatchEnabled = enabled; |
| 3912 | mDispatchFrozen = frozen; |
| 3913 | changed = true; |
| 3914 | } else { |
| 3915 | changed = false; |
| 3916 | } |
| 3917 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3918 | if (DEBUG_FOCUS) { |
| 3919 | logDispatchStateLocked(); |
| 3920 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3921 | } // release lock |
| 3922 | |
| 3923 | if (changed) { |
| 3924 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3925 | mLooper->wake(); |
| 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3930 | if (DEBUG_FOCUS) { |
| 3931 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 3932 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3933 | |
| 3934 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3935 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | |
| 3937 | if (mInputFilterEnabled == enabled) { |
| 3938 | return; |
| 3939 | } |
| 3940 | |
| 3941 | mInputFilterEnabled = enabled; |
| 3942 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 3943 | } // release lock |
| 3944 | |
| 3945 | // Wake up poll loop since there might be work to do to drop everything. |
| 3946 | mLooper->wake(); |
| 3947 | } |
| 3948 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 3949 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 3950 | std::scoped_lock lock(mLock); |
| 3951 | mInTouchMode = inTouchMode; |
| 3952 | } |
| 3953 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3954 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 3955 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3956 | if (DEBUG_FOCUS) { |
| 3957 | ALOGD("Trivial transfer to same window."); |
| 3958 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3959 | return true; |
| 3960 | } |
| 3961 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3962 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3963 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3965 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 3966 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3967 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3968 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3969 | return false; |
| 3970 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3971 | if (DEBUG_FOCUS) { |
| 3972 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 3973 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 3974 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3975 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3976 | if (DEBUG_FOCUS) { |
| 3977 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 3978 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3979 | return false; |
| 3980 | } |
| 3981 | |
| 3982 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3983 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 3984 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3985 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 3986 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 3987 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 3988 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 3989 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3991 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3992 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3993 | int32_t newTargetFlags = oldTargetFlags & |
| 3994 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 3995 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3996 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3997 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3998 | found = true; |
| 3999 | goto Found; |
| 4000 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | } |
| 4002 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4003 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4004 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4005 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4006 | if (DEBUG_FOCUS) { |
| 4007 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4008 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4009 | return false; |
| 4010 | } |
| 4011 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4012 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4013 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4014 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4015 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4016 | CancelationOptions |
| 4017 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4018 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4019 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4020 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4021 | } |
| 4022 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4023 | if (DEBUG_FOCUS) { |
| 4024 | logDispatchStateLocked(); |
| 4025 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | } // release lock |
| 4027 | |
| 4028 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4029 | mLooper->wake(); |
| 4030 | return true; |
| 4031 | } |
| 4032 | |
| 4033 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4034 | if (DEBUG_FOCUS) { |
| 4035 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4036 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4037 | |
| 4038 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4039 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4040 | |
| 4041 | resetKeyRepeatLocked(); |
| 4042 | releasePendingEventLocked(); |
| 4043 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4044 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4046 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4047 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4048 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4049 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4053 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4054 | dumpDispatchStateLocked(dump); |
| 4055 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4056 | std::istringstream stream(dump); |
| 4057 | std::string line; |
| 4058 | |
| 4059 | while (std::getline(stream, line, '\n')) { |
| 4060 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | } |
| 4062 | } |
| 4063 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4064 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4065 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4066 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4067 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4068 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4069 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4070 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4071 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4072 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4073 | const int32_t displayId = it.first; |
| 4074 | const sp<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4075 | const int64_t timeoutMillis = millis( |
| 4076 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4077 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4078 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4079 | displayId, applicationHandle->getName().c_str(), timeoutMillis); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4080 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4081 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4082 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4083 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4084 | |
| 4085 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 4086 | dump += StringPrintf(INDENT "FocusedWindows:\n"); |
| 4087 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 4088 | const int32_t displayId = it.first; |
| 4089 | const sp<InputWindowHandle>& windowHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4090 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4091 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4092 | } |
| 4093 | } else { |
| 4094 | dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); |
| 4095 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4096 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4097 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4098 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4099 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4100 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4101 | 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] | 4102 | state.displayId, toString(state.down), toString(state.split), |
| 4103 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4104 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4105 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4106 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4107 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4108 | dump += StringPrintf(INDENT4 |
| 4109 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4110 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4111 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4112 | } |
| 4113 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4114 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4115 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4116 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4117 | dump += INDENT3 "Portal windows:\n"; |
| 4118 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4119 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4120 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4121 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4122 | } |
| 4123 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4124 | } |
| 4125 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4126 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4127 | } |
| 4128 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4129 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4130 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4131 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4132 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4133 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4134 | dump += INDENT2 "Windows:\n"; |
| 4135 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4136 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4137 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4139 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4140 | "portalToDisplayId=%d, paused=%s, hasFocus=%s, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4141 | "hasWallpaper=%s, visible=%s, canReceiveKeys=%s, " |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 4142 | "flags=%s, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4143 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4144 | "windowScale=(%f,%f), touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4145 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4146 | windowInfo->portalToDisplayId, |
| 4147 | toString(windowInfo->paused), |
| 4148 | toString(windowInfo->hasFocus), |
| 4149 | toString(windowInfo->hasWallpaper), |
| 4150 | toString(windowInfo->visible), |
| 4151 | toString(windowInfo->canReceiveKeys), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 4152 | windowInfo->flags.string(InputWindowInfo::flagToString) |
| 4153 | .c_str(), |
| 4154 | static_cast<int32_t>(windowInfo->type), |
| 4155 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4156 | windowInfo->frameRight, windowInfo->frameBottom, |
| 4157 | windowInfo->globalScaleFactor, windowInfo->windowXScale, |
| 4158 | windowInfo->windowYScale); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4159 | dumpRegion(dump, windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame^] | 4160 | dump += StringPrintf(", inputFeatures=%s", |
| 4161 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4162 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
| 4163 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4164 | windowInfo->ownerPid, windowInfo->ownerUid, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4165 | millis(windowInfo->dispatchingTimeout)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4166 | } |
| 4167 | } else { |
| 4168 | dump += INDENT2 "Windows: <none>\n"; |
| 4169 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4170 | } |
| 4171 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4172 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4173 | } |
| 4174 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4175 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4176 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4177 | const std::vector<Monitor>& monitors = it.second; |
| 4178 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4179 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4180 | } |
| 4181 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4182 | const std::vector<Monitor>& monitors = it.second; |
| 4183 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4184 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4185 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4186 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4187 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | nsecs_t currentTime = now(); |
| 4191 | |
| 4192 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4193 | if (!mRecentQueue.empty()) { |
| 4194 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4195 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4196 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4197 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4198 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4199 | } |
| 4200 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4201 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | // Dump event currently being dispatched. |
| 4205 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4206 | dump += INDENT "PendingEvent:\n"; |
| 4207 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4209 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4210 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4211 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4212 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4216 | if (!mInboundQueue.empty()) { |
| 4217 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4218 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4219 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4220 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4221 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4222 | } |
| 4223 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4224 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4227 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4228 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4229 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4230 | const KeyReplacement& replacement = pair.first; |
| 4231 | int32_t newKeyCode = pair.second; |
| 4232 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4233 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4234 | } |
| 4235 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4236 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4239 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4240 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4241 | for (const auto& pair : mConnectionsByFd) { |
| 4242 | const sp<Connection>& connection = pair.second; |
| 4243 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4244 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4245 | pair.first, connection->getInputChannelName().c_str(), |
| 4246 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4247 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4248 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4249 | if (!connection->outboundQueue.empty()) { |
| 4250 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4251 | connection->outboundQueue.size()); |
| 4252 | for (DispatchEntry* entry : connection->outboundQueue) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4253 | dump.append(INDENT4); |
| 4254 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4255 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 |
| 4256 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4257 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4258 | ns2ms(currentTime - entry->eventEntry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 | } |
| 4260 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4261 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4262 | } |
| 4263 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4264 | if (!connection->waitQueue.empty()) { |
| 4265 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4266 | connection->waitQueue.size()); |
| 4267 | for (DispatchEntry* entry : connection->waitQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4268 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4270 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4271 | "age=%" PRId64 "ms, wait=%" PRId64 "ms seq=%" PRIu32 "\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4272 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4273 | ns2ms(currentTime - entry->eventEntry->eventTime), |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4274 | ns2ms(currentTime - entry->deliveryTime), entry->seq); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4275 | } |
| 4276 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4277 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4281 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4285 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 4286 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4288 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4289 | } |
| 4290 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4291 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4292 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 4293 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 4294 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | } |
| 4296 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4297 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4298 | const size_t numMonitors = monitors.size(); |
| 4299 | for (size_t i = 0; i < numMonitors; i++) { |
| 4300 | const Monitor& monitor = monitors[i]; |
| 4301 | const sp<InputChannel>& channel = monitor.inputChannel; |
| 4302 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4303 | dump += "\n"; |
| 4304 | } |
| 4305 | } |
| 4306 | |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4307 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4309 | ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | #endif |
| 4311 | |
| 4312 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4313 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4314 | sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4315 | if (existingConnection != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | ALOGW("Attempted to register already registered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4317 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4318 | return BAD_VALUE; |
| 4319 | } |
| 4320 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4321 | sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4322 | |
| 4323 | int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4324 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4325 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4326 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4327 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4328 | } // release lock |
| 4329 | |
| 4330 | // Wake the looper because some connections have changed. |
| 4331 | mLooper->wake(); |
| 4332 | return OK; |
| 4333 | } |
| 4334 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4335 | status_t InputDispatcher::registerInputMonitor(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4336 | int32_t displayId, bool isGestureMonitor) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4337 | { // acquire lock |
| 4338 | std::scoped_lock _l(mLock); |
| 4339 | |
| 4340 | if (displayId < 0) { |
| 4341 | ALOGW("Attempted to register input monitor without a specified display."); |
| 4342 | return BAD_VALUE; |
| 4343 | } |
| 4344 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4345 | if (inputChannel->getConnectionToken() == nullptr) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4346 | ALOGW("Attempted to register input monitor without an identifying token."); |
| 4347 | return BAD_VALUE; |
| 4348 | } |
| 4349 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4350 | sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4351 | |
| 4352 | const int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4353 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4354 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4355 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4356 | auto& monitorsByDisplay = |
| 4357 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4358 | monitorsByDisplay[displayId].emplace_back(inputChannel); |
| 4359 | |
| 4360 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4361 | } |
| 4362 | // Wake the looper because some connections have changed. |
| 4363 | mLooper->wake(); |
| 4364 | return OK; |
| 4365 | } |
| 4366 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4367 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
| 4368 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4369 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4370 | #endif |
| 4371 | |
| 4372 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4373 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | |
| 4375 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
| 4376 | if (status) { |
| 4377 | return status; |
| 4378 | } |
| 4379 | } // release lock |
| 4380 | |
| 4381 | // Wake the poll loop because removing the connection may have changed the current |
| 4382 | // synchronization state. |
| 4383 | mLooper->wake(); |
| 4384 | return OK; |
| 4385 | } |
| 4386 | |
| 4387 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4388 | bool notify) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4389 | sp<Connection> connection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4390 | if (connection == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4391 | ALOGW("Attempted to unregister already unregistered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4392 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4393 | return BAD_VALUE; |
| 4394 | } |
| 4395 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4396 | removeConnectionLocked(connection); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4397 | mInputChannelsByToken.erase(inputChannel->getConnectionToken()); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4398 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | if (connection->monitor) { |
| 4400 | removeMonitorChannelLocked(inputChannel); |
| 4401 | } |
| 4402 | |
| 4403 | mLooper->removeFd(inputChannel->getFd()); |
| 4404 | |
| 4405 | nsecs_t currentTime = now(); |
| 4406 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4407 | |
| 4408 | connection->status = Connection::STATUS_ZOMBIE; |
| 4409 | return OK; |
| 4410 | } |
| 4411 | |
| 4412 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4413 | removeMonitorChannelLocked(inputChannel, mGlobalMonitorsByDisplay); |
| 4414 | removeMonitorChannelLocked(inputChannel, mGestureMonitorsByDisplay); |
| 4415 | } |
| 4416 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4417 | void InputDispatcher::removeMonitorChannelLocked( |
| 4418 | const sp<InputChannel>& inputChannel, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4419 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4420 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4421 | std::vector<Monitor>& monitors = it->second; |
| 4422 | const size_t numMonitors = monitors.size(); |
| 4423 | for (size_t i = 0; i < numMonitors; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4424 | if (monitors[i].inputChannel == inputChannel) { |
| 4425 | monitors.erase(monitors.begin() + i); |
| 4426 | break; |
| 4427 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4428 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4429 | if (monitors.empty()) { |
| 4430 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4431 | } else { |
| 4432 | ++it; |
| 4433 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4434 | } |
| 4435 | } |
| 4436 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4437 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4438 | { // acquire lock |
| 4439 | std::scoped_lock _l(mLock); |
| 4440 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4441 | |
| 4442 | if (!foundDisplayId) { |
| 4443 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4444 | return BAD_VALUE; |
| 4445 | } |
| 4446 | int32_t displayId = foundDisplayId.value(); |
| 4447 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4448 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4449 | mTouchStatesByDisplay.find(displayId); |
| 4450 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4451 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4452 | return BAD_VALUE; |
| 4453 | } |
| 4454 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4455 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4456 | std::optional<int32_t> foundDeviceId; |
| 4457 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4458 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4459 | foundDeviceId = state.deviceId; |
| 4460 | } |
| 4461 | } |
| 4462 | if (!foundDeviceId || !state.down) { |
| 4463 | 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] | 4464 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4465 | return BAD_VALUE; |
| 4466 | } |
| 4467 | int32_t deviceId = foundDeviceId.value(); |
| 4468 | |
| 4469 | // Send cancel events to all the input channels we're stealing from. |
| 4470 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4471 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4472 | options.deviceId = deviceId; |
| 4473 | options.displayId = displayId; |
| 4474 | for (const TouchedWindow& window : state.windows) { |
| 4475 | sp<InputChannel> channel = getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4476 | if (channel != nullptr) { |
| 4477 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4478 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4479 | } |
| 4480 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4481 | state.filterNonMonitors(); |
| 4482 | } |
| 4483 | return OK; |
| 4484 | } |
| 4485 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4486 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4487 | const sp<IBinder>& token) { |
| 4488 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4489 | const std::vector<Monitor>& monitors = it.second; |
| 4490 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4491 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4492 | return it.first; |
| 4493 | } |
| 4494 | } |
| 4495 | } |
| 4496 | return std::nullopt; |
| 4497 | } |
| 4498 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4499 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4500 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4501 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4502 | } |
| 4503 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4504 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4505 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4506 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4507 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4508 | } |
| 4509 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4510 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4511 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4514 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4515 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4516 | removeByValue(mConnectionsByFd, connection); |
| 4517 | } |
| 4518 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4519 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4520 | const sp<Connection>& connection, uint32_t seq, |
| 4521 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4522 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4523 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4524 | commandEntry->connection = connection; |
| 4525 | commandEntry->eventTime = currentTime; |
| 4526 | commandEntry->seq = seq; |
| 4527 | commandEntry->handled = handled; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4528 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | } |
| 4530 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4531 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4532 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4533 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4534 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4535 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4536 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4537 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4538 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4539 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4540 | } |
| 4541 | |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4542 | void InputDispatcher::onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4543 | const sp<InputWindowHandle>& newFocus) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4544 | sp<IBinder> oldToken = oldFocus != nullptr ? oldFocus->getToken() : nullptr; |
| 4545 | sp<IBinder> newToken = newFocus != nullptr ? newFocus->getToken() : nullptr; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4546 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4547 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4548 | commandEntry->oldToken = oldToken; |
| 4549 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4550 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4551 | } |
| 4552 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4553 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 4554 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 4555 | // is already healthy again. Don't raise ANR in this situation |
| 4556 | if (connection->waitQueue.empty()) { |
| 4557 | ALOGI("Not raising ANR because the connection %s has recovered", |
| 4558 | connection->inputChannel->getName().c_str()); |
| 4559 | return; |
| 4560 | } |
| 4561 | /** |
| 4562 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 4563 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 4564 | * has changed. This could cause newer entries to time out before the already dispatched |
| 4565 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 4566 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 4567 | * most useful. |
| 4568 | */ |
| 4569 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
| 4570 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 4571 | std::string reason = |
| 4572 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
| 4573 | connection->inputChannel->getName().c_str(), |
| 4574 | ns2ms(currentWait), |
| 4575 | oldestEntry->eventEntry->getDescription().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4576 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4577 | updateLastAnrStateLocked(getWindowHandleLocked(connection->inputChannel->getConnectionToken()), |
| 4578 | reason); |
| 4579 | |
| 4580 | std::unique_ptr<CommandEntry> commandEntry = |
| 4581 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4582 | commandEntry->inputApplicationHandle = nullptr; |
| 4583 | commandEntry->inputChannel = connection->inputChannel; |
| 4584 | commandEntry->reason = std::move(reason); |
| 4585 | postCommandLocked(std::move(commandEntry)); |
| 4586 | } |
| 4587 | |
| 4588 | void InputDispatcher::onAnrLocked(const sp<InputApplicationHandle>& application) { |
| 4589 | std::string reason = android::base::StringPrintf("%s does not have a focused window", |
| 4590 | application->getName().c_str()); |
| 4591 | |
| 4592 | updateLastAnrStateLocked(application, reason); |
| 4593 | |
| 4594 | std::unique_ptr<CommandEntry> commandEntry = |
| 4595 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4596 | commandEntry->inputApplicationHandle = application; |
| 4597 | commandEntry->inputChannel = nullptr; |
| 4598 | commandEntry->reason = std::move(reason); |
| 4599 | postCommandLocked(std::move(commandEntry)); |
| 4600 | } |
| 4601 | |
| 4602 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 4603 | const std::string& reason) { |
| 4604 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 4605 | updateLastAnrStateLocked(windowLabel, reason); |
| 4606 | } |
| 4607 | |
| 4608 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputApplicationHandle>& application, |
| 4609 | const std::string& reason) { |
| 4610 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); |
| 4611 | updateLastAnrStateLocked(windowLabel, reason); |
| 4612 | } |
| 4613 | |
| 4614 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 4615 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4616 | // 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] | 4617 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4618 | struct tm tm; |
| 4619 | localtime_r(&t, &tm); |
| 4620 | char timestr[64]; |
| 4621 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4622 | mLastAnrState.clear(); |
| 4623 | mLastAnrState += INDENT "ANR:\n"; |
| 4624 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4625 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 4626 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4627 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4628 | } |
| 4629 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4630 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4631 | mLock.unlock(); |
| 4632 | |
| 4633 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4634 | |
| 4635 | mLock.lock(); |
| 4636 | } |
| 4637 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4638 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4639 | sp<Connection> connection = commandEntry->connection; |
| 4640 | |
| 4641 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4642 | mLock.unlock(); |
| 4643 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4644 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4645 | |
| 4646 | mLock.lock(); |
| 4647 | } |
| 4648 | } |
| 4649 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4650 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4651 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4652 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4653 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4654 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4655 | mLock.lock(); |
| 4656 | } |
| 4657 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4658 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4659 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4660 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4661 | mLock.unlock(); |
| 4662 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4663 | const std::chrono::nanoseconds timeoutExtension = |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4664 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4665 | |
| 4666 | mLock.lock(); |
| 4667 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4668 | if (timeoutExtension > 0s) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4669 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); |
| 4670 | } else { |
| 4671 | // stop waking up for events in this connection, it is already not responding |
| 4672 | sp<Connection> connection = getConnectionLocked(token); |
| 4673 | if (connection == nullptr) { |
| 4674 | return; |
| 4675 | } |
| 4676 | cancelEventsForAnrLocked(connection); |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | void InputDispatcher::extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application, |
| 4681 | const sp<IBinder>& connectionToken, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4682 | std::chrono::nanoseconds timeoutExtension) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4683 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 4684 | if (connection == nullptr) { |
| 4685 | if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) { |
| 4686 | // Maybe ANR happened because there's no focused window? |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4687 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4688 | mAwaitedFocusedApplication = application; |
| 4689 | } else { |
| 4690 | // It's also possible that the connection already disappeared. No action necessary. |
| 4691 | } |
| 4692 | return; |
| 4693 | } |
| 4694 | |
| 4695 | ALOGI("Raised ANR, but the policy wants to keep waiting on %s for %" PRId64 "ms longer", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4696 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4697 | |
| 4698 | connection->responsive = true; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4699 | const nsecs_t newTimeout = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4700 | for (DispatchEntry* entry : connection->waitQueue) { |
| 4701 | if (newTimeout >= entry->timeoutTime) { |
| 4702 | // Already removed old entries when connection was marked unresponsive |
| 4703 | entry->timeoutTime = newTimeout; |
| 4704 | mAnrTracker.insert(entry->timeoutTime, connectionToken); |
| 4705 | } |
| 4706 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 4710 | CommandEntry* commandEntry) { |
| 4711 | KeyEntry* entry = commandEntry->keyEntry; |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4712 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4713 | |
| 4714 | mLock.unlock(); |
| 4715 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4716 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4717 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4718 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4719 | : nullptr; |
| 4720 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4721 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4722 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4723 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4724 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4725 | |
| 4726 | mLock.lock(); |
| 4727 | |
| 4728 | if (delay < 0) { |
| 4729 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 4730 | } else if (!delay) { |
| 4731 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 4732 | } else { |
| 4733 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 4734 | entry->interceptKeyWakeupTime = now() + delay; |
| 4735 | } |
| 4736 | entry->release(); |
| 4737 | } |
| 4738 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4739 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 4740 | mLock.unlock(); |
| 4741 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 4742 | mLock.lock(); |
| 4743 | } |
| 4744 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4745 | /** |
| 4746 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 4747 | * current time. |
| 4748 | */ |
| 4749 | static bool isConnectionResponsive(const Connection& connection) { |
| 4750 | const nsecs_t currentTime = now(); |
| 4751 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 4752 | if (entry->timeoutTime < currentTime) { |
| 4753 | return false; |
| 4754 | } |
| 4755 | } |
| 4756 | return true; |
| 4757 | } |
| 4758 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4759 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4760 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4761 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4762 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4763 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4764 | |
| 4765 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4766 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4767 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4768 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4769 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4770 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4771 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4772 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4773 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 4774 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4775 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4776 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4777 | |
| 4778 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4779 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4780 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 4781 | restartEvent = |
| 4782 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4783 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4784 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 4785 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 4786 | handled); |
| 4787 | } else { |
| 4788 | restartEvent = false; |
| 4789 | } |
| 4790 | |
| 4791 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4792 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4793 | // contents of the wait queue to have been drained, so we need to double-check |
| 4794 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4795 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 4796 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 4797 | dispatchEntry = *dispatchEntryIt; |
| 4798 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4799 | mAnrTracker.erase(dispatchEntry->timeoutTime, |
| 4800 | connection->inputChannel->getConnectionToken()); |
| 4801 | if (!connection->responsive) { |
| 4802 | connection->responsive = isConnectionResponsive(*connection); |
| 4803 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4804 | traceWaitQueueLength(connection); |
| 4805 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4806 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4807 | traceOutboundQueueLength(connection); |
| 4808 | } else { |
| 4809 | releaseDispatchEntry(dispatchEntry); |
| 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | // Start the next dispatch cycle for this connection. |
| 4814 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4818 | DispatchEntry* dispatchEntry, |
| 4819 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4820 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4821 | if (!handled) { |
| 4822 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4823 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4824 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4825 | return false; |
| 4826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4828 | // Get the fallback key state. |
| 4829 | // Clear it out after dispatching the UP. |
| 4830 | int32_t originalKeyCode = keyEntry->keyCode; |
| 4831 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 4832 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 4833 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4834 | } |
| 4835 | |
| 4836 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 4837 | // If the application handles the original key for which we previously |
| 4838 | // generated a fallback or if the window is not a foreground window, |
| 4839 | // then cancel the associated fallback key, if any. |
| 4840 | if (fallbackKeyCode != -1) { |
| 4841 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4842 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4843 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4844 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4845 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 4846 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4847 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4848 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4849 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4850 | |
| 4851 | mLock.unlock(); |
| 4852 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4853 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4854 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4855 | |
| 4856 | mLock.lock(); |
| 4857 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4858 | // Cancel the fallback key. |
| 4859 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4861 | "application handled the original non-fallback key " |
| 4862 | "or is no longer a foreground target, " |
| 4863 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4864 | options.keyCode = fallbackKeyCode; |
| 4865 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4866 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4867 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4868 | } |
| 4869 | } else { |
| 4870 | // If the application did not handle a non-fallback key, first check |
| 4871 | // that we are in a good state to perform unhandled key event processing |
| 4872 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4873 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4874 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4875 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4876 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4877 | "since this is not an initial down. " |
| 4878 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4879 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4880 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4881 | return false; |
| 4882 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4883 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4884 | // Dispatch the unhandled key to the policy. |
| 4885 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4886 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4887 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4888 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4889 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4890 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4891 | |
| 4892 | mLock.unlock(); |
| 4893 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4894 | bool fallback = |
| 4895 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 4896 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4897 | |
| 4898 | mLock.lock(); |
| 4899 | |
| 4900 | if (connection->status != Connection::STATUS_NORMAL) { |
| 4901 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4902 | return false; |
| 4903 | } |
| 4904 | |
| 4905 | // Latch the fallback keycode for this key on an initial down. |
| 4906 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 4907 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4909 | fallbackKeyCode = event.getKeyCode(); |
| 4910 | } else { |
| 4911 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4912 | } |
| 4913 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 4914 | } |
| 4915 | |
| 4916 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 4917 | |
| 4918 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 4919 | // We will continue to dispatch the key to the policy but we will no |
| 4920 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4921 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 4922 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4923 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4924 | if (fallback) { |
| 4925 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4926 | "as a fallback for %d, but on the DOWN it had requested " |
| 4927 | "to send %d instead. Fallback canceled.", |
| 4928 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4929 | } else { |
| 4930 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4931 | "but on the DOWN it had requested to send %d. " |
| 4932 | "Fallback canceled.", |
| 4933 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4934 | } |
| 4935 | #endif |
| 4936 | |
| 4937 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 4938 | "canceling fallback, policy no longer desires it"); |
| 4939 | options.keyCode = fallbackKeyCode; |
| 4940 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 4941 | |
| 4942 | fallback = false; |
| 4943 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4944 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4945 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4946 | } |
| 4947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | |
| 4949 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4950 | { |
| 4951 | std::string msg; |
| 4952 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 4953 | connection->inputState.getFallbackKeys(); |
| 4954 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4955 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4956 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4957 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4958 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4959 | } |
| 4960 | #endif |
| 4961 | |
| 4962 | if (fallback) { |
| 4963 | // Restart the dispatch cycle using the fallback key. |
| 4964 | keyEntry->eventTime = event.getEventTime(); |
| 4965 | keyEntry->deviceId = event.getDeviceId(); |
| 4966 | keyEntry->source = event.getSource(); |
| 4967 | keyEntry->displayId = event.getDisplayId(); |
| 4968 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 4969 | keyEntry->keyCode = fallbackKeyCode; |
| 4970 | keyEntry->scanCode = event.getScanCode(); |
| 4971 | keyEntry->metaState = event.getMetaState(); |
| 4972 | keyEntry->repeatCount = event.getRepeatCount(); |
| 4973 | keyEntry->downTime = event.getDownTime(); |
| 4974 | keyEntry->syntheticRepeat = false; |
| 4975 | |
| 4976 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4977 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4978 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 4979 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4980 | #endif |
| 4981 | return true; // restart the event |
| 4982 | } else { |
| 4983 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4984 | ALOGD("Unhandled key event: No fallback key."); |
| 4985 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4986 | |
| 4987 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4988 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | return false; |
| 4992 | } |
| 4993 | |
| 4994 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4995 | DispatchEntry* dispatchEntry, |
| 4996 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4997 | return false; |
| 4998 | } |
| 4999 | |
| 5000 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5001 | mLock.unlock(); |
| 5002 | |
| 5003 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 5004 | |
| 5005 | mLock.lock(); |
| 5006 | } |
| 5007 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5008 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 5009 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5010 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5011 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 5012 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5013 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5014 | } |
| 5015 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5016 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5017 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5018 | // TODO Write some statistics about how long we spend waiting. |
| 5019 | } |
| 5020 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5021 | /** |
| 5022 | * Report the touch event latency to the statsd server. |
| 5023 | * Input events are reported for statistics if: |
| 5024 | * - This is a touchscreen event |
| 5025 | * - InputFilter is not enabled |
| 5026 | * - Event is not injected or synthesized |
| 5027 | * |
| 5028 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5029 | * from getting aggregated with the "old" data. |
| 5030 | */ |
| 5031 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5032 | REQUIRES(mLock) { |
| 5033 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5034 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5035 | if (!reportForStatistics) { |
| 5036 | return; |
| 5037 | } |
| 5038 | |
| 5039 | if (mTouchStatistics.shouldReport()) { |
| 5040 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5041 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5042 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5043 | mTouchStatistics.reset(); |
| 5044 | } |
| 5045 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5046 | mTouchStatistics.addValue(latencyMicros); |
| 5047 | } |
| 5048 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5049 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5050 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5051 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5052 | } |
| 5053 | } |
| 5054 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5055 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | if (ATRACE_ENABLED()) { |
| 5057 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5058 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5059 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5060 | } |
| 5061 | } |
| 5062 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5063 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | if (ATRACE_ENABLED()) { |
| 5065 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5066 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5067 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 | } |
| 5069 | } |
| 5070 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5071 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5072 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5073 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5074 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5075 | dumpDispatchStateLocked(dump); |
| 5076 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5077 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5078 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5079 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | void InputDispatcher::monitor() { |
| 5084 | // 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] | 5085 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5086 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5087 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5088 | } |
| 5089 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5090 | /** |
| 5091 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5092 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5093 | * this method can be safely called from any thread, as long as you've ensured that |
| 5094 | * the work you are interested in completing has already been queued. |
| 5095 | */ |
| 5096 | bool InputDispatcher::waitForIdle() { |
| 5097 | /** |
| 5098 | * Timeout should represent the longest possible time that a device might spend processing |
| 5099 | * events and commands. |
| 5100 | */ |
| 5101 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5102 | std::unique_lock lock(mLock); |
| 5103 | mLooper->wake(); |
| 5104 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5105 | return result == std::cv_status::no_timeout; |
| 5106 | } |
| 5107 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5108 | } // namespace android::inputdispatcher |