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; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 77 | using android::os::BlockUntrustedTouchesMode; |
| 78 | using android::os::InputEventInjectionResult; |
| 79 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 80 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 81 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 | |
| 83 | // Default input dispatching timeout if there is no focused application or paused window |
| 84 | // from which to determine an appropriate dispatching timeout. |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 85 | constexpr std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = |
| 86 | std::chrono::milliseconds(android::os::IInputConstants::DEFAULT_DISPATCHING_TIMEOUT_MILLIS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 | |
| 88 | // Amount of time to allow for all pending events to be processed when an app switch |
| 89 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 90 | // 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] | 91 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
| 93 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 94 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 95 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | // 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] | 98 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 99 | |
| 100 | // Log a warning when an interception call takes longer than this to process. |
| 101 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 102 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 103 | // Additional key latency in case a connection is still processing some motion events. |
| 104 | // This will help with the case when a user touched a button that opens a new window, |
| 105 | // and gives us the chance to dispatch the key to this new window. |
| 106 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 107 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 109 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 110 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 111 | // Event log tags. See EventLogTags.logtags for reference |
| 112 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 113 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 114 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | static inline nsecs_t now() { |
| 116 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 117 | } |
| 118 | |
| 119 | static inline const char* toString(bool value) { |
| 120 | return value ? "true" : "false"; |
| 121 | } |
| 122 | |
| 123 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 124 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 125 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static bool isValidKeyAction(int32_t action) { |
| 129 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 130 | case AKEY_EVENT_ACTION_DOWN: |
| 131 | case AKEY_EVENT_ACTION_UP: |
| 132 | return true; |
| 133 | default: |
| 134 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 139 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | ALOGE("Key event has invalid action code 0x%x", action); |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 146 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 148 | case AMOTION_EVENT_ACTION_DOWN: |
| 149 | case AMOTION_EVENT_ACTION_UP: |
| 150 | case AMOTION_EVENT_ACTION_CANCEL: |
| 151 | case AMOTION_EVENT_ACTION_MOVE: |
| 152 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 153 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 154 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 155 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 156 | case AMOTION_EVENT_ACTION_SCROLL: |
| 157 | return true; |
| 158 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 159 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 160 | int32_t index = getMotionEventActionPointerIndex(action); |
| 161 | return index >= 0 && index < pointerCount; |
| 162 | } |
| 163 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 164 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 165 | return actionButton != 0; |
| 166 | default: |
| 167 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 171 | static int64_t millis(std::chrono::nanoseconds t) { |
| 172 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 173 | } |
| 174 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 175 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 176 | const PointerProperties* pointerProperties) { |
| 177 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 178 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 179 | return false; |
| 180 | } |
| 181 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 182 | 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] | 183 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | BitSet32 pointerIdBits; |
| 187 | for (size_t i = 0; i < pointerCount; i++) { |
| 188 | int32_t id = pointerProperties[i].id; |
| 189 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 190 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 191 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | if (pointerIdBits.hasBit(id)) { |
| 195 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 196 | return false; |
| 197 | } |
| 198 | pointerIdBits.markBit(id); |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 203 | static void dumpRegion(std::string& dump, const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | if (region.isEmpty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 205 | dump += "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
| 209 | bool first = true; |
| 210 | Region::const_iterator cur = region.begin(); |
| 211 | Region::const_iterator const tail = region.end(); |
| 212 | while (cur != tail) { |
| 213 | if (first) { |
| 214 | first = false; |
| 215 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 216 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 217 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 218 | 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] | 219 | cur++; |
| 220 | } |
| 221 | } |
| 222 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 223 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
| 224 | constexpr size_t maxEntries = 50; // max events to print |
| 225 | constexpr size_t skipBegin = maxEntries / 2; |
| 226 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 227 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 228 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 229 | |
| 230 | std::string dump; |
| 231 | for (size_t i = 0; i < queue.size(); i++) { |
| 232 | const DispatchEntry& entry = *queue[i]; |
| 233 | if (i >= skipBegin && i < skipEnd) { |
| 234 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 235 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 236 | continue; |
| 237 | } |
| 238 | dump.append(INDENT4); |
| 239 | dump += entry.eventEntry->getDescription(); |
| 240 | dump += StringPrintf(", seq=%" PRIu32 |
| 241 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 242 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 243 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 244 | if (entry.deliveryTime != 0) { |
| 245 | // This entry was delivered, so add information on how long we've been waiting |
| 246 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 247 | } |
| 248 | dump.append("\n"); |
| 249 | } |
| 250 | return dump; |
| 251 | } |
| 252 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Find the entry in std::unordered_map by key, and return it. |
| 255 | * If the entry is not found, return a default constructed entry. |
| 256 | * |
| 257 | * Useful when the entries are vectors, since an empty vector will be returned |
| 258 | * if the entry is not found. |
| 259 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 260 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 261 | template <typename K, typename V> |
| 262 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 263 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 264 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Find the entry in std::unordered_map by value, and remove it. |
| 269 | * If more than one entry has the same value, then all matching |
| 270 | * key-value pairs will be removed. |
| 271 | * |
| 272 | * Return true if at least one value has been removed. |
| 273 | */ |
| 274 | template <typename K, typename V> |
| 275 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 276 | bool removed = false; |
| 277 | for (auto it = map.begin(); it != map.end();) { |
| 278 | if (it->second == value) { |
| 279 | it = map.erase(it); |
| 280 | removed = true; |
| 281 | } else { |
| 282 | it++; |
| 283 | } |
| 284 | } |
| 285 | return removed; |
| 286 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 287 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Find the entry in std::unordered_map by key and return the value as an optional. |
| 290 | */ |
| 291 | template <typename K, typename V> |
| 292 | static std::optional<V> getOptionalValueByKey(const std::unordered_map<K, V>& map, K key) { |
| 293 | auto it = map.find(key); |
| 294 | return it != map.end() ? std::optional<V>{it->second} : std::nullopt; |
| 295 | } |
| 296 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 297 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 298 | if (first == second) { |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | if (first == nullptr || second == nullptr) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | return first->getToken() == second->getToken(); |
| 307 | } |
| 308 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 309 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 310 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 311 | } |
| 312 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 313 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 314 | EventEntry* eventEntry, |
| 315 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 316 | if (inputTarget.useDefaultPointerTransform()) { |
| 317 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 318 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 319 | inputTargetFlags, transform, |
| 320 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 324 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 325 | |
| 326 | PointerCoords pointerCoords[motionEntry.pointerCount]; |
| 327 | |
| 328 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 329 | // 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] | 330 | // uses the transform for the normalized pointer. |
| 331 | const ui::Transform& firstPointerTransform = |
| 332 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 333 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 334 | |
| 335 | // Iterate through all pointers in the event to normalize against the first. |
| 336 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 337 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 338 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 339 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 340 | |
| 341 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 342 | // First, apply the current pointer's transform to update the coordinates into |
| 343 | // window space. |
| 344 | pointerCoords[pointerIndex].transform(currTransform); |
| 345 | // Next, apply the inverse transform of the normalized coordinates so the |
| 346 | // current coordinates are transformed into the normalized coordinate space. |
| 347 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | MotionEntry* combinedMotionEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 351 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 352 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, |
| 353 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, |
| 354 | motionEntry.metaState, motionEntry.buttonState, |
| 355 | motionEntry.classification, motionEntry.edgeFlags, |
| 356 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 357 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 358 | motionEntry.downTime, motionEntry.pointerCount, |
| 359 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, |
| 360 | 0 /* yOffset */); |
| 361 | |
| 362 | if (motionEntry.injectionState) { |
| 363 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 364 | combinedMotionEntry->injectionState->refCount += 1; |
| 365 | } |
| 366 | |
| 367 | std::unique_ptr<DispatchEntry> dispatchEntry = |
| 368 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 369 | inputTargetFlags, firstPointerTransform, |
| 370 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 371 | combinedMotionEntry->release(); |
| 372 | return dispatchEntry; |
| 373 | } |
| 374 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 375 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 376 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 377 | float yOffset = 0) { |
| 378 | if (monitors.empty()) { |
| 379 | return; |
| 380 | } |
| 381 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 382 | for (const Monitor& monitor : monitors) { |
| 383 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 384 | } |
| 385 | } |
| 386 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 387 | static status_t openInputChannelPair(const std::string& name, |
| 388 | std::shared_ptr<InputChannel>& serverChannel, |
| 389 | std::unique_ptr<InputChannel>& clientChannel) { |
| 390 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 391 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 392 | |
| 393 | serverChannel = std::move(uniqueServerChannel); |
| 394 | return result; |
| 395 | } |
| 396 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 397 | const char* InputDispatcher::typeToString(InputDispatcher::FocusResult result) { |
| 398 | switch (result) { |
| 399 | case InputDispatcher::FocusResult::OK: |
| 400 | return "Ok"; |
| 401 | case InputDispatcher::FocusResult::NO_WINDOW: |
| 402 | return "Window not found"; |
| 403 | case InputDispatcher::FocusResult::NOT_FOCUSABLE: |
| 404 | return "Window not focusable"; |
| 405 | case InputDispatcher::FocusResult::NOT_VISIBLE: |
| 406 | return "Window not visible"; |
| 407 | } |
| 408 | } |
| 409 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 410 | template <typename T> |
| 411 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 412 | if (lhs == nullptr && rhs == nullptr) { |
| 413 | return true; |
| 414 | } |
| 415 | if (lhs == nullptr || rhs == nullptr) { |
| 416 | return false; |
| 417 | } |
| 418 | return *lhs == *rhs; |
| 419 | } |
| 420 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 421 | // --- InputDispatcher --- |
| 422 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 423 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 424 | : mPolicy(policy), |
| 425 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 426 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 427 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 428 | mAppSwitchSawKeyDown(false), |
| 429 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 430 | mNextUnblockedEvent(nullptr), |
| 431 | mDispatchEnabled(false), |
| 432 | mDispatchFrozen(false), |
| 433 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 434 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 435 | // To avoid leaking stack in case that call never comes, and for tests, |
| 436 | // initialize it here anyways. |
| 437 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 438 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 439 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 440 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 441 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 442 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 443 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 444 | |
| 445 | policy->getDispatcherConfiguration(&mConfig); |
| 446 | } |
| 447 | |
| 448 | InputDispatcher::~InputDispatcher() { |
| 449 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 450 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 451 | |
| 452 | resetKeyRepeatLocked(); |
| 453 | releasePendingEventLocked(); |
| 454 | drainInboundQueueLocked(); |
| 455 | } |
| 456 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 457 | while (!mConnectionsByFd.empty()) { |
| 458 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 459 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 463 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 464 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 465 | return ALREADY_EXISTS; |
| 466 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 467 | mThread = std::make_unique<InputThread>( |
| 468 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 469 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 473 | if (mThread && mThread->isCallingThread()) { |
| 474 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 475 | return INVALID_OPERATION; |
| 476 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 477 | mThread.reset(); |
| 478 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 481 | void InputDispatcher::dispatchOnce() { |
| 482 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 483 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 484 | std::scoped_lock _l(mLock); |
| 485 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 486 | |
| 487 | // Run a dispatch loop if there are no pending commands. |
| 488 | // The dispatch loop might enqueue commands to run afterwards. |
| 489 | if (!haveCommandsLocked()) { |
| 490 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 491 | } |
| 492 | |
| 493 | // Run all pending commands if there are any. |
| 494 | // If any commands were run then force the next poll to wake up immediately. |
| 495 | if (runCommandsLockedInterruptible()) { |
| 496 | nextWakeupTime = LONG_LONG_MIN; |
| 497 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 498 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 499 | // If we are still waiting for ack on some events, |
| 500 | // we might have to wake up earlier to check if an app is anr'ing. |
| 501 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 502 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 503 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 504 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 505 | // pending or queued events |
| 506 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 507 | mDispatcherEnteredIdle.notify_all(); |
| 508 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 509 | } // release lock |
| 510 | |
| 511 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 512 | nsecs_t currentTime = now(); |
| 513 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 514 | mLooper->pollOnce(timeoutMillis); |
| 515 | } |
| 516 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 517 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 518 | * Raise ANR if there is no focused window. |
| 519 | * Before the ANR is raised, do a final state check: |
| 520 | * 1. The currently focused application must be the same one we are waiting for. |
| 521 | * 2. Ensure we still don't have a focused window. |
| 522 | */ |
| 523 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 524 | // Check if the application that we are waiting for is still focused. |
| 525 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 526 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 527 | if (focusedApplication == nullptr || |
| 528 | focusedApplication->getApplicationToken() != |
| 529 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 530 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 531 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 532 | focusedApplication->getName().c_str()); |
| 533 | return; // The focused application has changed. |
| 534 | } |
| 535 | |
| 536 | const sp<InputWindowHandle>& focusedWindowHandle = |
| 537 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 538 | if (focusedWindowHandle != nullptr) { |
| 539 | return; // We now have a focused window. No need for ANR. |
| 540 | } |
| 541 | onAnrLocked(mAwaitedFocusedApplication); |
| 542 | } |
| 543 | |
| 544 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 545 | * Check if any of the connections' wait queues have events that are too old. |
| 546 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 547 | * Return the time at which we should wake up next. |
| 548 | */ |
| 549 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 550 | const nsecs_t currentTime = now(); |
| 551 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 552 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 553 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 554 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 555 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 556 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 557 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 558 | return LONG_LONG_MIN; |
| 559 | } else { |
| 560 | // Keep waiting |
| 561 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); |
| 562 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); |
| 563 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // Check if any connection ANRs are due |
| 568 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 569 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 570 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 571 | } |
| 572 | |
| 573 | // If we reached here, we have an unresponsive connection. |
| 574 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 575 | if (connection == nullptr) { |
| 576 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 577 | return nextAnrCheck; |
| 578 | } |
| 579 | connection->responsive = false; |
| 580 | // Stop waking up for this unresponsive connection |
| 581 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 582 | onAnrLocked(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 583 | return LONG_LONG_MIN; |
| 584 | } |
| 585 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 586 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 587 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 588 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 589 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 590 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 591 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 595 | nsecs_t currentTime = now(); |
| 596 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 597 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 598 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 599 | // repeat if the device is just coming out of sleep. |
| 600 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | resetKeyRepeatLocked(); |
| 602 | } |
| 603 | |
| 604 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 605 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 606 | if (DEBUG_FOCUS) { |
| 607 | ALOGD("Dispatch frozen. Waiting some more."); |
| 608 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 609 | return; |
| 610 | } |
| 611 | |
| 612 | // Optimize latency of app switches. |
| 613 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 614 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 615 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 616 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 617 | *nextWakeupTime = mAppSwitchDueTime; |
| 618 | } |
| 619 | |
| 620 | // Ready to start a new event. |
| 621 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 622 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 623 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | if (isAppSwitchDue) { |
| 625 | // The inbound queue is empty so the app switch key we were waiting |
| 626 | // for will never arrive. Stop waiting for it. |
| 627 | resetPendingAppSwitchLocked(false); |
| 628 | isAppSwitchDue = false; |
| 629 | } |
| 630 | |
| 631 | // Synthesize a key repeat if appropriate. |
| 632 | if (mKeyRepeatState.lastKeyEntry) { |
| 633 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 634 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 635 | } else { |
| 636 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 637 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // Nothing to do if there is no pending event. |
| 643 | if (!mPendingEvent) { |
| 644 | return; |
| 645 | } |
| 646 | } else { |
| 647 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 648 | mPendingEvent = mInboundQueue.front(); |
| 649 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | traceInboundQueueLengthLocked(); |
| 651 | } |
| 652 | |
| 653 | // Poke user activity for this event. |
| 654 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 655 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // Now we have an event to dispatch. |
| 660 | // 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] | 661 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 662 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 663 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 664 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 665 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 667 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 671 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 675 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 676 | ConfigurationChangedEntry* typedEntry = |
| 677 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
| 678 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 679 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 680 | break; |
| 681 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 682 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 683 | case EventEntry::Type::DEVICE_RESET: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 684 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); |
| 685 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 686 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 687 | break; |
| 688 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 690 | case EventEntry::Type::FOCUS: { |
| 691 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); |
| 692 | dispatchFocusLocked(currentTime, typedEntry); |
| 693 | done = true; |
| 694 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 695 | break; |
| 696 | } |
| 697 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 698 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 699 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
| 700 | if (isAppSwitchDue) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 701 | if (isAppSwitchKeyEvent(*typedEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 702 | resetPendingAppSwitchLocked(true); |
| 703 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 704 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 705 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 708 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 709 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 710 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 711 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 712 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 713 | } |
| 714 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 715 | break; |
| 716 | } |
| 717 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 718 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 719 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 720 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 721 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 723 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 724 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 725 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 726 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 727 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 728 | } |
| 729 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
| 730 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 731 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 735 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 736 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 737 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 738 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | |
| 740 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 741 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 745 | /** |
| 746 | * Return true if the events preceding this incoming motion event should be dropped |
| 747 | * Return false otherwise (the default behaviour) |
| 748 | */ |
| 749 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 750 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 751 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 752 | |
| 753 | // Optimize case where the current application is unresponsive and the user |
| 754 | // decides to touch a window in a different application. |
| 755 | // If the application takes too long to catch up then we drop all events preceding |
| 756 | // the touch into the other window. |
| 757 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 758 | int32_t displayId = motionEntry.displayId; |
| 759 | int32_t x = static_cast<int32_t>( |
| 760 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 761 | int32_t y = static_cast<int32_t>( |
| 762 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 763 | sp<InputWindowHandle> touchedWindowHandle = |
| 764 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 765 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 766 | touchedWindowHandle->getApplicationToken() != |
| 767 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 768 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 769 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 770 | "for %s", |
| 771 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 772 | return true; |
| 773 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 774 | |
| 775 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 776 | std::vector<TouchedMonitor> gestureMonitors = |
| 777 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 778 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 779 | sp<Connection> connection = |
| 780 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 781 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 782 | // This monitor could take more input. Drop all events preceding this |
| 783 | // event, so that gesture monitor could get a chance to receive the stream |
| 784 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 785 | "responsive gesture monitor that may handle the event", |
| 786 | mAwaitedFocusedApplication->getName().c_str()); |
| 787 | return true; |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 793 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 794 | // events to be processed before dispatching the key event. This is because these motion events |
| 795 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 796 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 797 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 798 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 799 | "just send the pending key event to the focused window."); |
| 800 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 801 | } |
| 802 | return false; |
| 803 | } |
| 804 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 806 | bool needWake = mInboundQueue.empty(); |
| 807 | mInboundQueue.push_back(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 808 | traceInboundQueueLengthLocked(); |
| 809 | |
| 810 | switch (entry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 811 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 812 | // Optimize app switch latency. |
| 813 | // If the application takes too long to catch up then we drop all events preceding |
| 814 | // the app switch key. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 815 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 816 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 817 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 818 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 819 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 820 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 822 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 824 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 825 | mAppSwitchSawKeyDown = false; |
| 826 | needWake = true; |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | break; |
| 831 | } |
| 832 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 833 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 834 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { |
| 835 | mNextUnblockedEvent = entry; |
| 836 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 837 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 838 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 839 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 840 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 841 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 842 | break; |
| 843 | } |
| 844 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 845 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 846 | // nothing to do |
| 847 | break; |
| 848 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return needWake; |
| 852 | } |
| 853 | |
| 854 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { |
| 855 | entry->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 856 | mRecentQueue.push_back(entry); |
| 857 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
| 858 | mRecentQueue.front()->release(); |
| 859 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 864 | int32_t y, TouchState* touchState, |
| 865 | bool addOutsideTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 866 | bool addPortalWindows) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 867 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 868 | LOG_ALWAYS_FATAL( |
| 869 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 870 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 871 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 872 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 873 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 874 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 875 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 876 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 877 | |
| 878 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 879 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 880 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 881 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 882 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 883 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 884 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 885 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 886 | if (addPortalWindows) { |
| 887 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 888 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 889 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 890 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 891 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 893 | // Found window. |
| 894 | return windowHandle; |
| 895 | } |
| 896 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 897 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 898 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 899 | touchState->addOrUpdateWindow(windowHandle, |
| 900 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 901 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 902 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 903 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } |
| 905 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 906 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 909 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 910 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 911 | std::vector<TouchedMonitor> touchedMonitors; |
| 912 | |
| 913 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 914 | addGestureMonitors(monitors, touchedMonitors); |
| 915 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 916 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 917 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 918 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 919 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 920 | } |
| 921 | return touchedMonitors; |
| 922 | } |
| 923 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 924 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | const char* reason; |
| 926 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 927 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 928 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 929 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 931 | reason = "inbound event was dropped because the policy consumed it"; |
| 932 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 933 | case DropReason::DISABLED: |
| 934 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 935 | ALOGI("Dropped event because input dispatch is disabled."); |
| 936 | } |
| 937 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 938 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 939 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 940 | ALOGI("Dropped event because of pending overdue app switch."); |
| 941 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 942 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 943 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 944 | ALOGI("Dropped event because the current application is not responding and the user " |
| 945 | "has started interacting with a different application."); |
| 946 | reason = "inbound event was dropped because the current application is not responding " |
| 947 | "and the user has started interacting with a different application"; |
| 948 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 949 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 950 | ALOGI("Dropped event because it is stale."); |
| 951 | reason = "inbound event was dropped because it is stale"; |
| 952 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 953 | case DropReason::NOT_DROPPED: { |
| 954 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 955 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 956 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 959 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 960 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 961 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 962 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 963 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 965 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 966 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 967 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 968 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 969 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 970 | } else { |
| 971 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 972 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 973 | } |
| 974 | break; |
| 975 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 976 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 977 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 978 | case EventEntry::Type::DEVICE_RESET: { |
| 979 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); |
| 980 | break; |
| 981 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 985 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 986 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 987 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 990 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 991 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 992 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 993 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 997 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 998 | } |
| 999 | |
| 1000 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1001 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1002 | |
| 1003 | #if DEBUG_APP_SWITCH |
| 1004 | if (handled) { |
| 1005 | ALOGD("App switch has arrived."); |
| 1006 | } else { |
| 1007 | ALOGD("App switch was abandoned."); |
| 1008 | } |
| 1009 | #endif |
| 1010 | } |
| 1011 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1012 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1013 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1017 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | return false; |
| 1019 | } |
| 1020 | |
| 1021 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1022 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1023 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1024 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1025 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1026 | |
| 1027 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1028 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1032 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1033 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1037 | while (!mInboundQueue.empty()) { |
| 1038 | EventEntry* entry = mInboundQueue.front(); |
| 1039 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1040 | releaseInboundEventLocked(entry); |
| 1041 | } |
| 1042 | traceInboundQueueLengthLocked(); |
| 1043 | } |
| 1044 | |
| 1045 | void InputDispatcher::releasePendingEventLocked() { |
| 1046 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1047 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1048 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
| 1053 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1054 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | #if DEBUG_DISPATCH_CYCLE |
| 1056 | ALOGD("Injected inbound event was dropped."); |
| 1057 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1058 | setInjectionResult(entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1059 | } |
| 1060 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1061 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | } |
| 1063 | addRecentEventLocked(entry); |
| 1064 | entry->release(); |
| 1065 | } |
| 1066 | |
| 1067 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1068 | if (mKeyRepeatState.lastKeyEntry) { |
| 1069 | mKeyRepeatState.lastKeyEntry->release(); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1070 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1074 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1075 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 1076 | |
| 1077 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1078 | uint32_t policyFlags = entry->policyFlags & |
| 1079 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | if (entry->refCount == 1) { |
| 1081 | entry->recycle(); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1082 | entry->id = mIdGenerator.nextId(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1083 | entry->eventTime = currentTime; |
| 1084 | entry->policyFlags = policyFlags; |
| 1085 | entry->repeatCount += 1; |
| 1086 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | KeyEntry* newEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1088 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1089 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1090 | entry->keyCode, entry->scanCode, entry->metaState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1091 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | |
| 1093 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 1094 | entry->release(); |
| 1095 | |
| 1096 | entry = newEntry; |
| 1097 | } |
| 1098 | entry->syntheticRepeat = true; |
| 1099 | |
| 1100 | // Increment reference count since we keep a reference to the event in |
| 1101 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 1102 | entry->refCount += 1; |
| 1103 | |
| 1104 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
| 1105 | return entry; |
| 1106 | } |
| 1107 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1108 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
| 1109 | ConfigurationChangedEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1111 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1112 | #endif |
| 1113 | |
| 1114 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1115 | resetKeyRepeatLocked(); |
| 1116 | |
| 1117 | // 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] | 1118 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1119 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | commandEntry->eventTime = entry->eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1121 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | return true; |
| 1123 | } |
| 1124 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1125 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1127 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1128 | entry->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1129 | #endif |
| 1130 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1131 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | options.deviceId = entry->deviceId; |
| 1133 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1137 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1138 | std::string_view reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1139 | if (mPendingEvent != nullptr) { |
| 1140 | // Move the pending event to the front of the queue. This will give the chance |
| 1141 | // for the pending event to get dispatched to the newly focused window |
| 1142 | mInboundQueue.push_front(mPendingEvent); |
| 1143 | mPendingEvent = nullptr; |
| 1144 | } |
| 1145 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1146 | FocusEntry* focusEntry = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1147 | new FocusEntry(mIdGenerator.nextId(), now(), windowToken, hasFocus, reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1148 | |
| 1149 | // This event should go to the front of the queue, but behind all other focus events |
| 1150 | // Find the last focus event, and insert right after it |
| 1151 | std::deque<EventEntry*>::reverse_iterator it = |
| 1152 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1153 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); |
| 1154 | |
| 1155 | // Maintain the order of focus events. Insert the entry after all other focus events. |
| 1156 | mInboundQueue.insert(it.base(), focusEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1160 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1161 | if (channel == nullptr) { |
| 1162 | return; // Window has gone away |
| 1163 | } |
| 1164 | InputTarget target; |
| 1165 | target.inputChannel = channel; |
| 1166 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1167 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1168 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1169 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1170 | std::string reason = std::string("reason=").append(entry->reason); |
| 1171 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1172 | dispatchEventLocked(currentTime, entry, {target}); |
| 1173 | } |
| 1174 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1176 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1178 | if (!entry->dispatchInProgress) { |
| 1179 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1180 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1181 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1182 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1183 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | // We have seen two identical key downs in a row which indicates that the device |
| 1185 | // driver is automatically generating key repeats itself. We take note of the |
| 1186 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1187 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1188 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1189 | // Make sure we don't get key down from a different device. If a different |
| 1190 | // device Id has same key pressed down, the new device Id will replace the |
| 1191 | // current one to hold the key repeat with repeat count reset. |
| 1192 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1193 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1195 | resetKeyRepeatLocked(); |
| 1196 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1197 | } else { |
| 1198 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1199 | resetKeyRepeatLocked(); |
| 1200 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1201 | } |
| 1202 | mKeyRepeatState.lastKeyEntry = entry; |
| 1203 | entry->refCount += 1; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1204 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1205 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
| 1206 | // The stale device releases the key, reset staleDeviceId. |
| 1207 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1208 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1209 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1210 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | resetKeyRepeatLocked(); |
| 1212 | } |
| 1213 | |
| 1214 | if (entry->repeatCount == 1) { |
| 1215 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1216 | } else { |
| 1217 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1218 | } |
| 1219 | |
| 1220 | entry->dispatchInProgress = true; |
| 1221 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1222 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | // Handle case where the policy asked us to try again later last time. |
| 1226 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1227 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1228 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1229 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1230 | } |
| 1231 | return false; // wait until next wakeup |
| 1232 | } |
| 1233 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1234 | entry->interceptKeyWakeupTime = 0; |
| 1235 | } |
| 1236 | |
| 1237 | // Give the policy a chance to intercept the key. |
| 1238 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1239 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1240 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1241 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1242 | sp<IBinder> focusedWindowToken = |
| 1243 | getValueByKey(mFocusedWindowTokenByDisplay, getTargetDisplayId(*entry)); |
| 1244 | if (focusedWindowToken != nullptr) { |
| 1245 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1246 | } |
| 1247 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1248 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | entry->refCount += 1; |
| 1250 | return false; // wait for the command to run |
| 1251 | } else { |
| 1252 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1253 | } |
| 1254 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1255 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1256 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1261 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1262 | setInjectionResult(entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1263 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1264 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1265 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | return true; |
| 1267 | } |
| 1268 | |
| 1269 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1270 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1271 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1272 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1273 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1274 | return false; |
| 1275 | } |
| 1276 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1277 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1278 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1282 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1283 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | |
| 1285 | // Dispatch the key. |
| 1286 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1290 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1292 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1293 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1294 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1295 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1296 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1297 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | #endif |
| 1299 | } |
| 1300 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1301 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, |
| 1302 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1303 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1305 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1306 | entry->dispatchInProgress = true; |
| 1307 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1308 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1312 | if (*dropReason != DropReason::NOT_DROPPED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1313 | setInjectionResult(entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1314 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1315 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1320 | |
| 1321 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1322 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1323 | |
| 1324 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1325 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1326 | if (isPointerEvent) { |
| 1327 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1328 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1329 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1330 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1331 | } else { |
| 1332 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1333 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1334 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1335 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1336 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | return false; |
| 1338 | } |
| 1339 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1340 | setInjectionResult(entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1341 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1342 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1343 | return true; |
| 1344 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1345 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1346 | CancelationOptions::Mode mode(isPointerEvent |
| 1347 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1348 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1349 | CancelationOptions options(mode, "input event injection failed"); |
| 1350 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1351 | return true; |
| 1352 | } |
| 1353 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1354 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1355 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1357 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1358 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1359 | mTouchStatesByDisplay.find(entry->displayId); |
| 1360 | if (it != mTouchStatesByDisplay.end()) { |
| 1361 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1362 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1363 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1364 | // the corresponding displays as well. |
| 1365 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1366 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1367 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1368 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1374 | // Dispatch the motion. |
| 1375 | if (conflictingPointerActions) { |
| 1376 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1377 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1378 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1379 | } |
| 1380 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1381 | return true; |
| 1382 | } |
| 1383 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1384 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1385 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1386 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1387 | ", policyFlags=0x%x, " |
| 1388 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1389 | "metaState=0x%x, buttonState=0x%x," |
| 1390 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1391 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1392 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1393 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1394 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1395 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1396 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1397 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1398 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1399 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1400 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1401 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1402 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1403 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1404 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1405 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1406 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1407 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1408 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1409 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1410 | } |
| 1411 | #endif |
| 1412 | } |
| 1413 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1414 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, |
| 1415 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1416 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1417 | #if DEBUG_DISPATCH_CYCLE |
| 1418 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1419 | #endif |
| 1420 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1421 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1422 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1423 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1424 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1425 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1426 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1427 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1428 | sp<Connection> connection = |
| 1429 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1430 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1431 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1432 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1433 | if (DEBUG_FOCUS) { |
| 1434 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1435 | "is no longer registered with the input dispatcher.", |
| 1436 | inputTarget.inputChannel->getName().c_str()); |
| 1437 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1442 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1443 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1444 | // If the policy decides to close the app, we will get a channel removal event via |
| 1445 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1446 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1447 | // pile up. |
| 1448 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1449 | connection->inputChannel->getName().c_str()); |
| 1450 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1451 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1452 | "application not responding"); |
| 1453 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1457 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1458 | if (DEBUG_FOCUS) { |
| 1459 | ALOGD("Resetting ANR timeouts."); |
| 1460 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1461 | |
| 1462 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1463 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1464 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1467 | /** |
| 1468 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1469 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1470 | * Focused display is the display that the user most recently interacted with. |
| 1471 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1472 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1473 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1474 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1475 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1476 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1477 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1480 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1481 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1482 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1483 | break; |
| 1484 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1485 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1486 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1487 | case EventEntry::Type::DEVICE_RESET: { |
| 1488 | 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] | 1489 | return ADISPLAY_ID_NONE; |
| 1490 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1491 | } |
| 1492 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1493 | } |
| 1494 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1495 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1496 | const char* focusedWindowName) { |
| 1497 | if (mAnrTracker.empty()) { |
| 1498 | // already processed all events that we waited for |
| 1499 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
| 1503 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1504 | // Start the timer |
| 1505 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " |
| 1506 | "focus to change", |
| 1507 | focusedWindowName); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1508 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1509 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1510 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1511 | return true; |
| 1512 | } |
| 1513 | |
| 1514 | // We still have pending events, and already started the timer |
| 1515 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1516 | return true; // Still waiting |
| 1517 | } |
| 1518 | |
| 1519 | // Waited too long, and some connection still hasn't processed all motions |
| 1520 | // Just send the key to the focused window |
| 1521 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1522 | focusedWindowName); |
| 1523 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1524 | return false; |
| 1525 | } |
| 1526 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1527 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1528 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1529 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1530 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1532 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1533 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1534 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1535 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1536 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | // If there is no currently focused window and no focused application |
| 1538 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1539 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1540 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1541 | "display %" PRId32 ".", |
| 1542 | EventEntry::typeToString(entry.type), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1543 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1546 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1547 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1548 | // start interacting with another application via touch (app switch). This code can be removed |
| 1549 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1550 | // an app is expected to have a focused window. |
| 1551 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1552 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1553 | // 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] | 1554 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1555 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1556 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1557 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1558 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1559 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1560 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1561 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1562 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1563 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1564 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1565 | // Already raised ANR. Drop the event |
| 1566 | ALOGE("Dropping %s event because there is no focused window", |
| 1567 | EventEntry::typeToString(entry.type)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1568 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1569 | } else { |
| 1570 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1571 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | // we have a valid, non-null focused window |
| 1576 | resetNoFocusedWindowTimeoutLocked(); |
| 1577 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1579 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1580 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1583 | if (focusedWindowHandle->getInfo()->paused) { |
| 1584 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1585 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | // If the event is a key event, then we must wait for all previous events to |
| 1589 | // complete before delivering it because previous events may have the |
| 1590 | // side-effect of transferring focus to a different window and we want to |
| 1591 | // ensure that the following keys are sent to the new window. |
| 1592 | // |
| 1593 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1594 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1595 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1596 | // often anticipate pending UI changes when typing on a keyboard. |
| 1597 | // To obtain this behavior, we must serialize key events with respect to all |
| 1598 | // prior input events. |
| 1599 | if (entry.type == EventEntry::Type::KEY) { |
| 1600 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1601 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1602 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1603 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1607 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1608 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1609 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1610 | |
| 1611 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1612 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1615 | /** |
| 1616 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1617 | * that are currently unresponsive. |
| 1618 | */ |
| 1619 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1620 | const std::vector<TouchedMonitor>& monitors) const { |
| 1621 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1622 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1623 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1624 | sp<Connection> connection = getConnectionLocked( |
| 1625 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1626 | if (connection == nullptr) { |
| 1627 | ALOGE("Could not find connection for monitor %s", |
| 1628 | monitor.monitor.inputChannel->getName().c_str()); |
| 1629 | return false; |
| 1630 | } |
| 1631 | if (!connection->responsive) { |
| 1632 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1633 | connection->inputChannel->getName().c_str()); |
| 1634 | return false; |
| 1635 | } |
| 1636 | return true; |
| 1637 | }); |
| 1638 | return responsiveMonitors; |
| 1639 | } |
| 1640 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1641 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1642 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1643 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1644 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1645 | enum InjectionPermission { |
| 1646 | INJECTION_PERMISSION_UNKNOWN, |
| 1647 | INJECTION_PERMISSION_GRANTED, |
| 1648 | INJECTION_PERMISSION_DENIED |
| 1649 | }; |
| 1650 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1651 | // For security reasons, we defer updating the touch state until we are sure that |
| 1652 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1653 | int32_t displayId = entry.displayId; |
| 1654 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1656 | |
| 1657 | // Update the touch state as needed based on the properties of the touch event. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1658 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1660 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1661 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1662 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1663 | // Copy current touch state into tempTouchState. |
| 1664 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1665 | // 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] | 1666 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1667 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1668 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1669 | mTouchStatesByDisplay.find(displayId); |
| 1670 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1671 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1672 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1675 | bool isSplit = tempTouchState.split; |
| 1676 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1677 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1678 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1679 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1680 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1681 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1682 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1683 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1684 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1685 | bool wrongDevice = false; |
| 1686 | if (newGesture) { |
| 1687 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1688 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1689 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1690 | "in display %" PRId32, |
| 1691 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1692 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1693 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1694 | switchedDevice = false; |
| 1695 | wrongDevice = true; |
| 1696 | goto Failed; |
| 1697 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1698 | tempTouchState.reset(); |
| 1699 | tempTouchState.down = down; |
| 1700 | tempTouchState.deviceId = entry.deviceId; |
| 1701 | tempTouchState.source = entry.source; |
| 1702 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1704 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1705 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1706 | "in display %" PRId32, |
| 1707 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1708 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1709 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1710 | switchedDevice = false; |
| 1711 | wrongDevice = true; |
| 1712 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1716 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1717 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1718 | int32_t x; |
| 1719 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1721 | // Always dispatch mouse events to cursor position. |
| 1722 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1723 | x = int32_t(entry.xCursorPosition); |
| 1724 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1725 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1726 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1727 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1728 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1729 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1730 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1731 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1732 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1733 | |
| 1734 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1735 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1736 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1739 | if (newTouchedWindowHandle != nullptr && |
| 1740 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1741 | // New window supports splitting, but we should never split mouse events. |
| 1742 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | } else if (isSplit) { |
| 1744 | // New window does not support splitting but we have already split events. |
| 1745 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1746 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1750 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1751 | // 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] | 1752 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1755 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1756 | ALOGI("Not sending touch event to %s because it is paused", |
| 1757 | newTouchedWindowHandle->getName().c_str()); |
| 1758 | newTouchedWindowHandle = nullptr; |
| 1759 | } |
| 1760 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1761 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1762 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1763 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1764 | if (!isResponsive) { |
| 1765 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1766 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1767 | newTouchedWindowHandle = nullptr; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1771 | // Drop events that can't be trusted due to occlusion |
| 1772 | if (newTouchedWindowHandle != nullptr && |
| 1773 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 1774 | TouchOcclusionInfo occlusionInfo = |
| 1775 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1776 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 1777 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 1778 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 1779 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 1780 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 1781 | newTouchedWindowHandle = nullptr; |
| 1782 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1786 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1787 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1788 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1789 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1790 | 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] | 1791 | "(%d, %d) in display %" PRId32 ".", |
| 1792 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1793 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1794 | goto Failed; |
| 1795 | } |
| 1796 | |
| 1797 | if (newTouchedWindowHandle != nullptr) { |
| 1798 | // Set target flags. |
| 1799 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 1800 | if (isSplit) { |
| 1801 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1802 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1803 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1804 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1805 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 1806 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1807 | } |
| 1808 | |
| 1809 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1810 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 1811 | newHoverWindowHandle = nullptr; |
| 1812 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1813 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // Update the temporary touch state. |
| 1817 | BitSet32 pointerIds; |
| 1818 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1819 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1820 | pointerIds.markBit(pointerId); |
| 1821 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1822 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1825 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1826 | } else { |
| 1827 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 1828 | |
| 1829 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1830 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1831 | if (DEBUG_FOCUS) { |
| 1832 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 1833 | "dropped the pointer down event in display %" PRId32, |
| 1834 | displayId); |
| 1835 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1836 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1837 | goto Failed; |
| 1838 | } |
| 1839 | |
| 1840 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1841 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1842 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1843 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1844 | 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] | 1845 | |
| 1846 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1847 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1848 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1849 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 1850 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1851 | if (DEBUG_FOCUS) { |
| 1852 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 1853 | oldTouchedWindowHandle->getName().c_str(), |
| 1854 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 1855 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1857 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 1858 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 1859 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1860 | |
| 1861 | // Make a slippery entrance into the new window. |
| 1862 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 1863 | isSplit = true; |
| 1864 | } |
| 1865 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1866 | int32_t targetFlags = |
| 1867 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1868 | if (isSplit) { |
| 1869 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1870 | } |
| 1871 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 1872 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1873 | } |
| 1874 | |
| 1875 | BitSet32 pointerIds; |
| 1876 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1877 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1878 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1879 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1885 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 1886 | // when dispatching it as is to newTouchedWindowHandle. |
| 1887 | if (mLastHoverWindowHandle != nullptr && |
| 1888 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 1889 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1890 | #if DEBUG_HOVER |
| 1891 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1892 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1893 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1894 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 1895 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1896 | } |
| 1897 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1898 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 1899 | // when dispatching it as is to newTouchedWindowHandle. |
| 1900 | if (newHoverWindowHandle != nullptr && |
| 1901 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1902 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | #if DEBUG_HOVER |
| 1904 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1905 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1906 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1907 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 1908 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 1909 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | // Check permission to inject into all touched foreground windows and ensure there |
| 1914 | // is at least one touched foreground window. |
| 1915 | { |
| 1916 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1917 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1919 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1920 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1921 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1923 | goto Failed; |
| 1924 | } |
| 1925 | } |
| 1926 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1927 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1928 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1929 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 1930 | "%" PRId32 " or gesture monitor to receive it.", |
| 1931 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1932 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1933 | goto Failed; |
| 1934 | } |
| 1935 | |
| 1936 | // Permission granted to injection into all touched foreground windows. |
| 1937 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1938 | } |
| 1939 | |
| 1940 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1941 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1942 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1943 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1944 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1945 | if (foregroundWindowHandle) { |
| 1946 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1947 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1948 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 1949 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 1950 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1951 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 1952 | InputTarget::FLAG_ZERO_COORDS, |
| 1953 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1954 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1961 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1962 | // of the touch gesture. |
| 1963 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1964 | // engine only supports touch events. We would need to add a mechanism similar |
| 1965 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1966 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1967 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1968 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1969 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1970 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1971 | getWindowHandlesLocked(displayId); |
| 1972 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1973 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1974 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1975 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1976 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1977 | .addOrUpdateWindow(windowHandle, |
| 1978 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 1979 | InputTarget:: |
| 1980 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 1981 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1982 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1989 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1990 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1991 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1992 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1993 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1996 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1997 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1998 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | // Drop the outside or hover touch windows since we will not care about them |
| 2002 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2003 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | |
| 2005 | Failed: |
| 2006 | // Check injection permission once and for all. |
| 2007 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2008 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2009 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2010 | } else { |
| 2011 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2012 | } |
| 2013 | } |
| 2014 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2015 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2016 | return injectionResult; |
| 2017 | } |
| 2018 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2019 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2020 | if (!wrongDevice) { |
| 2021 | if (switchedDevice) { |
| 2022 | if (DEBUG_FOCUS) { |
| 2023 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2024 | } |
| 2025 | *outConflictingPointerActions = true; |
| 2026 | } |
| 2027 | |
| 2028 | if (isHoverAction) { |
| 2029 | // Started hovering, therefore no longer down. |
| 2030 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2031 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2032 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2033 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2034 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2035 | *outConflictingPointerActions = true; |
| 2036 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2037 | tempTouchState.reset(); |
| 2038 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2039 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2040 | tempTouchState.deviceId = entry.deviceId; |
| 2041 | tempTouchState.source = entry.source; |
| 2042 | tempTouchState.displayId = displayId; |
| 2043 | } |
| 2044 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2045 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2046 | // All pointers up or canceled. |
| 2047 | tempTouchState.reset(); |
| 2048 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2049 | // First pointer went down. |
| 2050 | if (oldState && oldState->down) { |
| 2051 | if (DEBUG_FOCUS) { |
| 2052 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2053 | } |
| 2054 | *outConflictingPointerActions = true; |
| 2055 | } |
| 2056 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2057 | // One pointer went up. |
| 2058 | if (isSplit) { |
| 2059 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2060 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2062 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2063 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2064 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2065 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2066 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2067 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2068 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2069 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2071 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2073 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2074 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2075 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2076 | // Save changes unless the action was scroll in which case the temporary touch |
| 2077 | // state was only valid for this one action. |
| 2078 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2079 | if (tempTouchState.displayId >= 0) { |
| 2080 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2081 | } else { |
| 2082 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2084 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2085 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2086 | // Update hover state. |
| 2087 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2090 | return injectionResult; |
| 2091 | } |
| 2092 | |
| 2093 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2094 | int32_t targetFlags, BitSet32 pointerIds, |
| 2095 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2096 | std::vector<InputTarget>::iterator it = |
| 2097 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2098 | [&windowHandle](const InputTarget& inputTarget) { |
| 2099 | return inputTarget.inputChannel->getConnectionToken() == |
| 2100 | windowHandle->getToken(); |
| 2101 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2102 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2103 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2104 | |
| 2105 | if (it == inputTargets.end()) { |
| 2106 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2107 | std::shared_ptr<InputChannel> inputChannel = |
| 2108 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2109 | if (inputChannel == nullptr) { |
| 2110 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2111 | return; |
| 2112 | } |
| 2113 | inputTarget.inputChannel = inputChannel; |
| 2114 | inputTarget.flags = targetFlags; |
| 2115 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2116 | inputTargets.push_back(inputTarget); |
| 2117 | it = inputTargets.end() - 1; |
| 2118 | } |
| 2119 | |
| 2120 | ALOG_ASSERT(it->flags == targetFlags); |
| 2121 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2122 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2123 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | } |
| 2125 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2126 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2127 | int32_t displayId, float xOffset, |
| 2128 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2129 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2130 | mGlobalMonitorsByDisplay.find(displayId); |
| 2131 | |
| 2132 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2133 | const std::vector<Monitor>& monitors = it->second; |
| 2134 | for (const Monitor& monitor : monitors) { |
| 2135 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2136 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2140 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2141 | float yOffset, |
| 2142 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2143 | InputTarget target; |
| 2144 | target.inputChannel = monitor.inputChannel; |
| 2145 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2146 | ui::Transform t; |
| 2147 | t.set(xOffset, yOffset); |
| 2148 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2149 | inputTargets.push_back(target); |
| 2150 | } |
| 2151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2153 | const InjectionState* injectionState) { |
| 2154 | if (injectionState && |
| 2155 | (windowHandle == nullptr || |
| 2156 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2157 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2158 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2159 | 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] | 2160 | "owned by uid %d", |
| 2161 | injectionState->injectorPid, injectionState->injectorUid, |
| 2162 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2163 | } else { |
| 2164 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2165 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2166 | } |
| 2167 | return false; |
| 2168 | } |
| 2169 | return true; |
| 2170 | } |
| 2171 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2172 | /** |
| 2173 | * Indicate whether one window handle should be considered as obscuring |
| 2174 | * another window handle. We only check a few preconditions. Actually |
| 2175 | * checking the bounds is left to the caller. |
| 2176 | */ |
| 2177 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2178 | const sp<InputWindowHandle>& otherHandle) { |
| 2179 | // Compare by token so cloned layers aren't counted |
| 2180 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2181 | return false; |
| 2182 | } |
| 2183 | auto info = windowHandle->getInfo(); |
| 2184 | auto otherInfo = otherHandle->getInfo(); |
| 2185 | if (!otherInfo->visible) { |
| 2186 | return false; |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame^] | 2187 | } else if (otherInfo->alpha == 0 && |
| 2188 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 2189 | // Those act as if they were invisible, so we don't need to flag them. |
| 2190 | // We do want to potentially flag touchable windows even if they have 0 |
| 2191 | // opacity, since they can consume touches and alter the effects of the |
| 2192 | // user interaction (eg. apps that rely on |
| 2193 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2194 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2195 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2196 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2197 | // If ownerUid is the same we don't generate occlusion events as there |
| 2198 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2199 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2200 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2201 | return false; |
| 2202 | } else if (otherInfo->displayId != info->displayId) { |
| 2203 | return false; |
| 2204 | } |
| 2205 | return true; |
| 2206 | } |
| 2207 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2208 | /** |
| 2209 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2210 | * untrusted, one should check: |
| 2211 | * |
| 2212 | * 1. If result.hasBlockingOcclusion is true. |
| 2213 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2214 | * BLOCK_UNTRUSTED. |
| 2215 | * |
| 2216 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2217 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2218 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2219 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2220 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2221 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2222 | * |
| 2223 | * If neither of those is true, then it means the touch can be allowed. |
| 2224 | */ |
| 2225 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
| 2226 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2227 | int32_t displayId = windowHandle->getInfo()->displayId; |
| 2228 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2229 | TouchOcclusionInfo info; |
| 2230 | info.hasBlockingOcclusion = false; |
| 2231 | info.obscuringOpacity = 0; |
| 2232 | info.obscuringUid = -1; |
| 2233 | std::map<int32_t, float> opacityByUid; |
| 2234 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| 2235 | if (windowHandle == otherHandle) { |
| 2236 | break; // All future windows are below us. Exit early. |
| 2237 | } |
| 2238 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
| 2239 | if (canBeObscuredBy(windowHandle, otherHandle) && |
| 2240 | windowHandle->getInfo()->ownerUid != otherInfo->ownerUid && |
| 2241 | otherInfo->frameContainsPoint(x, y)) { |
| 2242 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2243 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2244 | // window's touch occlusion mode |
| 2245 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2246 | info.hasBlockingOcclusion = true; |
| 2247 | info.obscuringUid = otherInfo->ownerUid; |
| 2248 | info.obscuringPackage = otherInfo->packageName; |
| 2249 | break; |
| 2250 | } |
| 2251 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2252 | uint32_t uid = otherInfo->ownerUid; |
| 2253 | float opacity = |
| 2254 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2255 | // Given windows A and B: |
| 2256 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2257 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2258 | opacityByUid[uid] = opacity; |
| 2259 | if (opacity > info.obscuringOpacity) { |
| 2260 | info.obscuringOpacity = opacity; |
| 2261 | info.obscuringUid = uid; |
| 2262 | info.obscuringPackage = otherInfo->packageName; |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | return info; |
| 2268 | } |
| 2269 | |
| 2270 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2271 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2272 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2273 | occlusionInfo.obscuringUid); |
| 2274 | return false; |
| 2275 | } |
| 2276 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2277 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2278 | "%.2f, maximum allowed = %.2f)", |
| 2279 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2280 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2281 | return false; |
| 2282 | } |
| 2283 | return true; |
| 2284 | } |
| 2285 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2286 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2287 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2288 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2289 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2290 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2291 | if (windowHandle == otherHandle) { |
| 2292 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2295 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2296 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2297 | return true; |
| 2298 | } |
| 2299 | } |
| 2300 | return false; |
| 2301 | } |
| 2302 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2303 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2304 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2305 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2306 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2307 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2308 | if (windowHandle == otherHandle) { |
| 2309 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2310 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2311 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2312 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2313 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2314 | return true; |
| 2315 | } |
| 2316 | } |
| 2317 | return false; |
| 2318 | } |
| 2319 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2320 | std::string InputDispatcher::getApplicationWindowLabel( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2321 | const std::shared_ptr<InputApplicationHandle>& applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2323 | if (applicationHandle != nullptr) { |
| 2324 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2325 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2326 | } else { |
| 2327 | return applicationHandle->getName(); |
| 2328 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2329 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2330 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2331 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2332 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 | } |
| 2334 | } |
| 2335 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2336 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2337 | if (eventEntry.type == EventEntry::Type::FOCUS) { |
| 2338 | // Focus events are passed to apps, but do not represent user activity. |
| 2339 | return; |
| 2340 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2341 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2342 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2343 | if (focusedWindowHandle != nullptr) { |
| 2344 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2345 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2347 | 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] | 2348 | #endif |
| 2349 | return; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2354 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2355 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2356 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2357 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2358 | return; |
| 2359 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2360 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2361 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2362 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2363 | } |
| 2364 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2365 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2366 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2367 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2368 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2369 | return; |
| 2370 | } |
| 2371 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2372 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2373 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2374 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2375 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2376 | case EventEntry::Type::DEVICE_RESET: { |
| 2377 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 2378 | EventEntry::typeToString(eventEntry.type)); |
| 2379 | break; |
| 2380 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2383 | std::unique_ptr<CommandEntry> commandEntry = |
| 2384 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2385 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2386 | commandEntry->userActivityEventType = eventType; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2387 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2391 | const sp<Connection>& connection, |
| 2392 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2393 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2394 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2395 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2396 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2397 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2398 | ATRACE_NAME(message.c_str()); |
| 2399 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2400 | #if DEBUG_DISPATCH_CYCLE |
| 2401 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2402 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2403 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2404 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2405 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2406 | #endif |
| 2407 | |
| 2408 | // Skip this event if the connection status is not normal. |
| 2409 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2410 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2411 | #if DEBUG_DISPATCH_CYCLE |
| 2412 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2413 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2414 | #endif |
| 2415 | return; |
| 2416 | } |
| 2417 | |
| 2418 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2419 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2420 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2421 | "Entry type %s should not have FLAG_SPLIT", |
| 2422 | EventEntry::typeToString(eventEntry->type)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2423 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2424 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2425 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2426 | MotionEntry* splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2427 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2428 | if (!splitMotionEntry) { |
| 2429 | return; // split event was dropped |
| 2430 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2431 | if (DEBUG_FOCUS) { |
| 2432 | ALOGD("channel '%s' ~ Split motion event.", |
| 2433 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2434 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2435 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2436 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2437 | splitMotionEntry->release(); |
| 2438 | return; |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2443 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2444 | } |
| 2445 | |
| 2446 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2447 | const sp<Connection>& connection, |
| 2448 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2449 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2450 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2451 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2452 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2453 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2454 | ATRACE_NAME(message.c_str()); |
| 2455 | } |
| 2456 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2457 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2458 | |
| 2459 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2460 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2461 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2462 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2463 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2464 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2465 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2466 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2467 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2468 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2469 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2470 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2471 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2472 | |
| 2473 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2474 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2475 | startDispatchCycleLocked(currentTime, connection); |
| 2476 | } |
| 2477 | } |
| 2478 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2479 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 2480 | EventEntry* eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2481 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2482 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2483 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2484 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2485 | connection->getInputChannelName().c_str(), |
| 2486 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2487 | ATRACE_NAME(message.c_str()); |
| 2488 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2489 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2490 | if (!(inputTargetFlags & dispatchMode)) { |
| 2491 | return; |
| 2492 | } |
| 2493 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2494 | |
| 2495 | // This is a new event. |
| 2496 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2497 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2498 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2499 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2500 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2501 | // different EventEntry than what was passed in. |
| 2502 | EventEntry* newEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2503 | // Apply target flags and update the connection's input state. |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2504 | switch (newEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2505 | case EventEntry::Type::KEY: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2506 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2507 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2508 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2509 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2510 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2511 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2512 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2513 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2514 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2515 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2516 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2517 | return; // skip the inconsistent event |
| 2518 | } |
| 2519 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2520 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2521 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2522 | case EventEntry::Type::MOTION: { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2523 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2524 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2525 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2526 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2527 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2528 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2529 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2530 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2531 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2532 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2533 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2534 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2535 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2536 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2537 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2538 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2539 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2540 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2541 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2542 | } |
| 2543 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2544 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2545 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2546 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2547 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2548 | "event", |
| 2549 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2550 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2551 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2552 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2553 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2554 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2555 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2556 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2557 | } |
| 2558 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2559 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2560 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2561 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2562 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2563 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2564 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2565 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2566 | "event", |
| 2567 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2568 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2569 | return; // skip the inconsistent event |
| 2570 | } |
| 2571 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2572 | dispatchEntry->resolvedEventId = |
| 2573 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2574 | ? mIdGenerator.nextId() |
| 2575 | : motionEntry.id; |
| 2576 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2577 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2578 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2579 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2580 | ATRACE_NAME(message.c_str()); |
| 2581 | } |
| 2582 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2583 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2584 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2585 | |
| 2586 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2587 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2588 | case EventEntry::Type::FOCUS: { |
| 2589 | break; |
| 2590 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2591 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2592 | case EventEntry::Type::DEVICE_RESET: { |
| 2593 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2594 | EventEntry::typeToString(newEntry->type)); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2595 | break; |
| 2596 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | // Remember that we are waiting for this dispatch to complete. |
| 2600 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2601 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2605 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2606 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2609 | /** |
| 2610 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2611 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2612 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2613 | * We will see both launcher and wallpaper in that list. |
| 2614 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2615 | * until the set of interacted connections changes. |
| 2616 | * |
| 2617 | * The following items are skipped, to reduce the logspam: |
| 2618 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2619 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2620 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2621 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2622 | * Both of those ACTION_UP events would not be logged |
| 2623 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events |
| 2624 | * will not be logged. This is omitted to reduce the amount of data printed. |
| 2625 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore |
| 2626 | * gesture monitor is the only connection receiving the remainder of the gesture. |
| 2627 | */ |
| 2628 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2629 | const std::vector<InputTarget>& targets) { |
| 2630 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2631 | if (entry.type == EventEntry::Type::KEY) { |
| 2632 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2633 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2634 | return; |
| 2635 | } |
| 2636 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2637 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2638 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2639 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2640 | return; |
| 2641 | } |
| 2642 | } else { |
| 2643 | return; // Not a key or a motion |
| 2644 | } |
| 2645 | |
| 2646 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; |
| 2647 | std::vector<sp<Connection>> newConnections; |
| 2648 | for (const InputTarget& target : targets) { |
| 2649 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2650 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2651 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2652 | } |
| 2653 | |
| 2654 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2655 | sp<Connection> connection = getConnectionLocked(token); |
| 2656 | if (connection == nullptr || connection->monitor) { |
| 2657 | continue; // We only need to keep track of the non-monitor connections. |
| 2658 | } |
| 2659 | newConnectionTokens.insert(std::move(token)); |
| 2660 | newConnections.emplace_back(connection); |
| 2661 | } |
| 2662 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 2663 | return; // no change |
| 2664 | } |
| 2665 | mInteractionConnectionTokens = newConnectionTokens; |
| 2666 | |
| 2667 | std::string windowList; |
| 2668 | for (const sp<Connection>& connection : newConnections) { |
| 2669 | windowList += connection->getWindowName() + ", "; |
| 2670 | } |
| 2671 | std::string message = "Interaction with windows: " + windowList; |
| 2672 | if (windowList.empty()) { |
| 2673 | message += "<none>"; |
| 2674 | } |
| 2675 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 2676 | } |
| 2677 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2678 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2679 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2680 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2681 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2682 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2683 | return; |
| 2684 | } |
| 2685 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2686 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); |
| 2687 | if (focusedToken == token) { |
| 2688 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2689 | return; |
| 2690 | } |
| 2691 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2692 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 2693 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2694 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2695 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2699 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2700 | if (ATRACE_ENABLED()) { |
| 2701 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2702 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2703 | ATRACE_NAME(message.c_str()); |
| 2704 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2705 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2706 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2707 | #endif |
| 2708 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2709 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 2710 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2711 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2712 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2713 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2714 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2715 | |
| 2716 | // Publish the event. |
| 2717 | status_t status; |
| 2718 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
| 2719 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2720 | case EventEntry::Type::KEY: { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2721 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2722 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2723 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2724 | // Publish the key event. |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2725 | status = |
| 2726 | connection->inputPublisher |
| 2727 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, |
| 2728 | keyEntry->deviceId, keyEntry->source, |
| 2729 | keyEntry->displayId, std::move(hmac), |
| 2730 | dispatchEntry->resolvedAction, |
| 2731 | dispatchEntry->resolvedFlags, keyEntry->keyCode, |
| 2732 | keyEntry->scanCode, keyEntry->metaState, |
| 2733 | keyEntry->repeatCount, keyEntry->downTime, |
| 2734 | keyEntry->eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2735 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2736 | } |
| 2737 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2738 | case EventEntry::Type::MOTION: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2739 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2740 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2741 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2742 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
| 2743 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2744 | // 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] | 2745 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && |
| 2746 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2747 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2748 | if (globalScaleFactor != 1.0f) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2749 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2750 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2751 | // Don't apply window scale here since we don't want scale to affect raw |
| 2752 | // coordinates. The scale will be sent back to the client and applied |
| 2753 | // later when requesting relative coordinates. |
| 2754 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 2755 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2756 | } |
| 2757 | usingCoords = scaledCoords; |
| 2758 | } |
| 2759 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2760 | // We don't want the dispatch target to know. |
| 2761 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2762 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2763 | scaledCoords[i].clear(); |
| 2764 | } |
| 2765 | usingCoords = scaledCoords; |
| 2766 | } |
| 2767 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2768 | |
| 2769 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2770 | |
| 2771 | // Publish the motion event. |
| 2772 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2773 | .publishMotionEvent(dispatchEntry->seq, |
| 2774 | dispatchEntry->resolvedEventId, |
| 2775 | motionEntry->deviceId, motionEntry->source, |
| 2776 | motionEntry->displayId, std::move(hmac), |
| 2777 | dispatchEntry->resolvedAction, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2778 | motionEntry->actionButton, |
| 2779 | dispatchEntry->resolvedFlags, |
| 2780 | motionEntry->edgeFlags, motionEntry->metaState, |
| 2781 | motionEntry->buttonState, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2782 | motionEntry->classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2783 | dispatchEntry->transform, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2784 | motionEntry->xPrecision, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2785 | motionEntry->yPrecision, |
| 2786 | motionEntry->xCursorPosition, |
| 2787 | motionEntry->yCursorPosition, |
| 2788 | motionEntry->downTime, motionEntry->eventTime, |
| 2789 | motionEntry->pointerCount, |
| 2790 | motionEntry->pointerProperties, usingCoords); |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2791 | reportTouchEventForStatistics(*motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2792 | break; |
| 2793 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2794 | case EventEntry::Type::FOCUS: { |
| 2795 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); |
| 2796 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2797 | focusEntry->id, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2798 | focusEntry->hasFocus, |
| 2799 | mInTouchMode); |
| 2800 | break; |
| 2801 | } |
| 2802 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2803 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2804 | case EventEntry::Type::DEVICE_RESET: { |
| 2805 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
| 2806 | EventEntry::typeToString(eventEntry->type)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2807 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2808 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | // Check the result. |
| 2812 | if (status) { |
| 2813 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2814 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2815 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2816 | "This is unexpected because the wait queue is empty, so the pipe " |
| 2817 | "should be empty and we shouldn't have any problems writing an " |
| 2818 | "event to it, status=%d", |
| 2819 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2820 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2821 | } else { |
| 2822 | // Pipe is full and we are waiting for the app to finish process some events |
| 2823 | // before sending more events to it. |
| 2824 | #if DEBUG_DISPATCH_CYCLE |
| 2825 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2826 | "waiting for the application to catch up", |
| 2827 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2828 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2829 | } |
| 2830 | } else { |
| 2831 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2832 | "status=%d", |
| 2833 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2834 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 2835 | } |
| 2836 | return; |
| 2837 | } |
| 2838 | |
| 2839 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2840 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 2841 | connection->outboundQueue.end(), |
| 2842 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2843 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2844 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2845 | if (connection->responsive) { |
| 2846 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 2847 | connection->inputChannel->getConnectionToken()); |
| 2848 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2849 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2853 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 2854 | size_t size; |
| 2855 | switch (event.type) { |
| 2856 | case VerifiedInputEvent::Type::KEY: { |
| 2857 | size = sizeof(VerifiedKeyEvent); |
| 2858 | break; |
| 2859 | } |
| 2860 | case VerifiedInputEvent::Type::MOTION: { |
| 2861 | size = sizeof(VerifiedMotionEvent); |
| 2862 | break; |
| 2863 | } |
| 2864 | } |
| 2865 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 2866 | return mHmacKeyManager.sign(start, size); |
| 2867 | } |
| 2868 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2869 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2870 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 2871 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 2872 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 2873 | // Only sign events up and down events as the purely move events |
| 2874 | // are tied to their up/down counterparts so signing would be redundant. |
| 2875 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 2876 | verifiedEvent.actionMasked = actionMasked; |
| 2877 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2878 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2879 | } |
| 2880 | return INVALID_HMAC; |
| 2881 | } |
| 2882 | |
| 2883 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 2884 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 2885 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 2886 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 2887 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2888 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2891 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2892 | const sp<Connection>& connection, uint32_t seq, |
| 2893 | bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2894 | #if DEBUG_DISPATCH_CYCLE |
| 2895 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2896 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2897 | #endif |
| 2898 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | if (connection->status == Connection::STATUS_BROKEN || |
| 2900 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2901 | return; |
| 2902 | } |
| 2903 | |
| 2904 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2905 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
| 2906 | } |
| 2907 | |
| 2908 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2909 | const sp<Connection>& connection, |
| 2910 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2911 | #if DEBUG_DISPATCH_CYCLE |
| 2912 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2913 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2914 | #endif |
| 2915 | |
| 2916 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2917 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2918 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2919 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2920 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2921 | |
| 2922 | // The connection appears to be unrecoverably broken. |
| 2923 | // Ignore already broken or zombie connections. |
| 2924 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2925 | connection->status = Connection::STATUS_BROKEN; |
| 2926 | |
| 2927 | if (notify) { |
| 2928 | // Notify other system components. |
| 2929 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2930 | } |
| 2931 | } |
| 2932 | } |
| 2933 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2934 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 2935 | while (!queue.empty()) { |
| 2936 | DispatchEntry* dispatchEntry = queue.front(); |
| 2937 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2938 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2942 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2943 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2944 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | } |
| 2946 | delete dispatchEntry; |
| 2947 | } |
| 2948 | |
| 2949 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 2950 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2951 | |
| 2952 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2953 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2954 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2955 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2956 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2957 | "fd=%d, events=0x%x", |
| 2958 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2959 | return 0; // remove the callback |
| 2960 | } |
| 2961 | |
| 2962 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2963 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2964 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2965 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2966 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2967 | "events=0x%x", |
| 2968 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2969 | return 1; |
| 2970 | } |
| 2971 | |
| 2972 | nsecs_t currentTime = now(); |
| 2973 | bool gotOne = false; |
| 2974 | status_t status; |
| 2975 | for (;;) { |
| 2976 | uint32_t seq; |
| 2977 | bool handled; |
| 2978 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
| 2979 | if (status) { |
| 2980 | break; |
| 2981 | } |
| 2982 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
| 2983 | gotOne = true; |
| 2984 | } |
| 2985 | if (gotOne) { |
| 2986 | d->runCommandsLockedInterruptible(); |
| 2987 | if (status == WOULD_BLOCK) { |
| 2988 | return 1; |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 2993 | if (notify) { |
| 2994 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2995 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2996 | } |
| 2997 | } else { |
| 2998 | // Monitor channels are never explicitly unregistered. |
| 2999 | // We do it automatically when the remote endpoint is closed so don't warn |
| 3000 | // about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 3001 | const bool stillHaveWindowHandle = |
| 3002 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 3003 | nullptr; |
| 3004 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3005 | if (notify) { |
| 3006 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3007 | "events=0x%x", |
| 3008 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3012 | // Remove the channel. |
| 3013 | d->removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3014 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3015 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3018 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | const CancelationOptions& options) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3020 | for (const auto& pair : mConnectionsByFd) { |
| 3021 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3025 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3026 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3027 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3028 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3029 | } |
| 3030 | |
| 3031 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3032 | const CancelationOptions& options, |
| 3033 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3034 | for (const auto& it : monitorsByDisplay) { |
| 3035 | const std::vector<Monitor>& monitors = it.second; |
| 3036 | for (const Monitor& monitor : monitors) { |
| 3037 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3038 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3043 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3044 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3045 | if (connection == nullptr) { |
| 3046 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3048 | |
| 3049 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3053 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3054 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | nsecs_t currentTime = now(); |
| 3059 | |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3060 | std::vector<EventEntry*> cancelationEvents = |
| 3061 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3062 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3063 | if (cancelationEvents.empty()) { |
| 3064 | return; |
| 3065 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3067 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3068 | "with reality: %s, mode=%d.", |
| 3069 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3070 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3071 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3072 | |
| 3073 | InputTarget target; |
| 3074 | sp<InputWindowHandle> windowHandle = |
| 3075 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3076 | if (windowHandle != nullptr) { |
| 3077 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3078 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3079 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3080 | } |
| 3081 | target.inputChannel = connection->inputChannel; |
| 3082 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3083 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3084 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
| 3085 | EventEntry* cancelationEventEntry = cancelationEvents[i]; |
| 3086 | switch (cancelationEventEntry->type) { |
| 3087 | case EventEntry::Type::KEY: { |
| 3088 | logOutboundKeyDetails("cancel - ", |
| 3089 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3090 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3091 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3092 | case EventEntry::Type::MOTION: { |
| 3093 | logOutboundMotionDetails("cancel - ", |
| 3094 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3095 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3096 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3097 | case EventEntry::Type::FOCUS: { |
| 3098 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); |
| 3099 | break; |
| 3100 | } |
| 3101 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3102 | case EventEntry::Type::DEVICE_RESET: { |
| 3103 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 3104 | EventEntry::typeToString(cancelationEventEntry->type)); |
| 3105 | break; |
| 3106 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | } |
| 3108 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3109 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 3110 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 3111 | |
| 3112 | cancelationEventEntry->release(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3113 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3114 | |
| 3115 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3116 | } |
| 3117 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3118 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3119 | const sp<Connection>& connection) { |
| 3120 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3121 | return; |
| 3122 | } |
| 3123 | |
| 3124 | nsecs_t currentTime = now(); |
| 3125 | |
| 3126 | std::vector<EventEntry*> downEvents = |
| 3127 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3128 | |
| 3129 | if (downEvents.empty()) { |
| 3130 | return; |
| 3131 | } |
| 3132 | |
| 3133 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3134 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3135 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3136 | #endif |
| 3137 | |
| 3138 | InputTarget target; |
| 3139 | sp<InputWindowHandle> windowHandle = |
| 3140 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3141 | if (windowHandle != nullptr) { |
| 3142 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3143 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3144 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3145 | } |
| 3146 | target.inputChannel = connection->inputChannel; |
| 3147 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3148 | |
| 3149 | for (EventEntry* downEventEntry : downEvents) { |
| 3150 | switch (downEventEntry->type) { |
| 3151 | case EventEntry::Type::MOTION: { |
| 3152 | logOutboundMotionDetails("down - ", |
| 3153 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3154 | break; |
| 3155 | } |
| 3156 | |
| 3157 | case EventEntry::Type::KEY: |
| 3158 | case EventEntry::Type::FOCUS: |
| 3159 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3160 | case EventEntry::Type::DEVICE_RESET: { |
| 3161 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
| 3162 | EventEntry::typeToString(downEventEntry->type)); |
| 3163 | break; |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref |
| 3168 | target, InputTarget::FLAG_DISPATCH_AS_IS); |
| 3169 | |
| 3170 | downEventEntry->release(); |
| 3171 | } |
| 3172 | |
| 3173 | startDispatchCycleLocked(currentTime, connection); |
| 3174 | } |
| 3175 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3176 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 3177 | BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3178 | ALOG_ASSERT(pointerIds.value != 0); |
| 3179 | |
| 3180 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3181 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3182 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3183 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3184 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3185 | uint32_t splitPointerCount = 0; |
| 3186 | |
| 3187 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3188 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3189 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3190 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3191 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3192 | if (pointerIds.hasBit(pointerId)) { |
| 3193 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3194 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3195 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3196 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3197 | splitPointerCount += 1; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (splitPointerCount != pointerIds.count()) { |
| 3202 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3203 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3204 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3205 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3206 | // in this way. |
| 3207 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3208 | "we expected there to be %d pointers. This probably means we received " |
| 3209 | "a broken sequence of pointer ids from the input device.", |
| 3210 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3211 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3212 | } |
| 3213 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3214 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3215 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3216 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3217 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3218 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3219 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3220 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3221 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3222 | if (pointerIds.hasBit(pointerId)) { |
| 3223 | if (pointerIds.count() == 1) { |
| 3224 | // The first/last pointer went down/up. |
| 3225 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3226 | ? AMOTION_EVENT_ACTION_DOWN |
| 3227 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3228 | } else { |
| 3229 | // A secondary pointer went down/up. |
| 3230 | uint32_t splitPointerIndex = 0; |
| 3231 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3232 | splitPointerIndex += 1; |
| 3233 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3234 | action = maskedAction | |
| 3235 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3236 | } |
| 3237 | } else { |
| 3238 | // An unrelated pointer changed. |
| 3239 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3240 | } |
| 3241 | } |
| 3242 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3243 | int32_t newId = mIdGenerator.nextId(); |
| 3244 | if (ATRACE_ENABLED()) { |
| 3245 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3246 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3247 | originalMotionEntry.id, newId); |
| 3248 | ATRACE_NAME(message.c_str()); |
| 3249 | } |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3250 | MotionEntry* splitMotionEntry = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3251 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, |
| 3252 | originalMotionEntry.source, originalMotionEntry.displayId, |
| 3253 | originalMotionEntry.policyFlags, action, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3254 | originalMotionEntry.actionButton, originalMotionEntry.flags, |
| 3255 | originalMotionEntry.metaState, originalMotionEntry.buttonState, |
| 3256 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, |
| 3257 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, |
| 3258 | originalMotionEntry.xCursorPosition, |
| 3259 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3260 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3261 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3262 | if (originalMotionEntry.injectionState) { |
| 3263 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3264 | splitMotionEntry->injectionState->refCount += 1; |
| 3265 | } |
| 3266 | |
| 3267 | return splitMotionEntry; |
| 3268 | } |
| 3269 | |
| 3270 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3271 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3272 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | #endif |
| 3274 | |
| 3275 | bool needWake; |
| 3276 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3277 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3278 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3279 | ConfigurationChangedEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3280 | new ConfigurationChangedEntry(args->id, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | needWake = enqueueInboundEventLocked(newEntry); |
| 3282 | } // release lock |
| 3283 | |
| 3284 | if (needWake) { |
| 3285 | mLooper->wake(); |
| 3286 | } |
| 3287 | } |
| 3288 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3289 | /** |
| 3290 | * If one of the meta shortcuts is detected, process them here: |
| 3291 | * Meta + Backspace -> generate BACK |
| 3292 | * Meta + Enter -> generate HOME |
| 3293 | * This will potentially overwrite keyCode and metaState. |
| 3294 | */ |
| 3295 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3296 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3297 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3298 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3299 | if (keyCode == AKEYCODE_DEL) { |
| 3300 | newKeyCode = AKEYCODE_BACK; |
| 3301 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3302 | newKeyCode = AKEYCODE_HOME; |
| 3303 | } |
| 3304 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3305 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3306 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3307 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3308 | keyCode = newKeyCode; |
| 3309 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3310 | } |
| 3311 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3312 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3313 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3314 | // 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] | 3315 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3316 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3317 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3318 | if (replacementIt != mReplacedKeys.end()) { |
| 3319 | keyCode = replacementIt->second; |
| 3320 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3321 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3326 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3327 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3328 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3329 | "policyFlags=0x%x, action=0x%x, " |
| 3330 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3331 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3332 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3333 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3334 | #endif |
| 3335 | if (!validateKeyEvent(args->action)) { |
| 3336 | return; |
| 3337 | } |
| 3338 | |
| 3339 | uint32_t policyFlags = args->policyFlags; |
| 3340 | int32_t flags = args->flags; |
| 3341 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3342 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3343 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3344 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3345 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3346 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3347 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3348 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3349 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3350 | metaState |= AMETA_FUNCTION_ON; |
| 3351 | } |
| 3352 | |
| 3353 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3354 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3355 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3356 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3357 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3359 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3360 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3361 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3363 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3364 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3365 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3366 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3367 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3368 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3369 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | bool needWake; |
| 3371 | { // acquire lock |
| 3372 | mLock.lock(); |
| 3373 | |
| 3374 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3375 | mLock.unlock(); |
| 3376 | |
| 3377 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3378 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3379 | return; // event was consumed by the filter |
| 3380 | } |
| 3381 | |
| 3382 | mLock.lock(); |
| 3383 | } |
| 3384 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3385 | KeyEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3386 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3387 | args->displayId, policyFlags, args->action, flags, keyCode, |
| 3388 | args->scanCode, metaState, repeatCount, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3389 | |
| 3390 | needWake = enqueueInboundEventLocked(newEntry); |
| 3391 | mLock.unlock(); |
| 3392 | } // release lock |
| 3393 | |
| 3394 | if (needWake) { |
| 3395 | mLooper->wake(); |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3400 | return mInputFilterEnabled; |
| 3401 | } |
| 3402 | |
| 3403 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3404 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3405 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3406 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3407 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3408 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3409 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3410 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3411 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3412 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3413 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3414 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3415 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3416 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3417 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3418 | "orientation=%f", |
| 3419 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3420 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3421 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3422 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3423 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3424 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3425 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3426 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3427 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3428 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3429 | } |
| 3430 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3431 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3432 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3433 | return; |
| 3434 | } |
| 3435 | |
| 3436 | uint32_t policyFlags = args->policyFlags; |
| 3437 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3438 | |
| 3439 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3440 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3441 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3442 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3443 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3444 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3445 | |
| 3446 | bool needWake; |
| 3447 | { // acquire lock |
| 3448 | mLock.lock(); |
| 3449 | |
| 3450 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3451 | mLock.unlock(); |
| 3452 | |
| 3453 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3454 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3455 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3456 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3457 | args->metaState, args->buttonState, args->classification, transform, |
| 3458 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
| 3459 | args->yCursorPosition, args->downTime, args->eventTime, |
| 3460 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 | |
| 3462 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3463 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3464 | return; // event was consumed by the filter |
| 3465 | } |
| 3466 | |
| 3467 | mLock.lock(); |
| 3468 | } |
| 3469 | |
| 3470 | // Just enqueue a new motion event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3471 | MotionEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3472 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3473 | args->displayId, policyFlags, args->action, args->actionButton, |
| 3474 | args->flags, args->metaState, args->buttonState, |
| 3475 | args->classification, args->edgeFlags, args->xPrecision, |
| 3476 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 3477 | args->downTime, args->pointerCount, args->pointerProperties, |
| 3478 | args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | |
| 3480 | needWake = enqueueInboundEventLocked(newEntry); |
| 3481 | mLock.unlock(); |
| 3482 | } // release lock |
| 3483 | |
| 3484 | if (needWake) { |
| 3485 | mLooper->wake(); |
| 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3490 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3494 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3495 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3496 | "switchMask=0x%08x", |
| 3497 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3498 | #endif |
| 3499 | |
| 3500 | uint32_t policyFlags = args->policyFlags; |
| 3501 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3502 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3506 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3507 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3508 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 | #endif |
| 3510 | |
| 3511 | bool needWake; |
| 3512 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3513 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3514 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3515 | DeviceResetEntry* newEntry = |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3516 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3517 | needWake = enqueueInboundEventLocked(newEntry); |
| 3518 | } // release lock |
| 3519 | |
| 3520 | if (needWake) { |
| 3521 | mLooper->wake(); |
| 3522 | } |
| 3523 | } |
| 3524 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3525 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 3526 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 3527 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3529 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3530 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3531 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3532 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3533 | 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] | 3534 | |
| 3535 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3536 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3537 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3538 | } |
| 3539 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3540 | std::queue<EventEntry*> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3541 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3542 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3543 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3544 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3545 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3546 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3547 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3549 | int32_t flags = incomingKey.getFlags(); |
| 3550 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3551 | int32_t metaState = incomingKey.getMetaState(); |
| 3552 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3553 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3554 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3555 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3556 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3557 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3558 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3559 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3560 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3561 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3562 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3563 | |
| 3564 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3565 | android::base::Timer t; |
| 3566 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3567 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3568 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3569 | std::to_string(t.duration().count()).c_str()); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | mLock.lock(); |
| 3574 | KeyEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3575 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), |
| 3576 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3577 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, |
| 3578 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3579 | incomingKey.getDownTime()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3580 | injectedEntries.push(injectedEntry); |
| 3581 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3582 | } |
| 3583 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3584 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3585 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3586 | int32_t action = motionEvent->getAction(); |
| 3587 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3588 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3589 | int32_t actionButton = motionEvent->getActionButton(); |
| 3590 | int32_t displayId = motionEvent->getDisplayId(); |
| 3591 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3592 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3596 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3597 | android::base::Timer t; |
| 3598 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 3599 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3600 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 3601 | std::to_string(t.duration().count()).c_str()); |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | mLock.lock(); |
| 3606 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3607 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 3608 | MotionEntry* injectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3609 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, |
| 3610 | motionEvent->getSource(), motionEvent->getDisplayId(), |
| 3611 | policyFlags, action, actionButton, motionEvent->getFlags(), |
| 3612 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3613 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), |
| 3614 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3615 | motionEvent->getRawXCursorPosition(), |
| 3616 | motionEvent->getRawYCursorPosition(), |
| 3617 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3618 | pointerProperties, samplePointerCoords, |
| 3619 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3620 | injectedEntries.push(injectedEntry); |
| 3621 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3622 | sampleEventTimes += 1; |
| 3623 | samplePointerCoords += pointerCount; |
| 3624 | MotionEntry* nextInjectedEntry = |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3625 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3626 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3627 | motionEvent->getDisplayId(), policyFlags, action, |
| 3628 | actionButton, motionEvent->getFlags(), |
| 3629 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3630 | motionEvent->getClassification(), |
| 3631 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), |
| 3632 | motionEvent->getYPrecision(), |
| 3633 | motionEvent->getRawXCursorPosition(), |
| 3634 | motionEvent->getRawYCursorPosition(), |
| 3635 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 3636 | pointerProperties, samplePointerCoords, |
| 3637 | motionEvent->getXOffset(), motionEvent->getYOffset()); |
| 3638 | injectedEntries.push(nextInjectedEntry); |
| 3639 | } |
| 3640 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3641 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3643 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3644 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3645 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3649 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3650 | injectionState->injectionIsAsync = true; |
| 3651 | } |
| 3652 | |
| 3653 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3654 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3655 | |
| 3656 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3657 | while (!injectedEntries.empty()) { |
| 3658 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); |
| 3659 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | mLock.unlock(); |
| 3663 | |
| 3664 | if (needWake) { |
| 3665 | mLooper->wake(); |
| 3666 | } |
| 3667 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3668 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3669 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3670 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3671 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3672 | if (syncMode == InputEventInjectionSync::NONE) { |
| 3673 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3674 | } else { |
| 3675 | for (;;) { |
| 3676 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3677 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3678 | break; |
| 3679 | } |
| 3680 | |
| 3681 | nsecs_t remainingTimeout = endTime - now(); |
| 3682 | if (remainingTimeout <= 0) { |
| 3683 | #if DEBUG_INJECTION |
| 3684 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3685 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3686 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3687 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3688 | break; |
| 3689 | } |
| 3690 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3691 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3692 | } |
| 3693 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3694 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 3695 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3696 | while (injectionState->pendingForegroundDispatches != 0) { |
| 3697 | #if DEBUG_INJECTION |
| 3698 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3699 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3700 | #endif |
| 3701 | nsecs_t remainingTimeout = endTime - now(); |
| 3702 | if (remainingTimeout <= 0) { |
| 3703 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3704 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 3705 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3706 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3707 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | break; |
| 3709 | } |
| 3710 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3711 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | injectionState->release(); |
| 3717 | } // release lock |
| 3718 | |
| 3719 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3720 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3721 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3722 | #endif |
| 3723 | |
| 3724 | return injectionResult; |
| 3725 | } |
| 3726 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3727 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3728 | std::array<uint8_t, 32> calculatedHmac; |
| 3729 | std::unique_ptr<VerifiedInputEvent> result; |
| 3730 | switch (event.getType()) { |
| 3731 | case AINPUT_EVENT_TYPE_KEY: { |
| 3732 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 3733 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 3734 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3735 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3736 | break; |
| 3737 | } |
| 3738 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3739 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 3740 | VerifiedMotionEvent verifiedMotionEvent = |
| 3741 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 3742 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3743 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3744 | break; |
| 3745 | } |
| 3746 | default: { |
| 3747 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 3748 | return nullptr; |
| 3749 | } |
| 3750 | } |
| 3751 | if (calculatedHmac == INVALID_HMAC) { |
| 3752 | return nullptr; |
| 3753 | } |
| 3754 | if (calculatedHmac != event.getHmac()) { |
| 3755 | return nullptr; |
| 3756 | } |
| 3757 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3758 | } |
| 3759 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3760 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3761 | return injectorUid == 0 || |
| 3762 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | } |
| 3764 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3765 | void InputDispatcher::setInjectionResult(EventEntry* entry, |
| 3766 | InputEventInjectionResult injectionResult) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3767 | InjectionState* injectionState = entry->injectionState; |
| 3768 | if (injectionState) { |
| 3769 | #if DEBUG_INJECTION |
| 3770 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3771 | "injectorPid=%d, injectorUid=%d", |
| 3772 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | #endif |
| 3774 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3775 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | // Log the outcome since the injector did not wait for the injection result. |
| 3777 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3778 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3779 | ALOGV("Asynchronous input event injection succeeded."); |
| 3780 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3781 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3782 | ALOGW("Asynchronous input event injection failed."); |
| 3783 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3784 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3785 | ALOGW("Asynchronous input event injection permission denied."); |
| 3786 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3787 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | ALOGW("Asynchronous input event injection timed out."); |
| 3789 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3790 | case InputEventInjectionResult::PENDING: |
| 3791 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 3792 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | |
| 3796 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3797 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | } |
| 3799 | } |
| 3800 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3801 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3802 | InjectionState* injectionState = entry->injectionState; |
| 3803 | if (injectionState) { |
| 3804 | injectionState->pendingForegroundDispatches += 1; |
| 3805 | } |
| 3806 | } |
| 3807 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3808 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3809 | InjectionState* injectionState = entry->injectionState; |
| 3810 | if (injectionState) { |
| 3811 | injectionState->pendingForegroundDispatches -= 1; |
| 3812 | |
| 3813 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3814 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3819 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3820 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3821 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 3822 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 3823 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3824 | } |
| 3825 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3827 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3828 | if (windowHandleToken == nullptr) { |
| 3829 | return nullptr; |
| 3830 | } |
| 3831 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3832 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3833 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3834 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3835 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3836 | return windowHandle; |
| 3837 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3838 | } |
| 3839 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3840 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | } |
| 3842 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3843 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 3844 | int displayId) const { |
| 3845 | if (windowHandleToken == nullptr) { |
| 3846 | return nullptr; |
| 3847 | } |
| 3848 | |
| 3849 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 3850 | if (windowHandle->getToken() == windowHandleToken) { |
| 3851 | return windowHandle; |
| 3852 | } |
| 3853 | } |
| 3854 | return nullptr; |
| 3855 | } |
| 3856 | |
| 3857 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 3858 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 3859 | return getWindowHandleLocked(focusedToken, displayId); |
| 3860 | } |
| 3861 | |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3862 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 3863 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3864 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3865 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3866 | if (handle->getId() == windowHandle->getId() && |
| 3867 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3868 | if (windowHandle->getInfo()->displayId != it.first) { |
| 3869 | ALOGE("Found window %s in display %" PRId32 |
| 3870 | ", but it should belong to display %" PRId32, |
| 3871 | windowHandle->getName().c_str(), it.first, |
| 3872 | windowHandle->getInfo()->displayId); |
| 3873 | } |
| 3874 | return true; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3875 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3876 | } |
| 3877 | } |
| 3878 | return false; |
| 3879 | } |
| 3880 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3881 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 3882 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 3883 | const bool noInputChannel = |
| 3884 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3885 | if (connection != nullptr && noInputChannel) { |
| 3886 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 3887 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 3888 | return false; |
| 3889 | } |
| 3890 | |
| 3891 | if (connection == nullptr) { |
| 3892 | if (!noInputChannel) { |
| 3893 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 3894 | } |
| 3895 | return false; |
| 3896 | } |
| 3897 | if (!connection->responsive) { |
| 3898 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 3899 | return false; |
| 3900 | } |
| 3901 | return true; |
| 3902 | } |
| 3903 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3904 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 3905 | const sp<IBinder>& token) const { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3906 | size_t count = mInputChannelsByToken.count(token); |
| 3907 | if (count == 0) { |
| 3908 | return nullptr; |
| 3909 | } |
| 3910 | return mInputChannelsByToken.at(token); |
| 3911 | } |
| 3912 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3913 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 3914 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 3915 | if (inputWindowHandles.empty()) { |
| 3916 | // Remove all handles on a display if there are no windows left. |
| 3917 | mWindowHandlesByDisplay.erase(displayId); |
| 3918 | return; |
| 3919 | } |
| 3920 | |
| 3921 | // Since we compare the pointer of input window handles across window updates, we need |
| 3922 | // to make sure the handle object for the same window stays unchanged across updates. |
| 3923 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3924 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3925 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3926 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | std::vector<sp<InputWindowHandle>> newHandles; |
| 3930 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 3931 | if (!handle->updateInfo()) { |
| 3932 | // handle no longer valid |
| 3933 | continue; |
| 3934 | } |
| 3935 | |
| 3936 | const InputWindowInfo* info = handle->getInfo(); |
| 3937 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 3938 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 3939 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3940 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3941 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 3942 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3943 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3944 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3945 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3946 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3947 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | if (info->displayId != displayId) { |
| 3951 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 3952 | handle->getName().c_str(), displayId, info->displayId); |
| 3953 | continue; |
| 3954 | } |
| 3955 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3956 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 3957 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3958 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3959 | oldHandle->updateFrom(handle); |
| 3960 | newHandles.push_back(oldHandle); |
| 3961 | } else { |
| 3962 | newHandles.push_back(handle); |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | // Insert or replace |
| 3967 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 3968 | } |
| 3969 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3970 | void InputDispatcher::setInputWindows( |
| 3971 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 3972 | { // acquire lock |
| 3973 | std::scoped_lock _l(mLock); |
| 3974 | for (auto const& i : handlesPerDisplay) { |
| 3975 | setInputWindowsLocked(i.second, i.first); |
| 3976 | } |
| 3977 | } |
| 3978 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3979 | mLooper->wake(); |
| 3980 | } |
| 3981 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3982 | /** |
| 3983 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 3984 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 3985 | * If set an empty list, remove all handles from the specific display. |
| 3986 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 3987 | * For removed handle, check if need to send a cancel event if already in touch. |
| 3988 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3989 | void InputDispatcher::setInputWindowsLocked( |
| 3990 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3991 | if (DEBUG_FOCUS) { |
| 3992 | std::string windowList; |
| 3993 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 3994 | windowList += iwh->getName() + " "; |
| 3995 | } |
| 3996 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 3997 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3998 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3999 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4000 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 4001 | const bool noInputWindow = |
| 4002 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4003 | if (noInputWindow && window->getToken() != nullptr) { |
| 4004 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4005 | window->getName().c_str()); |
| 4006 | window->releaseChannel(); |
| 4007 | } |
| 4008 | } |
| 4009 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4010 | // Copy old handles for release if they are no longer present. |
| 4011 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4013 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4014 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4015 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 4016 | if (mLastHoverWindowHandle && |
| 4017 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4018 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4019 | mLastHoverWindowHandle = nullptr; |
| 4020 | } |
| 4021 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4022 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 4023 | if (focusedToken) { |
| 4024 | FocusResult result = checkTokenFocusableLocked(focusedToken, displayId); |
| 4025 | if (result != FocusResult::OK) { |
| 4026 | onFocusChangedLocked(focusedToken, nullptr, displayId, typeToString(result)); |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | std::optional<FocusRequest> focusRequest = |
| 4031 | getOptionalValueByKey(mPendingFocusRequests, displayId); |
| 4032 | if (focusRequest) { |
| 4033 | // If the window from the pending request is now visible, provide it focus. |
| 4034 | FocusResult result = handleFocusRequestLocked(*focusRequest); |
| 4035 | if (result != FocusResult::NOT_VISIBLE) { |
| 4036 | // Drop the request if we were able to change the focus or we cannot change |
| 4037 | // it for another reason. |
| 4038 | mPendingFocusRequests.erase(displayId); |
| 4039 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4040 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4041 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4042 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4043 | mTouchStatesByDisplay.find(displayId); |
| 4044 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4045 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4046 | for (size_t i = 0; i < state.windows.size();) { |
| 4047 | TouchedWindow& touchedWindow = state.windows[i]; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4048 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4049 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4050 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4051 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4052 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4053 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4054 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4055 | if (touchedInputChannel != nullptr) { |
| 4056 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4057 | "touched window was removed"); |
| 4058 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4060 | state.windows.erase(state.windows.begin() + i); |
| 4061 | } else { |
| 4062 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | } |
| 4064 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4065 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4066 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4067 | // Release information for windows that are no longer present. |
| 4068 | // This ensures that unused input channels are released promptly. |
| 4069 | // Otherwise, they might stick around until the window handle is destroyed |
| 4070 | // which might not happen until the next GC. |
| 4071 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4072 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4073 | if (DEBUG_FOCUS) { |
| 4074 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4075 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4076 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4077 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4078 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4082 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4083 | if (DEBUG_FOCUS) { |
| 4084 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4085 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4086 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4087 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4088 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4090 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4091 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4092 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4093 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4094 | 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] | 4095 | } |
| 4096 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4097 | // Set the new application handle. |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4098 | if (inputApplicationHandle != nullptr) { |
| 4099 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4100 | } else { |
| 4101 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4102 | } |
| 4103 | |
| 4104 | // No matter what the old focused application was, stop waiting on it because it is |
| 4105 | // no longer focused. |
| 4106 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4107 | } // release lock |
| 4108 | |
| 4109 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4110 | mLooper->wake(); |
| 4111 | } |
| 4112 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4113 | /** |
| 4114 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4115 | * the display not specified. |
| 4116 | * |
| 4117 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4118 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4119 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4120 | * display. The display-specified events won't be affected. |
| 4121 | */ |
| 4122 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4123 | if (DEBUG_FOCUS) { |
| 4124 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4125 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4126 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4127 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4128 | |
| 4129 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4130 | sp<IBinder> oldFocusedWindowToken = |
| 4131 | getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); |
| 4132 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4133 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4134 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4135 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4136 | CancelationOptions |
| 4137 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4138 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4139 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4140 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4141 | } |
| 4142 | } |
| 4143 | mFocusedDisplayId = displayId; |
| 4144 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4145 | // Find new focused window and validate |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4146 | sp<IBinder> newFocusedWindowToken = |
| 4147 | getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 4148 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4149 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4150 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4151 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4152 | if (!mFocusedWindowTokenByDisplay.empty()) { |
| 4153 | ALOGE("But another display has a focused window\n%s", |
| 4154 | dumpFocusedWindowsLocked().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4159 | if (DEBUG_FOCUS) { |
| 4160 | logDispatchStateLocked(); |
| 4161 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4162 | } // release lock |
| 4163 | |
| 4164 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4165 | mLooper->wake(); |
| 4166 | } |
| 4167 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4168 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4169 | if (DEBUG_FOCUS) { |
| 4170 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4171 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4172 | |
| 4173 | bool changed; |
| 4174 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4175 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | |
| 4177 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4178 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4179 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | if (mDispatchEnabled && !enabled) { |
| 4183 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4184 | } |
| 4185 | |
| 4186 | mDispatchEnabled = enabled; |
| 4187 | mDispatchFrozen = frozen; |
| 4188 | changed = true; |
| 4189 | } else { |
| 4190 | changed = false; |
| 4191 | } |
| 4192 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4193 | if (DEBUG_FOCUS) { |
| 4194 | logDispatchStateLocked(); |
| 4195 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4196 | } // release lock |
| 4197 | |
| 4198 | if (changed) { |
| 4199 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4200 | mLooper->wake(); |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4205 | if (DEBUG_FOCUS) { |
| 4206 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | |
| 4209 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4210 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4211 | |
| 4212 | if (mInputFilterEnabled == enabled) { |
| 4213 | return; |
| 4214 | } |
| 4215 | |
| 4216 | mInputFilterEnabled = enabled; |
| 4217 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4218 | } // release lock |
| 4219 | |
| 4220 | // Wake up poll loop since there might be work to do to drop everything. |
| 4221 | mLooper->wake(); |
| 4222 | } |
| 4223 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4224 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4225 | std::scoped_lock lock(mLock); |
| 4226 | mInTouchMode = inTouchMode; |
| 4227 | } |
| 4228 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4229 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4230 | if (opacity < 0 || opacity > 1) { |
| 4231 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4232 | return; |
| 4233 | } |
| 4234 | |
| 4235 | std::scoped_lock lock(mLock); |
| 4236 | mMaximumObscuringOpacityForTouch = opacity; |
| 4237 | } |
| 4238 | |
| 4239 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4240 | std::scoped_lock lock(mLock); |
| 4241 | mBlockUntrustedTouchesMode = mode; |
| 4242 | } |
| 4243 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4244 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { |
| 4245 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4246 | if (DEBUG_FOCUS) { |
| 4247 | ALOGD("Trivial transfer to same window."); |
| 4248 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4249 | return true; |
| 4250 | } |
| 4251 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4252 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4253 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4254 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4255 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4256 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4257 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4258 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 | return false; |
| 4260 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4261 | if (DEBUG_FOCUS) { |
| 4262 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4263 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4266 | if (DEBUG_FOCUS) { |
| 4267 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4268 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | return false; |
| 4270 | } |
| 4271 | |
| 4272 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4273 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4274 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4275 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4276 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4277 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4278 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4279 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4281 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4283 | int32_t newTargetFlags = oldTargetFlags & |
| 4284 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4285 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4286 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4288 | found = true; |
| 4289 | goto Found; |
| 4290 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4291 | } |
| 4292 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4293 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4295 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4296 | if (DEBUG_FOCUS) { |
| 4297 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4298 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4299 | return false; |
| 4300 | } |
| 4301 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4302 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4303 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4304 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4305 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4306 | CancelationOptions |
| 4307 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4308 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4309 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4310 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4311 | } |
| 4312 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4313 | if (DEBUG_FOCUS) { |
| 4314 | logDispatchStateLocked(); |
| 4315 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | } // release lock |
| 4317 | |
| 4318 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4319 | mLooper->wake(); |
| 4320 | return true; |
| 4321 | } |
| 4322 | |
| 4323 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4324 | if (DEBUG_FOCUS) { |
| 4325 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4326 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4327 | |
| 4328 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4329 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4330 | |
| 4331 | resetKeyRepeatLocked(); |
| 4332 | releasePendingEventLocked(); |
| 4333 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4334 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4336 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4337 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4338 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4339 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4343 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4344 | dumpDispatchStateLocked(dump); |
| 4345 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4346 | std::istringstream stream(dump); |
| 4347 | std::string line; |
| 4348 | |
| 4349 | while (std::getline(stream, line, '\n')) { |
| 4350 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 | } |
| 4352 | } |
| 4353 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4354 | std::string InputDispatcher::dumpFocusedWindowsLocked() { |
| 4355 | if (mFocusedWindowTokenByDisplay.empty()) { |
| 4356 | return INDENT "FocusedWindows: <none>\n"; |
| 4357 | } |
| 4358 | |
| 4359 | std::string dump; |
| 4360 | dump += INDENT "FocusedWindows:\n"; |
| 4361 | for (auto& it : mFocusedWindowTokenByDisplay) { |
| 4362 | const int32_t displayId = it.first; |
| 4363 | const sp<InputWindowHandle> windowHandle = getFocusedWindowHandleLocked(displayId); |
| 4364 | if (windowHandle) { |
| 4365 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, |
| 4366 | windowHandle->getName().c_str()); |
| 4367 | } else { |
| 4368 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
| 4369 | " has focused token without a window'\n", |
| 4370 | displayId); |
| 4371 | } |
| 4372 | } |
| 4373 | return dump; |
| 4374 | } |
| 4375 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4376 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4377 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4378 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4379 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4380 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4382 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4383 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4384 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4385 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4386 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4387 | const std::chrono::duration timeout = |
| 4388 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4389 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4390 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4391 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4393 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4394 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4395 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4396 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4397 | dump += dumpFocusedWindowsLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4398 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4399 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4400 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4401 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4402 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4403 | 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] | 4404 | state.displayId, toString(state.down), toString(state.split), |
| 4405 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4406 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4407 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4408 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4409 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4410 | dump += StringPrintf(INDENT4 |
| 4411 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4412 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4413 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4414 | } |
| 4415 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4416 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4417 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4418 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4419 | dump += INDENT3 "Portal windows:\n"; |
| 4420 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4421 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4422 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4423 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4424 | } |
| 4425 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | } |
| 4427 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4428 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4429 | } |
| 4430 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4431 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4432 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4433 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4434 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4435 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4436 | dump += INDENT2 "Windows:\n"; |
| 4437 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4438 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4439 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4441 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4442 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
| 4443 | "hasWallpaper=%s, visible=%s, " |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4444 | "flags=%s, type=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4445 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4446 | "applicationInfo=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4447 | "touchableRegion=", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4448 | i, windowInfo->name.c_str(), windowInfo->displayId, |
| 4449 | windowInfo->portalToDisplayId, |
| 4450 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4451 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4452 | toString(windowInfo->hasWallpaper), |
| 4453 | toString(windowInfo->visible), |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4454 | windowInfo->flags.string().c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4455 | static_cast<int32_t>(windowInfo->type), |
| 4456 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4457 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4458 | windowInfo->globalScaleFactor, |
| 4459 | windowInfo->applicationInfo.name.c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4460 | dumpRegion(dump, windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4461 | dump += StringPrintf(", inputFeatures=%s", |
| 4462 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4463 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
| 4464 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4465 | windowInfo->ownerPid, windowInfo->ownerUid, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4466 | millis(windowInfo->dispatchingTimeout)); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4467 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4468 | } |
| 4469 | } else { |
| 4470 | dump += INDENT2 "Windows: <none>\n"; |
| 4471 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4472 | } |
| 4473 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4474 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4475 | } |
| 4476 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4477 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4478 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4479 | const std::vector<Monitor>& monitors = it.second; |
| 4480 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4481 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4482 | } |
| 4483 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4484 | const std::vector<Monitor>& monitors = it.second; |
| 4485 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4486 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4487 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4488 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4489 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | nsecs_t currentTime = now(); |
| 4493 | |
| 4494 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4495 | if (!mRecentQueue.empty()) { |
| 4496 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
| 4497 | for (EventEntry* entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4498 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4499 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4500 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | } |
| 4502 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4503 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | // Dump event currently being dispatched. |
| 4507 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4508 | dump += INDENT "PendingEvent:\n"; |
| 4509 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4510 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4511 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4512 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4513 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4514 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4518 | if (!mInboundQueue.empty()) { |
| 4519 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
| 4520 | for (EventEntry* entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4521 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4522 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4523 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4524 | } |
| 4525 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4526 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4529 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4530 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4531 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4532 | const KeyReplacement& replacement = pair.first; |
| 4533 | int32_t newKeyCode = pair.second; |
| 4534 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4535 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4536 | } |
| 4537 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4538 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4539 | } |
| 4540 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4541 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4542 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4543 | for (const auto& pair : mConnectionsByFd) { |
| 4544 | const sp<Connection>& connection = pair.second; |
| 4545 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4546 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4547 | pair.first, connection->getInputChannelName().c_str(), |
| 4548 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4549 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4550 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4551 | if (!connection->outboundQueue.empty()) { |
| 4552 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4553 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4554 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 4555 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4556 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4557 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4558 | } |
| 4559 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4560 | if (!connection->waitQueue.empty()) { |
| 4561 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 4562 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4563 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4565 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4566 | } |
| 4567 | } |
| 4568 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4569 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4573 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 4574 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4575 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4576 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4577 | } |
| 4578 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4579 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4580 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 4581 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 4582 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4583 | } |
| 4584 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4585 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 4586 | const size_t numMonitors = monitors.size(); |
| 4587 | for (size_t i = 0; i < numMonitors; i++) { |
| 4588 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4589 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4590 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 4591 | dump += "\n"; |
| 4592 | } |
| 4593 | } |
| 4594 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4595 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel( |
| 4596 | const std::string& name) { |
| 4597 | #if DEBUG_CHANNEL_CREATION |
| 4598 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4599 | #endif |
| 4600 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4601 | std::shared_ptr<InputChannel> serverChannel; |
| 4602 | std::unique_ptr<InputChannel> clientChannel; |
| 4603 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 4604 | |
| 4605 | if (result) { |
| 4606 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 4607 | } |
| 4608 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4609 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4610 | std::scoped_lock _l(mLock); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4611 | sp<Connection> connection = new Connection(serverChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4612 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4613 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4614 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4615 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4616 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 4618 | } // release lock |
| 4619 | |
| 4620 | // Wake the looper because some connections have changed. |
| 4621 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4622 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4623 | } |
| 4624 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4625 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor( |
| 4626 | int32_t displayId, bool isGestureMonitor, const std::string& name) { |
| 4627 | std::shared_ptr<InputChannel> serverChannel; |
| 4628 | std::unique_ptr<InputChannel> clientChannel; |
| 4629 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 4630 | if (result) { |
| 4631 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 4632 | } |
| 4633 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4634 | { // acquire lock |
| 4635 | std::scoped_lock _l(mLock); |
| 4636 | |
| 4637 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4638 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 4639 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4642 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4643 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4644 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4645 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4646 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4647 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4648 | auto& monitorsByDisplay = |
| 4649 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4650 | monitorsByDisplay[displayId].emplace_back(serverChannel); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4651 | |
| 4652 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4653 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4654 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4655 | // Wake the looper because some connections have changed. |
| 4656 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4657 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4660 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4661 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4662 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4663 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4664 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4665 | if (status) { |
| 4666 | return status; |
| 4667 | } |
| 4668 | } // release lock |
| 4669 | |
| 4670 | // Wake the poll loop because removing the connection may have changed the current |
| 4671 | // synchronization state. |
| 4672 | mLooper->wake(); |
| 4673 | return OK; |
| 4674 | } |
| 4675 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4676 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 4677 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4678 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4679 | if (connection == nullptr) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4680 | ALOGW("Attempted to unregister already unregistered input channel"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4681 | return BAD_VALUE; |
| 4682 | } |
| 4683 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4684 | removeConnectionLocked(connection); |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4685 | mInputChannelsByToken.erase(connectionToken); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4686 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4687 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4688 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4689 | } |
| 4690 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4691 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4692 | |
| 4693 | nsecs_t currentTime = now(); |
| 4694 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 4695 | |
| 4696 | connection->status = Connection::STATUS_ZOMBIE; |
| 4697 | return OK; |
| 4698 | } |
| 4699 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4700 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 4701 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 4702 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4705 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4706 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4707 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4708 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4709 | std::vector<Monitor>& monitors = it->second; |
| 4710 | const size_t numMonitors = monitors.size(); |
| 4711 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4712 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4713 | monitors.erase(monitors.begin() + i); |
| 4714 | break; |
| 4715 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4716 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4717 | if (monitors.empty()) { |
| 4718 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4719 | } else { |
| 4720 | ++it; |
| 4721 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4725 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 4726 | { // acquire lock |
| 4727 | std::scoped_lock _l(mLock); |
| 4728 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 4729 | |
| 4730 | if (!foundDisplayId) { |
| 4731 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 4732 | return BAD_VALUE; |
| 4733 | } |
| 4734 | int32_t displayId = foundDisplayId.value(); |
| 4735 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4736 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4737 | mTouchStatesByDisplay.find(displayId); |
| 4738 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4739 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 4740 | return BAD_VALUE; |
| 4741 | } |
| 4742 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4743 | TouchState& state = stateIt->second; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4744 | std::optional<int32_t> foundDeviceId; |
| 4745 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4746 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4747 | foundDeviceId = state.deviceId; |
| 4748 | } |
| 4749 | } |
| 4750 | if (!foundDeviceId || !state.down) { |
| 4751 | 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] | 4752 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4753 | return BAD_VALUE; |
| 4754 | } |
| 4755 | int32_t deviceId = foundDeviceId.value(); |
| 4756 | |
| 4757 | // Send cancel events to all the input channels we're stealing from. |
| 4758 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4759 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4760 | options.deviceId = deviceId; |
| 4761 | options.displayId = displayId; |
| 4762 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4763 | std::shared_ptr<InputChannel> channel = |
| 4764 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4765 | if (channel != nullptr) { |
| 4766 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 4767 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4768 | } |
| 4769 | // Then clear the current touch state so we stop dispatching to them as well. |
| 4770 | state.filterNonMonitors(); |
| 4771 | } |
| 4772 | return OK; |
| 4773 | } |
| 4774 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4775 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 4776 | const sp<IBinder>& token) { |
| 4777 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 4778 | const std::vector<Monitor>& monitors = it.second; |
| 4779 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4780 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4781 | return it.first; |
| 4782 | } |
| 4783 | } |
| 4784 | } |
| 4785 | return std::nullopt; |
| 4786 | } |
| 4787 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4788 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4789 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4790 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4791 | } |
| 4792 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4793 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4794 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4795 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4796 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4797 | } |
| 4798 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4799 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4800 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4801 | } |
| 4802 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4803 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4804 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4805 | removeByValue(mConnectionsByFd, connection); |
| 4806 | } |
| 4807 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4808 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 4809 | const sp<Connection>& connection, uint32_t seq, |
| 4810 | bool handled) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4811 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4812 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4813 | commandEntry->connection = connection; |
| 4814 | commandEntry->eventTime = currentTime; |
| 4815 | commandEntry->seq = seq; |
| 4816 | commandEntry->handled = handled; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4817 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4818 | } |
| 4819 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4820 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 4821 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4822 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4823 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4824 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4825 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4826 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4828 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4829 | } |
| 4830 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4831 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 4832 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4833 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4834 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4835 | commandEntry->oldToken = oldToken; |
| 4836 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4837 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4838 | } |
| 4839 | |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4840 | void InputDispatcher::onAnrLocked(const Connection& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4841 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 4842 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4843 | if (connection.waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4844 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4845 | connection.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4846 | return; |
| 4847 | } |
| 4848 | /** |
| 4849 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 4850 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 4851 | * has changed. This could cause newer entries to time out before the already dispatched |
| 4852 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 4853 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 4854 | * most useful. |
| 4855 | */ |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4856 | DispatchEntry* oldestEntry = *connection.waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4857 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 4858 | std::string reason = |
| 4859 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4860 | connection.inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4861 | ns2ms(currentWait), |
| 4862 | oldestEntry->eventEntry->getDescription().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4863 | |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4864 | updateLastAnrStateLocked(getWindowHandleLocked(connection.inputChannel->getConnectionToken()), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4865 | reason); |
| 4866 | |
| 4867 | std::unique_ptr<CommandEntry> commandEntry = |
| 4868 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4869 | commandEntry->inputApplicationHandle = nullptr; |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4870 | commandEntry->inputChannel = connection.inputChannel; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4871 | commandEntry->reason = std::move(reason); |
| 4872 | postCommandLocked(std::move(commandEntry)); |
| 4873 | } |
| 4874 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4875 | void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4876 | std::string reason = android::base::StringPrintf("%s does not have a focused window", |
| 4877 | application->getName().c_str()); |
| 4878 | |
| 4879 | updateLastAnrStateLocked(application, reason); |
| 4880 | |
| 4881 | std::unique_ptr<CommandEntry> commandEntry = |
| 4882 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); |
| 4883 | commandEntry->inputApplicationHandle = application; |
| 4884 | commandEntry->inputChannel = nullptr; |
| 4885 | commandEntry->reason = std::move(reason); |
| 4886 | postCommandLocked(std::move(commandEntry)); |
| 4887 | } |
| 4888 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 4889 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 4890 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 4891 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 4892 | commandEntry->obscuringPackage = obscuringPackage; |
| 4893 | postCommandLocked(std::move(commandEntry)); |
| 4894 | } |
| 4895 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4896 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 4897 | const std::string& reason) { |
| 4898 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 4899 | updateLastAnrStateLocked(windowLabel, reason); |
| 4900 | } |
| 4901 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4902 | void InputDispatcher::updateLastAnrStateLocked( |
| 4903 | const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4904 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); |
| 4905 | updateLastAnrStateLocked(windowLabel, reason); |
| 4906 | } |
| 4907 | |
| 4908 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 4909 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 | // 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] | 4911 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4912 | struct tm tm; |
| 4913 | localtime_r(&t, &tm); |
| 4914 | char timestr[64]; |
| 4915 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4916 | mLastAnrState.clear(); |
| 4917 | mLastAnrState += INDENT "ANR:\n"; |
| 4918 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4919 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 4920 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4921 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4922 | } |
| 4923 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4924 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4925 | mLock.unlock(); |
| 4926 | |
| 4927 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 4928 | |
| 4929 | mLock.lock(); |
| 4930 | } |
| 4931 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4932 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4933 | sp<Connection> connection = commandEntry->connection; |
| 4934 | |
| 4935 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 4936 | mLock.unlock(); |
| 4937 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4938 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4939 | |
| 4940 | mLock.lock(); |
| 4941 | } |
| 4942 | } |
| 4943 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4944 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4945 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 4946 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4947 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4948 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4949 | mLock.lock(); |
| 4950 | } |
| 4951 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4952 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4953 | sp<IBinder> token = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4954 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | mLock.unlock(); |
| 4956 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4957 | const std::chrono::nanoseconds timeoutExtension = |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4958 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 | |
| 4960 | mLock.lock(); |
| 4961 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4962 | if (timeoutExtension > 0s) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4963 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); |
| 4964 | } else { |
| 4965 | // stop waking up for events in this connection, it is already not responding |
| 4966 | sp<Connection> connection = getConnectionLocked(token); |
| 4967 | if (connection == nullptr) { |
| 4968 | return; |
| 4969 | } |
| 4970 | cancelEventsForAnrLocked(connection); |
| 4971 | } |
| 4972 | } |
| 4973 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 4974 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 4975 | mLock.unlock(); |
| 4976 | |
| 4977 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 4978 | |
| 4979 | mLock.lock(); |
| 4980 | } |
| 4981 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4982 | void InputDispatcher::extendAnrTimeoutsLocked( |
| 4983 | const std::shared_ptr<InputApplicationHandle>& application, |
| 4984 | const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) { |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4985 | if (connectionToken == nullptr && application != nullptr) { |
| 4986 | // The ANR happened because there's no focused window |
| 4987 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); |
| 4988 | mAwaitedFocusedApplication = application; |
| 4989 | } |
| 4990 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4991 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 4992 | if (connection == nullptr) { |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4993 | // It's possible that the connection already disappeared. No action necessary. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4994 | return; |
| 4995 | } |
| 4996 | |
| 4997 | 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] | 4998 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4999 | |
| 5000 | connection->responsive = true; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 5001 | const nsecs_t newTimeout = now() + timeoutExtension.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5002 | for (DispatchEntry* entry : connection->waitQueue) { |
| 5003 | if (newTimeout >= entry->timeoutTime) { |
| 5004 | // Already removed old entries when connection was marked unresponsive |
| 5005 | entry->timeoutTime = newTimeout; |
| 5006 | mAnrTracker.insert(entry->timeoutTime, connectionToken); |
| 5007 | } |
| 5008 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5012 | CommandEntry* commandEntry) { |
| 5013 | KeyEntry* entry = commandEntry->keyEntry; |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5014 | KeyEvent event = createKeyEvent(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5015 | |
| 5016 | mLock.unlock(); |
| 5017 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5018 | android::base::Timer t; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5019 | sp<IBinder> token = commandEntry->inputChannel != nullptr |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5020 | ? commandEntry->inputChannel->getConnectionToken() |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5021 | : nullptr; |
| 5022 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5023 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5024 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5025 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5027 | |
| 5028 | mLock.lock(); |
| 5029 | |
| 5030 | if (delay < 0) { |
| 5031 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 5032 | } else if (!delay) { |
| 5033 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 5034 | } else { |
| 5035 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5036 | entry->interceptKeyWakeupTime = now() + delay; |
| 5037 | } |
| 5038 | entry->release(); |
| 5039 | } |
| 5040 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5041 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5042 | mLock.unlock(); |
| 5043 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5044 | mLock.lock(); |
| 5045 | } |
| 5046 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5047 | /** |
| 5048 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5049 | * current time. |
| 5050 | */ |
| 5051 | static bool isConnectionResponsive(const Connection& connection) { |
| 5052 | const nsecs_t currentTime = now(); |
| 5053 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5054 | if (entry->timeoutTime < currentTime) { |
| 5055 | return false; |
| 5056 | } |
| 5057 | } |
| 5058 | return true; |
| 5059 | } |
| 5060 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5061 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5062 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5063 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5065 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5066 | |
| 5067 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5068 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5069 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5070 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5071 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5072 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5073 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5074 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5075 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5076 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5077 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5078 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5079 | |
| 5080 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5081 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5082 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 5083 | restartEvent = |
| 5084 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5085 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5086 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 5087 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5088 | handled); |
| 5089 | } else { |
| 5090 | restartEvent = false; |
| 5091 | } |
| 5092 | |
| 5093 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5094 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5095 | // contents of the wait queue to have been drained, so we need to double-check |
| 5096 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5097 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5098 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5099 | dispatchEntry = *dispatchEntryIt; |
| 5100 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5101 | mAnrTracker.erase(dispatchEntry->timeoutTime, |
| 5102 | connection->inputChannel->getConnectionToken()); |
| 5103 | if (!connection->responsive) { |
| 5104 | connection->responsive = isConnectionResponsive(*connection); |
| 5105 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5106 | traceWaitQueueLength(connection); |
| 5107 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5108 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5109 | traceOutboundQueueLength(connection); |
| 5110 | } else { |
| 5111 | releaseDispatchEntry(dispatchEntry); |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | // Start the next dispatch cycle for this connection. |
| 5116 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5120 | DispatchEntry* dispatchEntry, |
| 5121 | KeyEntry* keyEntry, bool handled) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5122 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5123 | if (!handled) { |
| 5124 | // Report the key as unhandled, since the fallback was not handled. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5125 | mReporter->reportUnhandledKey(keyEntry->id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5126 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5127 | return false; |
| 5128 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5129 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5130 | // Get the fallback key state. |
| 5131 | // Clear it out after dispatching the UP. |
| 5132 | int32_t originalKeyCode = keyEntry->keyCode; |
| 5133 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 5134 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 5135 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5136 | } |
| 5137 | |
| 5138 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5139 | // If the application handles the original key for which we previously |
| 5140 | // generated a fallback or if the window is not a foreground window, |
| 5141 | // then cancel the associated fallback key, if any. |
| 5142 | if (fallbackKeyCode != -1) { |
| 5143 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5144 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5145 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5146 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5147 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
| 5148 | keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5149 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5150 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5151 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5152 | |
| 5153 | mLock.unlock(); |
| 5154 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5155 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5156 | keyEntry->policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | |
| 5158 | mLock.lock(); |
| 5159 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5160 | // Cancel the fallback key. |
| 5161 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5162 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5163 | "application handled the original non-fallback key " |
| 5164 | "or is no longer a foreground target, " |
| 5165 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5166 | options.keyCode = fallbackKeyCode; |
| 5167 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5169 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5170 | } |
| 5171 | } else { |
| 5172 | // If the application did not handle a non-fallback key, first check |
| 5173 | // that we are in a good state to perform unhandled key event processing |
| 5174 | // Then ask the policy what to do with it. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5175 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5176 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5178 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5179 | "since this is not an initial down. " |
| 5180 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5181 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5182 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5183 | return false; |
| 5184 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5185 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5186 | // Dispatch the unhandled key to the policy. |
| 5187 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5188 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5189 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5190 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5191 | #endif |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5192 | KeyEvent event = createKeyEvent(*keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5193 | |
| 5194 | mLock.unlock(); |
| 5195 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5196 | bool fallback = |
| 5197 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 5198 | &event, keyEntry->policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5199 | |
| 5200 | mLock.lock(); |
| 5201 | |
| 5202 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5203 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5204 | return false; |
| 5205 | } |
| 5206 | |
| 5207 | // Latch the fallback keycode for this key on an initial down. |
| 5208 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5209 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5210 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5211 | fallbackKeyCode = event.getKeyCode(); |
| 5212 | } else { |
| 5213 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5214 | } |
| 5215 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5216 | } |
| 5217 | |
| 5218 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5219 | |
| 5220 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5221 | // We will continue to dispatch the key to the policy but we will no |
| 5222 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5223 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5224 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5225 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5226 | if (fallback) { |
| 5227 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5228 | "as a fallback for %d, but on the DOWN it had requested " |
| 5229 | "to send %d instead. Fallback canceled.", |
| 5230 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5231 | } else { |
| 5232 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5233 | "but on the DOWN it had requested to send %d. " |
| 5234 | "Fallback canceled.", |
| 5235 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5236 | } |
| 5237 | #endif |
| 5238 | |
| 5239 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5240 | "canceling fallback, policy no longer desires it"); |
| 5241 | options.keyCode = fallbackKeyCode; |
| 5242 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5243 | |
| 5244 | fallback = false; |
| 5245 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5246 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5247 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5248 | } |
| 5249 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5250 | |
| 5251 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5252 | { |
| 5253 | std::string msg; |
| 5254 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5255 | connection->inputState.getFallbackKeys(); |
| 5256 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5257 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5258 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5259 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5260 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5261 | } |
| 5262 | #endif |
| 5263 | |
| 5264 | if (fallback) { |
| 5265 | // Restart the dispatch cycle using the fallback key. |
| 5266 | keyEntry->eventTime = event.getEventTime(); |
| 5267 | keyEntry->deviceId = event.getDeviceId(); |
| 5268 | keyEntry->source = event.getSource(); |
| 5269 | keyEntry->displayId = event.getDisplayId(); |
| 5270 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5271 | keyEntry->keyCode = fallbackKeyCode; |
| 5272 | keyEntry->scanCode = event.getScanCode(); |
| 5273 | keyEntry->metaState = event.getMetaState(); |
| 5274 | keyEntry->repeatCount = event.getRepeatCount(); |
| 5275 | keyEntry->downTime = event.getDownTime(); |
| 5276 | keyEntry->syntheticRepeat = false; |
| 5277 | |
| 5278 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5279 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5280 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 5281 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5282 | #endif |
| 5283 | return true; // restart the event |
| 5284 | } else { |
| 5285 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5286 | ALOGD("Unhandled key event: No fallback key."); |
| 5287 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5288 | |
| 5289 | // Report the key as unhandled, since there is no fallback key. |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5290 | mReporter->reportUnhandledKey(keyEntry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | return false; |
| 5294 | } |
| 5295 | |
| 5296 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5297 | DispatchEntry* dispatchEntry, |
| 5298 | MotionEntry* motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5299 | return false; |
| 5300 | } |
| 5301 | |
| 5302 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5303 | mLock.unlock(); |
| 5304 | |
| 5305 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
| 5306 | |
| 5307 | mLock.lock(); |
| 5308 | } |
| 5309 | |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5310 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { |
| 5311 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5312 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5313 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 5314 | entry.repeatCount, entry.downTime, entry.eventTime); |
Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5315 | return event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5316 | } |
| 5317 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5318 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5319 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5320 | // TODO Write some statistics about how long we spend waiting. |
| 5321 | } |
| 5322 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5323 | /** |
| 5324 | * Report the touch event latency to the statsd server. |
| 5325 | * Input events are reported for statistics if: |
| 5326 | * - This is a touchscreen event |
| 5327 | * - InputFilter is not enabled |
| 5328 | * - Event is not injected or synthesized |
| 5329 | * |
| 5330 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5331 | * from getting aggregated with the "old" data. |
| 5332 | */ |
| 5333 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5334 | REQUIRES(mLock) { |
| 5335 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5336 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5337 | if (!reportForStatistics) { |
| 5338 | return; |
| 5339 | } |
| 5340 | |
| 5341 | if (mTouchStatistics.shouldReport()) { |
| 5342 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5343 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5344 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5345 | mTouchStatistics.reset(); |
| 5346 | } |
| 5347 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5348 | mTouchStatistics.addValue(latencyMicros); |
| 5349 | } |
| 5350 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5351 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5352 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5353 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5354 | } |
| 5355 | } |
| 5356 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5357 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5358 | if (ATRACE_ENABLED()) { |
| 5359 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5360 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5361 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5362 | } |
| 5363 | } |
| 5364 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5365 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5366 | if (ATRACE_ENABLED()) { |
| 5367 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5368 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5369 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | } |
| 5371 | } |
| 5372 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5373 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5374 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5375 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5376 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5377 | dumpDispatchStateLocked(dump); |
| 5378 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5379 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5380 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5381 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | void InputDispatcher::monitor() { |
| 5386 | // 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] | 5387 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5388 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5389 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5390 | } |
| 5391 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5392 | /** |
| 5393 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5394 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5395 | * this method can be safely called from any thread, as long as you've ensured that |
| 5396 | * the work you are interested in completing has already been queued. |
| 5397 | */ |
| 5398 | bool InputDispatcher::waitForIdle() { |
| 5399 | /** |
| 5400 | * Timeout should represent the longest possible time that a device might spend processing |
| 5401 | * events and commands. |
| 5402 | */ |
| 5403 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5404 | std::unique_lock lock(mLock); |
| 5405 | mLooper->wake(); |
| 5406 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5407 | return result == std::cv_status::no_timeout; |
| 5408 | } |
| 5409 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5410 | /** |
| 5411 | * Sets focus to the window identified by the token. This must be called |
| 5412 | * after updating any input window handles. |
| 5413 | * |
| 5414 | * Params: |
| 5415 | * request.token - input channel token used to identify the window that should gain focus. |
| 5416 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 5417 | * specified token does not match the currently focused window, this request will be dropped. |
| 5418 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 5419 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 5420 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 5421 | * when requesting the focus change. This determines which request gets |
| 5422 | * precedence if there is a focus change request from another source such as pointer down. |
| 5423 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5424 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 5425 | { // acquire lock |
| 5426 | std::scoped_lock _l(mLock); |
| 5427 | |
| 5428 | const int32_t displayId = request.displayId; |
| 5429 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 5430 | if (request.focusedToken && oldFocusedToken != request.focusedToken) { |
| 5431 | ALOGD_IF(DEBUG_FOCUS, |
| 5432 | "setFocusedWindow on display %" PRId32 |
| 5433 | " ignored, reason: focusedToken is not focused", |
| 5434 | displayId); |
| 5435 | return; |
| 5436 | } |
| 5437 | |
| 5438 | mPendingFocusRequests.erase(displayId); |
| 5439 | FocusResult result = handleFocusRequestLocked(request); |
| 5440 | if (result == FocusResult::NOT_VISIBLE) { |
| 5441 | // The requested window is not currently visible. Wait for the window to become visible |
| 5442 | // and then provide it focus. This is to handle situations where a user action triggers |
| 5443 | // a new window to appear. We want to be able to queue any key events after the user |
| 5444 | // action and deliver it to the newly focused window. In order for this to happen, we |
| 5445 | // take focus from the currently focused window so key events can be queued. |
| 5446 | ALOGD_IF(DEBUG_FOCUS, |
| 5447 | "setFocusedWindow on display %" PRId32 |
| 5448 | " pending, reason: window is not visible", |
| 5449 | displayId); |
| 5450 | mPendingFocusRequests[displayId] = request; |
| 5451 | onFocusChangedLocked(oldFocusedToken, nullptr, displayId, |
| 5452 | "setFocusedWindow_AwaitingWindowVisibility"); |
| 5453 | } else if (result != FocusResult::OK) { |
| 5454 | ALOGW("setFocusedWindow on display %" PRId32 " ignored, reason:%s", displayId, |
| 5455 | typeToString(result)); |
| 5456 | } |
| 5457 | } // release lock |
| 5458 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5459 | mLooper->wake(); |
| 5460 | } |
| 5461 | |
| 5462 | InputDispatcher::FocusResult InputDispatcher::handleFocusRequestLocked( |
| 5463 | const FocusRequest& request) { |
| 5464 | const int32_t displayId = request.displayId; |
| 5465 | const sp<IBinder> newFocusedToken = request.token; |
| 5466 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); |
| 5467 | |
| 5468 | if (oldFocusedToken == request.token) { |
| 5469 | ALOGD_IF(DEBUG_FOCUS, |
| 5470 | "setFocusedWindow on display %" PRId32 " ignored, reason: already focused", |
| 5471 | displayId); |
| 5472 | return FocusResult::OK; |
| 5473 | } |
| 5474 | |
| 5475 | FocusResult result = checkTokenFocusableLocked(newFocusedToken, displayId); |
| 5476 | if (result != FocusResult::OK) { |
| 5477 | return result; |
| 5478 | } |
| 5479 | |
| 5480 | std::string_view reason = |
| 5481 | (request.focusedToken) ? "setFocusedWindow_FocusCheck" : "setFocusedWindow"; |
| 5482 | onFocusChangedLocked(oldFocusedToken, newFocusedToken, displayId, reason); |
| 5483 | return FocusResult::OK; |
| 5484 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5485 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5486 | void InputDispatcher::onFocusChangedLocked(const sp<IBinder>& oldFocusedToken, |
| 5487 | const sp<IBinder>& newFocusedToken, int32_t displayId, |
| 5488 | std::string_view reason) { |
| 5489 | if (oldFocusedToken) { |
| 5490 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(oldFocusedToken); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5491 | if (focusedInputChannel) { |
| 5492 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 5493 | "focus left window"); |
| 5494 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5495 | enqueueFocusEventLocked(oldFocusedToken, false /*hasFocus*/, reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5496 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5497 | mFocusedWindowTokenByDisplay.erase(displayId); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5498 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5499 | if (newFocusedToken) { |
| 5500 | mFocusedWindowTokenByDisplay[displayId] = newFocusedToken; |
| 5501 | enqueueFocusEventLocked(newFocusedToken, true /*hasFocus*/, reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | if (mFocusedDisplayId == displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5505 | notifyFocusChangedLocked(oldFocusedToken, newFocusedToken); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5506 | } |
| 5507 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5508 | |
| 5509 | /** |
| 5510 | * Checks if the window token can be focused on a display. The token can be focused if there is |
| 5511 | * at least one window handle that is visible with the same token and all window handles with the |
| 5512 | * same token are focusable. |
| 5513 | * |
| 5514 | * In the case of mirroring, two windows may share the same window token and their visibility |
| 5515 | * might be different. Example, the mirrored window can cover the window its mirroring. However, |
| 5516 | * we expect the focusability of the windows to match since its hard to reason why one window can |
| 5517 | * receive focus events and the other cannot when both are backed by the same input channel. |
| 5518 | */ |
| 5519 | InputDispatcher::FocusResult InputDispatcher::checkTokenFocusableLocked(const sp<IBinder>& token, |
| 5520 | int32_t displayId) const { |
| 5521 | bool allWindowsAreFocusable = true; |
| 5522 | bool visibleWindowFound = false; |
| 5523 | bool windowFound = false; |
| 5524 | for (const sp<InputWindowHandle>& window : getWindowHandlesLocked(displayId)) { |
| 5525 | if (window->getToken() != token) { |
| 5526 | continue; |
| 5527 | } |
| 5528 | windowFound = true; |
| 5529 | if (window->getInfo()->visible) { |
| 5530 | // Check if at least a single window is visible. |
| 5531 | visibleWindowFound = true; |
| 5532 | } |
| 5533 | if (!window->getInfo()->focusable) { |
| 5534 | // Check if all windows with the window token are focusable. |
| 5535 | allWindowsAreFocusable = false; |
| 5536 | break; |
| 5537 | } |
| 5538 | } |
| 5539 | |
| 5540 | if (!windowFound) { |
| 5541 | return FocusResult::NO_WINDOW; |
| 5542 | } |
| 5543 | if (!allWindowsAreFocusable) { |
| 5544 | return FocusResult::NOT_FOCUSABLE; |
| 5545 | } |
| 5546 | if (!visibleWindowFound) { |
| 5547 | return FocusResult::NOT_VISIBLE; |
| 5548 | } |
| 5549 | |
| 5550 | return FocusResult::OK; |
| 5551 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5552 | } // namespace android::inputdispatcher |