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 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 31 | // Log debug messages about registrations. |
| 32 | #define DEBUG_REGISTRATION 0 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 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> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 56 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 57 | #include <statslog.h> |
| 58 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 60 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 61 | #include <cerrno> |
| 62 | #include <cinttypes> |
| 63 | #include <climits> |
| 64 | #include <cstddef> |
| 65 | #include <ctime> |
| 66 | #include <queue> |
| 67 | #include <sstream> |
| 68 | |
| 69 | #include "Connection.h" |
| 70 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | #define INDENT " " |
| 72 | #define INDENT2 " " |
| 73 | #define INDENT3 " " |
| 74 | #define INDENT4 " " |
| 75 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 76 | using android::base::StringPrintf; |
| 77 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | |
| 80 | // Default input dispatching timeout if there is no focused application or paused window |
| 81 | // from which to determine an appropriate dispatching timeout. |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 82 | constexpr std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = |
| 83 | std::chrono::milliseconds(android::os::IInputConstants::DEFAULT_DISPATCHING_TIMEOUT_MILLIS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 84 | |
| 85 | // Amount of time to allow for all pending events to be processed when an app switch |
| 86 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 87 | // 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] | 88 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 | |
| 90 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 91 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 92 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | // 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] | 95 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 96 | |
| 97 | // Log a warning when an interception call takes longer than this to process. |
| 98 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 100 | // Additional key latency in case a connection is still processing some motion events. |
| 101 | // This will help with the case when a user touched a button that opens a new window, |
| 102 | // and gives us the chance to dispatch the key to this new window. |
| 103 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 104 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 106 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 107 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 108 | // Event log tags. See EventLogTags.logtags for reference |
| 109 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 110 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 111 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | static inline nsecs_t now() { |
| 113 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 114 | } |
| 115 | |
| 116 | static inline const char* toString(bool value) { |
| 117 | return value ? "true" : "false"; |
| 118 | } |
| 119 | |
| 120 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 121 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 122 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static bool isValidKeyAction(int32_t action) { |
| 126 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 127 | case AKEY_EVENT_ACTION_DOWN: |
| 128 | case AKEY_EVENT_ACTION_UP: |
| 129 | return true; |
| 130 | default: |
| 131 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 136 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | ALOGE("Key event has invalid action code 0x%x", action); |
| 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 143 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 145 | case AMOTION_EVENT_ACTION_DOWN: |
| 146 | case AMOTION_EVENT_ACTION_UP: |
| 147 | case AMOTION_EVENT_ACTION_CANCEL: |
| 148 | case AMOTION_EVENT_ACTION_MOVE: |
| 149 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 150 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 151 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 152 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 153 | case AMOTION_EVENT_ACTION_SCROLL: |
| 154 | return true; |
| 155 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 156 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 157 | int32_t index = getMotionEventActionPointerIndex(action); |
| 158 | return index >= 0 && index < pointerCount; |
| 159 | } |
| 160 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 161 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 162 | return actionButton != 0; |
| 163 | default: |
| 164 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 168 | static int64_t millis(std::chrono::nanoseconds t) { |
| 169 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 170 | } |
| 171 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 172 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 173 | const PointerProperties* pointerProperties) { |
| 174 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 176 | return false; |
| 177 | } |
| 178 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 179 | 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] | 180 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | BitSet32 pointerIdBits; |
| 184 | for (size_t i = 0; i < pointerCount; i++) { |
| 185 | int32_t id = pointerProperties[i].id; |
| 186 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 187 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 188 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | if (pointerIdBits.hasBit(id)) { |
| 192 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 193 | return false; |
| 194 | } |
| 195 | pointerIdBits.markBit(id); |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 200 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 201 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 202 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | bool first = true; |
| 207 | Region::const_iterator cur = region.begin(); |
| 208 | Region::const_iterator const tail = region.end(); |
| 209 | while (cur != tail) { |
| 210 | if (first) { |
| 211 | first = false; |
| 212 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 213 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 215 | 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] | 216 | cur++; |
| 217 | } |
| 218 | } |
| 219 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Find the entry in std::unordered_map by key, and return it. |
| 222 | * If the entry is not found, return a default constructed entry. |
| 223 | * |
| 224 | * Useful when the entries are vectors, since an empty vector will be returned |
| 225 | * if the entry is not found. |
| 226 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 227 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 228 | template <typename K, typename V> |
| 229 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 230 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 231 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Find the entry in std::unordered_map by value, and remove it. |
| 236 | * If more than one entry has the same value, then all matching |
| 237 | * key-value pairs will be removed. |
| 238 | * |
| 239 | * Return true if at least one value has been removed. |
| 240 | */ |
| 241 | template <typename K, typename V> |
| 242 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 243 | bool removed = false; |
| 244 | for (auto it = map.begin(); it != map.end();) { |
| 245 | if (it->second == value) { |
| 246 | it = map.erase(it); |
| 247 | removed = true; |
| 248 | } else { |
| 249 | it++; |
| 250 | } |
| 251 | } |
| 252 | return removed; |
| 253 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Find the entry in std::unordered_map by key and return the value as an optional. |
| 257 | */ |
| 258 | template <typename K, typename V> |
| 259 | static std::optional<V> getOptionalValueByKey(const std::unordered_map<K, V>& map, K key) { |
| 260 | auto it = map.find(key); |
| 261 | return it != map.end() ? std::optional<V>{it->second} : std::nullopt; |
| 262 | } |
| 263 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 264 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 265 | if (first == second) { |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | if (first == nullptr || second == nullptr) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | return first->getToken() == second->getToken(); |
| 274 | } |
| 275 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 276 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 277 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 278 | } |
| 279 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 280 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 281 | EventEntry* eventEntry, |
| 282 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 283 | if (inputTarget.useDefaultPointerTransform()) { |
| 284 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 285 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 286 | inputTargetFlags, transform, |
| 287 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 291 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 292 | |
| 293 | PointerCoords pointerCoords[motionEntry.pointerCount]; |
| 294 | |
| 295 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 296 | // 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] | 297 | // uses the transform for the normalized pointer. |
| 298 | const ui::Transform& firstPointerTransform = |
| 299 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 300 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 301 | |
| 302 | // Iterate through all pointers in the event to normalize against the first. |
| 303 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 304 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 305 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 306 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 307 | |
| 308 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 309 | // First, apply the current pointer's transform to update the coordinates into |
| 310 | // window space. |
| 311 | pointerCoords[pointerIndex].transform(currTransform); |
| 312 | // Next, apply the inverse transform of the normalized coordinates so the |
| 313 | // current coordinates are transformed into the normalized coordinate space. |
| 314 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | MotionEntry* combinedMotionEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 318 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 319 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, |
| 320 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, |
| 321 | motionEntry.metaState, motionEntry.buttonState, |
| 322 | motionEntry.classification, motionEntry.edgeFlags, |
| 323 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 324 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 325 | motionEntry.downTime, motionEntry.pointerCount, |
| 326 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, |
| 327 | 0 /* yOffset */); |
| 328 | |
| 329 | if (motionEntry.injectionState) { |
| 330 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 331 | combinedMotionEntry->injectionState->refCount += 1; |
| 332 | } |
| 333 | |
| 334 | std::unique_ptr<DispatchEntry> dispatchEntry = |
| 335 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 336 | inputTargetFlags, firstPointerTransform, |
| 337 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 338 | combinedMotionEntry->release(); |
| 339 | return dispatchEntry; |
| 340 | } |
| 341 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 342 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 343 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 344 | float yOffset = 0) { |
| 345 | if (monitors.empty()) { |
| 346 | return; |
| 347 | } |
| 348 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 349 | for (const Monitor& monitor : monitors) { |
| 350 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 351 | } |
| 352 | } |
| 353 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 354 | const char* InputDispatcher::typeToString(InputDispatcher::FocusResult result) { |
| 355 | switch (result) { |
| 356 | case InputDispatcher::FocusResult::OK: |
| 357 | return "Ok"; |
| 358 | case InputDispatcher::FocusResult::NO_WINDOW: |
| 359 | return "Window not found"; |
| 360 | case InputDispatcher::FocusResult::NOT_FOCUSABLE: |
| 361 | return "Window not focusable"; |
| 362 | case InputDispatcher::FocusResult::NOT_VISIBLE: |
| 363 | return "Window not visible"; |
| 364 | } |
| 365 | } |
| 366 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 367 | template <typename T> |
| 368 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 369 | if (lhs == nullptr && rhs == nullptr) { |
| 370 | return true; |
| 371 | } |
| 372 | if (lhs == nullptr || rhs == nullptr) { |
| 373 | return false; |
| 374 | } |
| 375 | return *lhs == *rhs; |
| 376 | } |
| 377 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 378 | // --- InputDispatcher --- |
| 379 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 380 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 381 | : mPolicy(policy), |
| 382 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 383 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 384 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 385 | mAppSwitchSawKeyDown(false), |
| 386 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 387 | mNextUnblockedEvent(nullptr), |
| 388 | mDispatchEnabled(false), |
| 389 | mDispatchFrozen(false), |
| 390 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 391 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 392 | // To avoid leaking stack in case that call never comes, and for tests, |
| 393 | // initialize it here anyways. |
| 394 | mInTouchMode(true), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 395 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 396 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 397 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 398 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 399 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 400 | |
| 401 | policy->getDispatcherConfiguration(&mConfig); |
| 402 | } |
| 403 | |
| 404 | InputDispatcher::~InputDispatcher() { |
| 405 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 406 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 407 | |
| 408 | resetKeyRepeatLocked(); |
| 409 | releasePendingEventLocked(); |
| 410 | drainInboundQueueLocked(); |
| 411 | } |
| 412 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 413 | while (!mConnectionsByFd.empty()) { |
| 414 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 415 | unregisterInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 419 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 420 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 421 | return ALREADY_EXISTS; |
| 422 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 423 | mThread = std::make_unique<InputThread>( |
| 424 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 425 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 429 | if (mThread && mThread->isCallingThread()) { |
| 430 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 431 | return INVALID_OPERATION; |
| 432 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 433 | mThread.reset(); |
| 434 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 437 | void InputDispatcher::dispatchOnce() { |
| 438 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 439 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 440 | std::scoped_lock _l(mLock); |
| 441 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 442 | |
| 443 | // Run a dispatch loop if there are no pending commands. |
| 444 | // The dispatch loop might enqueue commands to run afterwards. |
| 445 | if (!haveCommandsLocked()) { |
| 446 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 447 | } |
| 448 | |
| 449 | // Run all pending commands if there are any. |
| 450 | // If any commands were run then force the next poll to wake up immediately. |
| 451 | if (runCommandsLockedInterruptible()) { |
| 452 | nextWakeupTime = LONG_LONG_MIN; |
| 453 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 454 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 455 | // If we are still waiting for ack on some events, |
| 456 | // we might have to wake up earlier to check if an app is anr'ing. |
| 457 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 458 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 459 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 460 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 461 | // pending or queued events |
| 462 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 463 | mDispatcherEnteredIdle.notify_all(); |
| 464 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 465 | } // release lock |
| 466 | |
| 467 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 468 | nsecs_t currentTime = now(); |
| 469 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 470 | mLooper->pollOnce(timeoutMillis); |
| 471 | } |
| 472 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 473 | /** |
| 474 | * Check if any of the connections' wait queues have events that are too old. |
| 475 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 476 | * Return the time at which we should wake up next. |
| 477 | */ |
| 478 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 479 | const nsecs_t currentTime = now(); |
| 480 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 481 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 482 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 483 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
| 484 | onAnrLocked(mAwaitedFocusedApplication); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 485 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 486 | return LONG_LONG_MIN; |
| 487 | } else { |
| 488 | // Keep waiting |
| 489 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); |
| 490 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); |
| 491 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // Check if any connection ANRs are due |
| 496 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 497 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 498 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 499 | } |
| 500 | |
| 501 | // If we reached here, we have an unresponsive connection. |
| 502 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 503 | if (connection == nullptr) { |
| 504 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 505 | return nextAnrCheck; |
| 506 | } |
| 507 | connection->responsive = false; |
| 508 | // Stop waking up for this unresponsive connection |
| 509 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
| 510 | onAnrLocked(connection); |
| 511 | return LONG_LONG_MIN; |
| 512 | } |
| 513 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 514 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 515 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 516 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 517 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 518 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 519 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 522 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 523 | nsecs_t currentTime = now(); |
| 524 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 525 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 526 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 527 | // repeat if the device is just coming out of sleep. |
| 528 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | resetKeyRepeatLocked(); |
| 530 | } |
| 531 | |
| 532 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 533 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 534 | if (DEBUG_FOCUS) { |
| 535 | ALOGD("Dispatch frozen. Waiting some more."); |
| 536 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
| 540 | // Optimize latency of app switches. |
| 541 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 542 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 543 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 544 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 545 | *nextWakeupTime = mAppSwitchDueTime; |
| 546 | } |
| 547 | |
| 548 | // Ready to start a new event. |
| 549 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 550 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 551 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 552 | if (isAppSwitchDue) { |
| 553 | // The inbound queue is empty so the app switch key we were waiting |
| 554 | // for will never arrive. Stop waiting for it. |
| 555 | resetPendingAppSwitchLocked(false); |
| 556 | isAppSwitchDue = false; |
| 557 | } |
| 558 | |
| 559 | // Synthesize a key repeat if appropriate. |
| 560 | if (mKeyRepeatState.lastKeyEntry) { |
| 561 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 562 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 563 | } else { |
| 564 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 565 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // Nothing to do if there is no pending event. |
| 571 | if (!mPendingEvent) { |
| 572 | return; |
| 573 | } |
| 574 | } else { |
| 575 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 576 | mPendingEvent = mInboundQueue.front(); |
| 577 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 578 | traceInboundQueueLengthLocked(); |
| 579 | } |
| 580 | |
| 581 | // Poke user activity for this event. |
| 582 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 583 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 584 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Now we have an event to dispatch. |
| 588 | // 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] | 589 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 591 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 593 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 595 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 599 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 603 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 604 | ConfigurationChangedEntry* typedEntry = |
| 605 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 606 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 607 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 608 | break; |
| 609 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 611 | case EventEntry::Type::DEVICE_RESET: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 612 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); |
| 613 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 614 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 615 | break; |
| 616 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 618 | case EventEntry::Type::FOCUS: { |
| 619 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); |
| 620 | dispatchFocusLocked(currentTime, typedEntry); |
| 621 | done = true; |
| 622 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 623 | break; |
| 624 | } |
| 625 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 626 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 627 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 628 | if (isAppSwitchDue) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 629 | if (isAppSwitchKeyEvent(*typedEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 630 | resetPendingAppSwitchLocked(true); |
| 631 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 632 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 633 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 636 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 637 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 638 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 639 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 640 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 641 | } |
| 642 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 643 | break; |
| 644 | } |
| 645 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 646 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 647 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 648 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 649 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 651 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 652 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 653 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 654 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 655 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 656 | } |
| 657 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 658 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 663 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 664 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 666 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 667 | |
| 668 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 669 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 673 | /** |
| 674 | * Return true if the events preceding this incoming motion event should be dropped |
| 675 | * Return false otherwise (the default behaviour) |
| 676 | */ |
| 677 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 678 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 679 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 680 | |
| 681 | // Optimize case where the current application is unresponsive and the user |
| 682 | // decides to touch a window in a different application. |
| 683 | // If the application takes too long to catch up then we drop all events preceding |
| 684 | // the touch into the other window. |
| 685 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 686 | int32_t displayId = motionEntry.displayId; |
| 687 | int32_t x = static_cast<int32_t>( |
| 688 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 689 | int32_t y = static_cast<int32_t>( |
| 690 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 691 | sp<InputWindowHandle> touchedWindowHandle = |
| 692 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 693 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 694 | touchedWindowHandle->getApplicationToken() != |
| 695 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 696 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 697 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 698 | "for %s", |
| 699 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 700 | return true; |
| 701 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 702 | |
| 703 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 704 | std::vector<TouchedMonitor> gestureMonitors = |
| 705 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 706 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 707 | sp<Connection> connection = |
| 708 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 709 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 710 | // This monitor could take more input. Drop all events preceding this |
| 711 | // event, so that gesture monitor could get a chance to receive the stream |
| 712 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 713 | "responsive gesture monitor that may handle the event", |
| 714 | mAwaitedFocusedApplication->getName().c_str()); |
| 715 | return true; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 721 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 722 | // events to be processed before dispatching the key event. This is because these motion events |
| 723 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 724 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 725 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 726 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 727 | "just send the pending key event to the focused window."); |
| 728 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 729 | } |
| 730 | return false; |
| 731 | } |
| 732 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 733 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 734 | bool needWake = mInboundQueue.empty(); |
| 735 | mInboundQueue.push_back(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 736 | traceInboundQueueLengthLocked(); |
| 737 | |
| 738 | switch (entry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 739 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 740 | // Optimize app switch latency. |
| 741 | // If the application takes too long to catch up then we drop all events preceding |
| 742 | // the app switch key. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 743 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 744 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 745 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 746 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 747 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 748 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 750 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 751 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 752 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 753 | mAppSwitchSawKeyDown = false; |
| 754 | needWake = true; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | break; |
| 759 | } |
| 760 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 761 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 762 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { |
| 763 | mNextUnblockedEvent = entry; |
| 764 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 766 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 768 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 769 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 770 | break; |
| 771 | } |
| 772 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 773 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 774 | // nothing to do |
| 775 | break; |
| 776 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | return needWake; |
| 780 | } |
| 781 | |
| 782 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 783 | entry->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 784 | mRecentQueue.push_back(entry); |
| 785 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
| 786 | mRecentQueue.front()->release(); |
| 787 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 791 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 792 | int32_t y, TouchState* touchState, |
| 793 | bool addOutsideTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 794 | bool addPortalWindows) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 795 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 796 | LOG_ALWAYS_FATAL( |
| 797 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 798 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 800 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 801 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 803 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 804 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | |
| 806 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 807 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 808 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 809 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 811 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 812 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 813 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 814 | if (addPortalWindows) { |
| 815 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 816 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 817 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 818 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 819 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | // Found window. |
| 822 | return windowHandle; |
| 823 | } |
| 824 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 825 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 826 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 827 | touchState->addOrUpdateWindow(windowHandle, |
| 828 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 829 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 830 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 831 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 832 | } |
| 833 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 834 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 837 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 838 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 839 | std::vector<TouchedMonitor> touchedMonitors; |
| 840 | |
| 841 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 842 | addGestureMonitors(monitors, touchedMonitors); |
| 843 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 844 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 845 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 846 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 847 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 848 | } |
| 849 | return touchedMonitors; |
| 850 | } |
| 851 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 852 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 853 | const char* reason; |
| 854 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 855 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 856 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 858 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | reason = "inbound event was dropped because the policy consumed it"; |
| 860 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 861 | case DropReason::DISABLED: |
| 862 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | ALOGI("Dropped event because input dispatch is disabled."); |
| 864 | } |
| 865 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 866 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 867 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 868 | ALOGI("Dropped event because of pending overdue app switch."); |
| 869 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 870 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 872 | ALOGI("Dropped event because the current application is not responding and the user " |
| 873 | "has started interacting with a different application."); |
| 874 | reason = "inbound event was dropped because the current application is not responding " |
| 875 | "and the user has started interacting with a different application"; |
| 876 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | ALOGI("Dropped event because it is stale."); |
| 879 | reason = "inbound event was dropped because it is stale"; |
| 880 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 881 | case DropReason::NOT_DROPPED: { |
| 882 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 883 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 884 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 887 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 888 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 889 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 890 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 891 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 892 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 893 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 894 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 895 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 896 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 897 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 898 | } else { |
| 899 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 900 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 901 | } |
| 902 | break; |
| 903 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 904 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 905 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 906 | case EventEntry::Type::DEVICE_RESET: { |
| 907 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); |
| 908 | break; |
| 909 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 913 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 914 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 915 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 916 | } |
| 917 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 918 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 919 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 920 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 921 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 925 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 926 | } |
| 927 | |
| 928 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 929 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 930 | |
| 931 | #if DEBUG_APP_SWITCH |
| 932 | if (handled) { |
| 933 | ALOGD("App switch has arrived."); |
| 934 | } else { |
| 935 | ALOGD("App switch was abandoned."); |
| 936 | } |
| 937 | #endif |
| 938 | } |
| 939 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 940 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 941 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 945 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 946 | return false; |
| 947 | } |
| 948 | |
| 949 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 950 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 951 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 953 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 954 | |
| 955 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 956 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | return true; |
| 958 | } |
| 959 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 960 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 961 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 965 | while (!mInboundQueue.empty()) { |
| 966 | EventEntry* entry = mInboundQueue.front(); |
| 967 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 968 | releaseInboundEventLocked(entry); |
| 969 | } |
| 970 | traceInboundQueueLengthLocked(); |
| 971 | } |
| 972 | |
| 973 | void InputDispatcher::releasePendingEventLocked() { |
| 974 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 975 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 976 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 981 | InjectionState* injectionState = entry->injectionState; |
| 982 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 983 | #if DEBUG_DISPATCH_CYCLE |
| 984 | ALOGD("Injected inbound event was dropped."); |
| 985 | #endif |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 986 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 987 | } |
| 988 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 989 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 990 | } |
| 991 | addRecentEventLocked(entry); |
| 992 | entry->release(); |
| 993 | } |
| 994 | |
| 995 | void InputDispatcher::resetKeyRepeatLocked() { |
| 996 | if (mKeyRepeatState.lastKeyEntry) { |
| 997 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 998 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1002 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 1004 | |
| 1005 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1006 | uint32_t policyFlags = entry->policyFlags & |
| 1007 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1008 | if (entry->refCount == 1) { |
| 1009 | entry->recycle(); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1010 | entry->id = mIdGenerator.nextId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | entry->eventTime = currentTime; |
| 1012 | entry->policyFlags = policyFlags; |
| 1013 | entry->repeatCount += 1; |
| 1014 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1015 | KeyEntry* newEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1016 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1017 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1018 | entry->keyCode, entry->scanCode, entry->metaState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1019 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1020 | |
| 1021 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 1022 | entry->release(); |
| 1023 | |
| 1024 | entry = newEntry; |
| 1025 | } |
| 1026 | entry->syntheticRepeat = true; |
| 1027 | |
| 1028 | // Increment reference count since we keep a reference to the event in |
| 1029 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 1030 | entry->refCount += 1; |
| 1031 | |
| 1032 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 1033 | return entry; |
| 1034 | } |
| 1035 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1036 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
| 1037 | ConfigurationChangedEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1039 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1040 | #endif |
| 1041 | |
| 1042 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1043 | resetKeyRepeatLocked(); |
| 1044 | |
| 1045 | // 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] | 1046 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1047 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 | commandEntry->eventTime = entry->eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1049 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | return true; |
| 1051 | } |
| 1052 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1053 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* 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("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1056 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | #endif |
| 1058 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1059 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 | options.deviceId = entry->deviceId; |
| 1061 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1065 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1066 | std::string_view reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1067 | if (mPendingEvent != nullptr) { |
| 1068 | // Move the pending event to the front of the queue. This will give the chance |
| 1069 | // for the pending event to get dispatched to the newly focused window |
| 1070 | mInboundQueue.push_front(mPendingEvent); |
| 1071 | mPendingEvent = nullptr; |
| 1072 | } |
| 1073 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1074 | FocusEntry* focusEntry = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1075 | new FocusEntry(mIdGenerator.nextId(), now(), windowToken, hasFocus, reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1076 | |
| 1077 | // This event should go to the front of the queue, but behind all other focus events |
| 1078 | // Find the last focus event, and insert right after it |
| 1079 | std::deque<EventEntry*>::reverse_iterator it = |
| 1080 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1081 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); |
| 1082 | |
| 1083 | // Maintain the order of focus events. Insert the entry after all other focus events. |
| 1084 | mInboundQueue.insert(it.base(), focusEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1088 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1089 | if (channel == nullptr) { |
| 1090 | return; // Window has gone away |
| 1091 | } |
| 1092 | InputTarget target; |
| 1093 | target.inputChannel = channel; |
| 1094 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1095 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1096 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1097 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1098 | std::string reason = std::string("reason=").append(entry->reason); |
| 1099 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1100 | dispatchEventLocked(currentTime, entry, {target}); |
| 1101 | } |
| 1102 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1103 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1104 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1105 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1106 | if (!entry->dispatchInProgress) { |
| 1107 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1108 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1109 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1110 | if (mKeyRepeatState.lastKeyEntry && |
| 1111 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1112 | // We have seen two identical key downs in a row which indicates that the device |
| 1113 | // driver is automatically generating key repeats itself. We take note of the |
| 1114 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1115 | // we will not need to synthesize key repeats ourselves. |
| 1116 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1117 | resetKeyRepeatLocked(); |
| 1118 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1119 | } else { |
| 1120 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1121 | resetKeyRepeatLocked(); |
| 1122 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1123 | } |
| 1124 | mKeyRepeatState.lastKeyEntry = entry; |
| 1125 | entry->refCount += 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1126 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1127 | resetKeyRepeatLocked(); |
| 1128 | } |
| 1129 | |
| 1130 | if (entry->repeatCount == 1) { |
| 1131 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1132 | } else { |
| 1133 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1134 | } |
| 1135 | |
| 1136 | entry->dispatchInProgress = true; |
| 1137 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1138 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // Handle case where the policy asked us to try again later last time. |
| 1142 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1143 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1144 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1145 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1146 | } |
| 1147 | return false; // wait until next wakeup |
| 1148 | } |
| 1149 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1150 | entry->interceptKeyWakeupTime = 0; |
| 1151 | } |
| 1152 | |
| 1153 | // Give the policy a chance to intercept the key. |
| 1154 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1155 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1156 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1157 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1158 | sp<IBinder> focusedWindowToken = |
| 1159 | getValueByKey(mFocusedWindowTokenByDisplay, getTargetDisplayId(*entry)); |
| 1160 | if (focusedWindowToken != nullptr) { |
| 1161 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1162 | } |
| 1163 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1164 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | entry->refCount += 1; |
| 1166 | return false; // wait for the command to run |
| 1167 | } else { |
| 1168 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1169 | } |
| 1170 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1171 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1172 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1177 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1178 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1179 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1180 | : INPUT_EVENT_INJECTION_FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1181 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1182 | return true; |
| 1183 | } |
| 1184 | |
| 1185 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1186 | std::vector<InputTarget> inputTargets; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1187 | int32_t injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1188 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1193 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1198 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1199 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | |
| 1201 | // Dispatch the key. |
| 1202 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1206 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1208 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1209 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1210 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1211 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1212 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1213 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1214 | #endif |
| 1215 | } |
| 1216 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1217 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1218 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1219 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1221 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | entry->dispatchInProgress = true; |
| 1223 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1224 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1228 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1229 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1230 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1231 | : INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | return true; |
| 1233 | } |
| 1234 | |
| 1235 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1236 | |
| 1237 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1238 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | |
| 1240 | bool conflictingPointerActions = false; |
| 1241 | int32_t injectionResult; |
| 1242 | if (isPointerEvent) { |
| 1243 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1244 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1245 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1246 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | } else { |
| 1248 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1249 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1250 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1251 | } |
| 1252 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1253 | return false; |
| 1254 | } |
| 1255 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1256 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1257 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| 1258 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1259 | return true; |
| 1260 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1262 | CancelationOptions::Mode mode(isPointerEvent |
| 1263 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1264 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1265 | CancelationOptions options(mode, "input event injection failed"); |
| 1266 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1267 | return true; |
| 1268 | } |
| 1269 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1270 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1271 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1273 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1274 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1275 | mTouchStatesByDisplay.find(entry->displayId); |
| 1276 | if (it != mTouchStatesByDisplay.end()) { |
| 1277 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1278 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1279 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1280 | // the corresponding displays as well. |
| 1281 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1282 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1283 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1284 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1290 | // Dispatch the motion. |
| 1291 | if (conflictingPointerActions) { |
| 1292 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1293 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1294 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1295 | } |
| 1296 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1297 | return true; |
| 1298 | } |
| 1299 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1300 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1301 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1302 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1303 | ", policyFlags=0x%x, " |
| 1304 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1305 | "metaState=0x%x, buttonState=0x%x," |
| 1306 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1307 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1308 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1309 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1311 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1312 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1313 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1314 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1315 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1316 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1317 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1318 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1319 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1320 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1321 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1322 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1323 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1324 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1325 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1326 | } |
| 1327 | #endif |
| 1328 | } |
| 1329 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1330 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1331 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1332 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1333 | #if DEBUG_DISPATCH_CYCLE |
| 1334 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1335 | #endif |
| 1336 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1337 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1338 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1339 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1340 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1341 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1343 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1344 | sp<Connection> connection = |
| 1345 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1346 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1347 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1349 | if (DEBUG_FOCUS) { |
| 1350 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1351 | "is no longer registered with the input dispatcher.", |
| 1352 | inputTarget.inputChannel->getName().c_str()); |
| 1353 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1358 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1359 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1360 | // If the policy decides to close the app, we will get a channel removal event via |
| 1361 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1362 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1363 | // pile up. |
| 1364 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1365 | connection->inputChannel->getName().c_str()); |
| 1366 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1367 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1368 | "application not responding"); |
| 1369 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1373 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1374 | if (DEBUG_FOCUS) { |
| 1375 | ALOGD("Resetting ANR timeouts."); |
| 1376 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1377 | |
| 1378 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1379 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1380 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1383 | /** |
| 1384 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1385 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1386 | * Focused display is the display that the user most recently interacted with. |
| 1387 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1388 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1389 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1390 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1391 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1392 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1393 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1394 | break; |
| 1395 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1396 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1397 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1398 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1399 | break; |
| 1400 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1401 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1402 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1403 | case EventEntry::Type::DEVICE_RESET: { |
| 1404 | 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] | 1405 | return ADISPLAY_ID_NONE; |
| 1406 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1407 | } |
| 1408 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1409 | } |
| 1410 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1411 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1412 | const char* focusedWindowName) { |
| 1413 | if (mAnrTracker.empty()) { |
| 1414 | // already processed all events that we waited for |
| 1415 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
| 1419 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1420 | // Start the timer |
| 1421 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " |
| 1422 | "focus to change", |
| 1423 | focusedWindowName); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1424 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1425 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1426 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1427 | return true; |
| 1428 | } |
| 1429 | |
| 1430 | // We still have pending events, and already started the timer |
| 1431 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1432 | return true; // Still waiting |
| 1433 | } |
| 1434 | |
| 1435 | // Waited too long, and some connection still hasn't processed all motions |
| 1436 | // Just send the key to the focused window |
| 1437 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1438 | focusedWindowName); |
| 1439 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1443 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1444 | const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1445 | std::vector<InputTarget>& inputTargets, |
| 1446 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1447 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1448 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1449 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1450 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1451 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1452 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1453 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | // If there is no currently focused window and no focused application |
| 1455 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1456 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1457 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1458 | "display %" PRId32 ".", |
| 1459 | EventEntry::typeToString(entry.type), displayId); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1460 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1463 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1464 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1465 | // start interacting with another application via touch (app switch). This code can be removed |
| 1466 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1467 | // an app is expected to have a focused window. |
| 1468 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1469 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1470 | // 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] | 1471 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1472 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1473 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1474 | mAwaitedFocusedApplication = focusedApplicationHandle; |
| 1475 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1476 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1477 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1478 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
| 1479 | return INPUT_EVENT_INJECTION_PENDING; |
| 1480 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1481 | // Already raised ANR. Drop the event |
| 1482 | ALOGE("Dropping %s event because there is no focused window", |
| 1483 | EventEntry::typeToString(entry.type)); |
| 1484 | return INPUT_EVENT_INJECTION_FAILED; |
| 1485 | } else { |
| 1486 | // Still waiting for the focused window |
| 1487 | return INPUT_EVENT_INJECTION_PENDING; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // we have a valid, non-null focused window |
| 1492 | resetNoFocusedWindowTimeoutLocked(); |
| 1493 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1494 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1495 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1496 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1499 | if (focusedWindowHandle->getInfo()->paused) { |
| 1500 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
| 1501 | return INPUT_EVENT_INJECTION_PENDING; |
| 1502 | } |
| 1503 | |
| 1504 | // If the event is a key event, then we must wait for all previous events to |
| 1505 | // complete before delivering it because previous events may have the |
| 1506 | // side-effect of transferring focus to a different window and we want to |
| 1507 | // ensure that the following keys are sent to the new window. |
| 1508 | // |
| 1509 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1510 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1511 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1512 | // often anticipate pending UI changes when typing on a keyboard. |
| 1513 | // To obtain this behavior, we must serialize key events with respect to all |
| 1514 | // prior input events. |
| 1515 | if (entry.type == EventEntry::Type::KEY) { |
| 1516 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1517 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
| 1518 | return INPUT_EVENT_INJECTION_PENDING; |
| 1519 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1523 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1524 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1525 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | |
| 1527 | // Done. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1528 | return INPUT_EVENT_INJECTION_SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1531 | /** |
| 1532 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1533 | * that are currently unresponsive. |
| 1534 | */ |
| 1535 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1536 | const std::vector<TouchedMonitor>& monitors) const { |
| 1537 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1538 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1539 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1540 | sp<Connection> connection = getConnectionLocked( |
| 1541 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1542 | if (connection == nullptr) { |
| 1543 | ALOGE("Could not find connection for monitor %s", |
| 1544 | monitor.monitor.inputChannel->getName().c_str()); |
| 1545 | return false; |
| 1546 | } |
| 1547 | if (!connection->responsive) { |
| 1548 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1549 | connection->inputChannel->getName().c_str()); |
| 1550 | return false; |
| 1551 | } |
| 1552 | return true; |
| 1553 | }); |
| 1554 | return responsiveMonitors; |
| 1555 | } |
| 1556 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1558 | const MotionEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1559 | std::vector<InputTarget>& inputTargets, |
| 1560 | nsecs_t* nextWakeupTime, |
| 1561 | bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1562 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | enum InjectionPermission { |
| 1564 | INJECTION_PERMISSION_UNKNOWN, |
| 1565 | INJECTION_PERMISSION_GRANTED, |
| 1566 | INJECTION_PERMISSION_DENIED |
| 1567 | }; |
| 1568 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1569 | // For security reasons, we defer updating the touch state until we are sure that |
| 1570 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1571 | int32_t displayId = entry.displayId; |
| 1572 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1573 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1574 | |
| 1575 | // Update the touch state as needed based on the properties of the touch event. |
| 1576 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1577 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1578 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1579 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1580 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1581 | // Copy current touch state into tempTouchState. |
| 1582 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1583 | // 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] | 1584 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1585 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1586 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1587 | mTouchStatesByDisplay.find(displayId); |
| 1588 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1589 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1590 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1593 | bool isSplit = tempTouchState.split; |
| 1594 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1595 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1596 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1597 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1598 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1599 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1600 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1601 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1602 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | bool wrongDevice = false; |
| 1604 | if (newGesture) { |
| 1605 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1606 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1607 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1608 | "in display %" PRId32, |
| 1609 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1610 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1611 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1612 | switchedDevice = false; |
| 1613 | wrongDevice = true; |
| 1614 | goto Failed; |
| 1615 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1616 | tempTouchState.reset(); |
| 1617 | tempTouchState.down = down; |
| 1618 | tempTouchState.deviceId = entry.deviceId; |
| 1619 | tempTouchState.source = entry.source; |
| 1620 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1622 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1623 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1624 | "in display %" PRId32, |
| 1625 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1626 | // TODO: test multiple simultaneous input streams. |
| 1627 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1628 | switchedDevice = false; |
| 1629 | wrongDevice = true; |
| 1630 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1634 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1635 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1636 | int32_t x; |
| 1637 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1639 | // Always dispatch mouse events to cursor position. |
| 1640 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1641 | x = int32_t(entry.xCursorPosition); |
| 1642 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1643 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1644 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1645 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1646 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1647 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1648 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1649 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1650 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1651 | |
| 1652 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1653 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1654 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1656 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1657 | if (newTouchedWindowHandle != nullptr && |
| 1658 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1659 | // New window supports splitting, but we should never split mouse events. |
| 1660 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1661 | } else if (isSplit) { |
| 1662 | // New window does not support splitting but we have already split events. |
| 1663 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1664 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1668 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1669 | // 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] | 1670 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1673 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1674 | ALOGI("Not sending touch event to %s because it is paused", |
| 1675 | newTouchedWindowHandle->getName().c_str()); |
| 1676 | newTouchedWindowHandle = nullptr; |
| 1677 | } |
| 1678 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1679 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1680 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1681 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1682 | if (!isResponsive) { |
| 1683 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1684 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1685 | newTouchedWindowHandle = nullptr; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1690 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1691 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1692 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1693 | 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] | 1694 | "(%d, %d) in display %" PRId32 ".", |
| 1695 | x, y, displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1696 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1697 | goto Failed; |
| 1698 | } |
| 1699 | |
| 1700 | if (newTouchedWindowHandle != nullptr) { |
| 1701 | // Set target flags. |
| 1702 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1703 | if (isSplit) { |
| 1704 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1705 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1706 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1707 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1708 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1709 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1710 | } |
| 1711 | |
| 1712 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1713 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 1714 | newHoverWindowHandle = nullptr; |
| 1715 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1716 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | // Update the temporary touch state. |
| 1720 | BitSet32 pointerIds; |
| 1721 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1722 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1723 | pointerIds.markBit(pointerId); |
| 1724 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1725 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1728 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1729 | } else { |
| 1730 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1731 | |
| 1732 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1733 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1734 | if (DEBUG_FOCUS) { |
| 1735 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1736 | "dropped the pointer down event in display %" PRId32, |
| 1737 | displayId); |
| 1738 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1740 | goto Failed; |
| 1741 | } |
| 1742 | |
| 1743 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1744 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1745 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1746 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1747 | 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] | 1748 | |
| 1749 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1750 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1751 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1752 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1753 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1754 | if (DEBUG_FOCUS) { |
| 1755 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1756 | oldTouchedWindowHandle->getName().c_str(), |
| 1757 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1758 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1759 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1760 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1761 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1762 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1763 | |
| 1764 | // Make a slippery entrance into the new window. |
| 1765 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1766 | isSplit = true; |
| 1767 | } |
| 1768 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1769 | int32_t targetFlags = |
| 1770 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | if (isSplit) { |
| 1772 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1773 | } |
| 1774 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1775 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1776 | } |
| 1777 | |
| 1778 | BitSet32 pointerIds; |
| 1779 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1780 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1782 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1788 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 1789 | // when dispatching it as is to newTouchedWindowHandle. |
| 1790 | if (mLastHoverWindowHandle != nullptr && |
| 1791 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 1792 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | #if DEBUG_HOVER |
| 1794 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1795 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1797 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1798 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1801 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 1802 | // when dispatching it as is to newTouchedWindowHandle. |
| 1803 | if (newHoverWindowHandle != nullptr && |
| 1804 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1805 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | #if DEBUG_HOVER |
| 1807 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1808 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1810 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1811 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1812 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | // Check permission to inject into all touched foreground windows and ensure there |
| 1817 | // is at least one touched foreground window. |
| 1818 | { |
| 1819 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1820 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1822 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1823 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1824 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1825 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1826 | goto Failed; |
| 1827 | } |
| 1828 | } |
| 1829 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1830 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1831 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1832 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1833 | "%" PRId32 " or gesture monitor to receive it.", |
| 1834 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1836 | goto Failed; |
| 1837 | } |
| 1838 | |
| 1839 | // Permission granted to injection into all touched foreground windows. |
| 1840 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1841 | } |
| 1842 | |
| 1843 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1844 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1845 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1846 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1847 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1848 | if (foregroundWindowHandle) { |
| 1849 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1850 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1851 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1852 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1853 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1854 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1855 | InputTarget::FLAG_ZERO_COORDS, |
| 1856 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1857 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1863 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1864 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1865 | // of the touch gesture. |
| 1866 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1867 | // engine only supports touch events. We would need to add a mechanism similar |
| 1868 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1869 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1870 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1871 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1872 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1873 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1874 | getWindowHandlesLocked(displayId); |
| 1875 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1876 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1877 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1878 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1879 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1880 | .addOrUpdateWindow(windowHandle, |
| 1881 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1882 | InputTarget:: |
| 1883 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1884 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1885 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | // Success! Output targets. |
| 1892 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1893 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1894 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1895 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1896 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1899 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1900 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1901 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1904 | // Drop the outside or hover touch windows since we will not care about them |
| 1905 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1906 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1907 | |
| 1908 | Failed: |
| 1909 | // Check injection permission once and for all. |
| 1910 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1911 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1912 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1913 | } else { |
| 1914 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1915 | } |
| 1916 | } |
| 1917 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1918 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 1919 | return injectionResult; |
| 1920 | } |
| 1921 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1923 | if (!wrongDevice) { |
| 1924 | if (switchedDevice) { |
| 1925 | if (DEBUG_FOCUS) { |
| 1926 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1927 | } |
| 1928 | *outConflictingPointerActions = true; |
| 1929 | } |
| 1930 | |
| 1931 | if (isHoverAction) { |
| 1932 | // Started hovering, therefore no longer down. |
| 1933 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1934 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1935 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 1936 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1937 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1938 | *outConflictingPointerActions = true; |
| 1939 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1940 | tempTouchState.reset(); |
| 1941 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1942 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 1943 | tempTouchState.deviceId = entry.deviceId; |
| 1944 | tempTouchState.source = entry.source; |
| 1945 | tempTouchState.displayId = displayId; |
| 1946 | } |
| 1947 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 1948 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 1949 | // All pointers up or canceled. |
| 1950 | tempTouchState.reset(); |
| 1951 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1952 | // First pointer went down. |
| 1953 | if (oldState && oldState->down) { |
| 1954 | if (DEBUG_FOCUS) { |
| 1955 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1956 | } |
| 1957 | *outConflictingPointerActions = true; |
| 1958 | } |
| 1959 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1960 | // One pointer went up. |
| 1961 | if (isSplit) { |
| 1962 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1963 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1965 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 1966 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 1967 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1968 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1969 | if (touchedWindow.pointerIds.isEmpty()) { |
| 1970 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 1971 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1973 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1974 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1975 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1976 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1977 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1978 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1979 | // Save changes unless the action was scroll in which case the temporary touch |
| 1980 | // state was only valid for this one action. |
| 1981 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 1982 | if (tempTouchState.displayId >= 0) { |
| 1983 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 1984 | } else { |
| 1985 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1986 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1987 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1988 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1989 | // Update hover state. |
| 1990 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1991 | } |
| 1992 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1993 | return injectionResult; |
| 1994 | } |
| 1995 | |
| 1996 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1997 | int32_t targetFlags, BitSet32 pointerIds, |
| 1998 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 1999 | std::vector<InputTarget>::iterator it = |
| 2000 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2001 | [&windowHandle](const InputTarget& inputTarget) { |
| 2002 | return inputTarget.inputChannel->getConnectionToken() == |
| 2003 | windowHandle->getToken(); |
| 2004 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2005 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2006 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2007 | |
| 2008 | if (it == inputTargets.end()) { |
| 2009 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2010 | std::shared_ptr<InputChannel> inputChannel = |
| 2011 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2012 | if (inputChannel == nullptr) { |
| 2013 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2014 | return; |
| 2015 | } |
| 2016 | inputTarget.inputChannel = inputChannel; |
| 2017 | inputTarget.flags = targetFlags; |
| 2018 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2019 | inputTargets.push_back(inputTarget); |
| 2020 | it = inputTargets.end() - 1; |
| 2021 | } |
| 2022 | |
| 2023 | ALOG_ASSERT(it->flags == targetFlags); |
| 2024 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2025 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2026 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2029 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2030 | int32_t displayId, float xOffset, |
| 2031 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2032 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2033 | mGlobalMonitorsByDisplay.find(displayId); |
| 2034 | |
| 2035 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2036 | const std::vector<Monitor>& monitors = it->second; |
| 2037 | for (const Monitor& monitor : monitors) { |
| 2038 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2039 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2043 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2044 | float yOffset, |
| 2045 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2046 | InputTarget target; |
| 2047 | target.inputChannel = monitor.inputChannel; |
| 2048 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2049 | ui::Transform t; |
| 2050 | t.set(xOffset, yOffset); |
| 2051 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2052 | inputTargets.push_back(target); |
| 2053 | } |
| 2054 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2055 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2056 | const InjectionState* injectionState) { |
| 2057 | if (injectionState && |
| 2058 | (windowHandle == nullptr || |
| 2059 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2060 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2061 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2062 | 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] | 2063 | "owned by uid %d", |
| 2064 | injectionState->injectorPid, injectionState->injectorUid, |
| 2065 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2066 | } else { |
| 2067 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2068 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2069 | } |
| 2070 | return false; |
| 2071 | } |
| 2072 | return true; |
| 2073 | } |
| 2074 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2075 | /** |
| 2076 | * Indicate whether one window handle should be considered as obscuring |
| 2077 | * another window handle. We only check a few preconditions. Actually |
| 2078 | * checking the bounds is left to the caller. |
| 2079 | */ |
| 2080 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2081 | const sp<InputWindowHandle>& otherHandle) { |
| 2082 | // Compare by token so cloned layers aren't counted |
| 2083 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2084 | return false; |
| 2085 | } |
| 2086 | auto info = windowHandle->getInfo(); |
| 2087 | auto otherInfo = otherHandle->getInfo(); |
| 2088 | if (!otherInfo->visible) { |
| 2089 | return false; |
Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2090 | } else if (info->ownerPid == otherInfo->ownerPid) { |
| 2091 | // If ownerPid is the same we don't generate occlusion events as there |
| 2092 | // is no in-process security boundary. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2093 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2094 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2095 | return false; |
| 2096 | } else if (otherInfo->displayId != info->displayId) { |
| 2097 | return false; |
| 2098 | } |
| 2099 | return true; |
| 2100 | } |
| 2101 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2102 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2103 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2104 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2105 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2106 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2107 | if (windowHandle == otherHandle) { |
| 2108 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2109 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2110 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2111 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2112 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2113 | return true; |
| 2114 | } |
| 2115 | } |
| 2116 | return false; |
| 2117 | } |
| 2118 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2119 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2120 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2121 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2122 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 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 | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2126 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [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->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2130 | return true; |
| 2131 | } |
| 2132 | } |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2136 | std::string InputDispatcher::getApplicationWindowLabel( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2137 | const std::shared_ptr<InputApplicationHandle>& applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2138 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2139 | if (applicationHandle != nullptr) { |
| 2140 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2141 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2142 | } else { |
| 2143 | return applicationHandle->getName(); |
| 2144 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2145 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2146 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2147 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2148 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2152 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2153 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2154 | // Focus events are passed to apps, but do not represent user activity. |
| 2155 | return; |
| 2156 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2157 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2158 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2159 | if (focusedWindowHandle != nullptr) { |
| 2160 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2161 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2162 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2163 | 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] | 2164 | #endif |
| 2165 | return; |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2170 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2171 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2172 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2173 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2174 | return; |
| 2175 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2176 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2177 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2178 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2179 | } |
| 2180 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2181 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2182 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2183 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2184 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2185 | return; |
| 2186 | } |
| 2187 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2188 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2189 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2190 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2191 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2192 | case EventEntry::Type::DEVICE_RESET: { |
| 2193 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2194 | EventEntry::typeToString(eventEntry.type)); |
| 2195 | break; |
| 2196 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2199 | std::unique_ptr<CommandEntry> commandEntry = |
| 2200 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2201 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2203 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2207 | const sp<Connection>& connection, |
| 2208 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2209 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2210 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2211 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2212 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2213 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2214 | ATRACE_NAME(message.c_str()); |
| 2215 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2216 | #if DEBUG_DISPATCH_CYCLE |
| 2217 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2218 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2219 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2220 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2221 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2222 | #endif |
| 2223 | |
| 2224 | // Skip this event if the connection status is not normal. |
| 2225 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2226 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2227 | #if DEBUG_DISPATCH_CYCLE |
| 2228 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2229 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2230 | #endif |
| 2231 | return; |
| 2232 | } |
| 2233 | |
| 2234 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2235 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2236 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2237 | "Entry type %s should not have FLAG_SPLIT", |
| 2238 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2239 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2240 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2241 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2242 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2243 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2244 | if (!splitMotionEntry) { |
| 2245 | return; // split event was dropped |
| 2246 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2247 | if (DEBUG_FOCUS) { |
| 2248 | ALOGD("channel '%s' ~ Split motion event.", |
| 2249 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2250 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2251 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2252 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2253 | splitMotionEntry->release(); |
| 2254 | return; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2259 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2260 | } |
| 2261 | |
| 2262 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2263 | const sp<Connection>& connection, |
| 2264 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2265 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2266 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2267 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2268 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2269 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2270 | ATRACE_NAME(message.c_str()); |
| 2271 | } |
| 2272 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2273 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2274 | |
| 2275 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2276 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2277 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2278 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2279 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2280 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2281 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2282 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2283 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2284 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2285 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2286 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2287 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2288 | |
| 2289 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2290 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2291 | startDispatchCycleLocked(currentTime, connection); |
| 2292 | } |
| 2293 | } |
| 2294 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2295 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2296 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2297 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2298 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2299 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2300 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2301 | connection->getInputChannelName().c_str(), |
| 2302 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2303 | ATRACE_NAME(message.c_str()); |
| 2304 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2305 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2306 | if (!(inputTargetFlags & dispatchMode)) { |
| 2307 | return; |
| 2308 | } |
| 2309 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2310 | |
| 2311 | // This is a new event. |
| 2312 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2313 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2314 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2315 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2316 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2317 | // different EventEntry than what was passed in. |
| 2318 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2319 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2320 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2321 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2322 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2323 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2324 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2325 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2326 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2327 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2328 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2329 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2330 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2331 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2333 | return; // skip the inconsistent event |
| 2334 | } |
| 2335 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2337 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2338 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2339 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2340 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2341 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2342 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2343 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2344 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2345 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2346 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2347 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2348 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2349 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2350 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2351 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2352 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2353 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2354 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2355 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2356 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2357 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2358 | } |
| 2359 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2360 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2361 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2362 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2363 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2364 | "event", |
| 2365 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2366 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2367 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2368 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2369 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2370 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2371 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2372 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2373 | } |
| 2374 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2375 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2376 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2377 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2378 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2379 | dispatchEntry->resolvedFlags)) { |
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: skipping inconsistent motion " |
| 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 | return; // skip the inconsistent event |
| 2386 | } |
| 2387 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2388 | dispatchEntry->resolvedEventId = |
| 2389 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2390 | ? mIdGenerator.nextId() |
| 2391 | : motionEntry.id; |
| 2392 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2393 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2394 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2395 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2396 | ATRACE_NAME(message.c_str()); |
| 2397 | } |
| 2398 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2399 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2400 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2401 | |
| 2402 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2403 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2404 | case EventEntry::Type::FOCUS: { |
| 2405 | break; |
| 2406 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2407 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2408 | case EventEntry::Type::DEVICE_RESET: { |
| 2409 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2410 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2411 | break; |
| 2412 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | // Remember that we are waiting for this dispatch to complete. |
| 2416 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2417 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2421 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2422 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2425 | /** |
| 2426 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2427 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2428 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2429 | * We will see both launcher and wallpaper in that list. |
| 2430 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2431 | * until the set of interacted connections changes. |
| 2432 | * |
| 2433 | * The following items are skipped, to reduce the logspam: |
| 2434 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2435 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2436 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2437 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2438 | * Both of those ACTION_UP events would not be logged |
| 2439 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events |
| 2440 | * will not be logged. This is omitted to reduce the amount of data printed. |
| 2441 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore |
| 2442 | * gesture monitor is the only connection receiving the remainder of the gesture. |
| 2443 | */ |
| 2444 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2445 | const std::vector<InputTarget>& targets) { |
| 2446 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2447 | if (entry.type == EventEntry::Type::KEY) { |
| 2448 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2449 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2450 | return; |
| 2451 | } |
| 2452 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2453 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2454 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2455 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2456 | return; |
| 2457 | } |
| 2458 | } else { |
| 2459 | return; // Not a key or a motion |
| 2460 | } |
| 2461 | |
| 2462 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; |
| 2463 | std::vector<sp<Connection>> newConnections; |
| 2464 | for (const InputTarget& target : targets) { |
| 2465 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2466 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2467 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2468 | } |
| 2469 | |
| 2470 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2471 | sp<Connection> connection = getConnectionLocked(token); |
| 2472 | if (connection == nullptr || connection->monitor) { |
| 2473 | continue; // We only need to keep track of the non-monitor connections. |
| 2474 | } |
| 2475 | newConnectionTokens.insert(std::move(token)); |
| 2476 | newConnections.emplace_back(connection); |
| 2477 | } |
| 2478 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 2479 | return; // no change |
| 2480 | } |
| 2481 | mInteractionConnectionTokens = newConnectionTokens; |
| 2482 | |
| 2483 | std::string windowList; |
| 2484 | for (const sp<Connection>& connection : newConnections) { |
| 2485 | windowList += connection->getWindowName() + ", "; |
| 2486 | } |
| 2487 | std::string message = "Interaction with windows: " + windowList; |
| 2488 | if (windowList.empty()) { |
| 2489 | message += "<none>"; |
| 2490 | } |
| 2491 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 2492 | } |
| 2493 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2494 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2495 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2496 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2497 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2498 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2499 | return; |
| 2500 | } |
| 2501 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2502 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); |
| 2503 | if (focusedToken == token) { |
| 2504 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2505 | return; |
| 2506 | } |
| 2507 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2508 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2509 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2510 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2511 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2515 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2516 | if (ATRACE_ENABLED()) { |
| 2517 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2518 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2519 | ATRACE_NAME(message.c_str()); |
| 2520 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2521 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2522 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2523 | #endif |
| 2524 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2525 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2526 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2527 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2528 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2529 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2530 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2531 | |
| 2532 | // Publish the event. |
| 2533 | status_t status; |
| 2534 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2535 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2536 | case EventEntry::Type::KEY: { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2537 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2538 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2539 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2540 | // Publish the key event. |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2541 | status = |
| 2542 | connection->inputPublisher |
| 2543 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2544 | keyEntry->deviceId, keyEntry->source, |
| 2545 | keyEntry->displayId, std::move(hmac), |
| 2546 | dispatchEntry->resolvedAction, |
| 2547 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2548 | keyEntry->scanCode, keyEntry->metaState, |
| 2549 | keyEntry->repeatCount, keyEntry->downTime, |
| 2550 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2551 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2552 | } |
| 2553 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2554 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2555 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2556 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2557 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2558 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2559 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2560 | // 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] | 2561 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2562 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2563 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2564 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2565 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2566 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2567 | // Don't apply window scale here since we don't want scale to affect raw |
| 2568 | // coordinates. The scale will be sent back to the client and applied |
| 2569 | // later when requesting relative coordinates. |
| 2570 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2571 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2572 | } |
| 2573 | usingCoords = scaledCoords; |
| 2574 | } |
| 2575 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2576 | // We don't want the dispatch target to know. |
| 2577 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2578 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2579 | scaledCoords[i].clear(); |
| 2580 | } |
| 2581 | usingCoords = scaledCoords; |
| 2582 | } |
| 2583 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2584 | |
| 2585 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2586 | |
| 2587 | // Publish the motion event. |
| 2588 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2589 | .publishMotionEvent(dispatchEntry->seq, |
| 2590 | dispatchEntry->resolvedEventId, |
| 2591 | motionEntry->deviceId, motionEntry->source, |
| 2592 | motionEntry->displayId, std::move(hmac), |
| 2593 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2594 | motionEntry->actionButton, |
| 2595 | dispatchEntry->resolvedFlags, |
| 2596 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2597 | motionEntry->buttonState, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2598 | motionEntry->classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2599 | dispatchEntry->transform, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2600 | motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2601 | motionEntry->yPrecision, |
| 2602 | motionEntry->xCursorPosition, |
| 2603 | motionEntry->yCursorPosition, |
| 2604 | motionEntry->downTime, motionEntry->eventTime, |
| 2605 | motionEntry->pointerCount, |
| 2606 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2607 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2608 | break; |
| 2609 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2610 | case EventEntry::Type::FOCUS: { |
| 2611 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2612 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2613 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2614 | focusEntry->hasFocus, |
| 2615 | mInTouchMode); |
| 2616 | break; |
| 2617 | } |
| 2618 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2619 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2620 | case EventEntry::Type::DEVICE_RESET: { |
| 2621 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2622 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2623 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2624 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | // Check the result. |
| 2628 | if (status) { |
| 2629 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2630 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2631 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2632 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2633 | "should be empty and we shouldn't have any problems writing an " |
| 2634 | "event to it, status=%d", |
| 2635 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2636 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2637 | } else { |
| 2638 | // Pipe is full and we are waiting for the app to finish process some events |
| 2639 | // before sending more events to it. |
| 2640 | #if DEBUG_DISPATCH_CYCLE |
| 2641 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2642 | "waiting for the application to catch up", |
| 2643 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2644 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2645 | } |
| 2646 | } else { |
| 2647 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2648 | "status=%d", |
| 2649 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2650 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2651 | } |
| 2652 | return; |
| 2653 | } |
| 2654 | |
| 2655 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2656 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2657 | connection->outboundQueue.end(), |
| 2658 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2659 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2660 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2661 | if (connection->responsive) { |
| 2662 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 2663 | connection->inputChannel->getConnectionToken()); |
| 2664 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2665 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2669 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 2670 | size_t size; |
| 2671 | switch (event.type) { |
| 2672 | case VerifiedInputEvent::Type::KEY: { |
| 2673 | size = sizeof(VerifiedKeyEvent); |
| 2674 | break; |
| 2675 | } |
| 2676 | case VerifiedInputEvent::Type::MOTION: { |
| 2677 | size = sizeof(VerifiedMotionEvent); |
| 2678 | break; |
| 2679 | } |
| 2680 | } |
| 2681 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 2682 | return mHmacKeyManager.sign(start, size); |
| 2683 | } |
| 2684 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2685 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2686 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 2687 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2688 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 2689 | // Only sign events up and down events as the purely move events |
| 2690 | // are tied to their up/down counterparts so signing would be redundant. |
| 2691 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 2692 | verifiedEvent.actionMasked = actionMasked; |
| 2693 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2694 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2695 | } |
| 2696 | return INVALID_HMAC; |
| 2697 | } |
| 2698 | |
| 2699 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2700 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 2701 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 2702 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2703 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2704 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2707 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2708 | const sp<Connection>& connection, uint32_t seq, |
| 2709 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 | #if DEBUG_DISPATCH_CYCLE |
| 2711 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2712 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2713 | #endif |
| 2714 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2715 | if (connection->status == Connection::STATUS_BROKEN || |
| 2716 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | return; |
| 2718 | } |
| 2719 | |
| 2720 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2721 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2722 | } |
| 2723 | |
| 2724 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2725 | const sp<Connection>& connection, |
| 2726 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | #if DEBUG_DISPATCH_CYCLE |
| 2728 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2729 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2730 | #endif |
| 2731 | |
| 2732 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2733 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2734 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2735 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2736 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2737 | |
| 2738 | // The connection appears to be unrecoverably broken. |
| 2739 | // Ignore already broken or zombie connections. |
| 2740 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2741 | connection->status = Connection::STATUS_BROKEN; |
| 2742 | |
| 2743 | if (notify) { |
| 2744 | // Notify other system components. |
| 2745 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2750 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2751 | while (!queue.empty()) { |
| 2752 | DispatchEntry* dispatchEntry = queue.front(); |
| 2753 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2754 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2758 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2759 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2760 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2761 | } |
| 2762 | delete dispatchEntry; |
| 2763 | } |
| 2764 | |
| 2765 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2766 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2767 | |
| 2768 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2769 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2770 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2771 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2773 | "fd=%d, events=0x%x", |
| 2774 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2775 | return 0; // remove the callback |
| 2776 | } |
| 2777 | |
| 2778 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2779 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2781 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2782 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2783 | "events=0x%x", |
| 2784 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2785 | return 1; |
| 2786 | } |
| 2787 | |
| 2788 | nsecs_t currentTime = now(); |
| 2789 | bool gotOne = false; |
| 2790 | status_t status; |
| 2791 | for (;;) { |
| 2792 | uint32_t seq; |
| 2793 | bool handled; |
| 2794 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2795 | if (status) { |
| 2796 | break; |
| 2797 | } |
| 2798 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2799 | gotOne = true; |
| 2800 | } |
| 2801 | if (gotOne) { |
| 2802 | d->runCommandsLockedInterruptible(); |
| 2803 | if (status == WOULD_BLOCK) { |
| 2804 | return 1; |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2809 | if (notify) { |
| 2810 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2811 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2812 | } |
| 2813 | } else { |
| 2814 | // Monitor channels are never explicitly unregistered. |
| 2815 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2816 | // about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2817 | const bool stillHaveWindowHandle = |
| 2818 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 2819 | nullptr; |
| 2820 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2821 | if (notify) { |
| 2822 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | "events=0x%x", |
| 2824 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | // Unregister the channel. |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 2829 | d->unregisterInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2830 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2831 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2832 | } |
| 2833 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2834 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2835 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2836 | for (const auto& pair : mConnectionsByFd) { |
| 2837 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2841 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2842 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2843 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 2844 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 2845 | } |
| 2846 | |
| 2847 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2848 | const CancelationOptions& options, |
| 2849 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 2850 | for (const auto& it : monitorsByDisplay) { |
| 2851 | const std::vector<Monitor>& monitors = it.second; |
| 2852 | for (const Monitor& monitor : monitors) { |
| 2853 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2854 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2858 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2859 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2860 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2861 | if (connection == nullptr) { |
| 2862 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2863 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2864 | |
| 2865 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2869 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2870 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2871 | return; |
| 2872 | } |
| 2873 | |
| 2874 | nsecs_t currentTime = now(); |
| 2875 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2876 | std::vector<EventEntry*> cancelationEvents = |
| 2877 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2878 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2879 | if (cancelationEvents.empty()) { |
| 2880 | return; |
| 2881 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2882 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2883 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 2884 | "with reality: %s, mode=%d.", |
| 2885 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 2886 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2887 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2888 | |
| 2889 | InputTarget target; |
| 2890 | sp<InputWindowHandle> windowHandle = |
| 2891 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2892 | if (windowHandle != nullptr) { |
| 2893 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2894 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2895 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2896 | } |
| 2897 | target.inputChannel = connection->inputChannel; |
| 2898 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2899 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2900 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2901 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 2902 | switch (cancelationEventEntry->type) { |
| 2903 | case EventEntry::Type::KEY: { |
| 2904 | logOutboundKeyDetails("cancel - ", |
| 2905 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 2906 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2908 | case EventEntry::Type::MOTION: { |
| 2909 | logOutboundMotionDetails("cancel - ", |
| 2910 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 2911 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2912 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2913 | case EventEntry::Type::FOCUS: { |
| 2914 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 2915 | break; |
| 2916 | } |
| 2917 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2918 | case EventEntry::Type::DEVICE_RESET: { |
| 2919 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2920 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 2921 | break; |
| 2922 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2923 | } |
| 2924 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2925 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2926 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2927 | |
| 2928 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2929 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2930 | |
| 2931 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2932 | } |
| 2933 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2934 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 2935 | const sp<Connection>& connection) { |
| 2936 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | nsecs_t currentTime = now(); |
| 2941 | |
| 2942 | std::vector<EventEntry*> downEvents = |
| 2943 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 2944 | |
| 2945 | if (downEvents.empty()) { |
| 2946 | return; |
| 2947 | } |
| 2948 | |
| 2949 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2950 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 2951 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 2952 | #endif |
| 2953 | |
| 2954 | InputTarget target; |
| 2955 | sp<InputWindowHandle> windowHandle = |
| 2956 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2957 | if (windowHandle != nullptr) { |
| 2958 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2959 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2960 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2961 | } |
| 2962 | target.inputChannel = connection->inputChannel; |
| 2963 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2964 | |
| 2965 | for (EventEntry* downEventEntry : downEvents) { |
| 2966 | switch (downEventEntry->type) { |
| 2967 | case EventEntry::Type::MOTION: { |
| 2968 | logOutboundMotionDetails("down - ", |
| 2969 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 2970 | break; |
| 2971 | } |
| 2972 | |
| 2973 | case EventEntry::Type::KEY: |
| 2974 | case EventEntry::Type::FOCUS: |
| 2975 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2976 | case EventEntry::Type::DEVICE_RESET: { |
| 2977 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2978 | EventEntry::typeToString(downEventEntry->type)); |
| 2979 | break; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 2984 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2985 | |
| 2986 | downEventEntry->release(); |
| 2987 | } |
| 2988 | |
| 2989 | startDispatchCycleLocked(currentTime, connection); |
| 2990 | } |
| 2991 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2992 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2993 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2994 | ALOG_ASSERT(pointerIds.value != 0); |
| 2995 | |
| 2996 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 2997 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 2998 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 2999 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3000 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3001 | uint32_t splitPointerCount = 0; |
| 3002 | |
| 3003 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3004 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3005 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3006 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3007 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3008 | if (pointerIds.hasBit(pointerId)) { |
| 3009 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3010 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3011 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3012 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3013 | splitPointerCount += 1; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | if (splitPointerCount != pointerIds.count()) { |
| 3018 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3019 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3020 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3021 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3022 | // in this way. |
| 3023 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3024 | "we expected there to be %d pointers. This probably means we received " |
| 3025 | "a broken sequence of pointer ids from the input device.", |
| 3026 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3027 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3028 | } |
| 3029 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3030 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3031 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3032 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3033 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3034 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3035 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3036 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3037 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3038 | if (pointerIds.hasBit(pointerId)) { |
| 3039 | if (pointerIds.count() == 1) { |
| 3040 | // The first/last pointer went down/up. |
| 3041 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3042 | ? AMOTION_EVENT_ACTION_DOWN |
| 3043 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3044 | } else { |
| 3045 | // A secondary pointer went down/up. |
| 3046 | uint32_t splitPointerIndex = 0; |
| 3047 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3048 | splitPointerIndex += 1; |
| 3049 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3050 | action = maskedAction | |
| 3051 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3052 | } |
| 3053 | } else { |
| 3054 | // An unrelated pointer changed. |
| 3055 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3056 | } |
| 3057 | } |
| 3058 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3059 | int32_t newId = mIdGenerator.nextId(); |
| 3060 | if (ATRACE_ENABLED()) { |
| 3061 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3062 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3063 | originalMotionEntry.id, newId); |
| 3064 | ATRACE_NAME(message.c_str()); |
| 3065 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3066 | MotionEntry* splitMotionEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3067 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 3068 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 3069 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3070 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 3071 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 3072 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 3073 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 3074 | originalMotionEntry.xCursorPosition, |
| 3075 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3076 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3077 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3078 | if (originalMotionEntry.injectionState) { |
| 3079 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3080 | splitMotionEntry->injectionState->refCount += 1; |
| 3081 | } |
| 3082 | |
| 3083 | return splitMotionEntry; |
| 3084 | } |
| 3085 | |
| 3086 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3087 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3088 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3089 | #endif |
| 3090 | |
| 3091 | bool needWake; |
| 3092 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3093 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3094 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3095 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3096 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3097 | needWake = enqueueInboundEventLocked(newEntry); |
| 3098 | } // release lock |
| 3099 | |
| 3100 | if (needWake) { |
| 3101 | mLooper->wake(); |
| 3102 | } |
| 3103 | } |
| 3104 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3105 | /** |
| 3106 | * If one of the meta shortcuts is detected, process them here: |
| 3107 | * Meta + Backspace -> generate BACK |
| 3108 | * Meta + Enter -> generate HOME |
| 3109 | * This will potentially overwrite keyCode and metaState. |
| 3110 | */ |
| 3111 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3112 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3113 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3114 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3115 | if (keyCode == AKEYCODE_DEL) { |
| 3116 | newKeyCode = AKEYCODE_BACK; |
| 3117 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3118 | newKeyCode = AKEYCODE_HOME; |
| 3119 | } |
| 3120 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3121 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3122 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3123 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3124 | keyCode = newKeyCode; |
| 3125 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3126 | } |
| 3127 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3128 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3129 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3130 | // 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] | 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 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3134 | if (replacementIt != mReplacedKeys.end()) { |
| 3135 | keyCode = replacementIt->second; |
| 3136 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3137 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3142 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3143 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3144 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3145 | "policyFlags=0x%x, action=0x%x, " |
| 3146 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3147 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3148 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3149 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3150 | #endif |
| 3151 | if (!validateKeyEvent(args->action)) { |
| 3152 | return; |
| 3153 | } |
| 3154 | |
| 3155 | uint32_t policyFlags = args->policyFlags; |
| 3156 | int32_t flags = args->flags; |
| 3157 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3158 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3159 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3160 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3161 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3162 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3163 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3164 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3165 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3166 | metaState |= AMETA_FUNCTION_ON; |
| 3167 | } |
| 3168 | |
| 3169 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3170 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3171 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3172 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3173 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3174 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3175 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3176 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3177 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3178 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3179 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3180 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3181 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3182 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3183 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3184 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3185 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3186 | bool needWake; |
| 3187 | { // acquire lock |
| 3188 | mLock.lock(); |
| 3189 | |
| 3190 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3191 | mLock.unlock(); |
| 3192 | |
| 3193 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3194 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3195 | return; // event was consumed by the filter |
| 3196 | } |
| 3197 | |
| 3198 | mLock.lock(); |
| 3199 | } |
| 3200 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3201 | KeyEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3202 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3203 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3204 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3205 | |
| 3206 | needWake = enqueueInboundEventLocked(newEntry); |
| 3207 | mLock.unlock(); |
| 3208 | } // release lock |
| 3209 | |
| 3210 | if (needWake) { |
| 3211 | mLooper->wake(); |
| 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3216 | return mInputFilterEnabled; |
| 3217 | } |
| 3218 | |
| 3219 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3220 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3221 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3222 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3223 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3224 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3225 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3226 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3227 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3228 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3229 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3230 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3231 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3232 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3233 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3234 | "orientation=%f", |
| 3235 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3236 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3237 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3238 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3239 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3240 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3241 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3242 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3243 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3244 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3245 | } |
| 3246 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3247 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3248 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3249 | return; |
| 3250 | } |
| 3251 | |
| 3252 | uint32_t policyFlags = args->policyFlags; |
| 3253 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3254 | |
| 3255 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3256 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3257 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3258 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3259 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3260 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3261 | |
| 3262 | bool needWake; |
| 3263 | { // acquire lock |
| 3264 | mLock.lock(); |
| 3265 | |
| 3266 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3267 | mLock.unlock(); |
| 3268 | |
| 3269 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3270 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3271 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3272 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3273 | args->metaState, args->buttonState, args->classification, transform, |
| 3274 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
| 3275 | args->yCursorPosition, args->downTime, args->eventTime, |
| 3276 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3277 | |
| 3278 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3279 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3280 | return; // event was consumed by the filter |
| 3281 | } |
| 3282 | |
| 3283 | mLock.lock(); |
| 3284 | } |
| 3285 | |
| 3286 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3287 | MotionEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3288 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3289 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3290 | args->flags, args->metaState, args->buttonState, |
| 3291 | args->classification, args->edgeFlags, args->xPrecision, |
| 3292 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3293 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3294 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3295 | |
| 3296 | needWake = enqueueInboundEventLocked(newEntry); |
| 3297 | mLock.unlock(); |
| 3298 | } // release lock |
| 3299 | |
| 3300 | if (needWake) { |
| 3301 | mLooper->wake(); |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3306 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3310 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3311 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3312 | "switchMask=0x%08x", |
| 3313 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3314 | #endif |
| 3315 | |
| 3316 | uint32_t policyFlags = args->policyFlags; |
| 3317 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3318 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3322 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3323 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3324 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3325 | #endif |
| 3326 | |
| 3327 | bool needWake; |
| 3328 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3329 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3330 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3331 | DeviceResetEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3332 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3333 | needWake = enqueueInboundEventLocked(newEntry); |
| 3334 | } // release lock |
| 3335 | |
| 3336 | if (needWake) { |
| 3337 | mLooper->wake(); |
| 3338 | } |
| 3339 | } |
| 3340 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3341 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 3342 | int32_t injectorUid, int32_t syncMode, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3343 | std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3344 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3345 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3346 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3347 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3348 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3349 | 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] | 3350 | |
| 3351 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3352 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3353 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3354 | } |
| 3355 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3356 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3357 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3358 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3359 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3360 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3361 | if (!validateKeyEvent(action)) { |
| 3362 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3364 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3365 | int32_t flags = incomingKey.getFlags(); |
| 3366 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3367 | int32_t metaState = incomingKey.getMetaState(); |
| 3368 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3369 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3370 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3371 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3372 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3373 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3374 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3375 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3376 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3377 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3378 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3379 | |
| 3380 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3381 | android::base::Timer t; |
| 3382 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3383 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3384 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3385 | std::to_string(t.duration().count()).c_str()); |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | mLock.lock(); |
| 3390 | KeyEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3391 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3392 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3393 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, |
| 3394 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3395 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3396 | injectedEntries.push(injectedEntry); |
| 3397 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3400 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3401 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3402 | int32_t action = motionEvent->getAction(); |
| 3403 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3404 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3405 | int32_t actionButton = motionEvent->getActionButton(); |
| 3406 | int32_t displayId = motionEvent->getDisplayId(); |
| 3407 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
| 3408 | return INPUT_EVENT_INJECTION_FAILED; |
| 3409 | } |
| 3410 | |
| 3411 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3412 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3413 | android::base::Timer t; |
| 3414 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3415 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3416 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3417 | std::to_string(t.duration().count()).c_str()); |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | mLock.lock(); |
| 3422 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3423 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3424 | MotionEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3425 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3426 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3427 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3428 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3429 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3430 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3431 | motionEvent->getRawXCursorPosition(), |
| 3432 | motionEvent->getRawYCursorPosition(), |
| 3433 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3434 | pointerProperties, samplePointerCoords, |
| 3435 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3436 | injectedEntries.push(injectedEntry); |
| 3437 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3438 | sampleEventTimes += 1; |
| 3439 | samplePointerCoords += pointerCount; |
| 3440 | MotionEntry* nextInjectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3441 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3442 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3443 | motionEvent->getDisplayId(), policyFlags, action, |
| 3444 | actionButton, motionEvent->getFlags(), |
| 3445 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3446 | motionEvent->getClassification(), |
| 3447 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3448 | motionEvent->getYPrecision(), |
| 3449 | motionEvent->getRawXCursorPosition(), |
| 3450 | motionEvent->getRawYCursorPosition(), |
| 3451 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3452 | pointerProperties, samplePointerCoords, |
| 3453 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3454 | injectedEntries.push(nextInjectedEntry); |
| 3455 | } |
| 3456 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3458 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3459 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3460 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3461 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 3465 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3466 | injectionState->injectionIsAsync = true; |
| 3467 | } |
| 3468 | |
| 3469 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3470 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3471 | |
| 3472 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3473 | while (!injectedEntries.empty()) { |
| 3474 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3475 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | mLock.unlock(); |
| 3479 | |
| 3480 | if (needWake) { |
| 3481 | mLooper->wake(); |
| 3482 | } |
| 3483 | |
| 3484 | int32_t injectionResult; |
| 3485 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3486 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3487 | |
| 3488 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3489 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3490 | } else { |
| 3491 | for (;;) { |
| 3492 | injectionResult = injectionState->injectionResult; |
| 3493 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3494 | break; |
| 3495 | } |
| 3496 | |
| 3497 | nsecs_t remainingTimeout = endTime - now(); |
| 3498 | if (remainingTimeout <= 0) { |
| 3499 | #if DEBUG_INJECTION |
| 3500 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3501 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | #endif |
| 3503 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3504 | break; |
| 3505 | } |
| 3506 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3507 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | } |
| 3509 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3510 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && |
| 3511 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3513 | #if DEBUG_INJECTION |
| 3514 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3515 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3516 | #endif |
| 3517 | nsecs_t remainingTimeout = endTime - now(); |
| 3518 | if (remainingTimeout <= 0) { |
| 3519 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3520 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3521 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | #endif |
| 3523 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3524 | break; |
| 3525 | } |
| 3526 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3527 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | injectionState->release(); |
| 3533 | } // release lock |
| 3534 | |
| 3535 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3536 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3537 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3538 | #endif |
| 3539 | |
| 3540 | return injectionResult; |
| 3541 | } |
| 3542 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3543 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3544 | std::array<uint8_t, 32> calculatedHmac; |
| 3545 | std::unique_ptr<VerifiedInputEvent> result; |
| 3546 | switch (event.getType()) { |
| 3547 | case AINPUT_EVENT_TYPE_KEY: { |
| 3548 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3549 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3550 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3551 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3552 | break; |
| 3553 | } |
| 3554 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3555 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3556 | VerifiedMotionEvent verifiedMotionEvent = |
| 3557 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3558 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3559 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3560 | break; |
| 3561 | } |
| 3562 | default: { |
| 3563 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3564 | return nullptr; |
| 3565 | } |
| 3566 | } |
| 3567 | if (calculatedHmac == INVALID_HMAC) { |
| 3568 | return nullptr; |
| 3569 | } |
| 3570 | if (calculatedHmac != event.getHmac()) { |
| 3571 | return nullptr; |
| 3572 | } |
| 3573 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3574 | } |
| 3575 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3576 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3577 | return injectorUid == 0 || |
| 3578 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3579 | } |
| 3580 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3581 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3582 | InjectionState* injectionState = entry->injectionState; |
| 3583 | if (injectionState) { |
| 3584 | #if DEBUG_INJECTION |
| 3585 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3586 | "injectorPid=%d, injectorUid=%d", |
| 3587 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3588 | #endif |
| 3589 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3590 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | // Log the outcome since the injector did not wait for the injection result. |
| 3592 | switch (injectionResult) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3593 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3594 | ALOGV("Asynchronous input event injection succeeded."); |
| 3595 | break; |
| 3596 | case INPUT_EVENT_INJECTION_FAILED: |
| 3597 | ALOGW("Asynchronous input event injection failed."); |
| 3598 | break; |
| 3599 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3600 | ALOGW("Asynchronous input event injection permission denied."); |
| 3601 | break; |
| 3602 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3603 | ALOGW("Asynchronous input event injection timed out."); |
| 3604 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3609 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3613 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3614 | InjectionState* injectionState = entry->injectionState; |
| 3615 | if (injectionState) { |
| 3616 | injectionState->pendingForegroundDispatches += 1; |
| 3617 | } |
| 3618 | } |
| 3619 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3620 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3621 | InjectionState* injectionState = entry->injectionState; |
| 3622 | if (injectionState) { |
| 3623 | injectionState->pendingForegroundDispatches -= 1; |
| 3624 | |
| 3625 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3626 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3627 | } |
| 3628 | } |
| 3629 | } |
| 3630 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3631 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3632 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3633 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 3634 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 3635 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3636 | } |
| 3637 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3639 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3640 | if (windowHandleToken == nullptr) { |
| 3641 | return nullptr; |
| 3642 | } |
| 3643 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3644 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3645 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3646 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3647 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3648 | return windowHandle; |
| 3649 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3650 | } |
| 3651 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3652 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3653 | } |
| 3654 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3655 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 3656 | int displayId) const { |
| 3657 | if (windowHandleToken == nullptr) { |
| 3658 | return nullptr; |
| 3659 | } |
| 3660 | |
| 3661 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3662 | if (windowHandle->getToken() == windowHandleToken) { |
| 3663 | return windowHandle; |
| 3664 | } |
| 3665 | } |
| 3666 | return nullptr; |
| 3667 | } |
| 3668 | |
| 3669 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 3670 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3671 | return getWindowHandleLocked(focusedToken, displayId); |
| 3672 | } |
| 3673 | |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3674 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 3675 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3676 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3677 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3678 | if (handle->getId() == windowHandle->getId() && |
| 3679 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3680 | if (windowHandle->getInfo()->displayId != it.first) { |
| 3681 | ALOGE("Found window %s in display %" PRId32 |
| 3682 | ", but it should belong to display %" PRId32, |
| 3683 | windowHandle->getName().c_str(), it.first, |
| 3684 | windowHandle->getInfo()->displayId); |
| 3685 | } |
| 3686 | return true; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3687 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3688 | } |
| 3689 | } |
| 3690 | return false; |
| 3691 | } |
| 3692 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3693 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 3694 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 3695 | const bool noInputChannel = |
| 3696 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3697 | if (connection != nullptr && noInputChannel) { |
| 3698 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 3699 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 3700 | return false; |
| 3701 | } |
| 3702 | |
| 3703 | if (connection == nullptr) { |
| 3704 | if (!noInputChannel) { |
| 3705 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 3706 | } |
| 3707 | return false; |
| 3708 | } |
| 3709 | if (!connection->responsive) { |
| 3710 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 3711 | return false; |
| 3712 | } |
| 3713 | return true; |
| 3714 | } |
| 3715 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3716 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 3717 | const sp<IBinder>& token) const { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3718 | size_t count = mInputChannelsByToken.count(token); |
| 3719 | if (count == 0) { |
| 3720 | return nullptr; |
| 3721 | } |
| 3722 | return mInputChannelsByToken.at(token); |
| 3723 | } |
| 3724 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3725 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3726 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3727 | if (inputWindowHandles.empty()) { |
| 3728 | // Remove all handles on a display if there are no windows left. |
| 3729 | mWindowHandlesByDisplay.erase(displayId); |
| 3730 | return; |
| 3731 | } |
| 3732 | |
| 3733 | // Since we compare the pointer of input window handles across window updates, we need |
| 3734 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3735 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3736 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3737 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3738 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3742 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3743 | if (!handle->updateInfo()) { |
| 3744 | // handle no longer valid |
| 3745 | continue; |
| 3746 | } |
| 3747 | |
| 3748 | const InputWindowInfo* info = handle->getInfo(); |
| 3749 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3750 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3751 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3752 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3753 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 3754 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3755 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3756 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3757 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3758 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3759 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | if (info->displayId != displayId) { |
| 3763 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3764 | handle->getName().c_str(), displayId, info->displayId); |
| 3765 | continue; |
| 3766 | } |
| 3767 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3768 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 3769 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3770 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3771 | oldHandle->updateFrom(handle); |
| 3772 | newHandles.push_back(oldHandle); |
| 3773 | } else { |
| 3774 | newHandles.push_back(handle); |
| 3775 | } |
| 3776 | } |
| 3777 | |
| 3778 | // Insert or replace |
| 3779 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3780 | } |
| 3781 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3782 | void InputDispatcher::setInputWindows( |
| 3783 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3784 | { // acquire lock |
| 3785 | std::scoped_lock _l(mLock); |
| 3786 | for (auto const& i : handlesPerDisplay) { |
| 3787 | setInputWindowsLocked(i.second, i.first); |
| 3788 | } |
| 3789 | } |
| 3790 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3791 | mLooper->wake(); |
| 3792 | } |
| 3793 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3794 | /** |
| 3795 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3796 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3797 | * If set an empty list, remove all handles from the specific display. |
| 3798 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3799 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3800 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3801 | void InputDispatcher::setInputWindowsLocked( |
| 3802 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3803 | if (DEBUG_FOCUS) { |
| 3804 | std::string windowList; |
| 3805 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3806 | windowList += iwh->getName() + " "; |
| 3807 | } |
| 3808 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3809 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3810 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3811 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 3812 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 3813 | const bool noInputWindow = |
| 3814 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3815 | if (noInputWindow && window->getToken() != nullptr) { |
| 3816 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 3817 | window->getName().c_str()); |
| 3818 | window->releaseChannel(); |
| 3819 | } |
| 3820 | } |
| 3821 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3822 | // Copy old handles for release if they are no longer present. |
| 3823 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3825 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3826 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3827 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3828 | if (mLastHoverWindowHandle && |
| 3829 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 3830 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3831 | mLastHoverWindowHandle = nullptr; |
| 3832 | } |
| 3833 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3834 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3835 | if (focusedToken) { |
| 3836 | FocusResult result = checkTokenFocusableLocked(focusedToken, displayId); |
| 3837 | if (result != FocusResult::OK) { |
| 3838 | onFocusChangedLocked(focusedToken, nullptr, displayId, typeToString(result)); |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | std::optional<FocusRequest> focusRequest = |
| 3843 | getOptionalValueByKey(mPendingFocusRequests, displayId); |
| 3844 | if (focusRequest) { |
| 3845 | // If the window from the pending request is now visible, provide it focus. |
| 3846 | FocusResult result = handleFocusRequestLocked(*focusRequest); |
| 3847 | if (result != FocusResult::NOT_VISIBLE) { |
| 3848 | // Drop the request if we were able to change the focus or we cannot change |
| 3849 | // it for another reason. |
| 3850 | mPendingFocusRequests.erase(displayId); |
| 3851 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3852 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3853 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3854 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 3855 | mTouchStatesByDisplay.find(displayId); |
| 3856 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 3857 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3858 | for (size_t i = 0; i < state.windows.size();) { |
| 3859 | TouchedWindow& touchedWindow = state.windows[i]; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3860 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3861 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3862 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 3863 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3864 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3865 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3866 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 3867 | if (touchedInputChannel != nullptr) { |
| 3868 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3869 | "touched window was removed"); |
| 3870 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3871 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3872 | state.windows.erase(state.windows.begin() + i); |
| 3873 | } else { |
| 3874 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | } |
| 3876 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3877 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3878 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3879 | // Release information for windows that are no longer present. |
| 3880 | // This ensures that unused input channels are released promptly. |
| 3881 | // Otherwise, they might stick around until the window handle is destroyed |
| 3882 | // which might not happen until the next GC. |
| 3883 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3884 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3885 | if (DEBUG_FOCUS) { |
| 3886 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3887 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3888 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3889 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3890 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3894 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3895 | if (DEBUG_FOCUS) { |
| 3896 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 3897 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 3898 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3899 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3900 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3902 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3903 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3904 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3905 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 3906 | return; // This application is already focused. No need to wake up or change anything. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3907 | } |
| 3908 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3909 | // Set the new application handle. |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3910 | if (inputApplicationHandle != nullptr) { |
| 3911 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 3912 | } else { |
| 3913 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 3914 | } |
| 3915 | |
| 3916 | // No matter what the old focused application was, stop waiting on it because it is |
| 3917 | // no longer focused. |
| 3918 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3919 | } // release lock |
| 3920 | |
| 3921 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3922 | mLooper->wake(); |
| 3923 | } |
| 3924 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3925 | /** |
| 3926 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3927 | * the display not specified. |
| 3928 | * |
| 3929 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3930 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3931 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3932 | * display. The display-specified events won't be affected. |
| 3933 | */ |
| 3934 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3935 | if (DEBUG_FOCUS) { |
| 3936 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3937 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3938 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3939 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3940 | |
| 3941 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3942 | sp<IBinder> oldFocusedWindowToken = |
| 3943 | getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); |
| 3944 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3945 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3946 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3947 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3948 | CancelationOptions |
| 3949 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3950 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3951 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3952 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3953 | } |
| 3954 | } |
| 3955 | mFocusedDisplayId = displayId; |
| 3956 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 3957 | // Find new focused window and validate |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3958 | sp<IBinder> newFocusedWindowToken = |
| 3959 | getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3960 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3961 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3962 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3963 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3964 | if (!mFocusedWindowTokenByDisplay.empty()) { |
| 3965 | ALOGE("But another display has a focused window\n%s", |
| 3966 | dumpFocusedWindowsLocked().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3967 | } |
| 3968 | } |
| 3969 | } |
| 3970 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3971 | if (DEBUG_FOCUS) { |
| 3972 | logDispatchStateLocked(); |
| 3973 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3974 | } // release lock |
| 3975 | |
| 3976 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3977 | mLooper->wake(); |
| 3978 | } |
| 3979 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3980 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3981 | if (DEBUG_FOCUS) { |
| 3982 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3983 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 | |
| 3985 | bool changed; |
| 3986 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3987 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | |
| 3989 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 3990 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3991 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | if (mDispatchEnabled && !enabled) { |
| 3995 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3996 | } |
| 3997 | |
| 3998 | mDispatchEnabled = enabled; |
| 3999 | mDispatchFrozen = frozen; |
| 4000 | changed = true; |
| 4001 | } else { |
| 4002 | changed = false; |
| 4003 | } |
| 4004 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4005 | if (DEBUG_FOCUS) { |
| 4006 | logDispatchStateLocked(); |
| 4007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4008 | } // release lock |
| 4009 | |
| 4010 | if (changed) { |
| 4011 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4012 | mLooper->wake(); |
| 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4017 | if (DEBUG_FOCUS) { |
| 4018 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4019 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4020 | |
| 4021 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4022 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4023 | |
| 4024 | if (mInputFilterEnabled == enabled) { |
| 4025 | return; |
| 4026 | } |
| 4027 | |
| 4028 | mInputFilterEnabled = enabled; |
| 4029 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4030 | } // release lock |
| 4031 | |
| 4032 | // Wake up poll loop since there might be work to do to drop everything. |
| 4033 | mLooper->wake(); |
| 4034 | } |
| 4035 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4036 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4037 | std::scoped_lock lock(mLock); |
| 4038 | mInTouchMode = inTouchMode; |
| 4039 | } |
| 4040 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4041 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 4042 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4043 | if (DEBUG_FOCUS) { |
| 4044 | ALOGD("Trivial transfer to same window."); |
| 4045 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4046 | return true; |
| 4047 | } |
| 4048 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4049 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4050 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4051 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4052 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4053 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4054 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4055 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4056 | return false; |
| 4057 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4058 | if (DEBUG_FOCUS) { |
| 4059 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4060 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4061 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4062 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4063 | if (DEBUG_FOCUS) { |
| 4064 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4065 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 | return false; |
| 4067 | } |
| 4068 | |
| 4069 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4070 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4071 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4072 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4073 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4074 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4075 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4076 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4077 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4078 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4079 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4080 | int32_t newTargetFlags = oldTargetFlags & |
| 4081 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4082 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4083 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4084 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4085 | found = true; |
| 4086 | goto Found; |
| 4087 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4088 | } |
| 4089 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4090 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4091 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4092 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4093 | if (DEBUG_FOCUS) { |
| 4094 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4095 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4096 | return false; |
| 4097 | } |
| 4098 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4099 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4100 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4101 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4102 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4103 | CancelationOptions |
| 4104 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4105 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4107 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4108 | } |
| 4109 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4110 | if (DEBUG_FOCUS) { |
| 4111 | logDispatchStateLocked(); |
| 4112 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | } // release lock |
| 4114 | |
| 4115 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4116 | mLooper->wake(); |
| 4117 | return true; |
| 4118 | } |
| 4119 | |
| 4120 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4121 | if (DEBUG_FOCUS) { |
| 4122 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4123 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4124 | |
| 4125 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4126 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4127 | |
| 4128 | resetKeyRepeatLocked(); |
| 4129 | releasePendingEventLocked(); |
| 4130 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4131 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4133 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4134 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4136 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4140 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4141 | dumpDispatchStateLocked(dump); |
| 4142 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4143 | std::istringstream stream(dump); |
| 4144 | std::string line; |
| 4145 | |
| 4146 | while (std::getline(stream, line, '\n')) { |
| 4147 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4151 | std::string InputDispatcher::dumpFocusedWindowsLocked() { |
| 4152 | if (mFocusedWindowTokenByDisplay.empty()) { |
| 4153 | return INDENT "FocusedWindows: <none>\n"; |
| 4154 | } |
| 4155 | |
| 4156 | std::string dump; |
| 4157 | dump += INDENT "FocusedWindows:\n"; |
| 4158 | for (auto& it : mFocusedWindowTokenByDisplay) { |
| 4159 | const int32_t displayId = it.first; |
| 4160 | const sp<InputWindowHandle> windowHandle = getFocusedWindowHandleLocked(displayId); |
| 4161 | if (windowHandle) { |
| 4162 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4163 | windowHandle->getName().c_str()); |
| 4164 | } else { |
| 4165 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
| 4166 | " has focused token without a window'\n", |
| 4167 | displayId); |
| 4168 | } |
| 4169 | } |
| 4170 | return dump; |
| 4171 | } |
| 4172 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4173 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4174 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4175 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4176 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4177 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4178 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4179 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4180 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4181 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4182 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4183 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4184 | const std::chrono::duration timeout = |
| 4185 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4187 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4188 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4189 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4190 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4191 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4192 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4193 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4194 | dump += dumpFocusedWindowsLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4196 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4197 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4198 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4199 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4200 | 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] | 4201 | state.displayId, toString(state.down), toString(state.split), |
| 4202 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4203 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4204 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4205 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4206 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4207 | dump += StringPrintf(INDENT4 |
| 4208 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4209 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4210 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4211 | } |
| 4212 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4213 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4214 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4215 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4216 | dump += INDENT3 "Portal windows:\n"; |
| 4217 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4218 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4219 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4220 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4221 | } |
| 4222 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4223 | } |
| 4224 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4225 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4226 | } |
| 4227 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4228 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4229 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4230 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4231 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4232 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4233 | dump += INDENT2 "Windows:\n"; |
| 4234 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4235 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4236 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4237 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4238 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4239 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
| 4240 | "hasWallpaper=%s, visible=%s, " |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4241 | "flags=%s, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4242 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4243 | "applicationInfo=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4244 | "touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4245 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4246 | windowInfo->portalToDisplayId, |
| 4247 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4248 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4249 | toString(windowInfo->hasWallpaper), |
| 4250 | toString(windowInfo->visible), |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4251 | windowInfo->flags.string().c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4252 | static_cast<int32_t>(windowInfo->type), |
| 4253 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4254 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4255 | windowInfo->globalScaleFactor, |
| 4256 | windowInfo->applicationInfo.name.c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4257 | dumpRegion(dump, windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4258 | dump += StringPrintf(", inputFeatures=%s", |
| 4259 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4260 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
| 4261 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4262 | windowInfo->ownerPid, windowInfo->ownerUid, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4263 | millis(windowInfo->dispatchingTimeout)); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4264 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4265 | } |
| 4266 | } else { |
| 4267 | dump += INDENT2 "Windows: <none>\n"; |
| 4268 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | } |
| 4270 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4271 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4272 | } |
| 4273 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4274 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4275 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4276 | const std::vector<Monitor>& monitors = it.second; |
| 4277 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4278 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4279 | } |
| 4280 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4281 | const std::vector<Monitor>& monitors = it.second; |
| 4282 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4283 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4284 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4285 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4286 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | nsecs_t currentTime = now(); |
| 4290 | |
| 4291 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4292 | if (!mRecentQueue.empty()) { |
| 4293 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4294 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4295 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4296 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4297 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4298 | } |
| 4299 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4300 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | // Dump event currently being dispatched. |
| 4304 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4305 | dump += INDENT "PendingEvent:\n"; |
| 4306 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4308 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4309 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4311 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4315 | if (!mInboundQueue.empty()) { |
| 4316 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4317 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4318 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4319 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4320 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | } |
| 4322 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4323 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4324 | } |
| 4325 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4326 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4327 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4328 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4329 | const KeyReplacement& replacement = pair.first; |
| 4330 | int32_t newKeyCode = pair.second; |
| 4331 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4332 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4333 | } |
| 4334 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4335 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4336 | } |
| 4337 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4338 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4339 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4340 | for (const auto& pair : mConnectionsByFd) { |
| 4341 | const sp<Connection>& connection = pair.second; |
| 4342 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4343 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4344 | pair.first, connection->getInputChannelName().c_str(), |
| 4345 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4346 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4347 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4348 | if (!connection->outboundQueue.empty()) { |
| 4349 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4350 | connection->outboundQueue.size()); |
| 4351 | for (DispatchEntry* entry : connection->outboundQueue) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4352 | dump.append(INDENT4); |
| 4353 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4354 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 |
| 4355 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4356 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4357 | ns2ms(currentTime - entry->eventEntry->eventTime)); |
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 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4361 | } |
| 4362 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4363 | if (!connection->waitQueue.empty()) { |
| 4364 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4365 | connection->waitQueue.size()); |
| 4366 | for (DispatchEntry* entry : connection->waitQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4367 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4369 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4370 | "age=%" PRId64 "ms, wait=%" PRId64 "ms seq=%" PRIu32 "\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4371 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4372 | ns2ms(currentTime - entry->eventEntry->eventTime), |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4373 | ns2ms(currentTime - entry->deliveryTime), entry->seq); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | } |
| 4375 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4376 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4377 | } |
| 4378 | } |
| 4379 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4380 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4384 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 4385 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4386 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4387 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4388 | } |
| 4389 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4390 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4391 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 4392 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 4393 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4394 | } |
| 4395 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4396 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4397 | const size_t numMonitors = monitors.size(); |
| 4398 | for (size_t i = 0; i < numMonitors; i++) { |
| 4399 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4400 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4401 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4402 | dump += "\n"; |
| 4403 | } |
| 4404 | } |
| 4405 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4406 | status_t InputDispatcher::registerInputChannel(const std::shared_ptr<InputChannel>& inputChannel) { |
| 4407 | #if DEBUG_REGISTRATION |
| 4408 | ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4409 | #endif |
| 4410 | |
| 4411 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4412 | std::scoped_lock _l(mLock); |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4413 | sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); |
| 4414 | if (existingConnection != nullptr) { |
| 4415 | ALOGW("Attempted to register already registered input channel '%s'", |
| 4416 | inputChannel->getName().c_str()); |
| 4417 | return BAD_VALUE; |
| 4418 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4419 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4420 | sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); |
| 4421 | |
| 4422 | int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4423 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4424 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4425 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4427 | } // release lock |
| 4428 | |
| 4429 | // Wake the looper because some connections have changed. |
| 4430 | mLooper->wake(); |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4431 | return OK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4432 | } |
| 4433 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4434 | status_t InputDispatcher::registerInputMonitor(const std::shared_ptr<InputChannel>& inputChannel, |
| 4435 | int32_t displayId, bool isGestureMonitor) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4436 | { // acquire lock |
| 4437 | std::scoped_lock _l(mLock); |
| 4438 | |
| 4439 | if (displayId < 0) { |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4440 | ALOGW("Attempted to register input monitor without a specified display."); |
| 4441 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4444 | if (inputChannel->getConnectionToken() == nullptr) { |
| 4445 | ALOGW("Attempted to register input monitor without an identifying token."); |
| 4446 | return BAD_VALUE; |
| 4447 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4448 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4449 | sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); |
| 4450 | |
| 4451 | const int fd = inputChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4452 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4453 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4454 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4455 | auto& monitorsByDisplay = |
| 4456 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4457 | monitorsByDisplay[displayId].emplace_back(inputChannel); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4458 | |
| 4459 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4460 | } |
| 4461 | // Wake the looper because some connections have changed. |
| 4462 | mLooper->wake(); |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4463 | return OK; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Garfield Tan | 400128f | 2020-09-22 21:53:55 +0000 | [diff] [blame] | 4466 | status_t InputDispatcher::unregisterInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4467 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4468 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4469 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4470 | status_t status = unregisterInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4471 | if (status) { |
| 4472 | return status; |
| 4473 | } |
| 4474 | } // release lock |
| 4475 | |
| 4476 | // Wake the poll loop because removing the connection may have changed the current |
| 4477 | // synchronization state. |
| 4478 | mLooper->wake(); |
| 4479 | return OK; |
| 4480 | } |
| 4481 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4482 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<IBinder>& connectionToken, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4483 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4484 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4485 | if (connection == nullptr) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4486 | ALOGW("Attempted to unregister already unregistered input channel"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4487 | return BAD_VALUE; |
| 4488 | } |
| 4489 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4490 | removeConnectionLocked(connection); |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4491 | mInputChannelsByToken.erase(connectionToken); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4492 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4493 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4494 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4497 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4498 | |
| 4499 | nsecs_t currentTime = now(); |
| 4500 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4501 | |
| 4502 | connection->status = Connection::STATUS_ZOMBIE; |
| 4503 | return OK; |
| 4504 | } |
| 4505 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4506 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 4507 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 4508 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4511 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4512 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4513 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4514 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4515 | std::vector<Monitor>& monitors = it->second; |
| 4516 | const size_t numMonitors = monitors.size(); |
| 4517 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4518 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4519 | monitors.erase(monitors.begin() + i); |
| 4520 | break; |
| 4521 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4522 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4523 | if (monitors.empty()) { |
| 4524 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4525 | } else { |
| 4526 | ++it; |
| 4527 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4528 | } |
| 4529 | } |
| 4530 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4531 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4532 | { // acquire lock |
| 4533 | std::scoped_lock _l(mLock); |
| 4534 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4535 | |
| 4536 | if (!foundDisplayId) { |
| 4537 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4538 | return BAD_VALUE; |
| 4539 | } |
| 4540 | int32_t displayId = foundDisplayId.value(); |
| 4541 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4542 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4543 | mTouchStatesByDisplay.find(displayId); |
| 4544 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4545 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4546 | return BAD_VALUE; |
| 4547 | } |
| 4548 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4549 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4550 | std::optional<int32_t> foundDeviceId; |
| 4551 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4552 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4553 | foundDeviceId = state.deviceId; |
| 4554 | } |
| 4555 | } |
| 4556 | if (!foundDeviceId || !state.down) { |
| 4557 | 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] | 4558 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4559 | return BAD_VALUE; |
| 4560 | } |
| 4561 | int32_t deviceId = foundDeviceId.value(); |
| 4562 | |
| 4563 | // Send cancel events to all the input channels we're stealing from. |
| 4564 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4565 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4566 | options.deviceId = deviceId; |
| 4567 | options.displayId = displayId; |
| 4568 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4569 | std::shared_ptr<InputChannel> channel = |
| 4570 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4571 | if (channel != nullptr) { |
| 4572 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4573 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4574 | } |
| 4575 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4576 | state.filterNonMonitors(); |
| 4577 | } |
| 4578 | return OK; |
| 4579 | } |
| 4580 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4581 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4582 | const sp<IBinder>& token) { |
| 4583 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4584 | const std::vector<Monitor>& monitors = it.second; |
| 4585 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4586 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4587 | return it.first; |
| 4588 | } |
| 4589 | } |
| 4590 | } |
| 4591 | return std::nullopt; |
| 4592 | } |
| 4593 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4594 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4595 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4596 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4597 | } |
| 4598 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4599 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4600 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4601 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4602 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4603 | } |
| 4604 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4605 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4606 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4607 | } |
| 4608 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4609 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4610 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4611 | removeByValue(mConnectionsByFd, connection); |
| 4612 | } |
| 4613 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4614 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4615 | const sp<Connection>& connection, uint32_t seq, |
| 4616 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4617 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4618 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4619 | commandEntry->connection = connection; |
| 4620 | commandEntry->eventTime = currentTime; |
| 4621 | commandEntry->seq = seq; |
| 4622 | commandEntry->handled = handled; |
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 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4626 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4627 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4628 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4629 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4630 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4631 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4632 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4633 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4634 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4635 | } |
| 4636 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4637 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 4638 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4639 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4640 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4641 | commandEntry->oldToken = oldToken; |
| 4642 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4643 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4644 | } |
| 4645 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4646 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 4647 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 4648 | // is already healthy again. Don't raise ANR in this situation |
| 4649 | if (connection->waitQueue.empty()) { |
| 4650 | ALOGI("Not raising ANR because the connection %s has recovered", |
| 4651 | connection->inputChannel->getName().c_str()); |
| 4652 | return; |
| 4653 | } |
| 4654 | /** |
| 4655 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 4656 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 4657 | * has changed. This could cause newer entries to time out before the already dispatched |
| 4658 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 4659 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 4660 | * most useful. |
| 4661 | */ |
| 4662 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
| 4663 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 4664 | std::string reason = |
| 4665 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
| 4666 | connection->inputChannel->getName().c_str(), |
| 4667 | ns2ms(currentWait), |
| 4668 | oldestEntry->eventEntry->getDescription().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4669 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4670 | updateLastAnrStateLocked(getWindowHandleLocked(connection->inputChannel->getConnectionToken()), |
| 4671 | reason); |
| 4672 | |
| 4673 | std::unique_ptr<CommandEntry> commandEntry = |
| 4674 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4675 | commandEntry->inputApplicationHandle = nullptr; |
| 4676 | commandEntry->inputChannel = connection->inputChannel; |
| 4677 | commandEntry->reason = std::move(reason); |
| 4678 | postCommandLocked(std::move(commandEntry)); |
| 4679 | } |
| 4680 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4681 | void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4682 | std::string reason = android::base::StringPrintf("%s does not have a focused window", |
| 4683 | application->getName().c_str()); |
| 4684 | |
| 4685 | updateLastAnrStateLocked(application, reason); |
| 4686 | |
| 4687 | std::unique_ptr<CommandEntry> commandEntry = |
| 4688 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4689 | commandEntry->inputApplicationHandle = application; |
| 4690 | commandEntry->inputChannel = nullptr; |
| 4691 | commandEntry->reason = std::move(reason); |
| 4692 | postCommandLocked(std::move(commandEntry)); |
| 4693 | } |
| 4694 | |
| 4695 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 4696 | const std::string& reason) { |
| 4697 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 4698 | updateLastAnrStateLocked(windowLabel, reason); |
| 4699 | } |
| 4700 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4701 | void InputDispatcher::updateLastAnrStateLocked( |
| 4702 | const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4703 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); |
| 4704 | updateLastAnrStateLocked(windowLabel, reason); |
| 4705 | } |
| 4706 | |
| 4707 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 4708 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4709 | // 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] | 4710 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4711 | struct tm tm; |
| 4712 | localtime_r(&t, &tm); |
| 4713 | char timestr[64]; |
| 4714 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4715 | mLastAnrState.clear(); |
| 4716 | mLastAnrState += INDENT "ANR:\n"; |
| 4717 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4718 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 4719 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4720 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4721 | } |
| 4722 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4723 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4724 | mLock.unlock(); |
| 4725 | |
| 4726 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4727 | |
| 4728 | mLock.lock(); |
| 4729 | } |
| 4730 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4731 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4732 | sp<Connection> connection = commandEntry->connection; |
| 4733 | |
| 4734 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4735 | mLock.unlock(); |
| 4736 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4737 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | |
| 4739 | mLock.lock(); |
| 4740 | } |
| 4741 | } |
| 4742 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4743 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4744 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4745 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4746 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4747 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4748 | mLock.lock(); |
| 4749 | } |
| 4750 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4751 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4752 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4753 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4754 | mLock.unlock(); |
| 4755 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4756 | const std::chrono::nanoseconds timeoutExtension = |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4757 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4758 | |
| 4759 | mLock.lock(); |
| 4760 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4761 | if (timeoutExtension > 0s) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4762 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); |
| 4763 | } else { |
| 4764 | // stop waking up for events in this connection, it is already not responding |
| 4765 | sp<Connection> connection = getConnectionLocked(token); |
| 4766 | if (connection == nullptr) { |
| 4767 | return; |
| 4768 | } |
| 4769 | cancelEventsForAnrLocked(connection); |
| 4770 | } |
| 4771 | } |
| 4772 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4773 | void InputDispatcher::extendAnrTimeoutsLocked( |
| 4774 | const std::shared_ptr<InputApplicationHandle>& application, |
| 4775 | const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4776 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 4777 | if (connection == nullptr) { |
| 4778 | if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) { |
| 4779 | // Maybe ANR happened because there's no focused window? |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4780 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4781 | mAwaitedFocusedApplication = application; |
| 4782 | } else { |
| 4783 | // It's also possible that the connection already disappeared. No action necessary. |
| 4784 | } |
| 4785 | return; |
| 4786 | } |
| 4787 | |
| 4788 | 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] | 4789 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4790 | |
| 4791 | connection->responsive = true; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4792 | const nsecs_t newTimeout = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4793 | for (DispatchEntry* entry : connection->waitQueue) { |
| 4794 | if (newTimeout >= entry->timeoutTime) { |
| 4795 | // Already removed old entries when connection was marked unresponsive |
| 4796 | entry->timeoutTime = newTimeout; |
| 4797 | mAnrTracker.insert(entry->timeoutTime, connectionToken); |
| 4798 | } |
| 4799 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 4803 | CommandEntry* commandEntry) { |
| 4804 | KeyEntry* entry = commandEntry->keyEntry; |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4805 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4806 | |
| 4807 | mLock.unlock(); |
| 4808 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4809 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4810 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4811 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4812 | : nullptr; |
| 4813 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4814 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4815 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4816 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4817 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4818 | |
| 4819 | mLock.lock(); |
| 4820 | |
| 4821 | if (delay < 0) { |
| 4822 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 4823 | } else if (!delay) { |
| 4824 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 4825 | } else { |
| 4826 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 4827 | entry->interceptKeyWakeupTime = now() + delay; |
| 4828 | } |
| 4829 | entry->release(); |
| 4830 | } |
| 4831 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4832 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 4833 | mLock.unlock(); |
| 4834 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 4835 | mLock.lock(); |
| 4836 | } |
| 4837 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4838 | /** |
| 4839 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 4840 | * current time. |
| 4841 | */ |
| 4842 | static bool isConnectionResponsive(const Connection& connection) { |
| 4843 | const nsecs_t currentTime = now(); |
| 4844 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 4845 | if (entry->timeoutTime < currentTime) { |
| 4846 | return false; |
| 4847 | } |
| 4848 | } |
| 4849 | return true; |
| 4850 | } |
| 4851 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4852 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4853 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4854 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4855 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4856 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4857 | |
| 4858 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4859 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4860 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4861 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4862 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4863 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4864 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4865 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4866 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 4867 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4868 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4869 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4870 | |
| 4871 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4872 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4873 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 4874 | restartEvent = |
| 4875 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4876 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4877 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 4878 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 4879 | handled); |
| 4880 | } else { |
| 4881 | restartEvent = false; |
| 4882 | } |
| 4883 | |
| 4884 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4885 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4886 | // contents of the wait queue to have been drained, so we need to double-check |
| 4887 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4888 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 4889 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 4890 | dispatchEntry = *dispatchEntryIt; |
| 4891 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4892 | mAnrTracker.erase(dispatchEntry->timeoutTime, |
| 4893 | connection->inputChannel->getConnectionToken()); |
| 4894 | if (!connection->responsive) { |
| 4895 | connection->responsive = isConnectionResponsive(*connection); |
| 4896 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4897 | traceWaitQueueLength(connection); |
| 4898 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4899 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4900 | traceOutboundQueueLength(connection); |
| 4901 | } else { |
| 4902 | releaseDispatchEntry(dispatchEntry); |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | // Start the next dispatch cycle for this connection. |
| 4907 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4911 | DispatchEntry* dispatchEntry, |
| 4912 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4913 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4914 | if (!handled) { |
| 4915 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4916 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4917 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4918 | return false; |
| 4919 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4920 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4921 | // Get the fallback key state. |
| 4922 | // Clear it out after dispatching the UP. |
| 4923 | int32_t originalKeyCode = keyEntry->keyCode; |
| 4924 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 4925 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 4926 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4927 | } |
| 4928 | |
| 4929 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 4930 | // If the application handles the original key for which we previously |
| 4931 | // generated a fallback or if the window is not a foreground window, |
| 4932 | // then cancel the associated fallback key, if any. |
| 4933 | if (fallbackKeyCode != -1) { |
| 4934 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4936 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4937 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4938 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 4939 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4940 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4941 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4942 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4943 | |
| 4944 | mLock.unlock(); |
| 4945 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4946 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4947 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | |
| 4949 | mLock.lock(); |
| 4950 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4951 | // Cancel the fallback key. |
| 4952 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4953 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4954 | "application handled the original non-fallback key " |
| 4955 | "or is no longer a foreground target, " |
| 4956 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4957 | options.keyCode = fallbackKeyCode; |
| 4958 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4960 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4961 | } |
| 4962 | } else { |
| 4963 | // If the application did not handle a non-fallback key, first check |
| 4964 | // that we are in a good state to perform unhandled key event processing |
| 4965 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4966 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4967 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4968 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4969 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4970 | "since this is not an initial down. " |
| 4971 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4972 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4973 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4974 | return false; |
| 4975 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4976 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4977 | // Dispatch the unhandled key to the policy. |
| 4978 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4979 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4980 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4981 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4982 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4983 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4984 | |
| 4985 | mLock.unlock(); |
| 4986 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4987 | bool fallback = |
| 4988 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 4989 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4990 | |
| 4991 | mLock.lock(); |
| 4992 | |
| 4993 | if (connection->status != Connection::STATUS_NORMAL) { |
| 4994 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4995 | return false; |
| 4996 | } |
| 4997 | |
| 4998 | // Latch the fallback keycode for this key on an initial down. |
| 4999 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5000 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5001 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5002 | fallbackKeyCode = event.getKeyCode(); |
| 5003 | } else { |
| 5004 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5005 | } |
| 5006 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5007 | } |
| 5008 | |
| 5009 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5010 | |
| 5011 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5012 | // We will continue to dispatch the key to the policy but we will no |
| 5013 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5014 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5015 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5016 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5017 | if (fallback) { |
| 5018 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5019 | "as a fallback for %d, but on the DOWN it had requested " |
| 5020 | "to send %d instead. Fallback canceled.", |
| 5021 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5022 | } else { |
| 5023 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5024 | "but on the DOWN it had requested to send %d. " |
| 5025 | "Fallback canceled.", |
| 5026 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5027 | } |
| 5028 | #endif |
| 5029 | |
| 5030 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5031 | "canceling fallback, policy no longer desires it"); |
| 5032 | options.keyCode = fallbackKeyCode; |
| 5033 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5034 | |
| 5035 | fallback = false; |
| 5036 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5037 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5038 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5039 | } |
| 5040 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5041 | |
| 5042 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5043 | { |
| 5044 | std::string msg; |
| 5045 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5046 | connection->inputState.getFallbackKeys(); |
| 5047 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5048 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5049 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5050 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5051 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5052 | } |
| 5053 | #endif |
| 5054 | |
| 5055 | if (fallback) { |
| 5056 | // Restart the dispatch cycle using the fallback key. |
| 5057 | keyEntry->eventTime = event.getEventTime(); |
| 5058 | keyEntry->deviceId = event.getDeviceId(); |
| 5059 | keyEntry->source = event.getSource(); |
| 5060 | keyEntry->displayId = event.getDisplayId(); |
| 5061 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5062 | keyEntry->keyCode = fallbackKeyCode; |
| 5063 | keyEntry->scanCode = event.getScanCode(); |
| 5064 | keyEntry->metaState = event.getMetaState(); |
| 5065 | keyEntry->repeatCount = event.getRepeatCount(); |
| 5066 | keyEntry->downTime = event.getDownTime(); |
| 5067 | keyEntry->syntheticRepeat = false; |
| 5068 | |
| 5069 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5070 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5071 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 5072 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5073 | #endif |
| 5074 | return true; // restart the event |
| 5075 | } else { |
| 5076 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5077 | ALOGD("Unhandled key event: No fallback key."); |
| 5078 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5079 | |
| 5080 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5081 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | return false; |
| 5085 | } |
| 5086 | |
| 5087 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5088 | DispatchEntry* dispatchEntry, |
| 5089 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | return false; |
| 5091 | } |
| 5092 | |
| 5093 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5094 | mLock.unlock(); |
| 5095 | |
| 5096 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 5097 | |
| 5098 | mLock.lock(); |
| 5099 | } |
| 5100 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5101 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 5102 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5103 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5104 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 5105 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5106 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5109 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5110 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5111 | // TODO Write some statistics about how long we spend waiting. |
| 5112 | } |
| 5113 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5114 | /** |
| 5115 | * Report the touch event latency to the statsd server. |
| 5116 | * Input events are reported for statistics if: |
| 5117 | * - This is a touchscreen event |
| 5118 | * - InputFilter is not enabled |
| 5119 | * - Event is not injected or synthesized |
| 5120 | * |
| 5121 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5122 | * from getting aggregated with the "old" data. |
| 5123 | */ |
| 5124 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5125 | REQUIRES(mLock) { |
| 5126 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5127 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5128 | if (!reportForStatistics) { |
| 5129 | return; |
| 5130 | } |
| 5131 | |
| 5132 | if (mTouchStatistics.shouldReport()) { |
| 5133 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5134 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5135 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5136 | mTouchStatistics.reset(); |
| 5137 | } |
| 5138 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5139 | mTouchStatistics.addValue(latencyMicros); |
| 5140 | } |
| 5141 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5142 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5143 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5144 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5145 | } |
| 5146 | } |
| 5147 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5148 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5149 | if (ATRACE_ENABLED()) { |
| 5150 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5151 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5152 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5156 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | if (ATRACE_ENABLED()) { |
| 5158 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5159 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5160 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5164 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5165 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5166 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5167 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | dumpDispatchStateLocked(dump); |
| 5169 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5170 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5171 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5172 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | void InputDispatcher::monitor() { |
| 5177 | // 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] | 5178 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5179 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5180 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5181 | } |
| 5182 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5183 | /** |
| 5184 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5185 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5186 | * this method can be safely called from any thread, as long as you've ensured that |
| 5187 | * the work you are interested in completing has already been queued. |
| 5188 | */ |
| 5189 | bool InputDispatcher::waitForIdle() { |
| 5190 | /** |
| 5191 | * Timeout should represent the longest possible time that a device might spend processing |
| 5192 | * events and commands. |
| 5193 | */ |
| 5194 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5195 | std::unique_lock lock(mLock); |
| 5196 | mLooper->wake(); |
| 5197 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5198 | return result == std::cv_status::no_timeout; |
| 5199 | } |
| 5200 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5201 | /** |
| 5202 | * Sets focus to the window identified by the token. This must be called |
| 5203 | * after updating any input window handles. |
| 5204 | * |
| 5205 | * Params: |
| 5206 | * request.token - input channel token used to identify the window that should gain focus. |
| 5207 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 5208 | * specified token does not match the currently focused window, this request will be dropped. |
| 5209 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 5210 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 5211 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 5212 | * when requesting the focus change. This determines which request gets |
| 5213 | * precedence if there is a focus change request from another source such as pointer down. |
| 5214 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5215 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 5216 | { // acquire lock |
| 5217 | std::scoped_lock _l(mLock); |
| 5218 | |
| 5219 | const int32_t displayId = request.displayId; |
| 5220 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 5221 | if (request.focusedToken && oldFocusedToken != request.focusedToken) { |
| 5222 | ALOGD_IF(DEBUG_FOCUS, |
| 5223 | "setFocusedWindow on display %" PRId32 |
| 5224 | " ignored, reason: focusedToken is not focused", |
| 5225 | displayId); |
| 5226 | return; |
| 5227 | } |
| 5228 | |
| 5229 | mPendingFocusRequests.erase(displayId); |
| 5230 | FocusResult result = handleFocusRequestLocked(request); |
| 5231 | if (result == FocusResult::NOT_VISIBLE) { |
| 5232 | // The requested window is not currently visible. Wait for the window to become visible |
| 5233 | // and then provide it focus. This is to handle situations where a user action triggers |
| 5234 | // a new window to appear. We want to be able to queue any key events after the user |
| 5235 | // action and deliver it to the newly focused window. In order for this to happen, we |
| 5236 | // take focus from the currently focused window so key events can be queued. |
| 5237 | ALOGD_IF(DEBUG_FOCUS, |
| 5238 | "setFocusedWindow on display %" PRId32 |
| 5239 | " pending, reason: window is not visible", |
| 5240 | displayId); |
| 5241 | mPendingFocusRequests[displayId] = request; |
| 5242 | onFocusChangedLocked(oldFocusedToken, nullptr, displayId, |
| 5243 | "setFocusedWindow_AwaitingWindowVisibility"); |
| 5244 | } else if (result != FocusResult::OK) { |
| 5245 | ALOGW("setFocusedWindow on display %" PRId32 " ignored, reason:%s", displayId, |
| 5246 | typeToString(result)); |
| 5247 | } |
| 5248 | } // release lock |
| 5249 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5250 | mLooper->wake(); |
| 5251 | } |
| 5252 | |
| 5253 | InputDispatcher::FocusResult InputDispatcher::handleFocusRequestLocked( |
| 5254 | const FocusRequest& request) { |
| 5255 | const int32_t displayId = request.displayId; |
| 5256 | const sp<IBinder> newFocusedToken = request.token; |
| 5257 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 5258 | |
| 5259 | if (oldFocusedToken == request.token) { |
| 5260 | ALOGD_IF(DEBUG_FOCUS, |
| 5261 | "setFocusedWindow on display %" PRId32 " ignored, reason: already focused", |
| 5262 | displayId); |
| 5263 | return FocusResult::OK; |
| 5264 | } |
| 5265 | |
| 5266 | FocusResult result = checkTokenFocusableLocked(newFocusedToken, displayId); |
| 5267 | if (result != FocusResult::OK) { |
| 5268 | return result; |
| 5269 | } |
| 5270 | |
| 5271 | std::string_view reason = |
| 5272 | (request.focusedToken) ? "setFocusedWindow_FocusCheck" : "setFocusedWindow"; |
| 5273 | onFocusChangedLocked(oldFocusedToken, newFocusedToken, displayId, reason); |
| 5274 | return FocusResult::OK; |
| 5275 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5276 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5277 | void InputDispatcher::onFocusChangedLocked(const sp<IBinder>& oldFocusedToken, |
| 5278 | const sp<IBinder>& newFocusedToken, int32_t displayId, |
| 5279 | std::string_view reason) { |
| 5280 | if (oldFocusedToken) { |
| 5281 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(oldFocusedToken); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5282 | if (focusedInputChannel) { |
| 5283 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 5284 | "focus left window"); |
| 5285 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5286 | enqueueFocusEventLocked(oldFocusedToken, false /*hasFocus*/, reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5287 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5288 | mFocusedWindowTokenByDisplay.erase(displayId); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5289 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5290 | if (newFocusedToken) { |
| 5291 | mFocusedWindowTokenByDisplay[displayId] = newFocusedToken; |
| 5292 | enqueueFocusEventLocked(newFocusedToken, true /*hasFocus*/, reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
| 5295 | if (mFocusedDisplayId == displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5296 | notifyFocusChangedLocked(oldFocusedToken, newFocusedToken); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5297 | } |
| 5298 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5299 | |
| 5300 | /** |
| 5301 | * Checks if the window token can be focused on a display. The token can be focused if there is |
| 5302 | * at least one window handle that is visible with the same token and all window handles with the |
| 5303 | * same token are focusable. |
| 5304 | * |
| 5305 | * In the case of mirroring, two windows may share the same window token and their visibility |
| 5306 | * might be different. Example, the mirrored window can cover the window its mirroring. However, |
| 5307 | * we expect the focusability of the windows to match since its hard to reason why one window can |
| 5308 | * receive focus events and the other cannot when both are backed by the same input channel. |
| 5309 | */ |
| 5310 | InputDispatcher::FocusResult InputDispatcher::checkTokenFocusableLocked(const sp<IBinder>& token, |
| 5311 | int32_t displayId) const { |
| 5312 | bool allWindowsAreFocusable = true; |
| 5313 | bool visibleWindowFound = false; |
| 5314 | bool windowFound = false; |
| 5315 | for (const sp<InputWindowHandle>& window : getWindowHandlesLocked(displayId)) { |
| 5316 | if (window->getToken() != token) { |
| 5317 | continue; |
| 5318 | } |
| 5319 | windowFound = true; |
| 5320 | if (window->getInfo()->visible) { |
| 5321 | // Check if at least a single window is visible. |
| 5322 | visibleWindowFound = true; |
| 5323 | } |
| 5324 | if (!window->getInfo()->focusable) { |
| 5325 | // Check if all windows with the window token are focusable. |
| 5326 | allWindowsAreFocusable = false; |
| 5327 | break; |
| 5328 | } |
| 5329 | } |
| 5330 | |
| 5331 | if (!windowFound) { |
| 5332 | return FocusResult::NO_WINDOW; |
| 5333 | } |
| 5334 | if (!allWindowsAreFocusable) { |
| 5335 | return FocusResult::NOT_FOCUSABLE; |
| 5336 | } |
| 5337 | if (!visibleWindowFound) { |
| 5338 | return FocusResult::NOT_VISIBLE; |
| 5339 | } |
| 5340 | |
| 5341 | return FocusResult::OK; |
| 5342 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5343 | } // namespace android::inputdispatcher |