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> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 50 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 51 | #include <binder/Binder.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 52 | #include <input/InputDevice.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 53 | #include <input/InputWindow.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 54 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 55 | #include <log/log_event_list.h> |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 56 | #include <openssl/hmac.h> |
| 57 | #include <openssl/rand.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 58 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 59 | #include <statslog.h> |
| 60 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 61 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 63 | #include <cerrno> |
| 64 | #include <cinttypes> |
| 65 | #include <climits> |
| 66 | #include <cstddef> |
| 67 | #include <ctime> |
| 68 | #include <queue> |
| 69 | #include <sstream> |
| 70 | |
| 71 | #include "Connection.h" |
| 72 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 73 | #define INDENT " " |
| 74 | #define INDENT2 " " |
| 75 | #define INDENT3 " " |
| 76 | #define INDENT4 " " |
| 77 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 78 | using android::base::StringPrintf; |
| 79 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 80 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 81 | |
| 82 | // Default input dispatching timeout if there is no focused application or paused window |
| 83 | // from which to determine an appropriate dispatching timeout. |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 84 | constexpr std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = |
| 85 | std::chrono::milliseconds(android::os::IInputConstants::DEFAULT_DISPATCHING_TIMEOUT_MILLIS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | |
| 87 | // Amount of time to allow for all pending events to be processed when an app switch |
| 88 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 89 | // 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] | 90 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | |
| 92 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 93 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 94 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | // 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] | 97 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 98 | |
| 99 | // Log a warning when an interception call takes longer than this to process. |
| 100 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 102 | // Additional key latency in case a connection is still processing some motion events. |
| 103 | // This will help with the case when a user touched a button that opens a new window, |
| 104 | // and gives us the chance to dispatch the key to this new window. |
| 105 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 108 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 109 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 110 | // Event log tags. See EventLogTags.logtags for reference |
| 111 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 112 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 113 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | static inline nsecs_t now() { |
| 115 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 116 | } |
| 117 | |
| 118 | static inline const char* toString(bool value) { |
| 119 | return value ? "true" : "false"; |
| 120 | } |
| 121 | |
| 122 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 123 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 124 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static bool isValidKeyAction(int32_t action) { |
| 128 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 129 | case AKEY_EVENT_ACTION_DOWN: |
| 130 | case AKEY_EVENT_ACTION_UP: |
| 131 | return true; |
| 132 | default: |
| 133 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 138 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | ALOGE("Key event has invalid action code 0x%x", action); |
| 140 | return false; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 145 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 146 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 147 | case AMOTION_EVENT_ACTION_DOWN: |
| 148 | case AMOTION_EVENT_ACTION_UP: |
| 149 | case AMOTION_EVENT_ACTION_CANCEL: |
| 150 | case AMOTION_EVENT_ACTION_MOVE: |
| 151 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 152 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 153 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 154 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 155 | case AMOTION_EVENT_ACTION_SCROLL: |
| 156 | return true; |
| 157 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 158 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 159 | int32_t index = getMotionEventActionPointerIndex(action); |
| 160 | return index >= 0 && index < pointerCount; |
| 161 | } |
| 162 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 163 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 164 | return actionButton != 0; |
| 165 | default: |
| 166 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 170 | static int64_t millis(std::chrono::nanoseconds t) { |
| 171 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 172 | } |
| 173 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 174 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 175 | const PointerProperties* pointerProperties) { |
| 176 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 177 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 178 | return false; |
| 179 | } |
| 180 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 181 | 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] | 182 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | BitSet32 pointerIdBits; |
| 186 | for (size_t i = 0; i < pointerCount; i++) { |
| 187 | int32_t id = pointerProperties[i].id; |
| 188 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 189 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 190 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | if (pointerIdBits.hasBit(id)) { |
| 194 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 195 | return false; |
| 196 | } |
| 197 | pointerIdBits.markBit(id); |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 202 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 204 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | bool first = true; |
| 209 | Region::const_iterator cur = region.begin(); |
| 210 | Region::const_iterator const tail = region.end(); |
| 211 | while (cur != tail) { |
| 212 | if (first) { |
| 213 | first = false; |
| 214 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 215 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 217 | 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] | 218 | cur++; |
| 219 | } |
| 220 | } |
| 221 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Find the entry in std::unordered_map by key, and return it. |
| 224 | * If the entry is not found, return a default constructed entry. |
| 225 | * |
| 226 | * Useful when the entries are vectors, since an empty vector will be returned |
| 227 | * if the entry is not found. |
| 228 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 229 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 230 | template <typename K, typename V> |
| 231 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 232 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 233 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 234 | } |
| 235 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 236 | /** |
| 237 | * Find the entry in std::unordered_map by value, and remove it. |
| 238 | * If more than one entry has the same value, then all matching |
| 239 | * key-value pairs will be removed. |
| 240 | * |
| 241 | * Return true if at least one value has been removed. |
| 242 | */ |
| 243 | template <typename K, typename V> |
| 244 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 245 | bool removed = false; |
| 246 | for (auto it = map.begin(); it != map.end();) { |
| 247 | if (it->second == value) { |
| 248 | it = map.erase(it); |
| 249 | removed = true; |
| 250 | } else { |
| 251 | it++; |
| 252 | } |
| 253 | } |
| 254 | return removed; |
| 255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 256 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 257 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 258 | if (first == second) { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | if (first == nullptr || second == nullptr) { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | return first->getToken() == second->getToken(); |
| 267 | } |
| 268 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 269 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 270 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 271 | } |
| 272 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 273 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 274 | EventEntry* eventEntry, |
| 275 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 276 | if (inputTarget.useDefaultPointerTransform()) { |
| 277 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 278 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 279 | inputTargetFlags, transform, |
| 280 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 284 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 285 | |
| 286 | PointerCoords pointerCoords[motionEntry.pointerCount]; |
| 287 | |
| 288 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 289 | // as long as all other pointers are normalized to the same value and the final DispatchEntry |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 290 | // uses the transform for the normalized pointer. |
| 291 | const ui::Transform& firstPointerTransform = |
| 292 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 293 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 294 | |
| 295 | // Iterate through all pointers in the event to normalize against the first. |
| 296 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 297 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 298 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 299 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 300 | |
| 301 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 302 | // First, apply the current pointer's transform to update the coordinates into |
| 303 | // window space. |
| 304 | pointerCoords[pointerIndex].transform(currTransform); |
| 305 | // Next, apply the inverse transform of the normalized coordinates so the |
| 306 | // current coordinates are transformed into the normalized coordinate space. |
| 307 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | MotionEntry* combinedMotionEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 311 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 312 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, |
| 313 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, |
| 314 | motionEntry.metaState, motionEntry.buttonState, |
| 315 | motionEntry.classification, motionEntry.edgeFlags, |
| 316 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 317 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 318 | motionEntry.downTime, motionEntry.pointerCount, |
| 319 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, |
| 320 | 0 /* yOffset */); |
| 321 | |
| 322 | if (motionEntry.injectionState) { |
| 323 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 324 | combinedMotionEntry->injectionState->refCount += 1; |
| 325 | } |
| 326 | |
| 327 | std::unique_ptr<DispatchEntry> dispatchEntry = |
| 328 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 329 | inputTargetFlags, firstPointerTransform, |
| 330 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 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; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 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); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 501 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 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 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 530 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 531 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 532 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 533 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 534 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 535 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 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 | |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1081 | void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus, |
| 1082 | std::string_view reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1083 | if (mPendingEvent != nullptr) { |
| 1084 | // Move the pending event to the front of the queue. This will give the chance |
| 1085 | // for the pending event to get dispatched to the newly focused window |
| 1086 | mInboundQueue.push_front(mPendingEvent); |
| 1087 | mPendingEvent = nullptr; |
| 1088 | } |
| 1089 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1090 | FocusEntry* focusEntry = |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1091 | new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus, reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1092 | |
| 1093 | // This event should go to the front of the queue, but behind all other focus events |
| 1094 | // Find the last focus event, and insert right after it |
| 1095 | std::deque<EventEntry*>::reverse_iterator it = |
| 1096 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1097 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); |
| 1098 | |
| 1099 | // Maintain the order of focus events. Insert the entry after all other focus events. |
| 1100 | mInboundQueue.insert(it.base(), focusEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1104 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1105 | if (channel == nullptr) { |
| 1106 | return; // Window has gone away |
| 1107 | } |
| 1108 | InputTarget target; |
| 1109 | target.inputChannel = channel; |
| 1110 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1111 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1112 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1113 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1114 | std::string reason = std::string("reason=").append(entry->reason); |
| 1115 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1116 | dispatchEventLocked(currentTime, entry, {target}); |
| 1117 | } |
| 1118 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1119 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1120 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1121 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1122 | if (!entry->dispatchInProgress) { |
| 1123 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1124 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1125 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1126 | if (mKeyRepeatState.lastKeyEntry && |
| 1127 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1128 | // We have seen two identical key downs in a row which indicates that the device |
| 1129 | // driver is automatically generating key repeats itself. We take note of the |
| 1130 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1131 | // we will not need to synthesize key repeats ourselves. |
| 1132 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1133 | resetKeyRepeatLocked(); |
| 1134 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1135 | } else { |
| 1136 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1137 | resetKeyRepeatLocked(); |
| 1138 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1139 | } |
| 1140 | mKeyRepeatState.lastKeyEntry = entry; |
| 1141 | entry->refCount += 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1142 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1143 | resetKeyRepeatLocked(); |
| 1144 | } |
| 1145 | |
| 1146 | if (entry->repeatCount == 1) { |
| 1147 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1148 | } else { |
| 1149 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1150 | } |
| 1151 | |
| 1152 | entry->dispatchInProgress = true; |
| 1153 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1154 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | // Handle case where the policy asked us to try again later last time. |
| 1158 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1159 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1160 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1161 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1162 | } |
| 1163 | return false; // wait until next wakeup |
| 1164 | } |
| 1165 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1166 | entry->interceptKeyWakeupTime = 0; |
| 1167 | } |
| 1168 | |
| 1169 | // Give the policy a chance to intercept the key. |
| 1170 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1171 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1172 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1173 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1174 | sp<InputWindowHandle> focusedWindowHandle = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1175 | getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(*entry)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1176 | if (focusedWindowHandle != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1177 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowHandle->getToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | } |
| 1179 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1180 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 | entry->refCount += 1; |
| 1182 | return false; // wait for the command to run |
| 1183 | } else { |
| 1184 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1185 | } |
| 1186 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1187 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1188 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1193 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1194 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1195 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1196 | : INPUT_EVENT_INJECTION_FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1197 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1202 | std::vector<InputTarget> inputTargets; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1203 | int32_t injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1204 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1209 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1214 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1215 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | |
| 1217 | // Dispatch the key. |
| 1218 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1219 | return true; |
| 1220 | } |
| 1221 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1222 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1224 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1225 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1226 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1227 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1228 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1229 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | #endif |
| 1231 | } |
| 1232 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1233 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1234 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1235 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1237 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1238 | entry->dispatchInProgress = true; |
| 1239 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1240 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1244 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1245 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1246 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1247 | : INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | return true; |
| 1249 | } |
| 1250 | |
| 1251 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1252 | |
| 1253 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1254 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1255 | |
| 1256 | bool conflictingPointerActions = false; |
| 1257 | int32_t injectionResult; |
| 1258 | if (isPointerEvent) { |
| 1259 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1260 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1261 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1262 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | } else { |
| 1264 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1265 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1266 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1267 | } |
| 1268 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1269 | return false; |
| 1270 | } |
| 1271 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1272 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1273 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| 1274 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1275 | return true; |
| 1276 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1278 | CancelationOptions::Mode mode(isPointerEvent |
| 1279 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1280 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1281 | CancelationOptions options(mode, "input event injection failed"); |
| 1282 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | return true; |
| 1284 | } |
| 1285 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1286 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1287 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1288 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1289 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1290 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1291 | mTouchStatesByDisplay.find(entry->displayId); |
| 1292 | if (it != mTouchStatesByDisplay.end()) { |
| 1293 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1294 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1295 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1296 | // the corresponding displays as well. |
| 1297 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1298 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1299 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1300 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1306 | // Dispatch the motion. |
| 1307 | if (conflictingPointerActions) { |
| 1308 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1309 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1311 | } |
| 1312 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1313 | return true; |
| 1314 | } |
| 1315 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1316 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1318 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1319 | ", policyFlags=0x%x, " |
| 1320 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1321 | "metaState=0x%x, buttonState=0x%x," |
| 1322 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1323 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1324 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1325 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1326 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1327 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1328 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1329 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1330 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1331 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1332 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1333 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1334 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1335 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1336 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1337 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1338 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1339 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1340 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1341 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 | } |
| 1343 | #endif |
| 1344 | } |
| 1345 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1346 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1347 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1348 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1349 | #if DEBUG_DISPATCH_CYCLE |
| 1350 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1351 | #endif |
| 1352 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1353 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1354 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1355 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1356 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1357 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1358 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1359 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1360 | sp<Connection> connection = |
| 1361 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1362 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1363 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1365 | if (DEBUG_FOCUS) { |
| 1366 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1367 | "is no longer registered with the input dispatcher.", |
| 1368 | inputTarget.inputChannel->getName().c_str()); |
| 1369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1374 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1375 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1376 | // If the policy decides to close the app, we will get a channel removal event via |
| 1377 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1378 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1379 | // pile up. |
| 1380 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1381 | connection->inputChannel->getName().c_str()); |
| 1382 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1383 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1384 | "application not responding"); |
| 1385 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1389 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1390 | if (DEBUG_FOCUS) { |
| 1391 | ALOGD("Resetting ANR timeouts."); |
| 1392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1393 | |
| 1394 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1395 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1396 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1399 | /** |
| 1400 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1401 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1402 | * Focused display is the display that the user most recently interacted with. |
| 1403 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1404 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1405 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1406 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1407 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1408 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1409 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1410 | break; |
| 1411 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1412 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1413 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1414 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1415 | break; |
| 1416 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1417 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1418 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1419 | case EventEntry::Type::DEVICE_RESET: { |
| 1420 | 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] | 1421 | return ADISPLAY_ID_NONE; |
| 1422 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1423 | } |
| 1424 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1425 | } |
| 1426 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1427 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1428 | const char* focusedWindowName) { |
| 1429 | if (mAnrTracker.empty()) { |
| 1430 | // already processed all events that we waited for |
| 1431 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1436 | // Start the timer |
| 1437 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " |
| 1438 | "focus to change", |
| 1439 | focusedWindowName); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1440 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1441 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1442 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1443 | return true; |
| 1444 | } |
| 1445 | |
| 1446 | // We still have pending events, and already started the timer |
| 1447 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1448 | return true; // Still waiting |
| 1449 | } |
| 1450 | |
| 1451 | // Waited too long, and some connection still hasn't processed all motions |
| 1452 | // Just send the key to the focused window |
| 1453 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1454 | focusedWindowName); |
| 1455 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1456 | return false; |
| 1457 | } |
| 1458 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1459 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1460 | const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1461 | std::vector<InputTarget>& inputTargets, |
| 1462 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1463 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1464 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1465 | int32_t displayId = getTargetDisplayId(entry); |
| 1466 | sp<InputWindowHandle> focusedWindowHandle = |
| 1467 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1468 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1469 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1470 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1471 | // If there is no currently focused window and no focused application |
| 1472 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1473 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1474 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1475 | "display %" PRId32 ".", |
| 1476 | EventEntry::typeToString(entry.type), displayId); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1477 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1480 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1481 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1482 | // start interacting with another application via touch (app switch). This code can be removed |
| 1483 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1484 | // an app is expected to have a focused window. |
| 1485 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1486 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1487 | // 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] | 1488 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1489 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1490 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1491 | mAwaitedFocusedApplication = focusedApplicationHandle; |
| 1492 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1493 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1494 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1495 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
| 1496 | return INPUT_EVENT_INJECTION_PENDING; |
| 1497 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1498 | // Already raised ANR. Drop the event |
| 1499 | ALOGE("Dropping %s event because there is no focused window", |
| 1500 | EventEntry::typeToString(entry.type)); |
| 1501 | return INPUT_EVENT_INJECTION_FAILED; |
| 1502 | } else { |
| 1503 | // Still waiting for the focused window |
| 1504 | return INPUT_EVENT_INJECTION_PENDING; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | // we have a valid, non-null focused window |
| 1509 | resetNoFocusedWindowTimeoutLocked(); |
| 1510 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1511 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1512 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1513 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1516 | if (focusedWindowHandle->getInfo()->paused) { |
| 1517 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
| 1518 | return INPUT_EVENT_INJECTION_PENDING; |
| 1519 | } |
| 1520 | |
| 1521 | // If the event is a key event, then we must wait for all previous events to |
| 1522 | // complete before delivering it because previous events may have the |
| 1523 | // side-effect of transferring focus to a different window and we want to |
| 1524 | // ensure that the following keys are sent to the new window. |
| 1525 | // |
| 1526 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1527 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1528 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1529 | // often anticipate pending UI changes when typing on a keyboard. |
| 1530 | // To obtain this behavior, we must serialize key events with respect to all |
| 1531 | // prior input events. |
| 1532 | if (entry.type == EventEntry::Type::KEY) { |
| 1533 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1534 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
| 1535 | return INPUT_EVENT_INJECTION_PENDING; |
| 1536 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1540 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1541 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1542 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | |
| 1544 | // Done. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1545 | return INPUT_EVENT_INJECTION_SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 | } |
| 1547 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1548 | /** |
| 1549 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1550 | * that are currently unresponsive. |
| 1551 | */ |
| 1552 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1553 | const std::vector<TouchedMonitor>& monitors) const { |
| 1554 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1555 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1556 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1557 | sp<Connection> connection = getConnectionLocked( |
| 1558 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1559 | if (connection == nullptr) { |
| 1560 | ALOGE("Could not find connection for monitor %s", |
| 1561 | monitor.monitor.inputChannel->getName().c_str()); |
| 1562 | return false; |
| 1563 | } |
| 1564 | if (!connection->responsive) { |
| 1565 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1566 | connection->inputChannel->getName().c_str()); |
| 1567 | return false; |
| 1568 | } |
| 1569 | return true; |
| 1570 | }); |
| 1571 | return responsiveMonitors; |
| 1572 | } |
| 1573 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1575 | const MotionEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1576 | std::vector<InputTarget>& inputTargets, |
| 1577 | nsecs_t* nextWakeupTime, |
| 1578 | bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1579 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1580 | enum InjectionPermission { |
| 1581 | INJECTION_PERMISSION_UNKNOWN, |
| 1582 | INJECTION_PERMISSION_GRANTED, |
| 1583 | INJECTION_PERMISSION_DENIED |
| 1584 | }; |
| 1585 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | // For security reasons, we defer updating the touch state until we are sure that |
| 1587 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1588 | int32_t displayId = entry.displayId; |
| 1589 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1591 | |
| 1592 | // Update the touch state as needed based on the properties of the touch event. |
| 1593 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1594 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1595 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1596 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1598 | // Copy current touch state into tempTouchState. |
| 1599 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1600 | // 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] | 1601 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1602 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1603 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1604 | mTouchStatesByDisplay.find(displayId); |
| 1605 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1606 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1607 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1610 | bool isSplit = tempTouchState.split; |
| 1611 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1612 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1613 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1614 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1615 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1616 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1617 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1618 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1619 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1620 | bool wrongDevice = false; |
| 1621 | if (newGesture) { |
| 1622 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1623 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1624 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1625 | "in display %" PRId32, |
| 1626 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1627 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1628 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1629 | switchedDevice = false; |
| 1630 | wrongDevice = true; |
| 1631 | goto Failed; |
| 1632 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1633 | tempTouchState.reset(); |
| 1634 | tempTouchState.down = down; |
| 1635 | tempTouchState.deviceId = entry.deviceId; |
| 1636 | tempTouchState.source = entry.source; |
| 1637 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1639 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1640 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1641 | "in display %" PRId32, |
| 1642 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1643 | // TODO: test multiple simultaneous input streams. |
| 1644 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1645 | switchedDevice = false; |
| 1646 | wrongDevice = true; |
| 1647 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1651 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1652 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1653 | int32_t x; |
| 1654 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1656 | // Always dispatch mouse events to cursor position. |
| 1657 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1658 | x = int32_t(entry.xCursorPosition); |
| 1659 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1660 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1661 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1662 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1663 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1664 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1665 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1666 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1667 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1668 | |
| 1669 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1670 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1671 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1672 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1674 | if (newTouchedWindowHandle != nullptr && |
| 1675 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1676 | // New window supports splitting, but we should never split mouse events. |
| 1677 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | } else if (isSplit) { |
| 1679 | // New window does not support splitting but we have already split events. |
| 1680 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1681 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1685 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1686 | // 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] | 1687 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1690 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1691 | ALOGI("Not sending touch event to %s because it is paused", |
| 1692 | newTouchedWindowHandle->getName().c_str()); |
| 1693 | newTouchedWindowHandle = nullptr; |
| 1694 | } |
| 1695 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1696 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1697 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1698 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1699 | if (!isResponsive) { |
| 1700 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1701 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1702 | newTouchedWindowHandle = nullptr; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1707 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1708 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1709 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1710 | 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] | 1711 | "(%d, %d) in display %" PRId32 ".", |
| 1712 | x, y, displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1713 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1714 | goto Failed; |
| 1715 | } |
| 1716 | |
| 1717 | if (newTouchedWindowHandle != nullptr) { |
| 1718 | // Set target flags. |
| 1719 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1720 | if (isSplit) { |
| 1721 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1723 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1724 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1725 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1726 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1727 | } |
| 1728 | |
| 1729 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1730 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 1731 | newHoverWindowHandle = nullptr; |
| 1732 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1733 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | // Update the temporary touch state. |
| 1737 | BitSet32 pointerIds; |
| 1738 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1739 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1740 | pointerIds.markBit(pointerId); |
| 1741 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1742 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1745 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1746 | } else { |
| 1747 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1748 | |
| 1749 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1750 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1751 | if (DEBUG_FOCUS) { |
| 1752 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1753 | "dropped the pointer down event in display %" PRId32, |
| 1754 | displayId); |
| 1755 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1756 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1757 | goto Failed; |
| 1758 | } |
| 1759 | |
| 1760 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1761 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1762 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1763 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1764 | 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] | 1765 | |
| 1766 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1767 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1768 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1769 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1770 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1771 | if (DEBUG_FOCUS) { |
| 1772 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1773 | oldTouchedWindowHandle->getName().c_str(), |
| 1774 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1775 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1776 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1777 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1778 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1779 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | |
| 1781 | // Make a slippery entrance into the new window. |
| 1782 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1783 | isSplit = true; |
| 1784 | } |
| 1785 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1786 | int32_t targetFlags = |
| 1787 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | if (isSplit) { |
| 1789 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1790 | } |
| 1791 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1792 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1793 | } |
| 1794 | |
| 1795 | BitSet32 pointerIds; |
| 1796 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1797 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1798 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1799 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1805 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 1806 | // when dispatching it as is to newTouchedWindowHandle. |
| 1807 | if (mLastHoverWindowHandle != nullptr && |
| 1808 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 1809 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1810 | #if DEBUG_HOVER |
| 1811 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1812 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1813 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1814 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1815 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1818 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 1819 | // when dispatching it as is to newTouchedWindowHandle. |
| 1820 | if (newHoverWindowHandle != nullptr && |
| 1821 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1822 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | #if DEBUG_HOVER |
| 1824 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1825 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1826 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1827 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1828 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1829 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | // Check permission to inject into all touched foreground windows and ensure there |
| 1834 | // is at least one touched foreground window. |
| 1835 | { |
| 1836 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1837 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1839 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1840 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1842 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1843 | goto Failed; |
| 1844 | } |
| 1845 | } |
| 1846 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1847 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1848 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1849 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1850 | "%" PRId32 " or gesture monitor to receive it.", |
| 1851 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1853 | goto Failed; |
| 1854 | } |
| 1855 | |
| 1856 | // Permission granted to injection into all touched foreground windows. |
| 1857 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1858 | } |
| 1859 | |
| 1860 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1861 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1862 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1863 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1864 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1865 | if (foregroundWindowHandle) { |
| 1866 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1867 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1868 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1869 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1870 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1871 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1872 | InputTarget::FLAG_ZERO_COORDS, |
| 1873 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1874 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1881 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1882 | // of the touch gesture. |
| 1883 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1884 | // engine only supports touch events. We would need to add a mechanism similar |
| 1885 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1886 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1887 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1888 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1889 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1890 | const std::vector<sp<InputWindowHandle>> windowHandles = |
| 1891 | getWindowHandlesLocked(displayId); |
| 1892 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1893 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1894 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1895 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1896 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1897 | .addOrUpdateWindow(windowHandle, |
| 1898 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1899 | InputTarget:: |
| 1900 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1901 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1902 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | // Success! Output targets. |
| 1909 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1910 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1911 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1912 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1913 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1916 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1917 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1918 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1921 | // Drop the outside or hover touch windows since we will not care about them |
| 1922 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1923 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1924 | |
| 1925 | Failed: |
| 1926 | // Check injection permission once and for all. |
| 1927 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1928 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1930 | } else { |
| 1931 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1932 | } |
| 1933 | } |
| 1934 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1935 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 1936 | return injectionResult; |
| 1937 | } |
| 1938 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1939 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1940 | if (!wrongDevice) { |
| 1941 | if (switchedDevice) { |
| 1942 | if (DEBUG_FOCUS) { |
| 1943 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1944 | } |
| 1945 | *outConflictingPointerActions = true; |
| 1946 | } |
| 1947 | |
| 1948 | if (isHoverAction) { |
| 1949 | // Started hovering, therefore no longer down. |
| 1950 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1951 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1952 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 1953 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1954 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1955 | *outConflictingPointerActions = true; |
| 1956 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1957 | tempTouchState.reset(); |
| 1958 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1959 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 1960 | tempTouchState.deviceId = entry.deviceId; |
| 1961 | tempTouchState.source = entry.source; |
| 1962 | tempTouchState.displayId = displayId; |
| 1963 | } |
| 1964 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 1965 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 1966 | // All pointers up or canceled. |
| 1967 | tempTouchState.reset(); |
| 1968 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1969 | // First pointer went down. |
| 1970 | if (oldState && oldState->down) { |
| 1971 | if (DEBUG_FOCUS) { |
| 1972 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1973 | } |
| 1974 | *outConflictingPointerActions = true; |
| 1975 | } |
| 1976 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1977 | // One pointer went up. |
| 1978 | if (isSplit) { |
| 1979 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1980 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1981 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1982 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 1983 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 1984 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1985 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1986 | if (touchedWindow.pointerIds.isEmpty()) { |
| 1987 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 1988 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1990 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1991 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1992 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1993 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1994 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1995 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1996 | // Save changes unless the action was scroll in which case the temporary touch |
| 1997 | // state was only valid for this one action. |
| 1998 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 1999 | if (tempTouchState.displayId >= 0) { |
| 2000 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2001 | } else { |
| 2002 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2003 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2006 | // Update hover state. |
| 2007 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2010 | return injectionResult; |
| 2011 | } |
| 2012 | |
| 2013 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2014 | int32_t targetFlags, BitSet32 pointerIds, |
| 2015 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2016 | std::vector<InputTarget>::iterator it = |
| 2017 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2018 | [&windowHandle](const InputTarget& inputTarget) { |
| 2019 | return inputTarget.inputChannel->getConnectionToken() == |
| 2020 | windowHandle->getToken(); |
| 2021 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2022 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2023 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (it == inputTargets.end()) { |
| 2026 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2027 | std::shared_ptr<InputChannel> inputChannel = |
| 2028 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2029 | if (inputChannel == nullptr) { |
| 2030 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2031 | return; |
| 2032 | } |
| 2033 | inputTarget.inputChannel = inputChannel; |
| 2034 | inputTarget.flags = targetFlags; |
| 2035 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2036 | inputTargets.push_back(inputTarget); |
| 2037 | it = inputTargets.end() - 1; |
| 2038 | } |
| 2039 | |
| 2040 | ALOG_ASSERT(it->flags == targetFlags); |
| 2041 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2042 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2043 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2044 | } |
| 2045 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2046 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2047 | int32_t displayId, float xOffset, |
| 2048 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2049 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2050 | mGlobalMonitorsByDisplay.find(displayId); |
| 2051 | |
| 2052 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2053 | const std::vector<Monitor>& monitors = it->second; |
| 2054 | for (const Monitor& monitor : monitors) { |
| 2055 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2057 | } |
| 2058 | } |
| 2059 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2060 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2061 | float yOffset, |
| 2062 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2063 | InputTarget target; |
| 2064 | target.inputChannel = monitor.inputChannel; |
| 2065 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2066 | ui::Transform t; |
| 2067 | t.set(xOffset, yOffset); |
| 2068 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2069 | inputTargets.push_back(target); |
| 2070 | } |
| 2071 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2073 | const InjectionState* injectionState) { |
| 2074 | if (injectionState && |
| 2075 | (windowHandle == nullptr || |
| 2076 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2077 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2078 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | 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] | 2080 | "owned by uid %d", |
| 2081 | injectionState->injectorPid, injectionState->injectorUid, |
| 2082 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | } else { |
| 2084 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2085 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2086 | } |
| 2087 | return false; |
| 2088 | } |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2092 | /** |
| 2093 | * Indicate whether one window handle should be considered as obscuring |
| 2094 | * another window handle. We only check a few preconditions. Actually |
| 2095 | * checking the bounds is left to the caller. |
| 2096 | */ |
| 2097 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2098 | const sp<InputWindowHandle>& otherHandle) { |
| 2099 | // Compare by token so cloned layers aren't counted |
| 2100 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2101 | return false; |
| 2102 | } |
| 2103 | auto info = windowHandle->getInfo(); |
| 2104 | auto otherInfo = otherHandle->getInfo(); |
| 2105 | if (!otherInfo->visible) { |
| 2106 | return false; |
Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2107 | } else if (info->ownerPid == otherInfo->ownerPid) { |
| 2108 | // If ownerPid is the same we don't generate occlusion events as there |
| 2109 | // is no in-process security boundary. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2110 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2111 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2112 | return false; |
| 2113 | } else if (otherInfo->displayId != info->displayId) { |
| 2114 | return false; |
| 2115 | } |
| 2116 | return true; |
| 2117 | } |
| 2118 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2119 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2120 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2121 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2122 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
| 2123 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2124 | if (windowHandle == otherHandle) { |
| 2125 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2127 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2128 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2129 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2130 | return true; |
| 2131 | } |
| 2132 | } |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2136 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2137 | int32_t displayId = windowHandle->getInfo()->displayId; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2138 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2139 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2140 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2141 | if (windowHandle == otherHandle) { |
| 2142 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2143 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2144 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2145 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2146 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2147 | return true; |
| 2148 | } |
| 2149 | } |
| 2150 | return false; |
| 2151 | } |
| 2152 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2153 | std::string InputDispatcher::getApplicationWindowLabel( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2154 | const std::shared_ptr<InputApplicationHandle>& applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2155 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2156 | if (applicationHandle != nullptr) { |
| 2157 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2158 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2159 | } else { |
| 2160 | return applicationHandle->getName(); |
| 2161 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2162 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2163 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2164 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2165 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2169 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2170 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2171 | // Focus events are passed to apps, but do not represent user activity. |
| 2172 | return; |
| 2173 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2174 | int32_t displayId = getTargetDisplayId(eventEntry); |
| 2175 | sp<InputWindowHandle> focusedWindowHandle = |
| 2176 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 2177 | if (focusedWindowHandle != nullptr) { |
| 2178 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2179 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2180 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2181 | 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] | 2182 | #endif |
| 2183 | return; |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2188 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2189 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2190 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2191 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2192 | return; |
| 2193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2194 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2195 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2196 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2197 | } |
| 2198 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2200 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2201 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2202 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2203 | return; |
| 2204 | } |
| 2205 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2206 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2207 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2208 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2209 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2210 | case EventEntry::Type::DEVICE_RESET: { |
| 2211 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2212 | EventEntry::typeToString(eventEntry.type)); |
| 2213 | break; |
| 2214 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2215 | } |
| 2216 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2217 | std::unique_ptr<CommandEntry> commandEntry = |
| 2218 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2219 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2220 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2221 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2225 | const sp<Connection>& connection, |
| 2226 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2227 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2228 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2229 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2230 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2231 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2232 | ATRACE_NAME(message.c_str()); |
| 2233 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | #if DEBUG_DISPATCH_CYCLE |
| 2235 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2236 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2237 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2238 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2239 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 | #endif |
| 2241 | |
| 2242 | // Skip this event if the connection status is not normal. |
| 2243 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2244 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2245 | #if DEBUG_DISPATCH_CYCLE |
| 2246 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2247 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2248 | #endif |
| 2249 | return; |
| 2250 | } |
| 2251 | |
| 2252 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2253 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2254 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2255 | "Entry type %s should not have FLAG_SPLIT", |
| 2256 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2257 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2258 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2259 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2260 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2261 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2262 | if (!splitMotionEntry) { |
| 2263 | return; // split event was dropped |
| 2264 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2265 | if (DEBUG_FOCUS) { |
| 2266 | ALOGD("channel '%s' ~ Split motion event.", |
| 2267 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2268 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2269 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2270 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2271 | splitMotionEntry->release(); |
| 2272 | return; |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2277 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2278 | } |
| 2279 | |
| 2280 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2281 | const sp<Connection>& connection, |
| 2282 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2283 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2284 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2285 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2286 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2287 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2288 | ATRACE_NAME(message.c_str()); |
| 2289 | } |
| 2290 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2291 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2292 | |
| 2293 | // Enqueue dispatch entries for the requested modes. |
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_HOVER_EXIT); |
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_OUTSIDE); |
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_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2300 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2301 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2302 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2303 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2304 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2305 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2306 | |
| 2307 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2308 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2309 | startDispatchCycleLocked(currentTime, connection); |
| 2310 | } |
| 2311 | } |
| 2312 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2313 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2314 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2315 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2316 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2317 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2318 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2319 | connection->getInputChannelName().c_str(), |
| 2320 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2321 | ATRACE_NAME(message.c_str()); |
| 2322 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2323 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | if (!(inputTargetFlags & dispatchMode)) { |
| 2325 | return; |
| 2326 | } |
| 2327 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2328 | |
| 2329 | // This is a new event. |
| 2330 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2331 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2332 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2334 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2335 | // different EventEntry than what was passed in. |
| 2336 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2337 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2338 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2339 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2340 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2341 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2342 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2343 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2345 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2346 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2348 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2349 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2350 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2351 | return; // skip the inconsistent event |
| 2352 | } |
| 2353 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2354 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2356 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2357 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2358 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2359 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2360 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2361 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2362 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2363 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2364 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2365 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2366 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2367 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2368 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2369 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2370 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2371 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2372 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2373 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2374 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2375 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2376 | } |
| 2377 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2378 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2379 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2380 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2381 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2382 | "event", |
| 2383 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2385 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2386 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2387 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2388 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2389 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2390 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2391 | } |
| 2392 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2393 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2394 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2395 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2396 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2397 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2398 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2399 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2400 | "event", |
| 2401 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2402 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2403 | return; // skip the inconsistent event |
| 2404 | } |
| 2405 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2406 | dispatchEntry->resolvedEventId = |
| 2407 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2408 | ? mIdGenerator.nextId() |
| 2409 | : motionEntry.id; |
| 2410 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2411 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2412 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2413 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2414 | ATRACE_NAME(message.c_str()); |
| 2415 | } |
| 2416 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2417 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2418 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2419 | |
| 2420 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2421 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2422 | case EventEntry::Type::FOCUS: { |
| 2423 | break; |
| 2424 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2425 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2426 | case EventEntry::Type::DEVICE_RESET: { |
| 2427 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2428 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2429 | break; |
| 2430 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | // Remember that we are waiting for this dispatch to complete. |
| 2434 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2435 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2439 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2440 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2443 | /** |
| 2444 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2445 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2446 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2447 | * We will see both launcher and wallpaper in that list. |
| 2448 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2449 | * until the set of interacted connections changes. |
| 2450 | * |
| 2451 | * The following items are skipped, to reduce the logspam: |
| 2452 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2453 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2454 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2455 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2456 | * Both of those ACTION_UP events would not be logged |
| 2457 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events |
| 2458 | * will not be logged. This is omitted to reduce the amount of data printed. |
| 2459 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore |
| 2460 | * gesture monitor is the only connection receiving the remainder of the gesture. |
| 2461 | */ |
| 2462 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2463 | const std::vector<InputTarget>& targets) { |
| 2464 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2465 | if (entry.type == EventEntry::Type::KEY) { |
| 2466 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2467 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2468 | return; |
| 2469 | } |
| 2470 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2471 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2472 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2473 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2474 | return; |
| 2475 | } |
| 2476 | } else { |
| 2477 | return; // Not a key or a motion |
| 2478 | } |
| 2479 | |
| 2480 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; |
| 2481 | std::vector<sp<Connection>> newConnections; |
| 2482 | for (const InputTarget& target : targets) { |
| 2483 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2484 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2485 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2486 | } |
| 2487 | |
| 2488 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2489 | sp<Connection> connection = getConnectionLocked(token); |
| 2490 | if (connection == nullptr || connection->monitor) { |
| 2491 | continue; // We only need to keep track of the non-monitor connections. |
| 2492 | } |
| 2493 | newConnectionTokens.insert(std::move(token)); |
| 2494 | newConnections.emplace_back(connection); |
| 2495 | } |
| 2496 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 2497 | return; // no change |
| 2498 | } |
| 2499 | mInteractionConnectionTokens = newConnectionTokens; |
| 2500 | |
| 2501 | std::string windowList; |
| 2502 | for (const sp<Connection>& connection : newConnections) { |
| 2503 | windowList += connection->getWindowName() + ", "; |
| 2504 | } |
| 2505 | std::string message = "Interaction with windows: " + windowList; |
| 2506 | if (windowList.empty()) { |
| 2507 | message += "<none>"; |
| 2508 | } |
| 2509 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 2510 | } |
| 2511 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2512 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2513 | const sp<IBinder>& newToken) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2514 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2515 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2516 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2517 | return; |
| 2518 | } |
| 2519 | |
| 2520 | sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); |
| 2521 | if (inputWindowHandle == nullptr) { |
| 2522 | return; |
| 2523 | } |
| 2524 | |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2525 | sp<InputWindowHandle> focusedWindowHandle = |
Tiger Huang | 0683fe7 | 2019-06-03 21:50:55 +0800 | [diff] [blame] | 2526 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2527 | |
| 2528 | bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; |
| 2529 | |
| 2530 | if (!hasFocusChanged) { |
| 2531 | return; |
| 2532 | } |
| 2533 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2534 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2535 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2536 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2537 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2541 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2542 | if (ATRACE_ENABLED()) { |
| 2543 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2544 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2545 | ATRACE_NAME(message.c_str()); |
| 2546 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2547 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2548 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2549 | #endif |
| 2550 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2551 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2552 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2553 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2554 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2555 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2556 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2557 | |
| 2558 | // Publish the event. |
| 2559 | status_t status; |
| 2560 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2561 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2562 | case EventEntry::Type::KEY: { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2563 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2564 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2565 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2566 | // Publish the key event. |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2567 | status = |
| 2568 | connection->inputPublisher |
| 2569 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2570 | keyEntry->deviceId, keyEntry->source, |
| 2571 | keyEntry->displayId, std::move(hmac), |
| 2572 | dispatchEntry->resolvedAction, |
| 2573 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2574 | keyEntry->scanCode, keyEntry->metaState, |
| 2575 | keyEntry->repeatCount, keyEntry->downTime, |
| 2576 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2577 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2578 | } |
| 2579 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2580 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2581 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2582 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2583 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2584 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2585 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2586 | // Set the X and Y offset and X and Y scale depending on the input source. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2587 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2588 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2589 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2590 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2591 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2592 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2593 | // Don't apply window scale here since we don't want scale to affect raw |
| 2594 | // coordinates. The scale will be sent back to the client and applied |
| 2595 | // later when requesting relative coordinates. |
| 2596 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2597 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2598 | } |
| 2599 | usingCoords = scaledCoords; |
| 2600 | } |
| 2601 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2602 | // We don't want the dispatch target to know. |
| 2603 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2604 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2605 | scaledCoords[i].clear(); |
| 2606 | } |
| 2607 | usingCoords = scaledCoords; |
| 2608 | } |
| 2609 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2610 | |
| 2611 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2612 | |
| 2613 | // Publish the motion event. |
| 2614 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2615 | .publishMotionEvent(dispatchEntry->seq, |
| 2616 | dispatchEntry->resolvedEventId, |
| 2617 | motionEntry->deviceId, motionEntry->source, |
| 2618 | motionEntry->displayId, std::move(hmac), |
| 2619 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2620 | motionEntry->actionButton, |
| 2621 | dispatchEntry->resolvedFlags, |
| 2622 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2623 | motionEntry->buttonState, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2624 | motionEntry->classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2625 | dispatchEntry->transform, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2626 | motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2627 | motionEntry->yPrecision, |
| 2628 | motionEntry->xCursorPosition, |
| 2629 | motionEntry->yCursorPosition, |
| 2630 | motionEntry->downTime, motionEntry->eventTime, |
| 2631 | motionEntry->pointerCount, |
| 2632 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2633 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2634 | break; |
| 2635 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2636 | case EventEntry::Type::FOCUS: { |
| 2637 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2638 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2639 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2640 | focusEntry->hasFocus, |
| 2641 | mInTouchMode); |
| 2642 | break; |
| 2643 | } |
| 2644 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2645 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2646 | case EventEntry::Type::DEVICE_RESET: { |
| 2647 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2648 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2649 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2650 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | // Check the result. |
| 2654 | if (status) { |
| 2655 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2656 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2657 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2658 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2659 | "should be empty and we shouldn't have any problems writing an " |
| 2660 | "event to it, status=%d", |
| 2661 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2662 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2663 | } else { |
| 2664 | // Pipe is full and we are waiting for the app to finish process some events |
| 2665 | // before sending more events to it. |
| 2666 | #if DEBUG_DISPATCH_CYCLE |
| 2667 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2668 | "waiting for the application to catch up", |
| 2669 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2670 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2671 | } |
| 2672 | } else { |
| 2673 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2674 | "status=%d", |
| 2675 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2676 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2677 | } |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2682 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2683 | connection->outboundQueue.end(), |
| 2684 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2685 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2686 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2687 | if (connection->responsive) { |
| 2688 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 2689 | connection->inputChannel->getConnectionToken()); |
| 2690 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2691 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2692 | } |
| 2693 | } |
| 2694 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2695 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2696 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 2697 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2698 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 2699 | // Only sign events up and down events as the purely move events |
| 2700 | // are tied to their up/down counterparts so signing would be redundant. |
| 2701 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 2702 | verifiedEvent.actionMasked = actionMasked; |
| 2703 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 2704 | return mHmacKeyManager.sign(verifiedEvent); |
| 2705 | } |
| 2706 | return INVALID_HMAC; |
| 2707 | } |
| 2708 | |
| 2709 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2710 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 2711 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 2712 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2713 | verifiedEvent.action = dispatchEntry.resolvedAction; |
| 2714 | return mHmacKeyManager.sign(verifiedEvent); |
| 2715 | } |
| 2716 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2718 | const sp<Connection>& connection, uint32_t seq, |
| 2719 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2720 | #if DEBUG_DISPATCH_CYCLE |
| 2721 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2722 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2723 | #endif |
| 2724 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2725 | if (connection->status == Connection::STATUS_BROKEN || |
| 2726 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | return; |
| 2728 | } |
| 2729 | |
| 2730 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2731 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2732 | } |
| 2733 | |
| 2734 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2735 | const sp<Connection>& connection, |
| 2736 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2737 | #if DEBUG_DISPATCH_CYCLE |
| 2738 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2739 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2740 | #endif |
| 2741 | |
| 2742 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2743 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2744 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2745 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2746 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2747 | |
| 2748 | // The connection appears to be unrecoverably broken. |
| 2749 | // Ignore already broken or zombie connections. |
| 2750 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2751 | connection->status = Connection::STATUS_BROKEN; |
| 2752 | |
| 2753 | if (notify) { |
| 2754 | // Notify other system components. |
| 2755 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2760 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2761 | while (!queue.empty()) { |
| 2762 | DispatchEntry* dispatchEntry = queue.front(); |
| 2763 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2764 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2768 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2769 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2770 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2771 | } |
| 2772 | delete dispatchEntry; |
| 2773 | } |
| 2774 | |
| 2775 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2776 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2777 | |
| 2778 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2779 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2781 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2782 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2783 | "fd=%d, events=0x%x", |
| 2784 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2785 | return 0; // remove the callback |
| 2786 | } |
| 2787 | |
| 2788 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2789 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2790 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2791 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2792 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2793 | "events=0x%x", |
| 2794 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2795 | return 1; |
| 2796 | } |
| 2797 | |
| 2798 | nsecs_t currentTime = now(); |
| 2799 | bool gotOne = false; |
| 2800 | status_t status; |
| 2801 | for (;;) { |
| 2802 | uint32_t seq; |
| 2803 | bool handled; |
| 2804 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2805 | if (status) { |
| 2806 | break; |
| 2807 | } |
| 2808 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2809 | gotOne = true; |
| 2810 | } |
| 2811 | if (gotOne) { |
| 2812 | d->runCommandsLockedInterruptible(); |
| 2813 | if (status == WOULD_BLOCK) { |
| 2814 | return 1; |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2819 | if (notify) { |
| 2820 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2821 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2822 | } |
| 2823 | } else { |
| 2824 | // Monitor channels are never explicitly unregistered. |
| 2825 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2826 | // about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2827 | const bool stillHaveWindowHandle = |
| 2828 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 2829 | nullptr; |
| 2830 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | if (notify) { |
| 2832 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2833 | "events=0x%x", |
| 2834 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | // Unregister the channel. |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2839 | d->unregisterInputChannelLocked(*connection->inputChannel, notify); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2840 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2841 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2842 | } |
| 2843 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2844 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2845 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2846 | for (const auto& pair : mConnectionsByFd) { |
| 2847 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2851 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2852 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2853 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 2854 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 2855 | } |
| 2856 | |
| 2857 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2858 | const CancelationOptions& options, |
| 2859 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 2860 | for (const auto& it : monitorsByDisplay) { |
| 2861 | const std::vector<Monitor>& monitors = it.second; |
| 2862 | for (const Monitor& monitor : monitors) { |
| 2863 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2864 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2869 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2870 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2871 | if (connection == nullptr) { |
| 2872 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2874 | |
| 2875 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2879 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2880 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2881 | return; |
| 2882 | } |
| 2883 | |
| 2884 | nsecs_t currentTime = now(); |
| 2885 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2886 | std::vector<EventEntry*> cancelationEvents = |
| 2887 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2888 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2889 | if (cancelationEvents.empty()) { |
| 2890 | return; |
| 2891 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2893 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 2894 | "with reality: %s, mode=%d.", |
| 2895 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 2896 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2897 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2898 | |
| 2899 | InputTarget target; |
| 2900 | sp<InputWindowHandle> windowHandle = |
| 2901 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2902 | if (windowHandle != nullptr) { |
| 2903 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2904 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2905 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2906 | } |
| 2907 | target.inputChannel = connection->inputChannel; |
| 2908 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2909 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2910 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2911 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 2912 | switch (cancelationEventEntry->type) { |
| 2913 | case EventEntry::Type::KEY: { |
| 2914 | logOutboundKeyDetails("cancel - ", |
| 2915 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 2916 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2917 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2918 | case EventEntry::Type::MOTION: { |
| 2919 | logOutboundMotionDetails("cancel - ", |
| 2920 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 2921 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2922 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2923 | case EventEntry::Type::FOCUS: { |
| 2924 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 2925 | break; |
| 2926 | } |
| 2927 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2928 | case EventEntry::Type::DEVICE_RESET: { |
| 2929 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2930 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 2931 | break; |
| 2932 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2933 | } |
| 2934 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2935 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2936 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2937 | |
| 2938 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2940 | |
| 2941 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2942 | } |
| 2943 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2944 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 2945 | const sp<Connection>& connection) { |
| 2946 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2947 | return; |
| 2948 | } |
| 2949 | |
| 2950 | nsecs_t currentTime = now(); |
| 2951 | |
| 2952 | std::vector<EventEntry*> downEvents = |
| 2953 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 2954 | |
| 2955 | if (downEvents.empty()) { |
| 2956 | return; |
| 2957 | } |
| 2958 | |
| 2959 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2960 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 2961 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 2962 | #endif |
| 2963 | |
| 2964 | InputTarget target; |
| 2965 | sp<InputWindowHandle> windowHandle = |
| 2966 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2967 | if (windowHandle != nullptr) { |
| 2968 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2969 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2970 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2971 | } |
| 2972 | target.inputChannel = connection->inputChannel; |
| 2973 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2974 | |
| 2975 | for (EventEntry* downEventEntry : downEvents) { |
| 2976 | switch (downEventEntry->type) { |
| 2977 | case EventEntry::Type::MOTION: { |
| 2978 | logOutboundMotionDetails("down - ", |
| 2979 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 2980 | break; |
| 2981 | } |
| 2982 | |
| 2983 | case EventEntry::Type::KEY: |
| 2984 | case EventEntry::Type::FOCUS: |
| 2985 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2986 | case EventEntry::Type::DEVICE_RESET: { |
| 2987 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2988 | EventEntry::typeToString(downEventEntry->type)); |
| 2989 | break; |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 2994 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2995 | |
| 2996 | downEventEntry->release(); |
| 2997 | } |
| 2998 | |
| 2999 | startDispatchCycleLocked(currentTime, connection); |
| 3000 | } |
| 3001 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3002 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 3003 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | ALOG_ASSERT(pointerIds.value != 0); |
| 3005 | |
| 3006 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3007 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3008 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3009 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3010 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3011 | uint32_t splitPointerCount = 0; |
| 3012 | |
| 3013 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3014 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3015 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3016 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3017 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3018 | if (pointerIds.hasBit(pointerId)) { |
| 3019 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3020 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3021 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3022 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3023 | splitPointerCount += 1; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | if (splitPointerCount != pointerIds.count()) { |
| 3028 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3029 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3030 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3031 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3032 | // in this way. |
| 3033 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3034 | "we expected there to be %d pointers. This probably means we received " |
| 3035 | "a broken sequence of pointer ids from the input device.", |
| 3036 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3037 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3038 | } |
| 3039 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3040 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3041 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3042 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3043 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3044 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3045 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3046 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3048 | if (pointerIds.hasBit(pointerId)) { |
| 3049 | if (pointerIds.count() == 1) { |
| 3050 | // The first/last pointer went down/up. |
| 3051 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3052 | ? AMOTION_EVENT_ACTION_DOWN |
| 3053 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3054 | } else { |
| 3055 | // A secondary pointer went down/up. |
| 3056 | uint32_t splitPointerIndex = 0; |
| 3057 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3058 | splitPointerIndex += 1; |
| 3059 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3060 | action = maskedAction | |
| 3061 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3062 | } |
| 3063 | } else { |
| 3064 | // An unrelated pointer changed. |
| 3065 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3066 | } |
| 3067 | } |
| 3068 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3069 | int32_t newId = mIdGenerator.nextId(); |
| 3070 | if (ATRACE_ENABLED()) { |
| 3071 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3072 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3073 | originalMotionEntry.id, newId); |
| 3074 | ATRACE_NAME(message.c_str()); |
| 3075 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3076 | MotionEntry* splitMotionEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3077 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 3078 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 3079 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3080 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 3081 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 3082 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 3083 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 3084 | originalMotionEntry.xCursorPosition, |
| 3085 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3086 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3087 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3088 | if (originalMotionEntry.injectionState) { |
| 3089 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3090 | splitMotionEntry->injectionState->refCount += 1; |
| 3091 | } |
| 3092 | |
| 3093 | return splitMotionEntry; |
| 3094 | } |
| 3095 | |
| 3096 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3097 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3098 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 | #endif |
| 3100 | |
| 3101 | bool needWake; |
| 3102 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3103 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3104 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3105 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3106 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | needWake = enqueueInboundEventLocked(newEntry); |
| 3108 | } // release lock |
| 3109 | |
| 3110 | if (needWake) { |
| 3111 | mLooper->wake(); |
| 3112 | } |
| 3113 | } |
| 3114 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3115 | /** |
| 3116 | * If one of the meta shortcuts is detected, process them here: |
| 3117 | * Meta + Backspace -> generate BACK |
| 3118 | * Meta + Enter -> generate HOME |
| 3119 | * This will potentially overwrite keyCode and metaState. |
| 3120 | */ |
| 3121 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3122 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3123 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3124 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3125 | if (keyCode == AKEYCODE_DEL) { |
| 3126 | newKeyCode = AKEYCODE_BACK; |
| 3127 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3128 | newKeyCode = AKEYCODE_HOME; |
| 3129 | } |
| 3130 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3131 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3132 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3133 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3134 | keyCode = newKeyCode; |
| 3135 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3136 | } |
| 3137 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3138 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3139 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3140 | // 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] | 3141 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3142 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3143 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3144 | if (replacementIt != mReplacedKeys.end()) { |
| 3145 | keyCode = replacementIt->second; |
| 3146 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3147 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3148 | } |
| 3149 | } |
| 3150 | } |
| 3151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3152 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3153 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3154 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3155 | "policyFlags=0x%x, action=0x%x, " |
| 3156 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3157 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3158 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3159 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3160 | #endif |
| 3161 | if (!validateKeyEvent(args->action)) { |
| 3162 | return; |
| 3163 | } |
| 3164 | |
| 3165 | uint32_t policyFlags = args->policyFlags; |
| 3166 | int32_t flags = args->flags; |
| 3167 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3168 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3169 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3170 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3172 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3173 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3174 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3175 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3176 | metaState |= AMETA_FUNCTION_ON; |
| 3177 | } |
| 3178 | |
| 3179 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3180 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3181 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3182 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3183 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3184 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3185 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3186 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3187 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3188 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3189 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3190 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3191 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3192 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3193 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3194 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3195 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3196 | bool needWake; |
| 3197 | { // acquire lock |
| 3198 | mLock.lock(); |
| 3199 | |
| 3200 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3201 | mLock.unlock(); |
| 3202 | |
| 3203 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3204 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3205 | return; // event was consumed by the filter |
| 3206 | } |
| 3207 | |
| 3208 | mLock.lock(); |
| 3209 | } |
| 3210 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3211 | KeyEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3212 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3213 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3214 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3215 | |
| 3216 | needWake = enqueueInboundEventLocked(newEntry); |
| 3217 | mLock.unlock(); |
| 3218 | } // release lock |
| 3219 | |
| 3220 | if (needWake) { |
| 3221 | mLooper->wake(); |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3226 | return mInputFilterEnabled; |
| 3227 | } |
| 3228 | |
| 3229 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3230 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3231 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3232 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3233 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3234 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3235 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3236 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3237 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3238 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3239 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3241 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3242 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3243 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3244 | "orientation=%f", |
| 3245 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3246 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3247 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3248 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3249 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3250 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3251 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3252 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3253 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3254 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3255 | } |
| 3256 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3257 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3258 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3259 | return; |
| 3260 | } |
| 3261 | |
| 3262 | uint32_t policyFlags = args->policyFlags; |
| 3263 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3264 | |
| 3265 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3266 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3267 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3268 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3269 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3270 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3271 | |
| 3272 | bool needWake; |
| 3273 | { // acquire lock |
| 3274 | mLock.lock(); |
| 3275 | |
| 3276 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3277 | mLock.unlock(); |
| 3278 | |
| 3279 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3280 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3281 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3282 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3283 | args->metaState, args->buttonState, args->classification, transform, |
| 3284 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
| 3285 | args->yCursorPosition, args->downTime, args->eventTime, |
| 3286 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3287 | |
| 3288 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3289 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3290 | return; // event was consumed by the filter |
| 3291 | } |
| 3292 | |
| 3293 | mLock.lock(); |
| 3294 | } |
| 3295 | |
| 3296 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3297 | MotionEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3298 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3299 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3300 | args->flags, args->metaState, args->buttonState, |
| 3301 | args->classification, args->edgeFlags, args->xPrecision, |
| 3302 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3303 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3304 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | |
| 3306 | needWake = enqueueInboundEventLocked(newEntry); |
| 3307 | mLock.unlock(); |
| 3308 | } // release lock |
| 3309 | |
| 3310 | if (needWake) { |
| 3311 | mLooper->wake(); |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3316 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3320 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3321 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3322 | "switchMask=0x%08x", |
| 3323 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3324 | #endif |
| 3325 | |
| 3326 | uint32_t policyFlags = args->policyFlags; |
| 3327 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3328 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3332 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3333 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3334 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3335 | #endif |
| 3336 | |
| 3337 | bool needWake; |
| 3338 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3339 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3340 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3341 | DeviceResetEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3342 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | needWake = enqueueInboundEventLocked(newEntry); |
| 3344 | } // release lock |
| 3345 | |
| 3346 | if (needWake) { |
| 3347 | mLooper->wake(); |
| 3348 | } |
| 3349 | } |
| 3350 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3351 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 3352 | int32_t injectorUid, int32_t syncMode, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3353 | std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3354 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3355 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3356 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3357 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | #endif |
| 3359 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3360 | 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] | 3361 | |
| 3362 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3363 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3364 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3365 | } |
| 3366 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3367 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3368 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3369 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3370 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3371 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3372 | if (!validateKeyEvent(action)) { |
| 3373 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3374 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3375 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3376 | int32_t flags = incomingKey.getFlags(); |
| 3377 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3378 | int32_t metaState = incomingKey.getMetaState(); |
| 3379 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3380 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3381 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3382 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3383 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3384 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3385 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3387 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3388 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3389 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3390 | |
| 3391 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3392 | android::base::Timer t; |
| 3393 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3394 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3395 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3396 | std::to_string(t.duration().count()).c_str()); |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | mLock.lock(); |
| 3401 | KeyEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3402 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3403 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3404 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, |
| 3405 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3406 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3407 | injectedEntries.push(injectedEntry); |
| 3408 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3409 | } |
| 3410 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3411 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3412 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3413 | int32_t action = motionEvent->getAction(); |
| 3414 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3415 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3416 | int32_t actionButton = motionEvent->getActionButton(); |
| 3417 | int32_t displayId = motionEvent->getDisplayId(); |
| 3418 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
| 3419 | return INPUT_EVENT_INJECTION_FAILED; |
| 3420 | } |
| 3421 | |
| 3422 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3423 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3424 | android::base::Timer t; |
| 3425 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3426 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3427 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3428 | std::to_string(t.duration().count()).c_str()); |
| 3429 | } |
| 3430 | } |
| 3431 | |
| 3432 | mLock.lock(); |
| 3433 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3434 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3435 | MotionEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3436 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3437 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3438 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3439 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3440 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3441 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3442 | motionEvent->getRawXCursorPosition(), |
| 3443 | motionEvent->getRawYCursorPosition(), |
| 3444 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3445 | pointerProperties, samplePointerCoords, |
| 3446 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3447 | injectedEntries.push(injectedEntry); |
| 3448 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3449 | sampleEventTimes += 1; |
| 3450 | samplePointerCoords += pointerCount; |
| 3451 | MotionEntry* nextInjectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3452 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3453 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3454 | motionEvent->getDisplayId(), policyFlags, action, |
| 3455 | actionButton, motionEvent->getFlags(), |
| 3456 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3457 | motionEvent->getClassification(), |
| 3458 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3459 | motionEvent->getYPrecision(), |
| 3460 | motionEvent->getRawXCursorPosition(), |
| 3461 | motionEvent->getRawYCursorPosition(), |
| 3462 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3463 | pointerProperties, samplePointerCoords, |
| 3464 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3465 | injectedEntries.push(nextInjectedEntry); |
| 3466 | } |
| 3467 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3468 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3469 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3470 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3471 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3472 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 3476 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3477 | injectionState->injectionIsAsync = true; |
| 3478 | } |
| 3479 | |
| 3480 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3481 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 | |
| 3483 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3484 | while (!injectedEntries.empty()) { |
| 3485 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3486 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | mLock.unlock(); |
| 3490 | |
| 3491 | if (needWake) { |
| 3492 | mLooper->wake(); |
| 3493 | } |
| 3494 | |
| 3495 | int32_t injectionResult; |
| 3496 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3497 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3498 | |
| 3499 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3500 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3501 | } else { |
| 3502 | for (;;) { |
| 3503 | injectionResult = injectionState->injectionResult; |
| 3504 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3505 | break; |
| 3506 | } |
| 3507 | |
| 3508 | nsecs_t remainingTimeout = endTime - now(); |
| 3509 | if (remainingTimeout <= 0) { |
| 3510 | #if DEBUG_INJECTION |
| 3511 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3512 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3513 | #endif |
| 3514 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3515 | break; |
| 3516 | } |
| 3517 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3518 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3519 | } |
| 3520 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3521 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && |
| 3522 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3523 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3524 | #if DEBUG_INJECTION |
| 3525 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3526 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3527 | #endif |
| 3528 | nsecs_t remainingTimeout = endTime - now(); |
| 3529 | if (remainingTimeout <= 0) { |
| 3530 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3531 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3532 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3533 | #endif |
| 3534 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3535 | break; |
| 3536 | } |
| 3537 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3538 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | injectionState->release(); |
| 3544 | } // release lock |
| 3545 | |
| 3546 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3547 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3548 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3549 | #endif |
| 3550 | |
| 3551 | return injectionResult; |
| 3552 | } |
| 3553 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3554 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3555 | std::array<uint8_t, 32> calculatedHmac; |
| 3556 | std::unique_ptr<VerifiedInputEvent> result; |
| 3557 | switch (event.getType()) { |
| 3558 | case AINPUT_EVENT_TYPE_KEY: { |
| 3559 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3560 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3561 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
| 3562 | calculatedHmac = mHmacKeyManager.sign(verifiedKeyEvent); |
| 3563 | break; |
| 3564 | } |
| 3565 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3566 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3567 | VerifiedMotionEvent verifiedMotionEvent = |
| 3568 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3569 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
| 3570 | calculatedHmac = mHmacKeyManager.sign(verifiedMotionEvent); |
| 3571 | break; |
| 3572 | } |
| 3573 | default: { |
| 3574 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3575 | return nullptr; |
| 3576 | } |
| 3577 | } |
| 3578 | if (calculatedHmac == INVALID_HMAC) { |
| 3579 | return nullptr; |
| 3580 | } |
| 3581 | if (calculatedHmac != event.getHmac()) { |
| 3582 | return nullptr; |
| 3583 | } |
| 3584 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3585 | } |
| 3586 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3587 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3588 | return injectorUid == 0 || |
| 3589 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3590 | } |
| 3591 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3592 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3593 | InjectionState* injectionState = entry->injectionState; |
| 3594 | if (injectionState) { |
| 3595 | #if DEBUG_INJECTION |
| 3596 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3597 | "injectorPid=%d, injectorUid=%d", |
| 3598 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | #endif |
| 3600 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3601 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3602 | // Log the outcome since the injector did not wait for the injection result. |
| 3603 | switch (injectionResult) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3604 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3605 | ALOGV("Asynchronous input event injection succeeded."); |
| 3606 | break; |
| 3607 | case INPUT_EVENT_INJECTION_FAILED: |
| 3608 | ALOGW("Asynchronous input event injection failed."); |
| 3609 | break; |
| 3610 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3611 | ALOGW("Asynchronous input event injection permission denied."); |
| 3612 | break; |
| 3613 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3614 | ALOGW("Asynchronous input event injection timed out."); |
| 3615 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3620 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3624 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3625 | InjectionState* injectionState = entry->injectionState; |
| 3626 | if (injectionState) { |
| 3627 | injectionState->pendingForegroundDispatches += 1; |
| 3628 | } |
| 3629 | } |
| 3630 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3631 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | InjectionState* injectionState = entry->injectionState; |
| 3633 | if (injectionState) { |
| 3634 | injectionState->pendingForegroundDispatches -= 1; |
| 3635 | |
| 3636 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3637 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | } |
| 3639 | } |
| 3640 | } |
| 3641 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3642 | std::vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked( |
| 3643 | int32_t displayId) const { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3644 | return getValueByKey(mWindowHandlesByDisplay, displayId); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3645 | } |
| 3646 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3647 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3648 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3649 | if (windowHandleToken == nullptr) { |
| 3650 | return nullptr; |
| 3651 | } |
| 3652 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3653 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3654 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3655 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3656 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3657 | return windowHandle; |
| 3658 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3659 | } |
| 3660 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3661 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3662 | } |
| 3663 | |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3664 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 3665 | for (auto& it : mWindowHandlesByDisplay) { |
| 3666 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
| 3667 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3668 | if (handle->getId() == windowHandle->getId() && |
| 3669 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3670 | if (windowHandle->getInfo()->displayId != it.first) { |
| 3671 | ALOGE("Found window %s in display %" PRId32 |
| 3672 | ", but it should belong to display %" PRId32, |
| 3673 | windowHandle->getName().c_str(), it.first, |
| 3674 | windowHandle->getInfo()->displayId); |
| 3675 | } |
| 3676 | return true; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3677 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | return false; |
| 3681 | } |
| 3682 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3683 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 3684 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 3685 | const bool noInputChannel = |
| 3686 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3687 | if (connection != nullptr && noInputChannel) { |
| 3688 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 3689 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 3690 | return false; |
| 3691 | } |
| 3692 | |
| 3693 | if (connection == nullptr) { |
| 3694 | if (!noInputChannel) { |
| 3695 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 3696 | } |
| 3697 | return false; |
| 3698 | } |
| 3699 | if (!connection->responsive) { |
| 3700 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 3701 | return false; |
| 3702 | } |
| 3703 | return true; |
| 3704 | } |
| 3705 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3706 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 3707 | const sp<IBinder>& token) const { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3708 | size_t count = mInputChannelsByToken.count(token); |
| 3709 | if (count == 0) { |
| 3710 | return nullptr; |
| 3711 | } |
| 3712 | return mInputChannelsByToken.at(token); |
| 3713 | } |
| 3714 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3715 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3716 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3717 | if (inputWindowHandles.empty()) { |
| 3718 | // Remove all handles on a display if there are no windows left. |
| 3719 | mWindowHandlesByDisplay.erase(displayId); |
| 3720 | return; |
| 3721 | } |
| 3722 | |
| 3723 | // Since we compare the pointer of input window handles across window updates, we need |
| 3724 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3725 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3726 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3727 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3728 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3732 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3733 | if (!handle->updateInfo()) { |
| 3734 | // handle no longer valid |
| 3735 | continue; |
| 3736 | } |
| 3737 | |
| 3738 | const InputWindowInfo* info = handle->getInfo(); |
| 3739 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3740 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3741 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3742 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3743 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 3744 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3745 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3746 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3747 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3748 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3749 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | if (info->displayId != displayId) { |
| 3753 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3754 | handle->getName().c_str(), displayId, info->displayId); |
| 3755 | continue; |
| 3756 | } |
| 3757 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3758 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 3759 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3760 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3761 | oldHandle->updateFrom(handle); |
| 3762 | newHandles.push_back(oldHandle); |
| 3763 | } else { |
| 3764 | newHandles.push_back(handle); |
| 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | // Insert or replace |
| 3769 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3770 | } |
| 3771 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3772 | void InputDispatcher::setInputWindows( |
| 3773 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3774 | { // acquire lock |
| 3775 | std::scoped_lock _l(mLock); |
| 3776 | for (auto const& i : handlesPerDisplay) { |
| 3777 | setInputWindowsLocked(i.second, i.first); |
| 3778 | } |
| 3779 | } |
| 3780 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3781 | mLooper->wake(); |
| 3782 | } |
| 3783 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3784 | /** |
| 3785 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3786 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3787 | * If set an empty list, remove all handles from the specific display. |
| 3788 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3789 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3790 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3791 | void InputDispatcher::setInputWindowsLocked( |
| 3792 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3793 | if (DEBUG_FOCUS) { |
| 3794 | std::string windowList; |
| 3795 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3796 | windowList += iwh->getName() + " "; |
| 3797 | } |
| 3798 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3799 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3800 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3801 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 3802 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 3803 | const bool noInputWindow = |
| 3804 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3805 | if (noInputWindow && window->getToken() != nullptr) { |
| 3806 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 3807 | window->getName().c_str()); |
| 3808 | window->releaseChannel(); |
| 3809 | } |
| 3810 | } |
| 3811 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3812 | // Copy old handles for release if they are no longer present. |
| 3813 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3814 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3815 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3816 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3817 | sp<InputWindowHandle> newFocusedWindowHandle = nullptr; |
| 3818 | bool foundHoveredWindow = false; |
| 3819 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3820 | // Set newFocusedWindowHandle to the top most focused window instead of the last one |
| 3821 | if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus && |
| 3822 | windowHandle->getInfo()->visible) { |
| 3823 | newFocusedWindowHandle = windowHandle; |
| 3824 | } |
| 3825 | if (windowHandle == mLastHoverWindowHandle) { |
| 3826 | foundHoveredWindow = true; |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | if (!foundHoveredWindow) { |
| 3831 | mLastHoverWindowHandle = nullptr; |
| 3832 | } |
| 3833 | |
| 3834 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3835 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
| 3836 | |
| 3837 | if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) { |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 3838 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle, displayId, |
| 3839 | "setInputWindowsLocked"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3840 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3842 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 3843 | mTouchStatesByDisplay.find(displayId); |
| 3844 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 3845 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3846 | for (size_t i = 0; i < state.windows.size();) { |
| 3847 | TouchedWindow& touchedWindow = state.windows[i]; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3848 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3849 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3850 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 3851 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3852 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3853 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3854 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 3855 | if (touchedInputChannel != nullptr) { |
| 3856 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3857 | "touched window was removed"); |
| 3858 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3859 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3860 | state.windows.erase(state.windows.begin() + i); |
| 3861 | } else { |
| 3862 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3863 | } |
| 3864 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3865 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3866 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3867 | // Release information for windows that are no longer present. |
| 3868 | // This ensures that unused input channels are released promptly. |
| 3869 | // Otherwise, they might stick around until the window handle is destroyed |
| 3870 | // which might not happen until the next GC. |
| 3871 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3872 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3873 | if (DEBUG_FOCUS) { |
| 3874 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3875 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3876 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3877 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3882 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3883 | if (DEBUG_FOCUS) { |
| 3884 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 3885 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 3886 | } |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3887 | if (inputApplicationHandle != nullptr && |
| 3888 | inputApplicationHandle->getApplicationToken() != nullptr) { |
| 3889 | // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3890 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3892 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3893 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3894 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3895 | // If oldFocusedApplicationHandle already exists |
| 3896 | if (oldFocusedApplicationHandle != nullptr) { |
| 3897 | // If a new focused application handle is different from the old one and |
| 3898 | // old focus application info is awaited focused application info. |
| 3899 | if (*oldFocusedApplicationHandle != *inputApplicationHandle && |
| 3900 | mAwaitedFocusedApplication != nullptr && |
| 3901 | *oldFocusedApplicationHandle == *mAwaitedFocusedApplication) { |
| 3902 | resetNoFocusedWindowTimeoutLocked(); |
| 3903 | } |
| 3904 | // Erase the old application from container first |
| 3905 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 3906 | // Should already get freed after removed from container but just double check. |
| 3907 | oldFocusedApplicationHandle.reset(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3908 | } |
| 3909 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3910 | // Set the new application handle. |
| 3911 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3912 | } // release lock |
| 3913 | |
| 3914 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3915 | mLooper->wake(); |
| 3916 | } |
| 3917 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3918 | /** |
| 3919 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3920 | * the display not specified. |
| 3921 | * |
| 3922 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3923 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3924 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3925 | * display. The display-specified events won't be affected. |
| 3926 | */ |
| 3927 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3928 | if (DEBUG_FOCUS) { |
| 3929 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3930 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3931 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3932 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3933 | |
| 3934 | if (mFocusedDisplayId != displayId) { |
| 3935 | sp<InputWindowHandle> oldFocusedWindowHandle = |
| 3936 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); |
| 3937 | if (oldFocusedWindowHandle != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3938 | std::shared_ptr<InputChannel> inputChannel = |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3939 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3940 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3941 | CancelationOptions |
| 3942 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3943 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3944 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3945 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3946 | } |
| 3947 | } |
| 3948 | mFocusedDisplayId = displayId; |
| 3949 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 3950 | // Find new focused window and validate |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3951 | sp<InputWindowHandle> newFocusedWindowHandle = |
| 3952 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 3953 | notifyFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3954 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3955 | if (newFocusedWindowHandle == nullptr) { |
| 3956 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
| 3957 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 3958 | ALOGE("But another display has a focused window:"); |
| 3959 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3960 | const sp<InputWindowHandle>& windowHandle = it.second; |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 3961 | ALOGE("Display #%" PRId32 " has focused window: '%s'\n", it.first, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3962 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3963 | } |
| 3964 | } |
| 3965 | } |
| 3966 | } |
| 3967 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3968 | if (DEBUG_FOCUS) { |
| 3969 | logDispatchStateLocked(); |
| 3970 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3971 | } // release lock |
| 3972 | |
| 3973 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3974 | mLooper->wake(); |
| 3975 | } |
| 3976 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3977 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3978 | if (DEBUG_FOCUS) { |
| 3979 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3980 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3981 | |
| 3982 | bool changed; |
| 3983 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3984 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3985 | |
| 3986 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 3987 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3988 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | if (mDispatchEnabled && !enabled) { |
| 3992 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3993 | } |
| 3994 | |
| 3995 | mDispatchEnabled = enabled; |
| 3996 | mDispatchFrozen = frozen; |
| 3997 | changed = true; |
| 3998 | } else { |
| 3999 | changed = false; |
| 4000 | } |
| 4001 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4002 | if (DEBUG_FOCUS) { |
| 4003 | logDispatchStateLocked(); |
| 4004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | } // release lock |
| 4006 | |
| 4007 | if (changed) { |
| 4008 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4009 | mLooper->wake(); |
| 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4014 | if (DEBUG_FOCUS) { |
| 4015 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4016 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4017 | |
| 4018 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4019 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4020 | |
| 4021 | if (mInputFilterEnabled == enabled) { |
| 4022 | return; |
| 4023 | } |
| 4024 | |
| 4025 | mInputFilterEnabled = enabled; |
| 4026 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4027 | } // release lock |
| 4028 | |
| 4029 | // Wake up poll loop since there might be work to do to drop everything. |
| 4030 | mLooper->wake(); |
| 4031 | } |
| 4032 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4033 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4034 | std::scoped_lock lock(mLock); |
| 4035 | mInTouchMode = inTouchMode; |
| 4036 | } |
| 4037 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4038 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 4039 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4040 | if (DEBUG_FOCUS) { |
| 4041 | ALOGD("Trivial transfer to same window."); |
| 4042 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4043 | return true; |
| 4044 | } |
| 4045 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4046 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4047 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4048 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4049 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4050 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4051 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4052 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4053 | return false; |
| 4054 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4055 | if (DEBUG_FOCUS) { |
| 4056 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4057 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4058 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4060 | if (DEBUG_FOCUS) { |
| 4061 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4062 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | return false; |
| 4064 | } |
| 4065 | |
| 4066 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4067 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4068 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4069 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4070 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4071 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4072 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4073 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4075 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4076 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4077 | int32_t newTargetFlags = oldTargetFlags & |
| 4078 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4079 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4080 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4081 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4082 | found = true; |
| 4083 | goto Found; |
| 4084 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4085 | } |
| 4086 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4087 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4088 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4089 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4090 | if (DEBUG_FOCUS) { |
| 4091 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4092 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | return false; |
| 4094 | } |
| 4095 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4096 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4097 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4098 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4099 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4100 | CancelationOptions |
| 4101 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4102 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4103 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4104 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4105 | } |
| 4106 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4107 | if (DEBUG_FOCUS) { |
| 4108 | logDispatchStateLocked(); |
| 4109 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | } // release lock |
| 4111 | |
| 4112 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4113 | mLooper->wake(); |
| 4114 | return true; |
| 4115 | } |
| 4116 | |
| 4117 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4118 | if (DEBUG_FOCUS) { |
| 4119 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | |
| 4122 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4123 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4124 | |
| 4125 | resetKeyRepeatLocked(); |
| 4126 | releasePendingEventLocked(); |
| 4127 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4128 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4129 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4130 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4131 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4133 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4137 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | dumpDispatchStateLocked(dump); |
| 4139 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4140 | std::istringstream stream(dump); |
| 4141 | std::string line; |
| 4142 | |
| 4143 | while (std::getline(stream, line, '\n')) { |
| 4144 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4148 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4149 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4150 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4151 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4152 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4154 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4155 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4156 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4157 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4158 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4159 | const std::chrono::duration timeout = |
| 4160 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4161 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4162 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4163 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4164 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4165 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4166 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4168 | |
| 4169 | if (!mFocusedWindowHandlesByDisplay.empty()) { |
| 4170 | dump += StringPrintf(INDENT "FocusedWindows:\n"); |
| 4171 | for (auto& it : mFocusedWindowHandlesByDisplay) { |
| 4172 | const int32_t displayId = it.first; |
| 4173 | const sp<InputWindowHandle>& windowHandle = it.second; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4174 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4175 | windowHandle->getName().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4176 | } |
| 4177 | } else { |
| 4178 | dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); |
| 4179 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4181 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4182 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4183 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4184 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4185 | 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] | 4186 | state.displayId, toString(state.down), toString(state.split), |
| 4187 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4188 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4189 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4190 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4191 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4192 | dump += StringPrintf(INDENT4 |
| 4193 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4194 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4195 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4196 | } |
| 4197 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4198 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4199 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4200 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4201 | dump += INDENT3 "Portal windows:\n"; |
| 4202 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4203 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4204 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4205 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4206 | } |
| 4207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | } |
| 4209 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4210 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4211 | } |
| 4212 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4213 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4214 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4215 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4216 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4217 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4218 | dump += INDENT2 "Windows:\n"; |
| 4219 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4220 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4221 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4222 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4223 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4224 | "portalToDisplayId=%d, paused=%s, hasFocus=%s, " |
chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4225 | "hasWallpaper=%s, visible=%s, canReceiveKeys=%s, " |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4226 | "flags=%s, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4227 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4228 | "touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4229 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4230 | windowInfo->portalToDisplayId, |
| 4231 | toString(windowInfo->paused), |
| 4232 | toString(windowInfo->hasFocus), |
| 4233 | toString(windowInfo->hasWallpaper), |
| 4234 | toString(windowInfo->visible), |
| 4235 | toString(windowInfo->canReceiveKeys), |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4236 | windowInfo->flags.string().c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4237 | static_cast<int32_t>(windowInfo->type), |
| 4238 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4239 | windowInfo->frameRight, windowInfo->frameBottom, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4240 | windowInfo->globalScaleFactor); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4241 | dumpRegion(dump, windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4242 | dump += StringPrintf(", inputFeatures=%s", |
| 4243 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4244 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
| 4245 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4246 | windowInfo->ownerPid, windowInfo->ownerUid, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4247 | millis(windowInfo->dispatchingTimeout)); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame^] | 4248 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4249 | } |
| 4250 | } else { |
| 4251 | dump += INDENT2 "Windows: <none>\n"; |
| 4252 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4253 | } |
| 4254 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4255 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | } |
| 4257 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4258 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4259 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4260 | const std::vector<Monitor>& monitors = it.second; |
| 4261 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4262 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4263 | } |
| 4264 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4265 | const std::vector<Monitor>& monitors = it.second; |
| 4266 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4267 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4268 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4270 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | nsecs_t currentTime = now(); |
| 4274 | |
| 4275 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4276 | if (!mRecentQueue.empty()) { |
| 4277 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4278 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4279 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4281 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | } |
| 4283 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4284 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | // Dump event currently being dispatched. |
| 4288 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4289 | dump += INDENT "PendingEvent:\n"; |
| 4290 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4291 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4292 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4293 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4295 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4299 | if (!mInboundQueue.empty()) { |
| 4300 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4301 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4302 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4304 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4305 | } |
| 4306 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4307 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | } |
| 4309 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4310 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4311 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4312 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4313 | const KeyReplacement& replacement = pair.first; |
| 4314 | int32_t newKeyCode = pair.second; |
| 4315 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4316 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4317 | } |
| 4318 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4319 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4320 | } |
| 4321 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4322 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4323 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4324 | for (const auto& pair : mConnectionsByFd) { |
| 4325 | const sp<Connection>& connection = pair.second; |
| 4326 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4327 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4328 | pair.first, connection->getInputChannelName().c_str(), |
| 4329 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4330 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4332 | if (!connection->outboundQueue.empty()) { |
| 4333 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4334 | connection->outboundQueue.size()); |
| 4335 | for (DispatchEntry* entry : connection->outboundQueue) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4336 | dump.append(INDENT4); |
| 4337 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4338 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 |
| 4339 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4340 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4341 | ns2ms(currentTime - entry->eventEntry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4342 | } |
| 4343 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4344 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4345 | } |
| 4346 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4347 | if (!connection->waitQueue.empty()) { |
| 4348 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4349 | connection->waitQueue.size()); |
| 4350 | for (DispatchEntry* entry : connection->waitQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4351 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4352 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4353 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4354 | "age=%" PRId64 "ms, wait=%" PRId64 "ms seq=%" PRIu32 "\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4355 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4356 | ns2ms(currentTime - entry->eventEntry->eventTime), |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4357 | ns2ms(currentTime - entry->deliveryTime), entry->seq); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | } |
| 4359 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4360 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4364 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4368 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 4369 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4370 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4371 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4372 | } |
| 4373 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4374 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4375 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 4376 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 4377 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | } |
| 4379 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4380 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4381 | const size_t numMonitors = monitors.size(); |
| 4382 | for (size_t i = 0; i < numMonitors; i++) { |
| 4383 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4384 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4385 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4386 | dump += "\n"; |
| 4387 | } |
| 4388 | } |
| 4389 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4390 | status_t InputDispatcher::registerInputChannel(const std::shared_ptr<InputChannel>& inputChannel) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4391 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4392 | ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4393 | #endif |
| 4394 | |
| 4395 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4396 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4397 | sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4398 | if (existingConnection != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | ALOGW("Attempted to register already registered input channel '%s'", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4400 | inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4401 | return BAD_VALUE; |
| 4402 | } |
| 4403 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4404 | sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4405 | |
| 4406 | int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4407 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4408 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4409 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4410 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4411 | } // release lock |
| 4412 | |
| 4413 | // Wake the looper because some connections have changed. |
| 4414 | mLooper->wake(); |
| 4415 | return OK; |
| 4416 | } |
| 4417 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4418 | status_t InputDispatcher::registerInputMonitor(const std::shared_ptr<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4419 | int32_t displayId, bool isGestureMonitor) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4420 | { // acquire lock |
| 4421 | std::scoped_lock _l(mLock); |
| 4422 | |
| 4423 | if (displayId < 0) { |
| 4424 | ALOGW("Attempted to register input monitor without a specified display."); |
| 4425 | return BAD_VALUE; |
| 4426 | } |
| 4427 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4428 | if (inputChannel->getConnectionToken() == nullptr) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4429 | ALOGW("Attempted to register input monitor without an identifying token."); |
| 4430 | return BAD_VALUE; |
| 4431 | } |
| 4432 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4433 | sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4434 | |
| 4435 | const int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4436 | mConnectionsByFd[fd] = connection; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4437 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4438 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4439 | auto& monitorsByDisplay = |
| 4440 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4441 | monitorsByDisplay[displayId].emplace_back(inputChannel); |
| 4442 | |
| 4443 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4444 | } |
| 4445 | // Wake the looper because some connections have changed. |
| 4446 | mLooper->wake(); |
| 4447 | return OK; |
| 4448 | } |
| 4449 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4450 | status_t InputDispatcher::unregisterInputChannel(const InputChannel& inputChannel) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4451 | #if DEBUG_REGISTRATION |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4452 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel.getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4453 | #endif |
| 4454 | |
| 4455 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4456 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4457 | |
| 4458 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
| 4459 | if (status) { |
| 4460 | return status; |
| 4461 | } |
| 4462 | } // release lock |
| 4463 | |
| 4464 | // Wake the poll loop because removing the connection may have changed the current |
| 4465 | // synchronization state. |
| 4466 | mLooper->wake(); |
| 4467 | return OK; |
| 4468 | } |
| 4469 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4470 | status_t InputDispatcher::unregisterInputChannelLocked(const InputChannel& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4471 | bool notify) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4472 | sp<Connection> connection = getConnectionLocked(inputChannel.getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4473 | if (connection == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4474 | ALOGW("Attempted to unregister already unregistered input channel '%s'", |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4475 | inputChannel.getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4476 | return BAD_VALUE; |
| 4477 | } |
| 4478 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4479 | removeConnectionLocked(connection); |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4480 | mInputChannelsByToken.erase(inputChannel.getConnectionToken()); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4481 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4482 | if (connection->monitor) { |
| 4483 | removeMonitorChannelLocked(inputChannel); |
| 4484 | } |
| 4485 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4486 | mLooper->removeFd(inputChannel.getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4487 | |
| 4488 | nsecs_t currentTime = now(); |
| 4489 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4490 | |
| 4491 | connection->status = Connection::STATUS_ZOMBIE; |
| 4492 | return OK; |
| 4493 | } |
| 4494 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4495 | void InputDispatcher::removeMonitorChannelLocked(const InputChannel& inputChannel) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4496 | removeMonitorChannelLocked(inputChannel, mGlobalMonitorsByDisplay); |
| 4497 | removeMonitorChannelLocked(inputChannel, mGestureMonitorsByDisplay); |
| 4498 | } |
| 4499 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4500 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4501 | const InputChannel& inputChannel, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4502 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4503 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4504 | std::vector<Monitor>& monitors = it->second; |
| 4505 | const size_t numMonitors = monitors.size(); |
| 4506 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4507 | if (*monitors[i].inputChannel == inputChannel) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4508 | monitors.erase(monitors.begin() + i); |
| 4509 | break; |
| 4510 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4511 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4512 | if (monitors.empty()) { |
| 4513 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4514 | } else { |
| 4515 | ++it; |
| 4516 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4520 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4521 | { // acquire lock |
| 4522 | std::scoped_lock _l(mLock); |
| 4523 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4524 | |
| 4525 | if (!foundDisplayId) { |
| 4526 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4527 | return BAD_VALUE; |
| 4528 | } |
| 4529 | int32_t displayId = foundDisplayId.value(); |
| 4530 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4531 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4532 | mTouchStatesByDisplay.find(displayId); |
| 4533 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4534 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4535 | return BAD_VALUE; |
| 4536 | } |
| 4537 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4538 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4539 | std::optional<int32_t> foundDeviceId; |
| 4540 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4541 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4542 | foundDeviceId = state.deviceId; |
| 4543 | } |
| 4544 | } |
| 4545 | if (!foundDeviceId || !state.down) { |
| 4546 | 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] | 4547 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4548 | return BAD_VALUE; |
| 4549 | } |
| 4550 | int32_t deviceId = foundDeviceId.value(); |
| 4551 | |
| 4552 | // Send cancel events to all the input channels we're stealing from. |
| 4553 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4554 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4555 | options.deviceId = deviceId; |
| 4556 | options.displayId = displayId; |
| 4557 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4558 | std::shared_ptr<InputChannel> channel = |
| 4559 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4560 | if (channel != nullptr) { |
| 4561 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4562 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4563 | } |
| 4564 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4565 | state.filterNonMonitors(); |
| 4566 | } |
| 4567 | return OK; |
| 4568 | } |
| 4569 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4570 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4571 | const sp<IBinder>& token) { |
| 4572 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4573 | const std::vector<Monitor>& monitors = it.second; |
| 4574 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4575 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4576 | return it.first; |
| 4577 | } |
| 4578 | } |
| 4579 | } |
| 4580 | return std::nullopt; |
| 4581 | } |
| 4582 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4583 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4584 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4585 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4586 | } |
| 4587 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4588 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4589 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4590 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4591 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4592 | } |
| 4593 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4594 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4595 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4596 | } |
| 4597 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4598 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4599 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4600 | removeByValue(mConnectionsByFd, connection); |
| 4601 | } |
| 4602 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4603 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4604 | const sp<Connection>& connection, uint32_t seq, |
| 4605 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4606 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4607 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4608 | commandEntry->connection = connection; |
| 4609 | commandEntry->eventTime = currentTime; |
| 4610 | commandEntry->seq = seq; |
| 4611 | commandEntry->handled = handled; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4612 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4613 | } |
| 4614 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4615 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4616 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4618 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4619 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4620 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4621 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4622 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4623 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4624 | } |
| 4625 | |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 4626 | void InputDispatcher::notifyFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, |
| 4627 | const sp<InputWindowHandle>& newFocus) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4628 | sp<IBinder> oldToken = oldFocus != nullptr ? oldFocus->getToken() : nullptr; |
| 4629 | sp<IBinder> newToken = newFocus != nullptr ? newFocus->getToken() : nullptr; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4630 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4631 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4632 | commandEntry->oldToken = oldToken; |
| 4633 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4634 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4635 | } |
| 4636 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4637 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 4638 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 4639 | // is already healthy again. Don't raise ANR in this situation |
| 4640 | if (connection->waitQueue.empty()) { |
| 4641 | ALOGI("Not raising ANR because the connection %s has recovered", |
| 4642 | connection->inputChannel->getName().c_str()); |
| 4643 | return; |
| 4644 | } |
| 4645 | /** |
| 4646 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 4647 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 4648 | * has changed. This could cause newer entries to time out before the already dispatched |
| 4649 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 4650 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 4651 | * most useful. |
| 4652 | */ |
| 4653 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
| 4654 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 4655 | std::string reason = |
| 4656 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
| 4657 | connection->inputChannel->getName().c_str(), |
| 4658 | ns2ms(currentWait), |
| 4659 | oldestEntry->eventEntry->getDescription().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4660 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4661 | updateLastAnrStateLocked(getWindowHandleLocked(connection->inputChannel->getConnectionToken()), |
| 4662 | reason); |
| 4663 | |
| 4664 | std::unique_ptr<CommandEntry> commandEntry = |
| 4665 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4666 | commandEntry->inputApplicationHandle = nullptr; |
| 4667 | commandEntry->inputChannel = connection->inputChannel; |
| 4668 | commandEntry->reason = std::move(reason); |
| 4669 | postCommandLocked(std::move(commandEntry)); |
| 4670 | } |
| 4671 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4672 | void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4673 | std::string reason = android::base::StringPrintf("%s does not have a focused window", |
| 4674 | application->getName().c_str()); |
| 4675 | |
| 4676 | updateLastAnrStateLocked(application, reason); |
| 4677 | |
| 4678 | std::unique_ptr<CommandEntry> commandEntry = |
| 4679 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4680 | commandEntry->inputApplicationHandle = application; |
| 4681 | commandEntry->inputChannel = nullptr; |
| 4682 | commandEntry->reason = std::move(reason); |
| 4683 | postCommandLocked(std::move(commandEntry)); |
| 4684 | } |
| 4685 | |
| 4686 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 4687 | const std::string& reason) { |
| 4688 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 4689 | updateLastAnrStateLocked(windowLabel, reason); |
| 4690 | } |
| 4691 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4692 | void InputDispatcher::updateLastAnrStateLocked( |
| 4693 | const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4694 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); |
| 4695 | updateLastAnrStateLocked(windowLabel, reason); |
| 4696 | } |
| 4697 | |
| 4698 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 4699 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4700 | // 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] | 4701 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4702 | struct tm tm; |
| 4703 | localtime_r(&t, &tm); |
| 4704 | char timestr[64]; |
| 4705 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4706 | mLastAnrState.clear(); |
| 4707 | mLastAnrState += INDENT "ANR:\n"; |
| 4708 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4709 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 4710 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4711 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4712 | } |
| 4713 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4714 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4715 | mLock.unlock(); |
| 4716 | |
| 4717 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4718 | |
| 4719 | mLock.lock(); |
| 4720 | } |
| 4721 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4722 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4723 | sp<Connection> connection = commandEntry->connection; |
| 4724 | |
| 4725 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4726 | mLock.unlock(); |
| 4727 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4728 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4729 | |
| 4730 | mLock.lock(); |
| 4731 | } |
| 4732 | } |
| 4733 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4734 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4735 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4736 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4737 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4738 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4739 | mLock.lock(); |
| 4740 | } |
| 4741 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4742 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4743 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4744 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4745 | mLock.unlock(); |
| 4746 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4747 | const std::chrono::nanoseconds timeoutExtension = |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4748 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4749 | |
| 4750 | mLock.lock(); |
| 4751 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4752 | if (timeoutExtension > 0s) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4753 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); |
| 4754 | } else { |
| 4755 | // stop waking up for events in this connection, it is already not responding |
| 4756 | sp<Connection> connection = getConnectionLocked(token); |
| 4757 | if (connection == nullptr) { |
| 4758 | return; |
| 4759 | } |
| 4760 | cancelEventsForAnrLocked(connection); |
| 4761 | } |
| 4762 | } |
| 4763 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4764 | void InputDispatcher::extendAnrTimeoutsLocked( |
| 4765 | const std::shared_ptr<InputApplicationHandle>& application, |
| 4766 | const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4767 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 4768 | if (connection == nullptr) { |
| 4769 | if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) { |
| 4770 | // Maybe ANR happened because there's no focused window? |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4771 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4772 | mAwaitedFocusedApplication = application; |
| 4773 | } else { |
| 4774 | // It's also possible that the connection already disappeared. No action necessary. |
| 4775 | } |
| 4776 | return; |
| 4777 | } |
| 4778 | |
| 4779 | 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] | 4780 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4781 | |
| 4782 | connection->responsive = true; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4783 | const nsecs_t newTimeout = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4784 | for (DispatchEntry* entry : connection->waitQueue) { |
| 4785 | if (newTimeout >= entry->timeoutTime) { |
| 4786 | // Already removed old entries when connection was marked unresponsive |
| 4787 | entry->timeoutTime = newTimeout; |
| 4788 | mAnrTracker.insert(entry->timeoutTime, connectionToken); |
| 4789 | } |
| 4790 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 4794 | CommandEntry* commandEntry) { |
| 4795 | KeyEntry* entry = commandEntry->keyEntry; |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4796 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4797 | |
| 4798 | mLock.unlock(); |
| 4799 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4800 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4801 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4802 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4803 | : nullptr; |
| 4804 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4805 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4806 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4807 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4808 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4809 | |
| 4810 | mLock.lock(); |
| 4811 | |
| 4812 | if (delay < 0) { |
| 4813 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 4814 | } else if (!delay) { |
| 4815 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 4816 | } else { |
| 4817 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 4818 | entry->interceptKeyWakeupTime = now() + delay; |
| 4819 | } |
| 4820 | entry->release(); |
| 4821 | } |
| 4822 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4823 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 4824 | mLock.unlock(); |
| 4825 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 4826 | mLock.lock(); |
| 4827 | } |
| 4828 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4829 | /** |
| 4830 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 4831 | * current time. |
| 4832 | */ |
| 4833 | static bool isConnectionResponsive(const Connection& connection) { |
| 4834 | const nsecs_t currentTime = now(); |
| 4835 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 4836 | if (entry->timeoutTime < currentTime) { |
| 4837 | return false; |
| 4838 | } |
| 4839 | } |
| 4840 | return true; |
| 4841 | } |
| 4842 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4843 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4844 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4845 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4846 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4847 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 | |
| 4849 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4850 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4851 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4852 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4853 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4854 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4855 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4856 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4857 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 4858 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4859 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4860 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4861 | |
| 4862 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4863 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4864 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 4865 | restartEvent = |
| 4866 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4867 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4868 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 4869 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 4870 | handled); |
| 4871 | } else { |
| 4872 | restartEvent = false; |
| 4873 | } |
| 4874 | |
| 4875 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4876 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4877 | // contents of the wait queue to have been drained, so we need to double-check |
| 4878 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4879 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 4880 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 4881 | dispatchEntry = *dispatchEntryIt; |
| 4882 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4883 | mAnrTracker.erase(dispatchEntry->timeoutTime, |
| 4884 | connection->inputChannel->getConnectionToken()); |
| 4885 | if (!connection->responsive) { |
| 4886 | connection->responsive = isConnectionResponsive(*connection); |
| 4887 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4888 | traceWaitQueueLength(connection); |
| 4889 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4890 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4891 | traceOutboundQueueLength(connection); |
| 4892 | } else { |
| 4893 | releaseDispatchEntry(dispatchEntry); |
| 4894 | } |
| 4895 | } |
| 4896 | |
| 4897 | // Start the next dispatch cycle for this connection. |
| 4898 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4902 | DispatchEntry* dispatchEntry, |
| 4903 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4904 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4905 | if (!handled) { |
| 4906 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4907 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4908 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4909 | return false; |
| 4910 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4911 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4912 | // Get the fallback key state. |
| 4913 | // Clear it out after dispatching the UP. |
| 4914 | int32_t originalKeyCode = keyEntry->keyCode; |
| 4915 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 4916 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 4917 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4918 | } |
| 4919 | |
| 4920 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 4921 | // If the application handles the original key for which we previously |
| 4922 | // generated a fallback or if the window is not a foreground window, |
| 4923 | // then cancel the associated fallback key, if any. |
| 4924 | if (fallbackKeyCode != -1) { |
| 4925 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4926 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4927 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4928 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4929 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 4930 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4931 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4932 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4933 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4934 | |
| 4935 | mLock.unlock(); |
| 4936 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4937 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4938 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4939 | |
| 4940 | mLock.lock(); |
| 4941 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4942 | // Cancel the fallback key. |
| 4943 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4945 | "application handled the original non-fallback key " |
| 4946 | "or is no longer a foreground target, " |
| 4947 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | options.keyCode = fallbackKeyCode; |
| 4949 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4950 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4951 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4952 | } |
| 4953 | } else { |
| 4954 | // If the application did not handle a non-fallback key, first check |
| 4955 | // that we are in a good state to perform unhandled key event processing |
| 4956 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4957 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4958 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4960 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4961 | "since this is not an initial down. " |
| 4962 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4963 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4964 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4965 | return false; |
| 4966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4967 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4968 | // Dispatch the unhandled key to the policy. |
| 4969 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4970 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4971 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4972 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4973 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4974 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4975 | |
| 4976 | mLock.unlock(); |
| 4977 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4978 | bool fallback = |
| 4979 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 4980 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4981 | |
| 4982 | mLock.lock(); |
| 4983 | |
| 4984 | if (connection->status != Connection::STATUS_NORMAL) { |
| 4985 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4986 | return false; |
| 4987 | } |
| 4988 | |
| 4989 | // Latch the fallback keycode for this key on an initial down. |
| 4990 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 4991 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4992 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4993 | fallbackKeyCode = event.getKeyCode(); |
| 4994 | } else { |
| 4995 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4996 | } |
| 4997 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 4998 | } |
| 4999 | |
| 5000 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5001 | |
| 5002 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5003 | // We will continue to dispatch the key to the policy but we will no |
| 5004 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5005 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5006 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5007 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5008 | if (fallback) { |
| 5009 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5010 | "as a fallback for %d, but on the DOWN it had requested " |
| 5011 | "to send %d instead. Fallback canceled.", |
| 5012 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5013 | } else { |
| 5014 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5015 | "but on the DOWN it had requested to send %d. " |
| 5016 | "Fallback canceled.", |
| 5017 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5018 | } |
| 5019 | #endif |
| 5020 | |
| 5021 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5022 | "canceling fallback, policy no longer desires it"); |
| 5023 | options.keyCode = fallbackKeyCode; |
| 5024 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5025 | |
| 5026 | fallback = false; |
| 5027 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5028 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5029 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5030 | } |
| 5031 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5032 | |
| 5033 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5034 | { |
| 5035 | std::string msg; |
| 5036 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5037 | connection->inputState.getFallbackKeys(); |
| 5038 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5039 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5040 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5041 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5042 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5043 | } |
| 5044 | #endif |
| 5045 | |
| 5046 | if (fallback) { |
| 5047 | // Restart the dispatch cycle using the fallback key. |
| 5048 | keyEntry->eventTime = event.getEventTime(); |
| 5049 | keyEntry->deviceId = event.getDeviceId(); |
| 5050 | keyEntry->source = event.getSource(); |
| 5051 | keyEntry->displayId = event.getDisplayId(); |
| 5052 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5053 | keyEntry->keyCode = fallbackKeyCode; |
| 5054 | keyEntry->scanCode = event.getScanCode(); |
| 5055 | keyEntry->metaState = event.getMetaState(); |
| 5056 | keyEntry->repeatCount = event.getRepeatCount(); |
| 5057 | keyEntry->downTime = event.getDownTime(); |
| 5058 | keyEntry->syntheticRepeat = false; |
| 5059 | |
| 5060 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5061 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5062 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 5063 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5064 | #endif |
| 5065 | return true; // restart the event |
| 5066 | } else { |
| 5067 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5068 | ALOGD("Unhandled key event: No fallback key."); |
| 5069 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5070 | |
| 5071 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5072 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5073 | } |
| 5074 | } |
| 5075 | return false; |
| 5076 | } |
| 5077 | |
| 5078 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5079 | DispatchEntry* dispatchEntry, |
| 5080 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5081 | return false; |
| 5082 | } |
| 5083 | |
| 5084 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5085 | mLock.unlock(); |
| 5086 | |
| 5087 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 5088 | |
| 5089 | mLock.lock(); |
| 5090 | } |
| 5091 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5092 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 5093 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5094 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5095 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 5096 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5097 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5098 | } |
| 5099 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5100 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5101 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5102 | // TODO Write some statistics about how long we spend waiting. |
| 5103 | } |
| 5104 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5105 | /** |
| 5106 | * Report the touch event latency to the statsd server. |
| 5107 | * Input events are reported for statistics if: |
| 5108 | * - This is a touchscreen event |
| 5109 | * - InputFilter is not enabled |
| 5110 | * - Event is not injected or synthesized |
| 5111 | * |
| 5112 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5113 | * from getting aggregated with the "old" data. |
| 5114 | */ |
| 5115 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5116 | REQUIRES(mLock) { |
| 5117 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5118 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5119 | if (!reportForStatistics) { |
| 5120 | return; |
| 5121 | } |
| 5122 | |
| 5123 | if (mTouchStatistics.shouldReport()) { |
| 5124 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5125 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5126 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5127 | mTouchStatistics.reset(); |
| 5128 | } |
| 5129 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5130 | mTouchStatistics.addValue(latencyMicros); |
| 5131 | } |
| 5132 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5133 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5134 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5135 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5136 | } |
| 5137 | } |
| 5138 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5139 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5140 | if (ATRACE_ENABLED()) { |
| 5141 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5142 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5143 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5147 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5148 | if (ATRACE_ENABLED()) { |
| 5149 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5150 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5151 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5152 | } |
| 5153 | } |
| 5154 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5155 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5156 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5158 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5159 | dumpDispatchStateLocked(dump); |
| 5160 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5161 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5162 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5163 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5164 | } |
| 5165 | } |
| 5166 | |
| 5167 | void InputDispatcher::monitor() { |
| 5168 | // 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] | 5169 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5170 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5171 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | } |
| 5173 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5174 | /** |
| 5175 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5176 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5177 | * this method can be safely called from any thread, as long as you've ensured that |
| 5178 | * the work you are interested in completing has already been queued. |
| 5179 | */ |
| 5180 | bool InputDispatcher::waitForIdle() { |
| 5181 | /** |
| 5182 | * Timeout should represent the longest possible time that a device might spend processing |
| 5183 | * events and commands. |
| 5184 | */ |
| 5185 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5186 | std::unique_lock lock(mLock); |
| 5187 | mLooper->wake(); |
| 5188 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5189 | return result == std::cv_status::no_timeout; |
| 5190 | } |
| 5191 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5192 | /** |
| 5193 | * Sets focus to the window identified by the token. This must be called |
| 5194 | * after updating any input window handles. |
| 5195 | * |
| 5196 | * Params: |
| 5197 | * request.token - input channel token used to identify the window that should gain focus. |
| 5198 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 5199 | * specified token does not match the currently focused window, this request will be dropped. |
| 5200 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 5201 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 5202 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 5203 | * when requesting the focus change. This determines which request gets |
| 5204 | * precedence if there is a focus change request from another source such as pointer down. |
| 5205 | */ |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5206 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) {} |
| 5207 | |
| 5208 | void InputDispatcher::onFocusChangedLocked(const sp<InputWindowHandle>& oldFocusedWindowHandle, |
| 5209 | const sp<InputWindowHandle>& newFocusedWindowHandle, |
| 5210 | int32_t displayId, std::string_view reason) { |
| 5211 | if (oldFocusedWindowHandle) { |
| 5212 | if (DEBUG_FOCUS) { |
| 5213 | ALOGD("Focus left window: %s in display %" PRId32, |
| 5214 | oldFocusedWindowHandle->getName().c_str(), displayId); |
| 5215 | } |
| 5216 | std::shared_ptr<InputChannel> focusedInputChannel = |
| 5217 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); |
| 5218 | if (focusedInputChannel) { |
| 5219 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 5220 | "focus left window"); |
| 5221 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
| 5222 | enqueueFocusEventLocked(*oldFocusedWindowHandle, false /*hasFocus*/, reason); |
| 5223 | } |
| 5224 | mFocusedWindowHandlesByDisplay.erase(displayId); |
| 5225 | } |
| 5226 | if (newFocusedWindowHandle) { |
| 5227 | if (DEBUG_FOCUS) { |
| 5228 | ALOGD("Focus entered window: %s in display %" PRId32, |
| 5229 | newFocusedWindowHandle->getName().c_str(), displayId); |
| 5230 | } |
| 5231 | mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; |
| 5232 | enqueueFocusEventLocked(*newFocusedWindowHandle, true /*hasFocus*/, reason); |
| 5233 | } |
| 5234 | |
| 5235 | if (mFocusedDisplayId == displayId) { |
| 5236 | notifyFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); |
| 5237 | } |
| 5238 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5239 | } // namespace android::inputdispatcher |