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 | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 31 | // Log debug messages about channel creation |
| 32 | #define DEBUG_CHANNEL_CREATION 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 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 354 | static status_t openInputChannelPair(const std::string& name, |
| 355 | std::shared_ptr<InputChannel>& serverChannel, |
| 356 | std::unique_ptr<InputChannel>& clientChannel) { |
| 357 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 358 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 359 | |
| 360 | serverChannel = std::move(uniqueServerChannel); |
| 361 | return result; |
| 362 | } |
| 363 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 364 | const char* InputDispatcher::typeToString(InputDispatcher::FocusResult result) { |
| 365 | switch (result) { |
| 366 | case InputDispatcher::FocusResult::OK: |
| 367 | return "Ok"; |
| 368 | case InputDispatcher::FocusResult::NO_WINDOW: |
| 369 | return "Window not found"; |
| 370 | case InputDispatcher::FocusResult::NOT_FOCUSABLE: |
| 371 | return "Window not focusable"; |
| 372 | case InputDispatcher::FocusResult::NOT_VISIBLE: |
| 373 | return "Window not visible"; |
| 374 | } |
| 375 | } |
| 376 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 377 | template <typename T> |
| 378 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 379 | if (lhs == nullptr && rhs == nullptr) { |
| 380 | return true; |
| 381 | } |
| 382 | if (lhs == nullptr || rhs == nullptr) { |
| 383 | return false; |
| 384 | } |
| 385 | return *lhs == *rhs; |
| 386 | } |
| 387 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 388 | // --- InputDispatcher --- |
| 389 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 390 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 391 | : mPolicy(policy), |
| 392 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 393 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 394 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 395 | mAppSwitchSawKeyDown(false), |
| 396 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 397 | mNextUnblockedEvent(nullptr), |
| 398 | mDispatchEnabled(false), |
| 399 | mDispatchFrozen(false), |
| 400 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 401 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 402 | // To avoid leaking stack in case that call never comes, and for tests, |
| 403 | // initialize it here anyways. |
| 404 | mInTouchMode(true), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 405 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 406 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 407 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 408 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 409 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 410 | |
| 411 | policy->getDispatcherConfiguration(&mConfig); |
| 412 | } |
| 413 | |
| 414 | InputDispatcher::~InputDispatcher() { |
| 415 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 416 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 417 | |
| 418 | resetKeyRepeatLocked(); |
| 419 | releasePendingEventLocked(); |
| 420 | drainInboundQueueLocked(); |
| 421 | } |
| 422 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 423 | while (!mConnectionsByFd.empty()) { |
| 424 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 425 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 429 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 430 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 431 | return ALREADY_EXISTS; |
| 432 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 433 | mThread = std::make_unique<InputThread>( |
| 434 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 435 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 439 | if (mThread && mThread->isCallingThread()) { |
| 440 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 441 | return INVALID_OPERATION; |
| 442 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 443 | mThread.reset(); |
| 444 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 447 | void InputDispatcher::dispatchOnce() { |
| 448 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 449 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 450 | std::scoped_lock _l(mLock); |
| 451 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 452 | |
| 453 | // Run a dispatch loop if there are no pending commands. |
| 454 | // The dispatch loop might enqueue commands to run afterwards. |
| 455 | if (!haveCommandsLocked()) { |
| 456 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 457 | } |
| 458 | |
| 459 | // Run all pending commands if there are any. |
| 460 | // If any commands were run then force the next poll to wake up immediately. |
| 461 | if (runCommandsLockedInterruptible()) { |
| 462 | nextWakeupTime = LONG_LONG_MIN; |
| 463 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 464 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 465 | // If we are still waiting for ack on some events, |
| 466 | // we might have to wake up earlier to check if an app is anr'ing. |
| 467 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 468 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 469 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 470 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 471 | // pending or queued events |
| 472 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 473 | mDispatcherEnteredIdle.notify_all(); |
| 474 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 475 | } // release lock |
| 476 | |
| 477 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 478 | nsecs_t currentTime = now(); |
| 479 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 480 | mLooper->pollOnce(timeoutMillis); |
| 481 | } |
| 482 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Check if any of the connections' wait queues have events that are too old. |
| 485 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 486 | * Return the time at which we should wake up next. |
| 487 | */ |
| 488 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 489 | const nsecs_t currentTime = now(); |
| 490 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 491 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 492 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 493 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
| 494 | onAnrLocked(mAwaitedFocusedApplication); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 495 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 496 | return LONG_LONG_MIN; |
| 497 | } else { |
| 498 | // Keep waiting |
| 499 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); |
| 500 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); |
| 501 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Check if any connection ANRs are due |
| 506 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 507 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 508 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 509 | } |
| 510 | |
| 511 | // If we reached here, we have an unresponsive connection. |
| 512 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 513 | if (connection == nullptr) { |
| 514 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 515 | return nextAnrCheck; |
| 516 | } |
| 517 | connection->responsive = false; |
| 518 | // Stop waking up for this unresponsive connection |
| 519 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
| 520 | onAnrLocked(connection); |
| 521 | return LONG_LONG_MIN; |
| 522 | } |
| 523 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 524 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 525 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 526 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 527 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 528 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 529 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 533 | nsecs_t currentTime = now(); |
| 534 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 535 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 536 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 537 | // repeat if the device is just coming out of sleep. |
| 538 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | resetKeyRepeatLocked(); |
| 540 | } |
| 541 | |
| 542 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 543 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 544 | if (DEBUG_FOCUS) { |
| 545 | ALOGD("Dispatch frozen. Waiting some more."); |
| 546 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | |
| 550 | // Optimize latency of app switches. |
| 551 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 552 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 553 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 554 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 555 | *nextWakeupTime = mAppSwitchDueTime; |
| 556 | } |
| 557 | |
| 558 | // Ready to start a new event. |
| 559 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 560 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 561 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | if (isAppSwitchDue) { |
| 563 | // The inbound queue is empty so the app switch key we were waiting |
| 564 | // for will never arrive. Stop waiting for it. |
| 565 | resetPendingAppSwitchLocked(false); |
| 566 | isAppSwitchDue = false; |
| 567 | } |
| 568 | |
| 569 | // Synthesize a key repeat if appropriate. |
| 570 | if (mKeyRepeatState.lastKeyEntry) { |
| 571 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 572 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 573 | } else { |
| 574 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 575 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // Nothing to do if there is no pending event. |
| 581 | if (!mPendingEvent) { |
| 582 | return; |
| 583 | } |
| 584 | } else { |
| 585 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 586 | mPendingEvent = mInboundQueue.front(); |
| 587 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | traceInboundQueueLengthLocked(); |
| 589 | } |
| 590 | |
| 591 | // Poke user activity for this event. |
| 592 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 593 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Now we have an event to dispatch. |
| 598 | // 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] | 599 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 601 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 603 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 604 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 605 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 609 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 613 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 614 | ConfigurationChangedEntry* typedEntry = |
| 615 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 616 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 617 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 618 | break; |
| 619 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 620 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 621 | case EventEntry::Type::DEVICE_RESET: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 622 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); |
| 623 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 624 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 625 | break; |
| 626 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 628 | case EventEntry::Type::FOCUS: { |
| 629 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); |
| 630 | dispatchFocusLocked(currentTime, typedEntry); |
| 631 | done = true; |
| 632 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 633 | break; |
| 634 | } |
| 635 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 636 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 637 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 638 | if (isAppSwitchDue) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 639 | if (isAppSwitchKeyEvent(*typedEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 640 | resetPendingAppSwitchLocked(true); |
| 641 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 642 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 643 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 646 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 647 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 648 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 649 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 650 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 651 | } |
| 652 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 653 | break; |
| 654 | } |
| 655 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 656 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 657 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 658 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 659 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 661 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 662 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 663 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 664 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 665 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 666 | } |
| 667 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 668 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 669 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 673 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 674 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 676 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 677 | |
| 678 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 679 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 683 | /** |
| 684 | * Return true if the events preceding this incoming motion event should be dropped |
| 685 | * Return false otherwise (the default behaviour) |
| 686 | */ |
| 687 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 688 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 689 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 690 | |
| 691 | // Optimize case where the current application is unresponsive and the user |
| 692 | // decides to touch a window in a different application. |
| 693 | // If the application takes too long to catch up then we drop all events preceding |
| 694 | // the touch into the other window. |
| 695 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 696 | int32_t displayId = motionEntry.displayId; |
| 697 | int32_t x = static_cast<int32_t>( |
| 698 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 699 | int32_t y = static_cast<int32_t>( |
| 700 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 701 | sp<InputWindowHandle> touchedWindowHandle = |
| 702 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 703 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 704 | touchedWindowHandle->getApplicationToken() != |
| 705 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 706 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 707 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 708 | "for %s", |
| 709 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 710 | return true; |
| 711 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | |
| 713 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 714 | std::vector<TouchedMonitor> gestureMonitors = |
| 715 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 716 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 717 | sp<Connection> connection = |
| 718 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 719 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 720 | // This monitor could take more input. Drop all events preceding this |
| 721 | // event, so that gesture monitor could get a chance to receive the stream |
| 722 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 723 | "responsive gesture monitor that may handle the event", |
| 724 | mAwaitedFocusedApplication->getName().c_str()); |
| 725 | return true; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 731 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 732 | // events to be processed before dispatching the key event. This is because these motion events |
| 733 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 734 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 735 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 736 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 737 | "just send the pending key event to the focused window."); |
| 738 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 743 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 744 | bool needWake = mInboundQueue.empty(); |
| 745 | mInboundQueue.push_back(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | traceInboundQueueLengthLocked(); |
| 747 | |
| 748 | switch (entry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 749 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 750 | // Optimize app switch latency. |
| 751 | // If the application takes too long to catch up then we drop all events preceding |
| 752 | // the app switch key. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 753 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 754 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 755 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 756 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 757 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 758 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 761 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 762 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 763 | mAppSwitchSawKeyDown = false; |
| 764 | needWake = true; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | break; |
| 769 | } |
| 770 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 771 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 772 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { |
| 773 | mNextUnblockedEvent = entry; |
| 774 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 776 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 778 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 779 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 780 | break; |
| 781 | } |
| 782 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 783 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 784 | // nothing to do |
| 785 | break; |
| 786 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | return needWake; |
| 790 | } |
| 791 | |
| 792 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 793 | entry->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 794 | mRecentQueue.push_back(entry); |
| 795 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
| 796 | mRecentQueue.front()->release(); |
| 797 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 801 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 802 | int32_t y, TouchState* touchState, |
| 803 | bool addOutsideTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 804 | bool addPortalWindows) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 805 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 806 | LOG_ALWAYS_FATAL( |
| 807 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 808 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 809 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 810 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 811 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 813 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 814 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | |
| 816 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 817 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 818 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 819 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 820 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 821 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 822 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 823 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 824 | if (addPortalWindows) { |
| 825 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 826 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 827 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 828 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 829 | addOutsideTargets, addPortalWindows); |
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 | // Found window. |
| 832 | return windowHandle; |
| 833 | } |
| 834 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 835 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 836 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 837 | touchState->addOrUpdateWindow(windowHandle, |
| 838 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 839 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 840 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 841 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 844 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 847 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 848 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 849 | std::vector<TouchedMonitor> touchedMonitors; |
| 850 | |
| 851 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 852 | addGestureMonitors(monitors, touchedMonitors); |
| 853 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 854 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 855 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 856 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 857 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 858 | } |
| 859 | return touchedMonitors; |
| 860 | } |
| 861 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 862 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 863 | const char* reason; |
| 864 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 867 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 868 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 | reason = "inbound event was dropped because the policy consumed it"; |
| 870 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | case DropReason::DISABLED: |
| 872 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 873 | ALOGI("Dropped event because input dispatch is disabled."); |
| 874 | } |
| 875 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 876 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | ALOGI("Dropped event because of pending overdue app switch."); |
| 879 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 880 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 881 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 882 | ALOGI("Dropped event because the current application is not responding and the user " |
| 883 | "has started interacting with a different application."); |
| 884 | reason = "inbound event was dropped because the current application is not responding " |
| 885 | "and the user has started interacting with a different application"; |
| 886 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 887 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | ALOGI("Dropped event because it is stale."); |
| 889 | reason = "inbound event was dropped because it is stale"; |
| 890 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 891 | case DropReason::NOT_DROPPED: { |
| 892 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 893 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 897 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 898 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 900 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 901 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 903 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 904 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 905 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 906 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 907 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 908 | } else { |
| 909 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 910 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 911 | } |
| 912 | break; |
| 913 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 914 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 915 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 916 | case EventEntry::Type::DEVICE_RESET: { |
| 917 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); |
| 918 | break; |
| 919 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 923 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 924 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 925 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 926 | } |
| 927 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 928 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 929 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 930 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 931 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 935 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 936 | } |
| 937 | |
| 938 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 939 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 940 | |
| 941 | #if DEBUG_APP_SWITCH |
| 942 | if (handled) { |
| 943 | ALOGD("App switch has arrived."); |
| 944 | } else { |
| 945 | ALOGD("App switch was abandoned."); |
| 946 | } |
| 947 | #endif |
| 948 | } |
| 949 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 951 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 955 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | return false; |
| 957 | } |
| 958 | |
| 959 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 960 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 961 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 962 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 963 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 | |
| 965 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 966 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 967 | return true; |
| 968 | } |
| 969 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 970 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 971 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 975 | while (!mInboundQueue.empty()) { |
| 976 | EventEntry* entry = mInboundQueue.front(); |
| 977 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 978 | releaseInboundEventLocked(entry); |
| 979 | } |
| 980 | traceInboundQueueLengthLocked(); |
| 981 | } |
| 982 | |
| 983 | void InputDispatcher::releasePendingEventLocked() { |
| 984 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 985 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 986 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 991 | InjectionState* injectionState = entry->injectionState; |
| 992 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 993 | #if DEBUG_DISPATCH_CYCLE |
| 994 | ALOGD("Injected inbound event was dropped."); |
| 995 | #endif |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 996 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | } |
| 998 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 999 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1000 | } |
| 1001 | addRecentEventLocked(entry); |
| 1002 | entry->release(); |
| 1003 | } |
| 1004 | |
| 1005 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1006 | if (mKeyRepeatState.lastKeyEntry) { |
| 1007 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1008 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1012 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1013 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 1014 | |
| 1015 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1016 | uint32_t policyFlags = entry->policyFlags & |
| 1017 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | if (entry->refCount == 1) { |
| 1019 | entry->recycle(); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1020 | entry->id = mIdGenerator.nextId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1021 | entry->eventTime = currentTime; |
| 1022 | entry->policyFlags = policyFlags; |
| 1023 | entry->repeatCount += 1; |
| 1024 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1025 | KeyEntry* newEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1026 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1027 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1028 | entry->keyCode, entry->scanCode, entry->metaState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1029 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | |
| 1031 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 1032 | entry->release(); |
| 1033 | |
| 1034 | entry = newEntry; |
| 1035 | } |
| 1036 | entry->syntheticRepeat = true; |
| 1037 | |
| 1038 | // Increment reference count since we keep a reference to the event in |
| 1039 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 1040 | entry->refCount += 1; |
| 1041 | |
| 1042 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 1043 | return entry; |
| 1044 | } |
| 1045 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1046 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
| 1047 | ConfigurationChangedEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1049 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | #endif |
| 1051 | |
| 1052 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1053 | resetKeyRepeatLocked(); |
| 1054 | |
| 1055 | // 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] | 1056 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1057 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | commandEntry->eventTime = entry->eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1059 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 | return true; |
| 1061 | } |
| 1062 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1063 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1065 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1066 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | #endif |
| 1068 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1069 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1070 | options.deviceId = entry->deviceId; |
| 1071 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1075 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1076 | std::string_view reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1077 | if (mPendingEvent != nullptr) { |
| 1078 | // Move the pending event to the front of the queue. This will give the chance |
| 1079 | // for the pending event to get dispatched to the newly focused window |
| 1080 | mInboundQueue.push_front(mPendingEvent); |
| 1081 | mPendingEvent = nullptr; |
| 1082 | } |
| 1083 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1084 | FocusEntry* focusEntry = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1085 | new FocusEntry(mIdGenerator.nextId(), now(), windowToken, hasFocus, reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1086 | |
| 1087 | // This event should go to the front of the queue, but behind all other focus events |
| 1088 | // Find the last focus event, and insert right after it |
| 1089 | std::deque<EventEntry*>::reverse_iterator it = |
| 1090 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1091 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); |
| 1092 | |
| 1093 | // Maintain the order of focus events. Insert the entry after all other focus events. |
| 1094 | mInboundQueue.insert(it.base(), focusEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1098 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1099 | if (channel == nullptr) { |
| 1100 | return; // Window has gone away |
| 1101 | } |
| 1102 | InputTarget target; |
| 1103 | target.inputChannel = channel; |
| 1104 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1105 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1106 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1107 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1108 | std::string reason = std::string("reason=").append(entry->reason); |
| 1109 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1110 | dispatchEventLocked(currentTime, entry, {target}); |
| 1111 | } |
| 1112 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1113 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1115 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1116 | if (!entry->dispatchInProgress) { |
| 1117 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1118 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1119 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1120 | if (mKeyRepeatState.lastKeyEntry && |
| 1121 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | // We have seen two identical key downs in a row which indicates that the device |
| 1123 | // driver is automatically generating key repeats itself. We take note of the |
| 1124 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1125 | // we will not need to synthesize key repeats ourselves. |
| 1126 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1127 | resetKeyRepeatLocked(); |
| 1128 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1129 | } else { |
| 1130 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1131 | resetKeyRepeatLocked(); |
| 1132 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1133 | } |
| 1134 | mKeyRepeatState.lastKeyEntry = entry; |
| 1135 | entry->refCount += 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1136 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1137 | resetKeyRepeatLocked(); |
| 1138 | } |
| 1139 | |
| 1140 | if (entry->repeatCount == 1) { |
| 1141 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1142 | } else { |
| 1143 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1144 | } |
| 1145 | |
| 1146 | entry->dispatchInProgress = true; |
| 1147 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1148 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | // Handle case where the policy asked us to try again later last time. |
| 1152 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1153 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1154 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1155 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1156 | } |
| 1157 | return false; // wait until next wakeup |
| 1158 | } |
| 1159 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1160 | entry->interceptKeyWakeupTime = 0; |
| 1161 | } |
| 1162 | |
| 1163 | // Give the policy a chance to intercept the key. |
| 1164 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1165 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1166 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1167 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1168 | sp<IBinder> focusedWindowToken = |
| 1169 | getValueByKey(mFocusedWindowTokenByDisplay, getTargetDisplayId(*entry)); |
| 1170 | if (focusedWindowToken != nullptr) { |
| 1171 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1172 | } |
| 1173 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1174 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 | entry->refCount += 1; |
| 1176 | return false; // wait for the command to run |
| 1177 | } else { |
| 1178 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1179 | } |
| 1180 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1181 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1182 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1187 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1188 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1189 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1190 | : INPUT_EVENT_INJECTION_FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1191 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | return true; |
| 1193 | } |
| 1194 | |
| 1195 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1196 | std::vector<InputTarget> inputTargets; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1197 | int32_t injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1198 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1199 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1203 | setInjectionResult(entry, injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 1205 | return true; |
| 1206 | } |
| 1207 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1208 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1209 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | |
| 1211 | // Dispatch the key. |
| 1212 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1216 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1218 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1219 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1220 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1221 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1222 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1223 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | #endif |
| 1225 | } |
| 1226 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1227 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1228 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1229 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1231 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | entry->dispatchInProgress = true; |
| 1233 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1234 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1238 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1239 | setInjectionResult(entry, |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1240 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1241 | : INPUT_EVENT_INJECTION_FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1246 | |
| 1247 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1248 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | |
| 1250 | bool conflictingPointerActions = false; |
| 1251 | int32_t injectionResult; |
| 1252 | if (isPointerEvent) { |
| 1253 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1254 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1255 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1256 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | } else { |
| 1258 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1259 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1260 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | } |
| 1262 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 1263 | return false; |
| 1264 | } |
| 1265 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1266 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1267 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { |
| 1268 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1269 | return true; |
| 1270 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1271 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1272 | CancelationOptions::Mode mode(isPointerEvent |
| 1273 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1274 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1275 | CancelationOptions options(mode, "input event injection failed"); |
| 1276 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | return true; |
| 1278 | } |
| 1279 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1280 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1281 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1282 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1283 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1284 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1285 | mTouchStatesByDisplay.find(entry->displayId); |
| 1286 | if (it != mTouchStatesByDisplay.end()) { |
| 1287 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1288 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1289 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1290 | // the corresponding displays as well. |
| 1291 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1292 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1293 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1294 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1300 | // Dispatch the motion. |
| 1301 | if (conflictingPointerActions) { |
| 1302 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1303 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1305 | } |
| 1306 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1307 | return true; |
| 1308 | } |
| 1309 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1310 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1311 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1312 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1313 | ", policyFlags=0x%x, " |
| 1314 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1315 | "metaState=0x%x, buttonState=0x%x," |
| 1316 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1317 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1318 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1319 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1320 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1321 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1323 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1324 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1325 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1326 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1327 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1328 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1329 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1330 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1331 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1332 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1333 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1334 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1335 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1336 | } |
| 1337 | #endif |
| 1338 | } |
| 1339 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1340 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1341 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1342 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1343 | #if DEBUG_DISPATCH_CYCLE |
| 1344 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1345 | #endif |
| 1346 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1347 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1348 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1349 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1350 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1351 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1353 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1354 | sp<Connection> connection = |
| 1355 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1356 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1357 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1358 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1359 | if (DEBUG_FOCUS) { |
| 1360 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1361 | "is no longer registered with the input dispatcher.", |
| 1362 | inputTarget.inputChannel->getName().c_str()); |
| 1363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1368 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1369 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1370 | // If the policy decides to close the app, we will get a channel removal event via |
| 1371 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1372 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1373 | // pile up. |
| 1374 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1375 | connection->inputChannel->getName().c_str()); |
| 1376 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1377 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1378 | "application not responding"); |
| 1379 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1383 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1384 | if (DEBUG_FOCUS) { |
| 1385 | ALOGD("Resetting ANR timeouts."); |
| 1386 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1387 | |
| 1388 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1389 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1390 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1393 | /** |
| 1394 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1395 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1396 | * Focused display is the display that the user most recently interacted with. |
| 1397 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1398 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1399 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1400 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1401 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1402 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1403 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1406 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1407 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1408 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1409 | break; |
| 1410 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1411 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1412 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1413 | case EventEntry::Type::DEVICE_RESET: { |
| 1414 | ALOGE("%s events do not have a target display", EventEntry::typeToString(entry.type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1415 | return ADISPLAY_ID_NONE; |
| 1416 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1417 | } |
| 1418 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1419 | } |
| 1420 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1421 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1422 | const char* focusedWindowName) { |
| 1423 | if (mAnrTracker.empty()) { |
| 1424 | // already processed all events that we waited for |
| 1425 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1430 | // Start the timer |
| 1431 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " |
| 1432 | "focus to change", |
| 1433 | focusedWindowName); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1434 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1435 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1436 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1437 | return true; |
| 1438 | } |
| 1439 | |
| 1440 | // We still have pending events, and already started the timer |
| 1441 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1442 | return true; // Still waiting |
| 1443 | } |
| 1444 | |
| 1445 | // Waited too long, and some connection still hasn't processed all motions |
| 1446 | // Just send the key to the focused window |
| 1447 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1448 | focusedWindowName); |
| 1449 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1450 | return false; |
| 1451 | } |
| 1452 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1453 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1454 | const EventEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1455 | std::vector<InputTarget>& inputTargets, |
| 1456 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1457 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1458 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1459 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1460 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1461 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1462 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1463 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1464 | // If there is no currently focused window and no focused application |
| 1465 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1466 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1467 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1468 | "display %" PRId32 ".", |
| 1469 | EventEntry::typeToString(entry.type), displayId); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1470 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1473 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1474 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1475 | // start interacting with another application via touch (app switch). This code can be removed |
| 1476 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1477 | // an app is expected to have a focused window. |
| 1478 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1479 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1480 | // 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] | 1481 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1482 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1483 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1484 | mAwaitedFocusedApplication = focusedApplicationHandle; |
| 1485 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1486 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1487 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1488 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
| 1489 | return INPUT_EVENT_INJECTION_PENDING; |
| 1490 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1491 | // Already raised ANR. Drop the event |
| 1492 | ALOGE("Dropping %s event because there is no focused window", |
| 1493 | EventEntry::typeToString(entry.type)); |
| 1494 | return INPUT_EVENT_INJECTION_FAILED; |
| 1495 | } else { |
| 1496 | // Still waiting for the focused window |
| 1497 | return INPUT_EVENT_INJECTION_PENDING; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // we have a valid, non-null focused window |
| 1502 | resetNoFocusedWindowTimeoutLocked(); |
| 1503 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1504 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1505 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1506 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1509 | if (focusedWindowHandle->getInfo()->paused) { |
| 1510 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
| 1511 | return INPUT_EVENT_INJECTION_PENDING; |
| 1512 | } |
| 1513 | |
| 1514 | // If the event is a key event, then we must wait for all previous events to |
| 1515 | // complete before delivering it because previous events may have the |
| 1516 | // side-effect of transferring focus to a different window and we want to |
| 1517 | // ensure that the following keys are sent to the new window. |
| 1518 | // |
| 1519 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1520 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1521 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1522 | // often anticipate pending UI changes when typing on a keyboard. |
| 1523 | // To obtain this behavior, we must serialize key events with respect to all |
| 1524 | // prior input events. |
| 1525 | if (entry.type == EventEntry::Type::KEY) { |
| 1526 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1527 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
| 1528 | return INPUT_EVENT_INJECTION_PENDING; |
| 1529 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1533 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1534 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1535 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1536 | |
| 1537 | // Done. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1538 | return INPUT_EVENT_INJECTION_SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1539 | } |
| 1540 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1541 | /** |
| 1542 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1543 | * that are currently unresponsive. |
| 1544 | */ |
| 1545 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1546 | const std::vector<TouchedMonitor>& monitors) const { |
| 1547 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1548 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1549 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1550 | sp<Connection> connection = getConnectionLocked( |
| 1551 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1552 | if (connection == nullptr) { |
| 1553 | ALOGE("Could not find connection for monitor %s", |
| 1554 | monitor.monitor.inputChannel->getName().c_str()); |
| 1555 | return false; |
| 1556 | } |
| 1557 | if (!connection->responsive) { |
| 1558 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1559 | connection->inputChannel->getName().c_str()); |
| 1560 | return false; |
| 1561 | } |
| 1562 | return true; |
| 1563 | }); |
| 1564 | return responsiveMonitors; |
| 1565 | } |
| 1566 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1568 | const MotionEntry& entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1569 | std::vector<InputTarget>& inputTargets, |
| 1570 | nsecs_t* nextWakeupTime, |
| 1571 | bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1572 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1573 | enum InjectionPermission { |
| 1574 | INJECTION_PERMISSION_UNKNOWN, |
| 1575 | INJECTION_PERMISSION_GRANTED, |
| 1576 | INJECTION_PERMISSION_DENIED |
| 1577 | }; |
| 1578 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | // For security reasons, we defer updating the touch state until we are sure that |
| 1580 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1581 | int32_t displayId = entry.displayId; |
| 1582 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1583 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1584 | |
| 1585 | // Update the touch state as needed based on the properties of the touch event. |
| 1586 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1587 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1588 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1589 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1591 | // Copy current touch state into tempTouchState. |
| 1592 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1593 | // 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] | 1594 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1595 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1596 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1597 | mTouchStatesByDisplay.find(displayId); |
| 1598 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1599 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1600 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1603 | bool isSplit = tempTouchState.split; |
| 1604 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1605 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1606 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1607 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1608 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1609 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1610 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1611 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1612 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1613 | bool wrongDevice = false; |
| 1614 | if (newGesture) { |
| 1615 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1616 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1617 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1618 | "in display %" PRId32, |
| 1619 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1620 | // TODO: test multiple simultaneous input streams. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1622 | switchedDevice = false; |
| 1623 | wrongDevice = true; |
| 1624 | goto Failed; |
| 1625 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1626 | tempTouchState.reset(); |
| 1627 | tempTouchState.down = down; |
| 1628 | tempTouchState.deviceId = entry.deviceId; |
| 1629 | tempTouchState.source = entry.source; |
| 1630 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1632 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1633 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1634 | "in display %" PRId32, |
| 1635 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1636 | // TODO: test multiple simultaneous input streams. |
| 1637 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1638 | switchedDevice = false; |
| 1639 | wrongDevice = true; |
| 1640 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1644 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1645 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1646 | int32_t x; |
| 1647 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1648 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1649 | // Always dispatch mouse events to cursor position. |
| 1650 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1651 | x = int32_t(entry.xCursorPosition); |
| 1652 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1653 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1654 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1655 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1656 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1657 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1658 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1659 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1660 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1661 | |
| 1662 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1663 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1664 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1665 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1667 | if (newTouchedWindowHandle != nullptr && |
| 1668 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1669 | // New window supports splitting, but we should never split mouse events. |
| 1670 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1671 | } else if (isSplit) { |
| 1672 | // New window does not support splitting but we have already split events. |
| 1673 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1674 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1678 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1679 | // 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] | 1680 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1683 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1684 | ALOGI("Not sending touch event to %s because it is paused", |
| 1685 | newTouchedWindowHandle->getName().c_str()); |
| 1686 | newTouchedWindowHandle = nullptr; |
| 1687 | } |
| 1688 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1689 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1690 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1691 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1692 | if (!isResponsive) { |
| 1693 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1694 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1695 | newTouchedWindowHandle = nullptr; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1700 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1701 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1702 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1703 | 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] | 1704 | "(%d, %d) in display %" PRId32 ".", |
| 1705 | x, y, displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1706 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1707 | goto Failed; |
| 1708 | } |
| 1709 | |
| 1710 | if (newTouchedWindowHandle != nullptr) { |
| 1711 | // Set target flags. |
| 1712 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1713 | if (isSplit) { |
| 1714 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1715 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1716 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1717 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1718 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1719 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1720 | } |
| 1721 | |
| 1722 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1723 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 1724 | newHoverWindowHandle = nullptr; |
| 1725 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1726 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | // Update the temporary touch state. |
| 1730 | BitSet32 pointerIds; |
| 1731 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1732 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1733 | pointerIds.markBit(pointerId); |
| 1734 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1735 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1738 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | } else { |
| 1740 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1741 | |
| 1742 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1743 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1744 | if (DEBUG_FOCUS) { |
| 1745 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1746 | "dropped the pointer down event in display %" PRId32, |
| 1747 | displayId); |
| 1748 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1750 | goto Failed; |
| 1751 | } |
| 1752 | |
| 1753 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1754 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1755 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1756 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1757 | 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] | 1758 | |
| 1759 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1760 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1761 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1762 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1763 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1764 | if (DEBUG_FOCUS) { |
| 1765 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1766 | oldTouchedWindowHandle->getName().c_str(), |
| 1767 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1768 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1769 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1770 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1771 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1772 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1773 | |
| 1774 | // Make a slippery entrance into the new window. |
| 1775 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1776 | isSplit = true; |
| 1777 | } |
| 1778 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1779 | int32_t targetFlags = |
| 1780 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | if (isSplit) { |
| 1782 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1783 | } |
| 1784 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1785 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1786 | } |
| 1787 | |
| 1788 | BitSet32 pointerIds; |
| 1789 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1790 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1791 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1792 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1798 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 1799 | // when dispatching it as is to newTouchedWindowHandle. |
| 1800 | if (mLastHoverWindowHandle != nullptr && |
| 1801 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 1802 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1803 | #if DEBUG_HOVER |
| 1804 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1805 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1807 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1808 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1811 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 1812 | // when dispatching it as is to newTouchedWindowHandle. |
| 1813 | if (newHoverWindowHandle != nullptr && |
| 1814 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1815 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | #if DEBUG_HOVER |
| 1817 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1818 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1820 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1821 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1822 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | // Check permission to inject into all touched foreground windows and ensure there |
| 1827 | // is at least one touched foreground window. |
| 1828 | { |
| 1829 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1830 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1832 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1833 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1835 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1836 | goto Failed; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1840 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1841 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1842 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1843 | "%" PRId32 " or gesture monitor to receive it.", |
| 1844 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1846 | goto Failed; |
| 1847 | } |
| 1848 | |
| 1849 | // Permission granted to injection into all touched foreground windows. |
| 1850 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1851 | } |
| 1852 | |
| 1853 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1854 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1855 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1856 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1857 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1858 | if (foregroundWindowHandle) { |
| 1859 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1860 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1861 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1862 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1863 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1864 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1865 | InputTarget::FLAG_ZERO_COORDS, |
| 1866 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1867 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1873 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1874 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1875 | // of the touch gesture. |
| 1876 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1877 | // engine only supports touch events. We would need to add a mechanism similar |
| 1878 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1879 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1880 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1881 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1882 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1883 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1884 | getWindowHandlesLocked(displayId); |
| 1885 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1886 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1887 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1888 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1889 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1890 | .addOrUpdateWindow(windowHandle, |
| 1891 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1892 | InputTarget:: |
| 1893 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1894 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1895 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // Success! Output targets. |
| 1902 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 1903 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1904 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1905 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1906 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1909 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1910 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1911 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | // Drop the outside or hover touch windows since we will not care about them |
| 1915 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1916 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1917 | |
| 1918 | Failed: |
| 1919 | // Check injection permission once and for all. |
| 1920 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1921 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1923 | } else { |
| 1924 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1925 | } |
| 1926 | } |
| 1927 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1928 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 1929 | return injectionResult; |
| 1930 | } |
| 1931 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1932 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1933 | if (!wrongDevice) { |
| 1934 | if (switchedDevice) { |
| 1935 | if (DEBUG_FOCUS) { |
| 1936 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 1937 | } |
| 1938 | *outConflictingPointerActions = true; |
| 1939 | } |
| 1940 | |
| 1941 | if (isHoverAction) { |
| 1942 | // Started hovering, therefore no longer down. |
| 1943 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1944 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1945 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 1946 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | *outConflictingPointerActions = true; |
| 1949 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1950 | tempTouchState.reset(); |
| 1951 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1952 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 1953 | tempTouchState.deviceId = entry.deviceId; |
| 1954 | tempTouchState.source = entry.source; |
| 1955 | tempTouchState.displayId = displayId; |
| 1956 | } |
| 1957 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 1958 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 1959 | // All pointers up or canceled. |
| 1960 | tempTouchState.reset(); |
| 1961 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1962 | // First pointer went down. |
| 1963 | if (oldState && oldState->down) { |
| 1964 | if (DEBUG_FOCUS) { |
| 1965 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 1966 | } |
| 1967 | *outConflictingPointerActions = true; |
| 1968 | } |
| 1969 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1970 | // One pointer went up. |
| 1971 | if (isSplit) { |
| 1972 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 1973 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1974 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1975 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 1976 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 1977 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1978 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1979 | if (touchedWindow.pointerIds.isEmpty()) { |
| 1980 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 1981 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1982 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1983 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1984 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1986 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1987 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1988 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1989 | // Save changes unless the action was scroll in which case the temporary touch |
| 1990 | // state was only valid for this one action. |
| 1991 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 1992 | if (tempTouchState.displayId >= 0) { |
| 1993 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 1994 | } else { |
| 1995 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1996 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1997 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1998 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1999 | // Update hover state. |
| 2000 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | } |
| 2002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2003 | return injectionResult; |
| 2004 | } |
| 2005 | |
| 2006 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2007 | int32_t targetFlags, BitSet32 pointerIds, |
| 2008 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2009 | std::vector<InputTarget>::iterator it = |
| 2010 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2011 | [&windowHandle](const InputTarget& inputTarget) { |
| 2012 | return inputTarget.inputChannel->getConnectionToken() == |
| 2013 | windowHandle->getToken(); |
| 2014 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2015 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2016 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (it == inputTargets.end()) { |
| 2019 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2020 | std::shared_ptr<InputChannel> inputChannel = |
| 2021 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2022 | if (inputChannel == nullptr) { |
| 2023 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2024 | return; |
| 2025 | } |
| 2026 | inputTarget.inputChannel = inputChannel; |
| 2027 | inputTarget.flags = targetFlags; |
| 2028 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2029 | inputTargets.push_back(inputTarget); |
| 2030 | it = inputTargets.end() - 1; |
| 2031 | } |
| 2032 | |
| 2033 | ALOG_ASSERT(it->flags == targetFlags); |
| 2034 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2035 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2036 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2039 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2040 | int32_t displayId, float xOffset, |
| 2041 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2042 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2043 | mGlobalMonitorsByDisplay.find(displayId); |
| 2044 | |
| 2045 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2046 | const std::vector<Monitor>& monitors = it->second; |
| 2047 | for (const Monitor& monitor : monitors) { |
| 2048 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2049 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2053 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2054 | float yOffset, |
| 2055 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2056 | InputTarget target; |
| 2057 | target.inputChannel = monitor.inputChannel; |
| 2058 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2059 | ui::Transform t; |
| 2060 | t.set(xOffset, yOffset); |
| 2061 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2062 | inputTargets.push_back(target); |
| 2063 | } |
| 2064 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2065 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2066 | const InjectionState* injectionState) { |
| 2067 | if (injectionState && |
| 2068 | (windowHandle == nullptr || |
| 2069 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2070 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2071 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | 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] | 2073 | "owned by uid %d", |
| 2074 | injectionState->injectorPid, injectionState->injectorUid, |
| 2075 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2076 | } else { |
| 2077 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2078 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | } |
| 2080 | return false; |
| 2081 | } |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2085 | /** |
| 2086 | * Indicate whether one window handle should be considered as obscuring |
| 2087 | * another window handle. We only check a few preconditions. Actually |
| 2088 | * checking the bounds is left to the caller. |
| 2089 | */ |
| 2090 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2091 | const sp<InputWindowHandle>& otherHandle) { |
| 2092 | // Compare by token so cloned layers aren't counted |
| 2093 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2094 | return false; |
| 2095 | } |
| 2096 | auto info = windowHandle->getInfo(); |
| 2097 | auto otherInfo = otherHandle->getInfo(); |
| 2098 | if (!otherInfo->visible) { |
| 2099 | return false; |
Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2100 | } else if (info->ownerPid == otherInfo->ownerPid) { |
| 2101 | // If ownerPid is the same we don't generate occlusion events as there |
| 2102 | // is no in-process security boundary. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2103 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2104 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2105 | return false; |
| 2106 | } else if (otherInfo->displayId != info->displayId) { |
| 2107 | return false; |
| 2108 | } |
| 2109 | return true; |
| 2110 | } |
| 2111 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2112 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2113 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2114 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2115 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2116 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2117 | if (windowHandle == otherHandle) { |
| 2118 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2119 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2120 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2121 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2122 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2123 | return true; |
| 2124 | } |
| 2125 | } |
| 2126 | return false; |
| 2127 | } |
| 2128 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2129 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2130 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2131 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2132 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2133 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2134 | if (windowHandle == otherHandle) { |
| 2135 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2136 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2137 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2138 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2139 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2140 | return true; |
| 2141 | } |
| 2142 | } |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2146 | std::string InputDispatcher::getApplicationWindowLabel( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2147 | const std::shared_ptr<InputApplicationHandle>& applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2148 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2149 | if (applicationHandle != nullptr) { |
| 2150 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2151 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | } else { |
| 2153 | return applicationHandle->getName(); |
| 2154 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2155 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2156 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2157 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2158 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2162 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2163 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2164 | // Focus events are passed to apps, but do not represent user activity. |
| 2165 | return; |
| 2166 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2167 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2168 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2169 | if (focusedWindowHandle != nullptr) { |
| 2170 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2171 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2172 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2173 | 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] | 2174 | #endif |
| 2175 | return; |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2180 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2181 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2182 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2183 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2184 | return; |
| 2185 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2186 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2187 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2188 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2189 | } |
| 2190 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2191 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2192 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2193 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2194 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2195 | return; |
| 2196 | } |
| 2197 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2198 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2200 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2201 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2202 | case EventEntry::Type::DEVICE_RESET: { |
| 2203 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2204 | EventEntry::typeToString(eventEntry.type)); |
| 2205 | break; |
| 2206 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2207 | } |
| 2208 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2209 | std::unique_ptr<CommandEntry> commandEntry = |
| 2210 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2211 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2212 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2213 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2217 | const sp<Connection>& connection, |
| 2218 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2219 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2220 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2221 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2222 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2223 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2224 | ATRACE_NAME(message.c_str()); |
| 2225 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2226 | #if DEBUG_DISPATCH_CYCLE |
| 2227 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2228 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2229 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2230 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2231 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | #endif |
| 2233 | |
| 2234 | // Skip this event if the connection status is not normal. |
| 2235 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2236 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2237 | #if DEBUG_DISPATCH_CYCLE |
| 2238 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2239 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 | #endif |
| 2241 | return; |
| 2242 | } |
| 2243 | |
| 2244 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2245 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2246 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2247 | "Entry type %s should not have FLAG_SPLIT", |
| 2248 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2249 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2250 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2251 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2252 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2253 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2254 | if (!splitMotionEntry) { |
| 2255 | return; // split event was dropped |
| 2256 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2257 | if (DEBUG_FOCUS) { |
| 2258 | ALOGD("channel '%s' ~ Split motion event.", |
| 2259 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2260 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2261 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2262 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2263 | splitMotionEntry->release(); |
| 2264 | return; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2269 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2270 | } |
| 2271 | |
| 2272 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2273 | const sp<Connection>& connection, |
| 2274 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2275 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2276 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2277 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2278 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2279 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2280 | ATRACE_NAME(message.c_str()); |
| 2281 | } |
| 2282 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2283 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | |
| 2285 | // Enqueue dispatch entries for the requested modes. |
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_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2288 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2289 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2290 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2291 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2292 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2293 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2294 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2295 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2296 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2297 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2298 | |
| 2299 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2300 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2301 | startDispatchCycleLocked(currentTime, connection); |
| 2302 | } |
| 2303 | } |
| 2304 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2305 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2306 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2307 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2308 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2309 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2310 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2311 | connection->getInputChannelName().c_str(), |
| 2312 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2313 | ATRACE_NAME(message.c_str()); |
| 2314 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2315 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 | if (!(inputTargetFlags & dispatchMode)) { |
| 2317 | return; |
| 2318 | } |
| 2319 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2320 | |
| 2321 | // This is a new event. |
| 2322 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2323 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2324 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2325 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2326 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2327 | // different EventEntry than what was passed in. |
| 2328 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2329 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2330 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2331 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2332 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2333 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2334 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2335 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2337 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2338 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2339 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2340 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2341 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2343 | return; // skip the inconsistent event |
| 2344 | } |
| 2345 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2348 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2349 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2350 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2351 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2352 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2353 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2354 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2355 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2356 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2357 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2358 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2359 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2360 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2361 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2362 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2363 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2364 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2365 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2366 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2367 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2368 | } |
| 2369 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2370 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2371 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2372 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2373 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2374 | "event", |
| 2375 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2376 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2377 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2378 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2379 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2380 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2381 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2382 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2383 | } |
| 2384 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2385 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2386 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2387 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2388 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2389 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2390 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2391 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2392 | "event", |
| 2393 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2394 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2395 | return; // skip the inconsistent event |
| 2396 | } |
| 2397 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2398 | dispatchEntry->resolvedEventId = |
| 2399 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2400 | ? mIdGenerator.nextId() |
| 2401 | : motionEntry.id; |
| 2402 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2403 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2404 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2405 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2406 | ATRACE_NAME(message.c_str()); |
| 2407 | } |
| 2408 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2409 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2410 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2411 | |
| 2412 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2413 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2414 | case EventEntry::Type::FOCUS: { |
| 2415 | break; |
| 2416 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2417 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2418 | case EventEntry::Type::DEVICE_RESET: { |
| 2419 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2420 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2421 | break; |
| 2422 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | // Remember that we are waiting for this dispatch to complete. |
| 2426 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2427 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2431 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2432 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2435 | /** |
| 2436 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2437 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2438 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2439 | * We will see both launcher and wallpaper in that list. |
| 2440 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2441 | * until the set of interacted connections changes. |
| 2442 | * |
| 2443 | * The following items are skipped, to reduce the logspam: |
| 2444 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2445 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2446 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2447 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2448 | * Both of those ACTION_UP events would not be logged |
| 2449 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events |
| 2450 | * will not be logged. This is omitted to reduce the amount of data printed. |
| 2451 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore |
| 2452 | * gesture monitor is the only connection receiving the remainder of the gesture. |
| 2453 | */ |
| 2454 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2455 | const std::vector<InputTarget>& targets) { |
| 2456 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2457 | if (entry.type == EventEntry::Type::KEY) { |
| 2458 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2459 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2460 | return; |
| 2461 | } |
| 2462 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2463 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2464 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2465 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2466 | return; |
| 2467 | } |
| 2468 | } else { |
| 2469 | return; // Not a key or a motion |
| 2470 | } |
| 2471 | |
| 2472 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; |
| 2473 | std::vector<sp<Connection>> newConnections; |
| 2474 | for (const InputTarget& target : targets) { |
| 2475 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2476 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2477 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2478 | } |
| 2479 | |
| 2480 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2481 | sp<Connection> connection = getConnectionLocked(token); |
| 2482 | if (connection == nullptr || connection->monitor) { |
| 2483 | continue; // We only need to keep track of the non-monitor connections. |
| 2484 | } |
| 2485 | newConnectionTokens.insert(std::move(token)); |
| 2486 | newConnections.emplace_back(connection); |
| 2487 | } |
| 2488 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 2489 | return; // no change |
| 2490 | } |
| 2491 | mInteractionConnectionTokens = newConnectionTokens; |
| 2492 | |
| 2493 | std::string windowList; |
| 2494 | for (const sp<Connection>& connection : newConnections) { |
| 2495 | windowList += connection->getWindowName() + ", "; |
| 2496 | } |
| 2497 | std::string message = "Interaction with windows: " + windowList; |
| 2498 | if (windowList.empty()) { |
| 2499 | message += "<none>"; |
| 2500 | } |
| 2501 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 2502 | } |
| 2503 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2504 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2505 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2506 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2507 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2508 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2509 | return; |
| 2510 | } |
| 2511 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2512 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); |
| 2513 | if (focusedToken == token) { |
| 2514 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2515 | return; |
| 2516 | } |
| 2517 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2518 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2519 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2520 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2521 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2525 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2526 | if (ATRACE_ENABLED()) { |
| 2527 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2528 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2529 | ATRACE_NAME(message.c_str()); |
| 2530 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2531 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2532 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2533 | #endif |
| 2534 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2535 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2536 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2537 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2538 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2539 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2540 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2541 | |
| 2542 | // Publish the event. |
| 2543 | status_t status; |
| 2544 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2545 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2546 | case EventEntry::Type::KEY: { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2547 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2548 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2549 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2550 | // Publish the key event. |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2551 | status = |
| 2552 | connection->inputPublisher |
| 2553 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2554 | keyEntry->deviceId, keyEntry->source, |
| 2555 | keyEntry->displayId, std::move(hmac), |
| 2556 | dispatchEntry->resolvedAction, |
| 2557 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2558 | keyEntry->scanCode, keyEntry->metaState, |
| 2559 | keyEntry->repeatCount, keyEntry->downTime, |
| 2560 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2561 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2562 | } |
| 2563 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2564 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2565 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2566 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2567 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2568 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2569 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2570 | // 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] | 2571 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2572 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2573 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2574 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2575 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2576 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2577 | // Don't apply window scale here since we don't want scale to affect raw |
| 2578 | // coordinates. The scale will be sent back to the client and applied |
| 2579 | // later when requesting relative coordinates. |
| 2580 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2581 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2582 | } |
| 2583 | usingCoords = scaledCoords; |
| 2584 | } |
| 2585 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2586 | // We don't want the dispatch target to know. |
| 2587 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2588 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2589 | scaledCoords[i].clear(); |
| 2590 | } |
| 2591 | usingCoords = scaledCoords; |
| 2592 | } |
| 2593 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2594 | |
| 2595 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2596 | |
| 2597 | // Publish the motion event. |
| 2598 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2599 | .publishMotionEvent(dispatchEntry->seq, |
| 2600 | dispatchEntry->resolvedEventId, |
| 2601 | motionEntry->deviceId, motionEntry->source, |
| 2602 | motionEntry->displayId, std::move(hmac), |
| 2603 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2604 | motionEntry->actionButton, |
| 2605 | dispatchEntry->resolvedFlags, |
| 2606 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2607 | motionEntry->buttonState, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2608 | motionEntry->classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2609 | dispatchEntry->transform, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2610 | motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2611 | motionEntry->yPrecision, |
| 2612 | motionEntry->xCursorPosition, |
| 2613 | motionEntry->yCursorPosition, |
| 2614 | motionEntry->downTime, motionEntry->eventTime, |
| 2615 | motionEntry->pointerCount, |
| 2616 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2617 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2618 | break; |
| 2619 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2620 | case EventEntry::Type::FOCUS: { |
| 2621 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2622 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2623 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2624 | focusEntry->hasFocus, |
| 2625 | mInTouchMode); |
| 2626 | break; |
| 2627 | } |
| 2628 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2629 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2630 | case EventEntry::Type::DEVICE_RESET: { |
| 2631 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2632 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2633 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2634 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | // Check the result. |
| 2638 | if (status) { |
| 2639 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2640 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2641 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2642 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2643 | "should be empty and we shouldn't have any problems writing an " |
| 2644 | "event to it, status=%d", |
| 2645 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2646 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2647 | } else { |
| 2648 | // Pipe is full and we are waiting for the app to finish process some events |
| 2649 | // before sending more events to it. |
| 2650 | #if DEBUG_DISPATCH_CYCLE |
| 2651 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2652 | "waiting for the application to catch up", |
| 2653 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2654 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2655 | } |
| 2656 | } else { |
| 2657 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2658 | "status=%d", |
| 2659 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2660 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2661 | } |
| 2662 | return; |
| 2663 | } |
| 2664 | |
| 2665 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2666 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2667 | connection->outboundQueue.end(), |
| 2668 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2669 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2670 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2671 | if (connection->responsive) { |
| 2672 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 2673 | connection->inputChannel->getConnectionToken()); |
| 2674 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2675 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2676 | } |
| 2677 | } |
| 2678 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2679 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 2680 | size_t size; |
| 2681 | switch (event.type) { |
| 2682 | case VerifiedInputEvent::Type::KEY: { |
| 2683 | size = sizeof(VerifiedKeyEvent); |
| 2684 | break; |
| 2685 | } |
| 2686 | case VerifiedInputEvent::Type::MOTION: { |
| 2687 | size = sizeof(VerifiedMotionEvent); |
| 2688 | break; |
| 2689 | } |
| 2690 | } |
| 2691 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 2692 | return mHmacKeyManager.sign(start, size); |
| 2693 | } |
| 2694 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2695 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2696 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 2697 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2698 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 2699 | // Only sign events up and down events as the purely move events |
| 2700 | // are tied to their up/down counterparts so signing would be redundant. |
| 2701 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 2702 | verifiedEvent.actionMasked = actionMasked; |
| 2703 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
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 | return INVALID_HMAC; |
| 2707 | } |
| 2708 | |
| 2709 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2710 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 2711 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 2712 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2713 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2714 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2718 | const sp<Connection>& connection, uint32_t seq, |
| 2719 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2720 | #if DEBUG_DISPATCH_CYCLE |
| 2721 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2722 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2723 | #endif |
| 2724 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2725 | if (connection->status == Connection::STATUS_BROKEN || |
| 2726 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | return; |
| 2728 | } |
| 2729 | |
| 2730 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2731 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2732 | } |
| 2733 | |
| 2734 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2735 | const sp<Connection>& connection, |
| 2736 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2737 | #if DEBUG_DISPATCH_CYCLE |
| 2738 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2739 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2740 | #endif |
| 2741 | |
| 2742 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2743 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2744 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2745 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2746 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2747 | |
| 2748 | // The connection appears to be unrecoverably broken. |
| 2749 | // Ignore already broken or zombie connections. |
| 2750 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2751 | connection->status = Connection::STATUS_BROKEN; |
| 2752 | |
| 2753 | if (notify) { |
| 2754 | // Notify other system components. |
| 2755 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2760 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2761 | while (!queue.empty()) { |
| 2762 | DispatchEntry* dispatchEntry = queue.front(); |
| 2763 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2764 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2768 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2769 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2770 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2771 | } |
| 2772 | delete dispatchEntry; |
| 2773 | } |
| 2774 | |
| 2775 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2776 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2777 | |
| 2778 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2779 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2781 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2782 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2783 | "fd=%d, events=0x%x", |
| 2784 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2785 | return 0; // remove the callback |
| 2786 | } |
| 2787 | |
| 2788 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2789 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2790 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2791 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2792 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2793 | "events=0x%x", |
| 2794 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2795 | return 1; |
| 2796 | } |
| 2797 | |
| 2798 | nsecs_t currentTime = now(); |
| 2799 | bool gotOne = false; |
| 2800 | status_t status; |
| 2801 | for (;;) { |
| 2802 | uint32_t seq; |
| 2803 | bool handled; |
| 2804 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2805 | if (status) { |
| 2806 | break; |
| 2807 | } |
| 2808 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2809 | gotOne = true; |
| 2810 | } |
| 2811 | if (gotOne) { |
| 2812 | d->runCommandsLockedInterruptible(); |
| 2813 | if (status == WOULD_BLOCK) { |
| 2814 | return 1; |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2819 | if (notify) { |
| 2820 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2821 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2822 | } |
| 2823 | } else { |
| 2824 | // Monitor channels are never explicitly unregistered. |
| 2825 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2826 | // about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2827 | const bool stillHaveWindowHandle = |
| 2828 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 2829 | nullptr; |
| 2830 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | if (notify) { |
| 2832 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2833 | "events=0x%x", |
| 2834 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 2838 | // Remove the channel. |
| 2839 | d->removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2840 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2841 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2842 | } |
| 2843 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2844 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2845 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2846 | for (const auto& pair : mConnectionsByFd) { |
| 2847 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2851 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2852 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2853 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 2854 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 2855 | } |
| 2856 | |
| 2857 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 2858 | const CancelationOptions& options, |
| 2859 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 2860 | for (const auto& it : monitorsByDisplay) { |
| 2861 | const std::vector<Monitor>& monitors = it.second; |
| 2862 | for (const Monitor& monitor : monitors) { |
| 2863 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2864 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2869 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2870 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2871 | if (connection == nullptr) { |
| 2872 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2874 | |
| 2875 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 2879 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 2880 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2881 | return; |
| 2882 | } |
| 2883 | |
| 2884 | nsecs_t currentTime = now(); |
| 2885 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2886 | std::vector<EventEntry*> cancelationEvents = |
| 2887 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2888 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2889 | if (cancelationEvents.empty()) { |
| 2890 | return; |
| 2891 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2893 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 2894 | "with reality: %s, mode=%d.", |
| 2895 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 2896 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2897 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2898 | |
| 2899 | InputTarget target; |
| 2900 | sp<InputWindowHandle> windowHandle = |
| 2901 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2902 | if (windowHandle != nullptr) { |
| 2903 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2904 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2905 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2906 | } |
| 2907 | target.inputChannel = connection->inputChannel; |
| 2908 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2909 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2910 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 2911 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 2912 | switch (cancelationEventEntry->type) { |
| 2913 | case EventEntry::Type::KEY: { |
| 2914 | logOutboundKeyDetails("cancel - ", |
| 2915 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 2916 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2917 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2918 | case EventEntry::Type::MOTION: { |
| 2919 | logOutboundMotionDetails("cancel - ", |
| 2920 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 2921 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2922 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2923 | case EventEntry::Type::FOCUS: { |
| 2924 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 2925 | break; |
| 2926 | } |
| 2927 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2928 | case EventEntry::Type::DEVICE_RESET: { |
| 2929 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2930 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 2931 | break; |
| 2932 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2933 | } |
| 2934 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2935 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2936 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2937 | |
| 2938 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2940 | |
| 2941 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2942 | } |
| 2943 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2944 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 2945 | const sp<Connection>& connection) { |
| 2946 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2947 | return; |
| 2948 | } |
| 2949 | |
| 2950 | nsecs_t currentTime = now(); |
| 2951 | |
| 2952 | std::vector<EventEntry*> downEvents = |
| 2953 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 2954 | |
| 2955 | if (downEvents.empty()) { |
| 2956 | return; |
| 2957 | } |
| 2958 | |
| 2959 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2960 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 2961 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 2962 | #endif |
| 2963 | |
| 2964 | InputTarget target; |
| 2965 | sp<InputWindowHandle> windowHandle = |
| 2966 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 2967 | if (windowHandle != nullptr) { |
| 2968 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2969 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2970 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2971 | } |
| 2972 | target.inputChannel = connection->inputChannel; |
| 2973 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2974 | |
| 2975 | for (EventEntry* downEventEntry : downEvents) { |
| 2976 | switch (downEventEntry->type) { |
| 2977 | case EventEntry::Type::MOTION: { |
| 2978 | logOutboundMotionDetails("down - ", |
| 2979 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 2980 | break; |
| 2981 | } |
| 2982 | |
| 2983 | case EventEntry::Type::KEY: |
| 2984 | case EventEntry::Type::FOCUS: |
| 2985 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2986 | case EventEntry::Type::DEVICE_RESET: { |
| 2987 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 2988 | EventEntry::typeToString(downEventEntry->type)); |
| 2989 | break; |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 2994 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 2995 | |
| 2996 | downEventEntry->release(); |
| 2997 | } |
| 2998 | |
| 2999 | startDispatchCycleLocked(currentTime, connection); |
| 3000 | } |
| 3001 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3002 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 3003 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | ALOG_ASSERT(pointerIds.value != 0); |
| 3005 | |
| 3006 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3007 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3008 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3009 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3010 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3011 | uint32_t splitPointerCount = 0; |
| 3012 | |
| 3013 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3014 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3015 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3016 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3017 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3018 | if (pointerIds.hasBit(pointerId)) { |
| 3019 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3020 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3021 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3022 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3023 | splitPointerCount += 1; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | if (splitPointerCount != pointerIds.count()) { |
| 3028 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3029 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3030 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3031 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3032 | // in this way. |
| 3033 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3034 | "we expected there to be %d pointers. This probably means we received " |
| 3035 | "a broken sequence of pointer ids from the input device.", |
| 3036 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3037 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3038 | } |
| 3039 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3040 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3041 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3042 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3043 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3044 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3045 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3046 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3048 | if (pointerIds.hasBit(pointerId)) { |
| 3049 | if (pointerIds.count() == 1) { |
| 3050 | // The first/last pointer went down/up. |
| 3051 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3052 | ? AMOTION_EVENT_ACTION_DOWN |
| 3053 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3054 | } else { |
| 3055 | // A secondary pointer went down/up. |
| 3056 | uint32_t splitPointerIndex = 0; |
| 3057 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3058 | splitPointerIndex += 1; |
| 3059 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3060 | action = maskedAction | |
| 3061 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3062 | } |
| 3063 | } else { |
| 3064 | // An unrelated pointer changed. |
| 3065 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3066 | } |
| 3067 | } |
| 3068 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3069 | int32_t newId = mIdGenerator.nextId(); |
| 3070 | if (ATRACE_ENABLED()) { |
| 3071 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3072 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3073 | originalMotionEntry.id, newId); |
| 3074 | ATRACE_NAME(message.c_str()); |
| 3075 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3076 | MotionEntry* splitMotionEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3077 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 3078 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 3079 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3080 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 3081 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 3082 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 3083 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 3084 | originalMotionEntry.xCursorPosition, |
| 3085 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3086 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3087 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3088 | if (originalMotionEntry.injectionState) { |
| 3089 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3090 | splitMotionEntry->injectionState->refCount += 1; |
| 3091 | } |
| 3092 | |
| 3093 | return splitMotionEntry; |
| 3094 | } |
| 3095 | |
| 3096 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3097 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3098 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 | #endif |
| 3100 | |
| 3101 | bool needWake; |
| 3102 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3103 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3104 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3105 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3106 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | needWake = enqueueInboundEventLocked(newEntry); |
| 3108 | } // release lock |
| 3109 | |
| 3110 | if (needWake) { |
| 3111 | mLooper->wake(); |
| 3112 | } |
| 3113 | } |
| 3114 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3115 | /** |
| 3116 | * If one of the meta shortcuts is detected, process them here: |
| 3117 | * Meta + Backspace -> generate BACK |
| 3118 | * Meta + Enter -> generate HOME |
| 3119 | * This will potentially overwrite keyCode and metaState. |
| 3120 | */ |
| 3121 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3122 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3123 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3124 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3125 | if (keyCode == AKEYCODE_DEL) { |
| 3126 | newKeyCode = AKEYCODE_BACK; |
| 3127 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3128 | newKeyCode = AKEYCODE_HOME; |
| 3129 | } |
| 3130 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3131 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3132 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3133 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3134 | keyCode = newKeyCode; |
| 3135 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3136 | } |
| 3137 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3138 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3139 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3140 | // even if the modifier was released between the down and the up events. |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3141 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3142 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3143 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3144 | if (replacementIt != mReplacedKeys.end()) { |
| 3145 | keyCode = replacementIt->second; |
| 3146 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3147 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3148 | } |
| 3149 | } |
| 3150 | } |
| 3151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3152 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3153 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3154 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3155 | "policyFlags=0x%x, action=0x%x, " |
| 3156 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3157 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3158 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3159 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3160 | #endif |
| 3161 | if (!validateKeyEvent(args->action)) { |
| 3162 | return; |
| 3163 | } |
| 3164 | |
| 3165 | uint32_t policyFlags = args->policyFlags; |
| 3166 | int32_t flags = args->flags; |
| 3167 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3168 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3169 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3170 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3172 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3173 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3174 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3175 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3176 | metaState |= AMETA_FUNCTION_ON; |
| 3177 | } |
| 3178 | |
| 3179 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3180 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3181 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3182 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3183 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3184 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3185 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3186 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3187 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3188 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3189 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3190 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3191 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3192 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3193 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3194 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3195 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3196 | bool needWake; |
| 3197 | { // acquire lock |
| 3198 | mLock.lock(); |
| 3199 | |
| 3200 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3201 | mLock.unlock(); |
| 3202 | |
| 3203 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3204 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3205 | return; // event was consumed by the filter |
| 3206 | } |
| 3207 | |
| 3208 | mLock.lock(); |
| 3209 | } |
| 3210 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3211 | KeyEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3212 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3213 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3214 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3215 | |
| 3216 | needWake = enqueueInboundEventLocked(newEntry); |
| 3217 | mLock.unlock(); |
| 3218 | } // release lock |
| 3219 | |
| 3220 | if (needWake) { |
| 3221 | mLooper->wake(); |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3226 | return mInputFilterEnabled; |
| 3227 | } |
| 3228 | |
| 3229 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3230 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3231 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3232 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3233 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3234 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3235 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3236 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3237 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3238 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3239 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3241 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3242 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3243 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3244 | "orientation=%f", |
| 3245 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3246 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3247 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3248 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3249 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3250 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3251 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3252 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3253 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3254 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3255 | } |
| 3256 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3257 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3258 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3259 | return; |
| 3260 | } |
| 3261 | |
| 3262 | uint32_t policyFlags = args->policyFlags; |
| 3263 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3264 | |
| 3265 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3266 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3267 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3268 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3269 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3270 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3271 | |
| 3272 | bool needWake; |
| 3273 | { // acquire lock |
| 3274 | mLock.lock(); |
| 3275 | |
| 3276 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3277 | mLock.unlock(); |
| 3278 | |
| 3279 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3280 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3281 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3282 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3283 | args->metaState, args->buttonState, args->classification, transform, |
| 3284 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
| 3285 | args->yCursorPosition, args->downTime, args->eventTime, |
| 3286 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3287 | |
| 3288 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3289 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3290 | return; // event was consumed by the filter |
| 3291 | } |
| 3292 | |
| 3293 | mLock.lock(); |
| 3294 | } |
| 3295 | |
| 3296 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3297 | MotionEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3298 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3299 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3300 | args->flags, args->metaState, args->buttonState, |
| 3301 | args->classification, args->edgeFlags, args->xPrecision, |
| 3302 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3303 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3304 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | |
| 3306 | needWake = enqueueInboundEventLocked(newEntry); |
| 3307 | mLock.unlock(); |
| 3308 | } // release lock |
| 3309 | |
| 3310 | if (needWake) { |
| 3311 | mLooper->wake(); |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3316 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3320 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3321 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3322 | "switchMask=0x%08x", |
| 3323 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3324 | #endif |
| 3325 | |
| 3326 | uint32_t policyFlags = args->policyFlags; |
| 3327 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3328 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3332 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3333 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3334 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3335 | #endif |
| 3336 | |
| 3337 | bool needWake; |
| 3338 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3339 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3340 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3341 | DeviceResetEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3342 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | needWake = enqueueInboundEventLocked(newEntry); |
| 3344 | } // release lock |
| 3345 | |
| 3346 | if (needWake) { |
| 3347 | mLooper->wake(); |
| 3348 | } |
| 3349 | } |
| 3350 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3351 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 3352 | int32_t injectorUid, int32_t syncMode, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3353 | std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3354 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3355 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3356 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3357 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3359 | 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] | 3360 | |
| 3361 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3362 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3363 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3364 | } |
| 3365 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3366 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3367 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3368 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3369 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3370 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3371 | if (!validateKeyEvent(action)) { |
| 3372 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3373 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3374 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3375 | int32_t flags = incomingKey.getFlags(); |
| 3376 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3377 | int32_t metaState = incomingKey.getMetaState(); |
| 3378 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3379 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3380 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3381 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3382 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3383 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3384 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3385 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3386 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3387 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3388 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3389 | |
| 3390 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3391 | android::base::Timer t; |
| 3392 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3393 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3394 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3395 | std::to_string(t.duration().count()).c_str()); |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | mLock.lock(); |
| 3400 | KeyEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3401 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3402 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3403 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, |
| 3404 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3405 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3406 | injectedEntries.push(injectedEntry); |
| 3407 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3408 | } |
| 3409 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3410 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3411 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3412 | int32_t action = motionEvent->getAction(); |
| 3413 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3414 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3415 | int32_t actionButton = motionEvent->getActionButton(); |
| 3416 | int32_t displayId = motionEvent->getDisplayId(); |
| 3417 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
| 3418 | return INPUT_EVENT_INJECTION_FAILED; |
| 3419 | } |
| 3420 | |
| 3421 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3422 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3423 | android::base::Timer t; |
| 3424 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3425 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3426 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3427 | std::to_string(t.duration().count()).c_str()); |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | mLock.lock(); |
| 3432 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3433 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3434 | MotionEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3435 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3436 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3437 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3438 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3439 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3440 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3441 | motionEvent->getRawXCursorPosition(), |
| 3442 | motionEvent->getRawYCursorPosition(), |
| 3443 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3444 | pointerProperties, samplePointerCoords, |
| 3445 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3446 | injectedEntries.push(injectedEntry); |
| 3447 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3448 | sampleEventTimes += 1; |
| 3449 | samplePointerCoords += pointerCount; |
| 3450 | MotionEntry* nextInjectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3451 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3452 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3453 | motionEvent->getDisplayId(), policyFlags, action, |
| 3454 | actionButton, motionEvent->getFlags(), |
| 3455 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3456 | motionEvent->getClassification(), |
| 3457 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3458 | motionEvent->getYPrecision(), |
| 3459 | motionEvent->getRawXCursorPosition(), |
| 3460 | motionEvent->getRawYCursorPosition(), |
| 3461 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3462 | pointerProperties, samplePointerCoords, |
| 3463 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3464 | injectedEntries.push(nextInjectedEntry); |
| 3465 | } |
| 3466 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3467 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3468 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3469 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3470 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3471 | return INPUT_EVENT_INJECTION_FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
| 3475 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3476 | injectionState->injectionIsAsync = true; |
| 3477 | } |
| 3478 | |
| 3479 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3480 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3481 | |
| 3482 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3483 | while (!injectedEntries.empty()) { |
| 3484 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3485 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | mLock.unlock(); |
| 3489 | |
| 3490 | if (needWake) { |
| 3491 | mLooper->wake(); |
| 3492 | } |
| 3493 | |
| 3494 | int32_t injectionResult; |
| 3495 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3496 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3497 | |
| 3498 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3499 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3500 | } else { |
| 3501 | for (;;) { |
| 3502 | injectionResult = injectionState->injectionResult; |
| 3503 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3504 | break; |
| 3505 | } |
| 3506 | |
| 3507 | nsecs_t remainingTimeout = endTime - now(); |
| 3508 | if (remainingTimeout <= 0) { |
| 3509 | #if DEBUG_INJECTION |
| 3510 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3511 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | #endif |
| 3513 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3514 | break; |
| 3515 | } |
| 3516 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3517 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3518 | } |
| 3519 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3520 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && |
| 3521 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3523 | #if DEBUG_INJECTION |
| 3524 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3525 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | #endif |
| 3527 | nsecs_t remainingTimeout = endTime - now(); |
| 3528 | if (remainingTimeout <= 0) { |
| 3529 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3530 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3531 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3532 | #endif |
| 3533 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3534 | break; |
| 3535 | } |
| 3536 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3537 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3538 | } |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | injectionState->release(); |
| 3543 | } // release lock |
| 3544 | |
| 3545 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3546 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3547 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | #endif |
| 3549 | |
| 3550 | return injectionResult; |
| 3551 | } |
| 3552 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3553 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3554 | std::array<uint8_t, 32> calculatedHmac; |
| 3555 | std::unique_ptr<VerifiedInputEvent> result; |
| 3556 | switch (event.getType()) { |
| 3557 | case AINPUT_EVENT_TYPE_KEY: { |
| 3558 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3559 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3560 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3561 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3562 | break; |
| 3563 | } |
| 3564 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3565 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3566 | VerifiedMotionEvent verifiedMotionEvent = |
| 3567 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3568 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3569 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3570 | break; |
| 3571 | } |
| 3572 | default: { |
| 3573 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3574 | return nullptr; |
| 3575 | } |
| 3576 | } |
| 3577 | if (calculatedHmac == INVALID_HMAC) { |
| 3578 | return nullptr; |
| 3579 | } |
| 3580 | if (calculatedHmac != event.getHmac()) { |
| 3581 | return nullptr; |
| 3582 | } |
| 3583 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3584 | } |
| 3585 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3587 | return injectorUid == 0 || |
| 3588 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3589 | } |
| 3590 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3591 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 | InjectionState* injectionState = entry->injectionState; |
| 3593 | if (injectionState) { |
| 3594 | #if DEBUG_INJECTION |
| 3595 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3596 | "injectorPid=%d, injectorUid=%d", |
| 3597 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | #endif |
| 3599 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3600 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3601 | // Log the outcome since the injector did not wait for the injection result. |
| 3602 | switch (injectionResult) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3603 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3604 | ALOGV("Asynchronous input event injection succeeded."); |
| 3605 | break; |
| 3606 | case INPUT_EVENT_INJECTION_FAILED: |
| 3607 | ALOGW("Asynchronous input event injection failed."); |
| 3608 | break; |
| 3609 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3610 | ALOGW("Asynchronous input event injection permission denied."); |
| 3611 | break; |
| 3612 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3613 | ALOGW("Asynchronous input event injection timed out."); |
| 3614 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3619 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3623 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 | InjectionState* injectionState = entry->injectionState; |
| 3625 | if (injectionState) { |
| 3626 | injectionState->pendingForegroundDispatches += 1; |
| 3627 | } |
| 3628 | } |
| 3629 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3630 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3631 | InjectionState* injectionState = entry->injectionState; |
| 3632 | if (injectionState) { |
| 3633 | injectionState->pendingForegroundDispatches -= 1; |
| 3634 | |
| 3635 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3636 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | } |
| 3640 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3641 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3642 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3643 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 3644 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 3645 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3646 | } |
| 3647 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3648 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3649 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3650 | if (windowHandleToken == nullptr) { |
| 3651 | return nullptr; |
| 3652 | } |
| 3653 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3654 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3655 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3656 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3657 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3658 | return windowHandle; |
| 3659 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3660 | } |
| 3661 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3662 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3663 | } |
| 3664 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3665 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 3666 | int displayId) const { |
| 3667 | if (windowHandleToken == nullptr) { |
| 3668 | return nullptr; |
| 3669 | } |
| 3670 | |
| 3671 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3672 | if (windowHandle->getToken() == windowHandleToken) { |
| 3673 | return windowHandle; |
| 3674 | } |
| 3675 | } |
| 3676 | return nullptr; |
| 3677 | } |
| 3678 | |
| 3679 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 3680 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3681 | return getWindowHandleLocked(focusedToken, displayId); |
| 3682 | } |
| 3683 | |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3684 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 3685 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3686 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3687 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3688 | if (handle->getId() == windowHandle->getId() && |
| 3689 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3690 | if (windowHandle->getInfo()->displayId != it.first) { |
| 3691 | ALOGE("Found window %s in display %" PRId32 |
| 3692 | ", but it should belong to display %" PRId32, |
| 3693 | windowHandle->getName().c_str(), it.first, |
| 3694 | windowHandle->getInfo()->displayId); |
| 3695 | } |
| 3696 | return true; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3697 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3698 | } |
| 3699 | } |
| 3700 | return false; |
| 3701 | } |
| 3702 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3703 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 3704 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 3705 | const bool noInputChannel = |
| 3706 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3707 | if (connection != nullptr && noInputChannel) { |
| 3708 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 3709 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 3710 | return false; |
| 3711 | } |
| 3712 | |
| 3713 | if (connection == nullptr) { |
| 3714 | if (!noInputChannel) { |
| 3715 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 3716 | } |
| 3717 | return false; |
| 3718 | } |
| 3719 | if (!connection->responsive) { |
| 3720 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 3721 | return false; |
| 3722 | } |
| 3723 | return true; |
| 3724 | } |
| 3725 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3726 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 3727 | const sp<IBinder>& token) const { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3728 | size_t count = mInputChannelsByToken.count(token); |
| 3729 | if (count == 0) { |
| 3730 | return nullptr; |
| 3731 | } |
| 3732 | return mInputChannelsByToken.at(token); |
| 3733 | } |
| 3734 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3735 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3736 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3737 | if (inputWindowHandles.empty()) { |
| 3738 | // Remove all handles on a display if there are no windows left. |
| 3739 | mWindowHandlesByDisplay.erase(displayId); |
| 3740 | return; |
| 3741 | } |
| 3742 | |
| 3743 | // Since we compare the pointer of input window handles across window updates, we need |
| 3744 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3745 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3746 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3747 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3748 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3752 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3753 | if (!handle->updateInfo()) { |
| 3754 | // handle no longer valid |
| 3755 | continue; |
| 3756 | } |
| 3757 | |
| 3758 | const InputWindowInfo* info = handle->getInfo(); |
| 3759 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3760 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3761 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3762 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3763 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 3764 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3765 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3766 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3767 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3768 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3769 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | if (info->displayId != displayId) { |
| 3773 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3774 | handle->getName().c_str(), displayId, info->displayId); |
| 3775 | continue; |
| 3776 | } |
| 3777 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3778 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 3779 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3780 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3781 | oldHandle->updateFrom(handle); |
| 3782 | newHandles.push_back(oldHandle); |
| 3783 | } else { |
| 3784 | newHandles.push_back(handle); |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | // Insert or replace |
| 3789 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3790 | } |
| 3791 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3792 | void InputDispatcher::setInputWindows( |
| 3793 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3794 | { // acquire lock |
| 3795 | std::scoped_lock _l(mLock); |
| 3796 | for (auto const& i : handlesPerDisplay) { |
| 3797 | setInputWindowsLocked(i.second, i.first); |
| 3798 | } |
| 3799 | } |
| 3800 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3801 | mLooper->wake(); |
| 3802 | } |
| 3803 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3804 | /** |
| 3805 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3806 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3807 | * If set an empty list, remove all handles from the specific display. |
| 3808 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3809 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3810 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3811 | void InputDispatcher::setInputWindowsLocked( |
| 3812 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3813 | if (DEBUG_FOCUS) { |
| 3814 | std::string windowList; |
| 3815 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3816 | windowList += iwh->getName() + " "; |
| 3817 | } |
| 3818 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3819 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3820 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3821 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 3822 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 3823 | const bool noInputWindow = |
| 3824 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3825 | if (noInputWindow && window->getToken() != nullptr) { |
| 3826 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 3827 | window->getName().c_str()); |
| 3828 | window->releaseChannel(); |
| 3829 | } |
| 3830 | } |
| 3831 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3832 | // Copy old handles for release if they are no longer present. |
| 3833 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3835 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3836 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3837 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3838 | if (mLastHoverWindowHandle && |
| 3839 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 3840 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3841 | mLastHoverWindowHandle = nullptr; |
| 3842 | } |
| 3843 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3844 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3845 | if (focusedToken) { |
| 3846 | FocusResult result = checkTokenFocusableLocked(focusedToken, displayId); |
| 3847 | if (result != FocusResult::OK) { |
| 3848 | onFocusChangedLocked(focusedToken, nullptr, displayId, typeToString(result)); |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | std::optional<FocusRequest> focusRequest = |
| 3853 | getOptionalValueByKey(mPendingFocusRequests, displayId); |
| 3854 | if (focusRequest) { |
| 3855 | // If the window from the pending request is now visible, provide it focus. |
| 3856 | FocusResult result = handleFocusRequestLocked(*focusRequest); |
| 3857 | if (result != FocusResult::NOT_VISIBLE) { |
| 3858 | // Drop the request if we were able to change the focus or we cannot change |
| 3859 | // it for another reason. |
| 3860 | mPendingFocusRequests.erase(displayId); |
| 3861 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3862 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3863 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3864 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 3865 | mTouchStatesByDisplay.find(displayId); |
| 3866 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 3867 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3868 | for (size_t i = 0; i < state.windows.size();) { |
| 3869 | TouchedWindow& touchedWindow = state.windows[i]; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3870 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3871 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3872 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 3873 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3874 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3875 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3876 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 3877 | if (touchedInputChannel != nullptr) { |
| 3878 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3879 | "touched window was removed"); |
| 3880 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3881 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3882 | state.windows.erase(state.windows.begin() + i); |
| 3883 | } else { |
| 3884 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | } |
| 3886 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3887 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3888 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3889 | // Release information for windows that are no longer present. |
| 3890 | // This ensures that unused input channels are released promptly. |
| 3891 | // Otherwise, they might stick around until the window handle is destroyed |
| 3892 | // which might not happen until the next GC. |
| 3893 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3894 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3895 | if (DEBUG_FOCUS) { |
| 3896 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3897 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3898 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3899 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3904 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3905 | if (DEBUG_FOCUS) { |
| 3906 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 3907 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 3908 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3909 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3910 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3911 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3912 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3913 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3914 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3915 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 3916 | 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] | 3917 | } |
| 3918 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3919 | // Set the new application handle. |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3920 | if (inputApplicationHandle != nullptr) { |
| 3921 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 3922 | } else { |
| 3923 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 3924 | } |
| 3925 | |
| 3926 | // No matter what the old focused application was, stop waiting on it because it is |
| 3927 | // no longer focused. |
| 3928 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | } // release lock |
| 3930 | |
| 3931 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3932 | mLooper->wake(); |
| 3933 | } |
| 3934 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3935 | /** |
| 3936 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 3937 | * the display not specified. |
| 3938 | * |
| 3939 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 3940 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 3941 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 3942 | * display. The display-specified events won't be affected. |
| 3943 | */ |
| 3944 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3945 | if (DEBUG_FOCUS) { |
| 3946 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 3947 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3948 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3949 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3950 | |
| 3951 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3952 | sp<IBinder> oldFocusedWindowToken = |
| 3953 | getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); |
| 3954 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3955 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3956 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3957 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3958 | CancelationOptions |
| 3959 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3960 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3961 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3962 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 3963 | } |
| 3964 | } |
| 3965 | mFocusedDisplayId = displayId; |
| 3966 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 3967 | // Find new focused window and validate |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3968 | sp<IBinder> newFocusedWindowToken = |
| 3969 | getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3970 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3971 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3972 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3973 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3974 | if (!mFocusedWindowTokenByDisplay.empty()) { |
| 3975 | ALOGE("But another display has a focused window\n%s", |
| 3976 | dumpFocusedWindowsLocked().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3977 | } |
| 3978 | } |
| 3979 | } |
| 3980 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3981 | if (DEBUG_FOCUS) { |
| 3982 | logDispatchStateLocked(); |
| 3983 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3984 | } // release lock |
| 3985 | |
| 3986 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3987 | mLooper->wake(); |
| 3988 | } |
| 3989 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3991 | if (DEBUG_FOCUS) { |
| 3992 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3993 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3994 | |
| 3995 | bool changed; |
| 3996 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3997 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3998 | |
| 3999 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4000 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4001 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | if (mDispatchEnabled && !enabled) { |
| 4005 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4006 | } |
| 4007 | |
| 4008 | mDispatchEnabled = enabled; |
| 4009 | mDispatchFrozen = frozen; |
| 4010 | changed = true; |
| 4011 | } else { |
| 4012 | changed = false; |
| 4013 | } |
| 4014 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4015 | if (DEBUG_FOCUS) { |
| 4016 | logDispatchStateLocked(); |
| 4017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4018 | } // release lock |
| 4019 | |
| 4020 | if (changed) { |
| 4021 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4022 | mLooper->wake(); |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4027 | if (DEBUG_FOCUS) { |
| 4028 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4029 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4030 | |
| 4031 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4032 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4033 | |
| 4034 | if (mInputFilterEnabled == enabled) { |
| 4035 | return; |
| 4036 | } |
| 4037 | |
| 4038 | mInputFilterEnabled = enabled; |
| 4039 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4040 | } // release lock |
| 4041 | |
| 4042 | // Wake up poll loop since there might be work to do to drop everything. |
| 4043 | mLooper->wake(); |
| 4044 | } |
| 4045 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4046 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4047 | std::scoped_lock lock(mLock); |
| 4048 | mInTouchMode = inTouchMode; |
| 4049 | } |
| 4050 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4051 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 4052 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4053 | if (DEBUG_FOCUS) { |
| 4054 | ALOGD("Trivial transfer to same window."); |
| 4055 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4056 | return true; |
| 4057 | } |
| 4058 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4060 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4062 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4063 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4064 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4065 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 | return false; |
| 4067 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4068 | if (DEBUG_FOCUS) { |
| 4069 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4070 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4071 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4072 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4073 | if (DEBUG_FOCUS) { |
| 4074 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4075 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4076 | return false; |
| 4077 | } |
| 4078 | |
| 4079 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4080 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4081 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4082 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4083 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4084 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4085 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4086 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4087 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4088 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4090 | int32_t newTargetFlags = oldTargetFlags & |
| 4091 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4092 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4093 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4094 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4095 | found = true; |
| 4096 | goto Found; |
| 4097 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4098 | } |
| 4099 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4100 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4101 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4102 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4103 | if (DEBUG_FOCUS) { |
| 4104 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4105 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 | return false; |
| 4107 | } |
| 4108 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4109 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4110 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4111 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4112 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4113 | CancelationOptions |
| 4114 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4115 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4116 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4117 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4118 | } |
| 4119 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4120 | if (DEBUG_FOCUS) { |
| 4121 | logDispatchStateLocked(); |
| 4122 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | } // release lock |
| 4124 | |
| 4125 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4126 | mLooper->wake(); |
| 4127 | return true; |
| 4128 | } |
| 4129 | |
| 4130 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4131 | if (DEBUG_FOCUS) { |
| 4132 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4133 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4134 | |
| 4135 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4136 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4137 | |
| 4138 | resetKeyRepeatLocked(); |
| 4139 | releasePendingEventLocked(); |
| 4140 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4141 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4142 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4143 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4144 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4146 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4150 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4151 | dumpDispatchStateLocked(dump); |
| 4152 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4153 | std::istringstream stream(dump); |
| 4154 | std::string line; |
| 4155 | |
| 4156 | while (std::getline(stream, line, '\n')) { |
| 4157 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4158 | } |
| 4159 | } |
| 4160 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4161 | std::string InputDispatcher::dumpFocusedWindowsLocked() { |
| 4162 | if (mFocusedWindowTokenByDisplay.empty()) { |
| 4163 | return INDENT "FocusedWindows: <none>\n"; |
| 4164 | } |
| 4165 | |
| 4166 | std::string dump; |
| 4167 | dump += INDENT "FocusedWindows:\n"; |
| 4168 | for (auto& it : mFocusedWindowTokenByDisplay) { |
| 4169 | const int32_t displayId = it.first; |
| 4170 | const sp<InputWindowHandle> windowHandle = getFocusedWindowHandleLocked(displayId); |
| 4171 | if (windowHandle) { |
| 4172 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4173 | windowHandle->getName().c_str()); |
| 4174 | } else { |
| 4175 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
| 4176 | " has focused token without a window'\n", |
| 4177 | displayId); |
| 4178 | } |
| 4179 | } |
| 4180 | return dump; |
| 4181 | } |
| 4182 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4183 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4184 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4185 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4186 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4187 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4188 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4189 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4190 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4191 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4192 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4193 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4194 | const std::chrono::duration timeout = |
| 4195 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4196 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4197 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4198 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4199 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4200 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4201 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4203 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4204 | dump += dumpFocusedWindowsLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4205 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4206 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4207 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4208 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4209 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4210 | 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] | 4211 | state.displayId, toString(state.down), toString(state.split), |
| 4212 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4213 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4214 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4215 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4216 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4217 | dump += StringPrintf(INDENT4 |
| 4218 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4219 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4220 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4221 | } |
| 4222 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4223 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4224 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4225 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4226 | dump += INDENT3 "Portal windows:\n"; |
| 4227 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4228 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4229 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4230 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4231 | } |
| 4232 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4233 | } |
| 4234 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4235 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4236 | } |
| 4237 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4238 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4239 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4240 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4241 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4242 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4243 | dump += INDENT2 "Windows:\n"; |
| 4244 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4245 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4246 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4247 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4248 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4249 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
| 4250 | "hasWallpaper=%s, visible=%s, " |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4251 | "flags=%s, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4252 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4253 | "applicationInfo=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4254 | "touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4255 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4256 | windowInfo->portalToDisplayId, |
| 4257 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4258 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4259 | toString(windowInfo->hasWallpaper), |
| 4260 | toString(windowInfo->visible), |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4261 | windowInfo->flags.string().c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4262 | static_cast<int32_t>(windowInfo->type), |
| 4263 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4264 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4265 | windowInfo->globalScaleFactor, |
| 4266 | windowInfo->applicationInfo.name.c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4267 | dumpRegion(dump, windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4268 | dump += StringPrintf(", inputFeatures=%s", |
| 4269 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4270 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
| 4271 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4272 | windowInfo->ownerPid, windowInfo->ownerUid, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4273 | millis(windowInfo->dispatchingTimeout)); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4274 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4275 | } |
| 4276 | } else { |
| 4277 | dump += INDENT2 "Windows: <none>\n"; |
| 4278 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4279 | } |
| 4280 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4281 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | } |
| 4283 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4284 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4285 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4286 | const std::vector<Monitor>& monitors = it.second; |
| 4287 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4288 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4289 | } |
| 4290 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4291 | const std::vector<Monitor>& monitors = it.second; |
| 4292 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4293 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4294 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4296 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | nsecs_t currentTime = now(); |
| 4300 | |
| 4301 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4302 | if (!mRecentQueue.empty()) { |
| 4303 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4304 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4305 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4306 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4307 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | } |
| 4309 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4310 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | // Dump event currently being dispatched. |
| 4314 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4315 | dump += INDENT "PendingEvent:\n"; |
| 4316 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | mPendingEvent->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4318 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4319 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4321 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4325 | if (!mInboundQueue.empty()) { |
| 4326 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4327 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4328 | dump += INDENT2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | entry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4330 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 | } |
| 4332 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4333 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | } |
| 4335 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4336 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4337 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4338 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4339 | const KeyReplacement& replacement = pair.first; |
| 4340 | int32_t newKeyCode = pair.second; |
| 4341 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4342 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4343 | } |
| 4344 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4345 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4346 | } |
| 4347 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4348 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4349 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4350 | for (const auto& pair : mConnectionsByFd) { |
| 4351 | const sp<Connection>& connection = pair.second; |
| 4352 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4353 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4354 | pair.first, connection->getInputChannelName().c_str(), |
| 4355 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4356 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4358 | if (!connection->outboundQueue.empty()) { |
| 4359 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4360 | connection->outboundQueue.size()); |
| 4361 | for (DispatchEntry* entry : connection->outboundQueue) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | dump.append(INDENT4); |
| 4363 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4364 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 |
| 4365 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4366 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4367 | ns2ms(currentTime - entry->eventEntry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | } |
| 4369 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4370 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4371 | } |
| 4372 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4373 | if (!connection->waitQueue.empty()) { |
| 4374 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4375 | connection->waitQueue.size()); |
| 4376 | for (DispatchEntry* entry : connection->waitQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4377 | dump += INDENT4; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | entry->eventEntry->appendDescription(dump); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4379 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4380 | "age=%" PRId64 "ms, wait=%" PRId64 "ms seq=%" PRIu32 "\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4381 | entry->targetFlags, entry->resolvedAction, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4382 | ns2ms(currentTime - entry->eventEntry->eventTime), |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4383 | ns2ms(currentTime - entry->deliveryTime), entry->seq); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | } |
| 4385 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4386 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | } |
| 4388 | } |
| 4389 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4390 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4394 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 4395 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4397 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4398 | } |
| 4399 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4400 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4401 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 4402 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 4403 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | } |
| 4405 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4406 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4407 | const size_t numMonitors = monitors.size(); |
| 4408 | for (size_t i = 0; i < numMonitors; i++) { |
| 4409 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4410 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4411 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4412 | dump += "\n"; |
| 4413 | } |
| 4414 | } |
| 4415 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4416 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel( |
| 4417 | const std::string& name) { |
| 4418 | #if DEBUG_CHANNEL_CREATION |
| 4419 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4420 | #endif |
| 4421 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4422 | std::shared_ptr<InputChannel> serverChannel; |
| 4423 | std::unique_ptr<InputChannel> clientChannel; |
| 4424 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 4425 | |
| 4426 | if (result) { |
| 4427 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 4428 | } |
| 4429 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4430 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4431 | std::scoped_lock _l(mLock); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4432 | sp<Connection> connection = new Connection(serverChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4433 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4434 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4435 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4436 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4437 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4438 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4439 | } // release lock |
| 4440 | |
| 4441 | // Wake the looper because some connections have changed. |
| 4442 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4443 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4444 | } |
| 4445 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4446 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor( |
| 4447 | int32_t displayId, bool isGestureMonitor, const std::string& name) { |
| 4448 | std::shared_ptr<InputChannel> serverChannel; |
| 4449 | std::unique_ptr<InputChannel> clientChannel; |
| 4450 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 4451 | if (result) { |
| 4452 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 4453 | } |
| 4454 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4455 | { // acquire lock |
| 4456 | std::scoped_lock _l(mLock); |
| 4457 | |
| 4458 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4459 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 4460 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4463 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4464 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4465 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4466 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4467 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4468 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4469 | auto& monitorsByDisplay = |
| 4470 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4471 | monitorsByDisplay[displayId].emplace_back(serverChannel); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4472 | |
| 4473 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4474 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4475 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4476 | // Wake the looper because some connections have changed. |
| 4477 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4478 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4481 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4482 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4483 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4484 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4485 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4486 | if (status) { |
| 4487 | return status; |
| 4488 | } |
| 4489 | } // release lock |
| 4490 | |
| 4491 | // Wake the poll loop because removing the connection may have changed the current |
| 4492 | // synchronization state. |
| 4493 | mLooper->wake(); |
| 4494 | return OK; |
| 4495 | } |
| 4496 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame^] | 4497 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 4498 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4499 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4500 | if (connection == nullptr) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4501 | ALOGW("Attempted to unregister already unregistered input channel"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4502 | return BAD_VALUE; |
| 4503 | } |
| 4504 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4505 | removeConnectionLocked(connection); |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4506 | mInputChannelsByToken.erase(connectionToken); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4507 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4508 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4509 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4510 | } |
| 4511 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4512 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4513 | |
| 4514 | nsecs_t currentTime = now(); |
| 4515 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4516 | |
| 4517 | connection->status = Connection::STATUS_ZOMBIE; |
| 4518 | return OK; |
| 4519 | } |
| 4520 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4521 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 4522 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 4523 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4526 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4527 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4528 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4529 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4530 | std::vector<Monitor>& monitors = it->second; |
| 4531 | const size_t numMonitors = monitors.size(); |
| 4532 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4533 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4534 | monitors.erase(monitors.begin() + i); |
| 4535 | break; |
| 4536 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4537 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4538 | if (monitors.empty()) { |
| 4539 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4540 | } else { |
| 4541 | ++it; |
| 4542 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4543 | } |
| 4544 | } |
| 4545 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4546 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4547 | { // acquire lock |
| 4548 | std::scoped_lock _l(mLock); |
| 4549 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4550 | |
| 4551 | if (!foundDisplayId) { |
| 4552 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4553 | return BAD_VALUE; |
| 4554 | } |
| 4555 | int32_t displayId = foundDisplayId.value(); |
| 4556 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4557 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4558 | mTouchStatesByDisplay.find(displayId); |
| 4559 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4560 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4561 | return BAD_VALUE; |
| 4562 | } |
| 4563 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4564 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4565 | std::optional<int32_t> foundDeviceId; |
| 4566 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4567 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4568 | foundDeviceId = state.deviceId; |
| 4569 | } |
| 4570 | } |
| 4571 | if (!foundDeviceId || !state.down) { |
| 4572 | 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] | 4573 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4574 | return BAD_VALUE; |
| 4575 | } |
| 4576 | int32_t deviceId = foundDeviceId.value(); |
| 4577 | |
| 4578 | // Send cancel events to all the input channels we're stealing from. |
| 4579 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4580 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4581 | options.deviceId = deviceId; |
| 4582 | options.displayId = displayId; |
| 4583 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4584 | std::shared_ptr<InputChannel> channel = |
| 4585 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4586 | if (channel != nullptr) { |
| 4587 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4588 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4589 | } |
| 4590 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4591 | state.filterNonMonitors(); |
| 4592 | } |
| 4593 | return OK; |
| 4594 | } |
| 4595 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4596 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4597 | const sp<IBinder>& token) { |
| 4598 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4599 | const std::vector<Monitor>& monitors = it.second; |
| 4600 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4601 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4602 | return it.first; |
| 4603 | } |
| 4604 | } |
| 4605 | } |
| 4606 | return std::nullopt; |
| 4607 | } |
| 4608 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4609 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4610 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4611 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4612 | } |
| 4613 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4614 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4615 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4616 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4617 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4618 | } |
| 4619 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4620 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4621 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4622 | } |
| 4623 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4624 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4625 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4626 | removeByValue(mConnectionsByFd, connection); |
| 4627 | } |
| 4628 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4629 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4630 | const sp<Connection>& connection, uint32_t seq, |
| 4631 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4632 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4633 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4634 | commandEntry->connection = connection; |
| 4635 | commandEntry->eventTime = currentTime; |
| 4636 | commandEntry->seq = seq; |
| 4637 | commandEntry->handled = handled; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4638 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4641 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4642 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4644 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4645 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4646 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4647 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4648 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4649 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4650 | } |
| 4651 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4652 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 4653 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4654 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4655 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4656 | commandEntry->oldToken = oldToken; |
| 4657 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4658 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4659 | } |
| 4660 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4661 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 4662 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 4663 | // is already healthy again. Don't raise ANR in this situation |
| 4664 | if (connection->waitQueue.empty()) { |
| 4665 | ALOGI("Not raising ANR because the connection %s has recovered", |
| 4666 | connection->inputChannel->getName().c_str()); |
| 4667 | return; |
| 4668 | } |
| 4669 | /** |
| 4670 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 4671 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 4672 | * has changed. This could cause newer entries to time out before the already dispatched |
| 4673 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 4674 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 4675 | * most useful. |
| 4676 | */ |
| 4677 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
| 4678 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 4679 | std::string reason = |
| 4680 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
| 4681 | connection->inputChannel->getName().c_str(), |
| 4682 | ns2ms(currentWait), |
| 4683 | oldestEntry->eventEntry->getDescription().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4684 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4685 | updateLastAnrStateLocked(getWindowHandleLocked(connection->inputChannel->getConnectionToken()), |
| 4686 | reason); |
| 4687 | |
| 4688 | std::unique_ptr<CommandEntry> commandEntry = |
| 4689 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4690 | commandEntry->inputApplicationHandle = nullptr; |
| 4691 | commandEntry->inputChannel = connection->inputChannel; |
| 4692 | commandEntry->reason = std::move(reason); |
| 4693 | postCommandLocked(std::move(commandEntry)); |
| 4694 | } |
| 4695 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4696 | void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4697 | std::string reason = android::base::StringPrintf("%s does not have a focused window", |
| 4698 | application->getName().c_str()); |
| 4699 | |
| 4700 | updateLastAnrStateLocked(application, reason); |
| 4701 | |
| 4702 | std::unique_ptr<CommandEntry> commandEntry = |
| 4703 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4704 | commandEntry->inputApplicationHandle = application; |
| 4705 | commandEntry->inputChannel = nullptr; |
| 4706 | commandEntry->reason = std::move(reason); |
| 4707 | postCommandLocked(std::move(commandEntry)); |
| 4708 | } |
| 4709 | |
| 4710 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 4711 | const std::string& reason) { |
| 4712 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 4713 | updateLastAnrStateLocked(windowLabel, reason); |
| 4714 | } |
| 4715 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4716 | void InputDispatcher::updateLastAnrStateLocked( |
| 4717 | const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4718 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); |
| 4719 | updateLastAnrStateLocked(windowLabel, reason); |
| 4720 | } |
| 4721 | |
| 4722 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 4723 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4724 | // 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] | 4725 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4726 | struct tm tm; |
| 4727 | localtime_r(&t, &tm); |
| 4728 | char timestr[64]; |
| 4729 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4730 | mLastAnrState.clear(); |
| 4731 | mLastAnrState += INDENT "ANR:\n"; |
| 4732 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4733 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 4734 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4735 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | } |
| 4737 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4738 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4739 | mLock.unlock(); |
| 4740 | |
| 4741 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4742 | |
| 4743 | mLock.lock(); |
| 4744 | } |
| 4745 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4746 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4747 | sp<Connection> connection = commandEntry->connection; |
| 4748 | |
| 4749 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4750 | mLock.unlock(); |
| 4751 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4752 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4753 | |
| 4754 | mLock.lock(); |
| 4755 | } |
| 4756 | } |
| 4757 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4758 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4759 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4760 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4761 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4762 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4763 | mLock.lock(); |
| 4764 | } |
| 4765 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4766 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4767 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4768 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4769 | mLock.unlock(); |
| 4770 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4771 | const std::chrono::nanoseconds timeoutExtension = |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4772 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | |
| 4774 | mLock.lock(); |
| 4775 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4776 | if (timeoutExtension > 0s) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4777 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); |
| 4778 | } else { |
| 4779 | // stop waking up for events in this connection, it is already not responding |
| 4780 | sp<Connection> connection = getConnectionLocked(token); |
| 4781 | if (connection == nullptr) { |
| 4782 | return; |
| 4783 | } |
| 4784 | cancelEventsForAnrLocked(connection); |
| 4785 | } |
| 4786 | } |
| 4787 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4788 | void InputDispatcher::extendAnrTimeoutsLocked( |
| 4789 | const std::shared_ptr<InputApplicationHandle>& application, |
| 4790 | const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4791 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 4792 | if (connection == nullptr) { |
| 4793 | if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) { |
| 4794 | // Maybe ANR happened because there's no focused window? |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4795 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4796 | mAwaitedFocusedApplication = application; |
| 4797 | } else { |
| 4798 | // It's also possible that the connection already disappeared. No action necessary. |
| 4799 | } |
| 4800 | return; |
| 4801 | } |
| 4802 | |
| 4803 | 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] | 4804 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4805 | |
| 4806 | connection->responsive = true; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4807 | const nsecs_t newTimeout = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4808 | for (DispatchEntry* entry : connection->waitQueue) { |
| 4809 | if (newTimeout >= entry->timeoutTime) { |
| 4810 | // Already removed old entries when connection was marked unresponsive |
| 4811 | entry->timeoutTime = newTimeout; |
| 4812 | mAnrTracker.insert(entry->timeoutTime, connectionToken); |
| 4813 | } |
| 4814 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 4818 | CommandEntry* commandEntry) { |
| 4819 | KeyEntry* entry = commandEntry->keyEntry; |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4820 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4821 | |
| 4822 | mLock.unlock(); |
| 4823 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4824 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4825 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4826 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4827 | : nullptr; |
| 4828 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4829 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4830 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4831 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4832 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4833 | |
| 4834 | mLock.lock(); |
| 4835 | |
| 4836 | if (delay < 0) { |
| 4837 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 4838 | } else if (!delay) { |
| 4839 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 4840 | } else { |
| 4841 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 4842 | entry->interceptKeyWakeupTime = now() + delay; |
| 4843 | } |
| 4844 | entry->release(); |
| 4845 | } |
| 4846 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4847 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 4848 | mLock.unlock(); |
| 4849 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 4850 | mLock.lock(); |
| 4851 | } |
| 4852 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4853 | /** |
| 4854 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 4855 | * current time. |
| 4856 | */ |
| 4857 | static bool isConnectionResponsive(const Connection& connection) { |
| 4858 | const nsecs_t currentTime = now(); |
| 4859 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 4860 | if (entry->timeoutTime < currentTime) { |
| 4861 | return false; |
| 4862 | } |
| 4863 | } |
| 4864 | return true; |
| 4865 | } |
| 4866 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4867 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4868 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4869 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4870 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4871 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4872 | |
| 4873 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4874 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4875 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4876 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4877 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4878 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4879 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4880 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4881 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 4882 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4883 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4884 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4885 | |
| 4886 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4887 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4888 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 4889 | restartEvent = |
| 4890 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4891 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4892 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 4893 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 4894 | handled); |
| 4895 | } else { |
| 4896 | restartEvent = false; |
| 4897 | } |
| 4898 | |
| 4899 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4900 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4901 | // contents of the wait queue to have been drained, so we need to double-check |
| 4902 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4903 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 4904 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 4905 | dispatchEntry = *dispatchEntryIt; |
| 4906 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4907 | mAnrTracker.erase(dispatchEntry->timeoutTime, |
| 4908 | connection->inputChannel->getConnectionToken()); |
| 4909 | if (!connection->responsive) { |
| 4910 | connection->responsive = isConnectionResponsive(*connection); |
| 4911 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4912 | traceWaitQueueLength(connection); |
| 4913 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4914 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4915 | traceOutboundQueueLength(connection); |
| 4916 | } else { |
| 4917 | releaseDispatchEntry(dispatchEntry); |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | // Start the next dispatch cycle for this connection. |
| 4922 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4926 | DispatchEntry* dispatchEntry, |
| 4927 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4928 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4929 | if (!handled) { |
| 4930 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4931 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4932 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4933 | return false; |
| 4934 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4936 | // Get the fallback key state. |
| 4937 | // Clear it out after dispatching the UP. |
| 4938 | int32_t originalKeyCode = keyEntry->keyCode; |
| 4939 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 4940 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 4941 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4942 | } |
| 4943 | |
| 4944 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 4945 | // If the application handles the original key for which we previously |
| 4946 | // generated a fallback or if the window is not a foreground window, |
| 4947 | // then cancel the associated fallback key, if any. |
| 4948 | if (fallbackKeyCode != -1) { |
| 4949 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4950 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4951 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4952 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4953 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 4954 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4956 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4957 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4958 | |
| 4959 | mLock.unlock(); |
| 4960 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4961 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4962 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4963 | |
| 4964 | mLock.lock(); |
| 4965 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4966 | // Cancel the fallback key. |
| 4967 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4968 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4969 | "application handled the original non-fallback key " |
| 4970 | "or is no longer a foreground target, " |
| 4971 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4972 | options.keyCode = fallbackKeyCode; |
| 4973 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4974 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4975 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 4976 | } |
| 4977 | } else { |
| 4978 | // If the application did not handle a non-fallback key, first check |
| 4979 | // that we are in a good state to perform unhandled key event processing |
| 4980 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4981 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4982 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4983 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4984 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4985 | "since this is not an initial down. " |
| 4986 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4987 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4988 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4989 | return false; |
| 4990 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4991 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4992 | // Dispatch the unhandled key to the policy. |
| 4993 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4994 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4995 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 4996 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4997 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4998 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4999 | |
| 5000 | mLock.unlock(); |
| 5001 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5002 | bool fallback = |
| 5003 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 5004 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5005 | |
| 5006 | mLock.lock(); |
| 5007 | |
| 5008 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5009 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5010 | return false; |
| 5011 | } |
| 5012 | |
| 5013 | // Latch the fallback keycode for this key on an initial down. |
| 5014 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5015 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5016 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5017 | fallbackKeyCode = event.getKeyCode(); |
| 5018 | } else { |
| 5019 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5020 | } |
| 5021 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5022 | } |
| 5023 | |
| 5024 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5025 | |
| 5026 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5027 | // We will continue to dispatch the key to the policy but we will no |
| 5028 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5029 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5030 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5031 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5032 | if (fallback) { |
| 5033 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5034 | "as a fallback for %d, but on the DOWN it had requested " |
| 5035 | "to send %d instead. Fallback canceled.", |
| 5036 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5037 | } else { |
| 5038 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5039 | "but on the DOWN it had requested to send %d. " |
| 5040 | "Fallback canceled.", |
| 5041 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5042 | } |
| 5043 | #endif |
| 5044 | |
| 5045 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5046 | "canceling fallback, policy no longer desires it"); |
| 5047 | options.keyCode = fallbackKeyCode; |
| 5048 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5049 | |
| 5050 | fallback = false; |
| 5051 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5052 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5053 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5054 | } |
| 5055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | |
| 5057 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5058 | { |
| 5059 | std::string msg; |
| 5060 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5061 | connection->inputState.getFallbackKeys(); |
| 5062 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5063 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5065 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5066 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5067 | } |
| 5068 | #endif |
| 5069 | |
| 5070 | if (fallback) { |
| 5071 | // Restart the dispatch cycle using the fallback key. |
| 5072 | keyEntry->eventTime = event.getEventTime(); |
| 5073 | keyEntry->deviceId = event.getDeviceId(); |
| 5074 | keyEntry->source = event.getSource(); |
| 5075 | keyEntry->displayId = event.getDisplayId(); |
| 5076 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5077 | keyEntry->keyCode = fallbackKeyCode; |
| 5078 | keyEntry->scanCode = event.getScanCode(); |
| 5079 | keyEntry->metaState = event.getMetaState(); |
| 5080 | keyEntry->repeatCount = event.getRepeatCount(); |
| 5081 | keyEntry->downTime = event.getDownTime(); |
| 5082 | keyEntry->syntheticRepeat = false; |
| 5083 | |
| 5084 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5085 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5086 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 5087 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5088 | #endif |
| 5089 | return true; // restart the event |
| 5090 | } else { |
| 5091 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5092 | ALOGD("Unhandled key event: No fallback key."); |
| 5093 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5094 | |
| 5095 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5096 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5097 | } |
| 5098 | } |
| 5099 | return false; |
| 5100 | } |
| 5101 | |
| 5102 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5103 | DispatchEntry* dispatchEntry, |
| 5104 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5105 | return false; |
| 5106 | } |
| 5107 | |
| 5108 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5109 | mLock.unlock(); |
| 5110 | |
| 5111 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 5112 | |
| 5113 | mLock.lock(); |
| 5114 | } |
| 5115 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5116 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 5117 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5118 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5119 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 5120 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5121 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5122 | } |
| 5123 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5124 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5125 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5126 | // TODO Write some statistics about how long we spend waiting. |
| 5127 | } |
| 5128 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5129 | /** |
| 5130 | * Report the touch event latency to the statsd server. |
| 5131 | * Input events are reported for statistics if: |
| 5132 | * - This is a touchscreen event |
| 5133 | * - InputFilter is not enabled |
| 5134 | * - Event is not injected or synthesized |
| 5135 | * |
| 5136 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5137 | * from getting aggregated with the "old" data. |
| 5138 | */ |
| 5139 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5140 | REQUIRES(mLock) { |
| 5141 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5142 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5143 | if (!reportForStatistics) { |
| 5144 | return; |
| 5145 | } |
| 5146 | |
| 5147 | if (mTouchStatistics.shouldReport()) { |
| 5148 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5149 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5150 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5151 | mTouchStatistics.reset(); |
| 5152 | } |
| 5153 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5154 | mTouchStatistics.addValue(latencyMicros); |
| 5155 | } |
| 5156 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5158 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5159 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5160 | } |
| 5161 | } |
| 5162 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5163 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5164 | if (ATRACE_ENABLED()) { |
| 5165 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5166 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5167 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | } |
| 5169 | } |
| 5170 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5171 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | if (ATRACE_ENABLED()) { |
| 5173 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5174 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5175 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5179 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5180 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5181 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5182 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5183 | dumpDispatchStateLocked(dump); |
| 5184 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5185 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5186 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5187 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | void InputDispatcher::monitor() { |
| 5192 | // 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] | 5193 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5194 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5195 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5196 | } |
| 5197 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5198 | /** |
| 5199 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5200 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5201 | * this method can be safely called from any thread, as long as you've ensured that |
| 5202 | * the work you are interested in completing has already been queued. |
| 5203 | */ |
| 5204 | bool InputDispatcher::waitForIdle() { |
| 5205 | /** |
| 5206 | * Timeout should represent the longest possible time that a device might spend processing |
| 5207 | * events and commands. |
| 5208 | */ |
| 5209 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5210 | std::unique_lock lock(mLock); |
| 5211 | mLooper->wake(); |
| 5212 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5213 | return result == std::cv_status::no_timeout; |
| 5214 | } |
| 5215 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5216 | /** |
| 5217 | * Sets focus to the window identified by the token. This must be called |
| 5218 | * after updating any input window handles. |
| 5219 | * |
| 5220 | * Params: |
| 5221 | * request.token - input channel token used to identify the window that should gain focus. |
| 5222 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 5223 | * specified token does not match the currently focused window, this request will be dropped. |
| 5224 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 5225 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 5226 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 5227 | * when requesting the focus change. This determines which request gets |
| 5228 | * precedence if there is a focus change request from another source such as pointer down. |
| 5229 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5230 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 5231 | { // acquire lock |
| 5232 | std::scoped_lock _l(mLock); |
| 5233 | |
| 5234 | const int32_t displayId = request.displayId; |
| 5235 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 5236 | if (request.focusedToken && oldFocusedToken != request.focusedToken) { |
| 5237 | ALOGD_IF(DEBUG_FOCUS, |
| 5238 | "setFocusedWindow on display %" PRId32 |
| 5239 | " ignored, reason: focusedToken is not focused", |
| 5240 | displayId); |
| 5241 | return; |
| 5242 | } |
| 5243 | |
| 5244 | mPendingFocusRequests.erase(displayId); |
| 5245 | FocusResult result = handleFocusRequestLocked(request); |
| 5246 | if (result == FocusResult::NOT_VISIBLE) { |
| 5247 | // The requested window is not currently visible. Wait for the window to become visible |
| 5248 | // and then provide it focus. This is to handle situations where a user action triggers |
| 5249 | // a new window to appear. We want to be able to queue any key events after the user |
| 5250 | // action and deliver it to the newly focused window. In order for this to happen, we |
| 5251 | // take focus from the currently focused window so key events can be queued. |
| 5252 | ALOGD_IF(DEBUG_FOCUS, |
| 5253 | "setFocusedWindow on display %" PRId32 |
| 5254 | " pending, reason: window is not visible", |
| 5255 | displayId); |
| 5256 | mPendingFocusRequests[displayId] = request; |
| 5257 | onFocusChangedLocked(oldFocusedToken, nullptr, displayId, |
| 5258 | "setFocusedWindow_AwaitingWindowVisibility"); |
| 5259 | } else if (result != FocusResult::OK) { |
| 5260 | ALOGW("setFocusedWindow on display %" PRId32 " ignored, reason:%s", displayId, |
| 5261 | typeToString(result)); |
| 5262 | } |
| 5263 | } // release lock |
| 5264 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5265 | mLooper->wake(); |
| 5266 | } |
| 5267 | |
| 5268 | InputDispatcher::FocusResult InputDispatcher::handleFocusRequestLocked( |
| 5269 | const FocusRequest& request) { |
| 5270 | const int32_t displayId = request.displayId; |
| 5271 | const sp<IBinder> newFocusedToken = request.token; |
| 5272 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 5273 | |
| 5274 | if (oldFocusedToken == request.token) { |
| 5275 | ALOGD_IF(DEBUG_FOCUS, |
| 5276 | "setFocusedWindow on display %" PRId32 " ignored, reason: already focused", |
| 5277 | displayId); |
| 5278 | return FocusResult::OK; |
| 5279 | } |
| 5280 | |
| 5281 | FocusResult result = checkTokenFocusableLocked(newFocusedToken, displayId); |
| 5282 | if (result != FocusResult::OK) { |
| 5283 | return result; |
| 5284 | } |
| 5285 | |
| 5286 | std::string_view reason = |
| 5287 | (request.focusedToken) ? "setFocusedWindow_FocusCheck" : "setFocusedWindow"; |
| 5288 | onFocusChangedLocked(oldFocusedToken, newFocusedToken, displayId, reason); |
| 5289 | return FocusResult::OK; |
| 5290 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5291 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5292 | void InputDispatcher::onFocusChangedLocked(const sp<IBinder>& oldFocusedToken, |
| 5293 | const sp<IBinder>& newFocusedToken, int32_t displayId, |
| 5294 | std::string_view reason) { |
| 5295 | if (oldFocusedToken) { |
| 5296 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(oldFocusedToken); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5297 | if (focusedInputChannel) { |
| 5298 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 5299 | "focus left window"); |
| 5300 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5301 | enqueueFocusEventLocked(oldFocusedToken, false /*hasFocus*/, reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5302 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5303 | mFocusedWindowTokenByDisplay.erase(displayId); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5304 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5305 | if (newFocusedToken) { |
| 5306 | mFocusedWindowTokenByDisplay[displayId] = newFocusedToken; |
| 5307 | enqueueFocusEventLocked(newFocusedToken, true /*hasFocus*/, reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | if (mFocusedDisplayId == displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5311 | notifyFocusChangedLocked(oldFocusedToken, newFocusedToken); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5312 | } |
| 5313 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5314 | |
| 5315 | /** |
| 5316 | * Checks if the window token can be focused on a display. The token can be focused if there is |
| 5317 | * at least one window handle that is visible with the same token and all window handles with the |
| 5318 | * same token are focusable. |
| 5319 | * |
| 5320 | * In the case of mirroring, two windows may share the same window token and their visibility |
| 5321 | * might be different. Example, the mirrored window can cover the window its mirroring. However, |
| 5322 | * we expect the focusability of the windows to match since its hard to reason why one window can |
| 5323 | * receive focus events and the other cannot when both are backed by the same input channel. |
| 5324 | */ |
| 5325 | InputDispatcher::FocusResult InputDispatcher::checkTokenFocusableLocked(const sp<IBinder>& token, |
| 5326 | int32_t displayId) const { |
| 5327 | bool allWindowsAreFocusable = true; |
| 5328 | bool visibleWindowFound = false; |
| 5329 | bool windowFound = false; |
| 5330 | for (const sp<InputWindowHandle>& window : getWindowHandlesLocked(displayId)) { |
| 5331 | if (window->getToken() != token) { |
| 5332 | continue; |
| 5333 | } |
| 5334 | windowFound = true; |
| 5335 | if (window->getInfo()->visible) { |
| 5336 | // Check if at least a single window is visible. |
| 5337 | visibleWindowFound = true; |
| 5338 | } |
| 5339 | if (!window->getInfo()->focusable) { |
| 5340 | // Check if all windows with the window token are focusable. |
| 5341 | allWindowsAreFocusable = false; |
| 5342 | break; |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | if (!windowFound) { |
| 5347 | return FocusResult::NO_WINDOW; |
| 5348 | } |
| 5349 | if (!allWindowsAreFocusable) { |
| 5350 | return FocusResult::NOT_FOCUSABLE; |
| 5351 | } |
| 5352 | if (!visibleWindowFound) { |
| 5353 | return FocusResult::NOT_VISIBLE; |
| 5354 | } |
| 5355 | |
| 5356 | return FocusResult::OK; |
| 5357 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5358 | } // namespace android::inputdispatcher |