| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #define LOG_TAG "InputDispatcher" | 
|  | 18 | #define ATRACE_TAG ATRACE_TAG_INPUT | 
|  | 19 |  | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 |  | 
|  | 22 | // Log detailed debug messages about each inbound event notification to the dispatcher. | 
|  | 23 | #define DEBUG_INBOUND_EVENT_DETAILS 0 | 
|  | 24 |  | 
|  | 25 | // Log detailed debug messages about each outbound event processed by the dispatcher. | 
|  | 26 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 | 
|  | 27 |  | 
|  | 28 | // Log debug messages about the dispatch cycle. | 
|  | 29 | #define DEBUG_DISPATCH_CYCLE 0 | 
|  | 30 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 31 | // Log debug messages about channel creation | 
|  | 32 | #define DEBUG_CHANNEL_CREATION 0 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | // Log debug messages about input event injection. | 
|  | 35 | #define DEBUG_INJECTION 0 | 
|  | 36 |  | 
|  | 37 | // Log debug messages about input focus tracking. | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 38 | static constexpr bool DEBUG_FOCUS = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 |  | 
|  | 40 | // Log debug messages about the app switch latency optimization. | 
|  | 41 | #define DEBUG_APP_SWITCH 0 | 
|  | 42 |  | 
|  | 43 | // Log debug messages about hover events. | 
|  | 44 | #define DEBUG_HOVER 0 | 
|  | 45 |  | 
|  | 46 | #include "InputDispatcher.h" | 
|  | 47 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 48 | #include <android-base/chrono_utils.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 49 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 50 | #include <android/os/IInputConstants.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 51 | #include <binder/Binder.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 52 | #include <input/InputDevice.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 53 | #include <input/InputWindow.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 54 | #include <log/log.h> | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 55 | #include <log/log_event_list.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 56 | #include <powermanager/PowerManager.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 57 | #include <statslog.h> | 
|  | 58 | #include <unistd.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 60 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 61 | #include <cerrno> | 
|  | 62 | #include <cinttypes> | 
|  | 63 | #include <climits> | 
|  | 64 | #include <cstddef> | 
|  | 65 | #include <ctime> | 
|  | 66 | #include <queue> | 
|  | 67 | #include <sstream> | 
|  | 68 |  | 
|  | 69 | #include "Connection.h" | 
|  | 70 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | #define INDENT "  " | 
|  | 72 | #define INDENT2 "    " | 
|  | 73 | #define INDENT3 "      " | 
|  | 74 | #define INDENT4 "        " | 
|  | 75 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 76 | using android::base::StringPrintf; | 
|  | 77 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 81 | // from which to determine an appropriate dispatching timeout. | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 82 | constexpr std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = | 
|  | 83 | std::chrono::milliseconds(android::os::IInputConstants::DEFAULT_DISPATCHING_TIMEOUT_MILLIS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | // Amount of time to allow for all pending events to be processed when an app switch | 
|  | 86 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
|  | 87 | // when an application takes too long to respond and the user has pressed an app switch key. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 88 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 |  | 
|  | 90 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) | 
|  | 91 | // before considering it stale and dropping it. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 92 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | // Log a warning when an event takes longer than this to process, even if an ANR does not occur. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 95 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
|  | 96 |  | 
|  | 97 | // Log a warning when an interception call takes longer than this to process. | 
|  | 98 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 100 | // Additional key latency in case a connection is still processing some motion events. | 
|  | 101 | // This will help with the case when a user touched a button that opens a new window, | 
|  | 102 | // and gives us the chance to dispatch the key to this new window. | 
|  | 103 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
|  | 104 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 106 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
|  | 107 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 108 | // Event log tags. See EventLogTags.logtags for reference | 
|  | 109 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; | 
|  | 110 | constexpr int LOGTAG_INPUT_FOCUS = 62001; | 
|  | 111 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | static inline nsecs_t now() { | 
|  | 113 | return systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | static inline const char* toString(bool value) { | 
|  | 117 | return value ? "true" : "false"; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 121 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
|  | 122 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
|  | 125 | static bool isValidKeyAction(int32_t action) { | 
|  | 126 | switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 127 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 128 | case AKEY_EVENT_ACTION_UP: | 
|  | 129 | return true; | 
|  | 130 | default: | 
|  | 131 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 132 | } | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | static bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 136 | if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | ALOGE("Key event has invalid action code 0x%x", action); | 
|  | 138 | return false; | 
|  | 139 | } | 
|  | 140 | return true; | 
|  | 141 | } | 
|  | 142 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 143 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 145 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 146 | case AMOTION_EVENT_ACTION_UP: | 
|  | 147 | case AMOTION_EVENT_ACTION_CANCEL: | 
|  | 148 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 149 | case AMOTION_EVENT_ACTION_OUTSIDE: | 
|  | 150 | case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
|  | 151 | case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
|  | 152 | case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
|  | 153 | case AMOTION_EVENT_ACTION_SCROLL: | 
|  | 154 | return true; | 
|  | 155 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 156 | case AMOTION_EVENT_ACTION_POINTER_UP: { | 
|  | 157 | int32_t index = getMotionEventActionPointerIndex(action); | 
|  | 158 | return index >= 0 && index < pointerCount; | 
|  | 159 | } | 
|  | 160 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
|  | 161 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
|  | 162 | return actionButton != 0; | 
|  | 163 | default: | 
|  | 164 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 168 | static int64_t millis(std::chrono::nanoseconds t) { | 
|  | 169 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | 
|  | 170 | } | 
|  | 171 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 172 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 173 | const PointerProperties* pointerProperties) { | 
|  | 174 | if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | ALOGE("Motion event has invalid action code 0x%x", action); | 
|  | 176 | return false; | 
|  | 177 | } | 
|  | 178 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 179 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 180 | pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | return false; | 
|  | 182 | } | 
|  | 183 | BitSet32 pointerIdBits; | 
|  | 184 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 185 | int32_t id = pointerProperties[i].id; | 
|  | 186 | if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 187 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
|  | 188 | MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 189 | return false; | 
|  | 190 | } | 
|  | 191 | if (pointerIdBits.hasBit(id)) { | 
|  | 192 | ALOGE("Motion event has duplicate pointer id %d", id); | 
|  | 193 | return false; | 
|  | 194 | } | 
|  | 195 | pointerIdBits.markBit(id); | 
|  | 196 | } | 
|  | 197 | return true; | 
|  | 198 | } | 
|  | 199 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 200 | static void dumpRegion(std::string& dump, const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 201 | if (region.isEmpty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 202 | dump += "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | return; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | bool first = true; | 
|  | 207 | Region::const_iterator cur = region.begin(); | 
|  | 208 | Region::const_iterator const tail = region.end(); | 
|  | 209 | while (cur != tail) { | 
|  | 210 | if (first) { | 
|  | 211 | first = false; | 
|  | 212 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 213 | dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 215 | dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | cur++; | 
|  | 217 | } | 
|  | 218 | } | 
|  | 219 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 220 | /** | 
|  | 221 | * Find the entry in std::unordered_map by key, and return it. | 
|  | 222 | * If the entry is not found, return a default constructed entry. | 
|  | 223 | * | 
|  | 224 | * Useful when the entries are vectors, since an empty vector will be returned | 
|  | 225 | * if the entry is not found. | 
|  | 226 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
|  | 227 | */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 228 | template <typename K, typename V> | 
|  | 229 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 230 | auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 231 | return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 232 | } | 
|  | 233 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 234 | /** | 
|  | 235 | * Find the entry in std::unordered_map by value, and remove it. | 
|  | 236 | * If more than one entry has the same value, then all matching | 
|  | 237 | * key-value pairs will be removed. | 
|  | 238 | * | 
|  | 239 | * Return true if at least one value has been removed. | 
|  | 240 | */ | 
|  | 241 | template <typename K, typename V> | 
|  | 242 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { | 
|  | 243 | bool removed = false; | 
|  | 244 | for (auto it = map.begin(); it != map.end();) { | 
|  | 245 | if (it->second == value) { | 
|  | 246 | it = map.erase(it); | 
|  | 247 | removed = true; | 
|  | 248 | } else { | 
|  | 249 | it++; | 
|  | 250 | } | 
|  | 251 | } | 
|  | 252 | return removed; | 
|  | 253 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 255 | /** | 
|  | 256 | * Find the entry in std::unordered_map by key and return the value as an optional. | 
|  | 257 | */ | 
|  | 258 | template <typename K, typename V> | 
|  | 259 | static std::optional<V> getOptionalValueByKey(const std::unordered_map<K, V>& map, K key) { | 
|  | 260 | auto it = map.find(key); | 
|  | 261 | return it != map.end() ? std::optional<V>{it->second} : std::nullopt; | 
|  | 262 | } | 
|  | 263 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 264 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { | 
|  | 265 | if (first == second) { | 
|  | 266 | return true; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | if (first == nullptr || second == nullptr) { | 
|  | 270 | return false; | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | return first->getToken() == second->getToken(); | 
|  | 274 | } | 
|  | 275 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 276 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
|  | 277 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; | 
|  | 278 | } | 
|  | 279 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 280 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
|  | 281 | EventEntry* eventEntry, | 
|  | 282 | int32_t inputTargetFlags) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 283 | if (inputTarget.useDefaultPointerTransform()) { | 
|  | 284 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 285 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 286 | inputTargetFlags, transform, | 
|  | 287 | inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
|  | 290 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
|  | 291 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
|  | 292 |  | 
|  | 293 | PointerCoords pointerCoords[motionEntry.pointerCount]; | 
|  | 294 |  | 
|  | 295 | // Use the first pointer information to normalize all other pointers. This could be any pointer | 
|  | 296 | // as long as all other pointers are normalized to the same value and the final DispatchEntry | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 297 | // uses the transform for the normalized pointer. | 
|  | 298 | const ui::Transform& firstPointerTransform = | 
|  | 299 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; | 
|  | 300 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 301 |  | 
|  | 302 | // Iterate through all pointers in the event to normalize against the first. | 
|  | 303 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
|  | 304 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
|  | 305 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 306 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 307 |  | 
|  | 308 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 309 | // First, apply the current pointer's transform to update the coordinates into | 
|  | 310 | // window space. | 
|  | 311 | pointerCoords[pointerIndex].transform(currTransform); | 
|  | 312 | // Next, apply the inverse transform of the normalized coordinates so the | 
|  | 313 | // current coordinates are transformed into the normalized coordinate space. | 
|  | 314 | pointerCoords[pointerIndex].transform(inverseFirstTransform); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
|  | 317 | MotionEntry* combinedMotionEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 318 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 319 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, | 
|  | 320 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, | 
|  | 321 | motionEntry.metaState, motionEntry.buttonState, | 
|  | 322 | motionEntry.classification, motionEntry.edgeFlags, | 
|  | 323 | motionEntry.xPrecision, motionEntry.yPrecision, | 
|  | 324 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, | 
|  | 325 | motionEntry.downTime, motionEntry.pointerCount, | 
|  | 326 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, | 
|  | 327 | 0 /* yOffset */); | 
|  | 328 |  | 
|  | 329 | if (motionEntry.injectionState) { | 
|  | 330 | combinedMotionEntry->injectionState = motionEntry.injectionState; | 
|  | 331 | combinedMotionEntry->injectionState->refCount += 1; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
|  | 335 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 336 | inputTargetFlags, firstPointerTransform, | 
|  | 337 | inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 338 | combinedMotionEntry->release(); | 
|  | 339 | return dispatchEntry; | 
|  | 340 | } | 
|  | 341 |  | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 342 | static void addGestureMonitors(const std::vector<Monitor>& monitors, | 
|  | 343 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, | 
|  | 344 | float yOffset = 0) { | 
|  | 345 | if (monitors.empty()) { | 
|  | 346 | return; | 
|  | 347 | } | 
|  | 348 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); | 
|  | 349 | for (const Monitor& monitor : monitors) { | 
|  | 350 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); | 
|  | 351 | } | 
|  | 352 | } | 
|  | 353 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 354 | static status_t openInputChannelPair(const std::string& name, | 
|  | 355 | std::shared_ptr<InputChannel>& serverChannel, | 
|  | 356 | std::unique_ptr<InputChannel>& clientChannel) { | 
|  | 357 | std::unique_ptr<InputChannel> uniqueServerChannel; | 
|  | 358 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); | 
|  | 359 |  | 
|  | 360 | serverChannel = std::move(uniqueServerChannel); | 
|  | 361 | return result; | 
|  | 362 | } | 
|  | 363 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 364 | const char* InputDispatcher::typeToString(InputDispatcher::FocusResult result) { | 
|  | 365 | switch (result) { | 
|  | 366 | case InputDispatcher::FocusResult::OK: | 
|  | 367 | return "Ok"; | 
|  | 368 | case InputDispatcher::FocusResult::NO_WINDOW: | 
|  | 369 | return "Window not found"; | 
|  | 370 | case InputDispatcher::FocusResult::NOT_FOCUSABLE: | 
|  | 371 | return "Window not focusable"; | 
|  | 372 | case InputDispatcher::FocusResult::NOT_VISIBLE: | 
|  | 373 | return "Window not visible"; | 
|  | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 377 | template <typename T> | 
|  | 378 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { | 
|  | 379 | if (lhs == nullptr && rhs == nullptr) { | 
|  | 380 | return true; | 
|  | 381 | } | 
|  | 382 | if (lhs == nullptr || rhs == nullptr) { | 
|  | 383 | return false; | 
|  | 384 | } | 
|  | 385 | return *lhs == *rhs; | 
|  | 386 | } | 
|  | 387 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 388 | // --- InputDispatcher --- | 
|  | 389 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 390 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
|  | 391 | : mPolicy(policy), | 
|  | 392 | mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 393 | mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 394 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 395 | mAppSwitchSawKeyDown(false), | 
|  | 396 | mAppSwitchDueTime(LONG_LONG_MAX), | 
|  | 397 | mNextUnblockedEvent(nullptr), | 
|  | 398 | mDispatchEnabled(false), | 
|  | 399 | mDispatchFrozen(false), | 
|  | 400 | mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 401 | // mInTouchMode will be initialized by the WindowManager to the default device config. | 
|  | 402 | // To avoid leaking stack in case that call never comes, and for tests, | 
|  | 403 | // initialize it here anyways. | 
|  | 404 | mInTouchMode(true), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 405 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 406 | mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 407 | mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 408 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 409 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 410 |  | 
|  | 411 | policy->getDispatcherConfiguration(&mConfig); | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | InputDispatcher::~InputDispatcher() { | 
|  | 415 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 416 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 417 |  | 
|  | 418 | resetKeyRepeatLocked(); | 
|  | 419 | releasePendingEventLocked(); | 
|  | 420 | drainInboundQueueLocked(); | 
|  | 421 | } | 
|  | 422 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 423 | while (!mConnectionsByFd.empty()) { | 
|  | 424 | sp<Connection> connection = mConnectionsByFd.begin()->second; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 425 | removeInputChannel(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 426 | } | 
|  | 427 | } | 
|  | 428 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 429 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 430 | if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 431 | return ALREADY_EXISTS; | 
|  | 432 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 433 | mThread = std::make_unique<InputThread>( | 
|  | 434 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
|  | 435 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 436 | } | 
|  | 437 |  | 
|  | 438 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 439 | if (mThread && mThread->isCallingThread()) { | 
|  | 440 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 441 | return INVALID_OPERATION; | 
|  | 442 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 443 | mThread.reset(); | 
|  | 444 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 445 | } | 
|  | 446 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 447 | void InputDispatcher::dispatchOnce() { | 
|  | 448 | nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
|  | 449 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 450 | std::scoped_lock _l(mLock); | 
|  | 451 | mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 452 |  | 
|  | 453 | // Run a dispatch loop if there are no pending commands. | 
|  | 454 | // The dispatch loop might enqueue commands to run afterwards. | 
|  | 455 | if (!haveCommandsLocked()) { | 
|  | 456 | dispatchOnceInnerLocked(&nextWakeupTime); | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | // Run all pending commands if there are any. | 
|  | 460 | // If any commands were run then force the next poll to wake up immediately. | 
|  | 461 | if (runCommandsLockedInterruptible()) { | 
|  | 462 | nextWakeupTime = LONG_LONG_MIN; | 
|  | 463 | } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 464 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 465 | // If we are still waiting for ack on some events, | 
|  | 466 | // we might have to wake up earlier to check if an app is anr'ing. | 
|  | 467 | const nsecs_t nextAnrCheck = processAnrsLocked(); | 
|  | 468 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
|  | 469 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 470 | // We are about to enter an infinitely long sleep, because we have no commands or | 
|  | 471 | // pending or queued events | 
|  | 472 | if (nextWakeupTime == LONG_LONG_MAX) { | 
|  | 473 | mDispatcherEnteredIdle.notify_all(); | 
|  | 474 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 475 | } // release lock | 
|  | 476 |  | 
|  | 477 | // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
|  | 478 | nsecs_t currentTime = now(); | 
|  | 479 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
|  | 480 | mLooper->pollOnce(timeoutMillis); | 
|  | 481 | } | 
|  | 482 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 483 | /** | 
|  | 484 | * Check if any of the connections' wait queues have events that are too old. | 
|  | 485 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
|  | 486 | * Return the time at which we should wake up next. | 
|  | 487 | */ | 
|  | 488 | nsecs_t InputDispatcher::processAnrsLocked() { | 
|  | 489 | const nsecs_t currentTime = now(); | 
|  | 490 | nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
|  | 491 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
|  | 492 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
|  | 493 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
|  | 494 | onAnrLocked(mAwaitedFocusedApplication); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 495 | mAwaitedFocusedApplication.reset(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 496 | return LONG_LONG_MIN; | 
|  | 497 | } else { | 
|  | 498 | // Keep waiting | 
|  | 499 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); | 
|  | 500 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); | 
|  | 501 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
|  | 502 | } | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | // Check if any connection ANRs are due | 
|  | 506 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
|  | 507 | if (currentTime < nextAnrCheck) { // most likely scenario | 
|  | 508 | return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | // If we reached here, we have an unresponsive connection. | 
|  | 512 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
|  | 513 | if (connection == nullptr) { | 
|  | 514 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
|  | 515 | return nextAnrCheck; | 
|  | 516 | } | 
|  | 517 | connection->responsive = false; | 
|  | 518 | // Stop waking up for this unresponsive connection | 
|  | 519 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 520 | onAnrLocked(*connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 521 | return LONG_LONG_MIN; | 
|  | 522 | } | 
|  | 523 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 524 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 525 | sp<InputWindowHandle> window = getWindowHandleLocked(token); | 
|  | 526 | if (window != nullptr) { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 527 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 528 | } | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 529 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
|  | 533 | nsecs_t currentTime = now(); | 
|  | 534 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 535 | // Reset the key repeat timer whenever normal dispatch is suspended while the | 
|  | 536 | // device is in a non-interactive state.  This is to ensure that we abort a key | 
|  | 537 | // repeat if the device is just coming out of sleep. | 
|  | 538 | if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | resetKeyRepeatLocked(); | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
|  | 543 | if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 544 | if (DEBUG_FOCUS) { | 
|  | 545 | ALOGD("Dispatch frozen.  Waiting some more."); | 
|  | 546 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 547 | return; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | // Optimize latency of app switches. | 
|  | 551 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
|  | 552 | // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
|  | 553 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
|  | 554 | if (mAppSwitchDueTime < *nextWakeupTime) { | 
|  | 555 | *nextWakeupTime = mAppSwitchDueTime; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | // Ready to start a new event. | 
|  | 559 | // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 560 | if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 561 | if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | if (isAppSwitchDue) { | 
|  | 563 | // The inbound queue is empty so the app switch key we were waiting | 
|  | 564 | // for will never arrive.  Stop waiting for it. | 
|  | 565 | resetPendingAppSwitchLocked(false); | 
|  | 566 | isAppSwitchDue = false; | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | // Synthesize a key repeat if appropriate. | 
|  | 570 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 571 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
|  | 572 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
|  | 573 | } else { | 
|  | 574 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
|  | 575 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
|  | 576 | } | 
|  | 577 | } | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | // Nothing to do if there is no pending event. | 
|  | 581 | if (!mPendingEvent) { | 
|  | 582 | return; | 
|  | 583 | } | 
|  | 584 | } else { | 
|  | 585 | // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 586 | mPendingEvent = mInboundQueue.front(); | 
|  | 587 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | traceInboundQueueLengthLocked(); | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | // Poke user activity for this event. | 
|  | 592 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 593 | pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 | } | 
|  | 596 |  | 
|  | 597 | // Now we have an event to dispatch. | 
|  | 598 | // All events are eventually dequeued and processed this way, even if we intend to drop them. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 599 | ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 601 | DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 603 | dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 604 | } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 605 | dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | } | 
|  | 607 |  | 
|  | 608 | if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 609 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | } | 
|  | 611 |  | 
|  | 612 | switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 613 | case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 614 | ConfigurationChangedEntry* typedEntry = | 
|  | 615 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); | 
|  | 616 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 617 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 618 | break; | 
|  | 619 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 620 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 621 | case EventEntry::Type::DEVICE_RESET: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 622 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); | 
|  | 623 | done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 624 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 625 | break; | 
|  | 626 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 628 | case EventEntry::Type::FOCUS: { | 
|  | 629 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); | 
|  | 630 | dispatchFocusLocked(currentTime, typedEntry); | 
|  | 631 | done = true; | 
|  | 632 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
|  | 633 | break; | 
|  | 634 | } | 
|  | 635 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 636 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 637 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); | 
|  | 638 | if (isAppSwitchDue) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 639 | if (isAppSwitchKeyEvent(*typedEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 640 | resetPendingAppSwitchLocked(true); | 
|  | 641 | isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 642 | } else if (dropReason == DropReason::NOT_DROPPED) { | 
|  | 643 | dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 644 | } | 
|  | 645 | } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 646 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 647 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 648 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 649 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 650 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 651 | } | 
|  | 652 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); | 
|  | 653 | break; | 
|  | 654 | } | 
|  | 655 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 656 | case EventEntry::Type::MOTION: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 657 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 658 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 659 | dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 661 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 662 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 663 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 664 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 665 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 666 | } | 
|  | 667 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); | 
|  | 668 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 669 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | } | 
|  | 671 |  | 
|  | 672 | if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 673 | if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 674 | dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 676 | mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 677 |  | 
|  | 678 | releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 679 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 680 | } | 
|  | 681 | } | 
|  | 682 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 683 | /** | 
|  | 684 | * Return true if the events preceding this incoming motion event should be dropped | 
|  | 685 | * Return false otherwise (the default behaviour) | 
|  | 686 | */ | 
|  | 687 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 688 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 689 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 690 |  | 
|  | 691 | // Optimize case where the current application is unresponsive and the user | 
|  | 692 | // decides to touch a window in a different application. | 
|  | 693 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 694 | // the touch into the other window. | 
|  | 695 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 696 | int32_t displayId = motionEntry.displayId; | 
|  | 697 | int32_t x = static_cast<int32_t>( | 
|  | 698 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 699 | int32_t y = static_cast<int32_t>( | 
|  | 700 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
|  | 701 | sp<InputWindowHandle> touchedWindowHandle = | 
|  | 702 | findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
|  | 703 | if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 704 | touchedWindowHandle->getApplicationToken() != | 
|  | 705 | mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 706 | // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 707 | ALOGI("Pruning input queue because user touched a different application while waiting " | 
|  | 708 | "for %s", | 
|  | 709 | mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 710 | return true; | 
|  | 711 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 |  | 
|  | 713 | // Alternatively, maybe there's a gesture monitor that could handle this event | 
|  | 714 | std::vector<TouchedMonitor> gestureMonitors = | 
|  | 715 | findTouchedGestureMonitorsLocked(displayId, {}); | 
|  | 716 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
|  | 717 | sp<Connection> connection = | 
|  | 718 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 719 | if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 720 | // This monitor could take more input. Drop all events preceding this | 
|  | 721 | // event, so that gesture monitor could get a chance to receive the stream | 
|  | 722 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
|  | 723 | "responsive gesture monitor that may handle the event", | 
|  | 724 | mAwaitedFocusedApplication->getName().c_str()); | 
|  | 725 | return true; | 
|  | 726 | } | 
|  | 727 | } | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
|  | 731 | // yet been processed by some connections, the dispatcher will wait for these motion | 
|  | 732 | // events to be processed before dispatching the key event. This is because these motion events | 
|  | 733 | // may cause a new window to be launched, which the user might expect to receive focus. | 
|  | 734 | // To prevent waiting forever for such events, just send the key to the currently focused window | 
|  | 735 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
|  | 736 | ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
|  | 737 | "just send the pending key event to the focused window."); | 
|  | 738 | mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 739 | } | 
|  | 740 | return false; | 
|  | 741 | } | 
|  | 742 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 743 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 744 | bool needWake = mInboundQueue.empty(); | 
|  | 745 | mInboundQueue.push_back(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | traceInboundQueueLengthLocked(); | 
|  | 747 |  | 
|  | 748 | switch (entry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 749 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 750 | // Optimize app switch latency. | 
|  | 751 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 752 | // the app switch key. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 753 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 754 | if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 755 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 756 | mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 757 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 758 | if (mAppSwitchSawKeyDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | #if DEBUG_APP_SWITCH | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 | ALOGD("App switch is pending!"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 761 | #endif | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 762 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 763 | mAppSwitchSawKeyDown = false; | 
|  | 764 | needWake = true; | 
|  | 765 | } | 
|  | 766 | } | 
|  | 767 | } | 
|  | 768 | break; | 
|  | 769 | } | 
|  | 770 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 771 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 772 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { | 
|  | 773 | mNextUnblockedEvent = entry; | 
|  | 774 | needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 776 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 778 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 779 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
|  | 780 | break; | 
|  | 781 | } | 
|  | 782 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 783 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 784 | // nothing to do | 
|  | 785 | break; | 
|  | 786 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | } | 
|  | 788 |  | 
|  | 789 | return needWake; | 
|  | 790 | } | 
|  | 791 |  | 
|  | 792 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { | 
|  | 793 | entry->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 794 | mRecentQueue.push_back(entry); | 
|  | 795 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
|  | 796 | mRecentQueue.front()->release(); | 
|  | 797 | mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | } | 
|  | 799 | } | 
|  | 800 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 801 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 802 | int32_t y, TouchState* touchState, | 
|  | 803 | bool addOutsideTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 804 | bool addPortalWindows) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 805 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { | 
|  | 806 | LOG_ALWAYS_FATAL( | 
|  | 807 | "Must provide a valid touch state if adding portal windows or outside targets"); | 
|  | 808 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 809 | // Traverse windows from front to back to find touched window. | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 810 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 811 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 813 | if (windowInfo->displayId == displayId) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 814 | auto flags = windowInfo->flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 |  | 
|  | 816 | if (windowInfo->visible) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 817 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
|  | 818 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && | 
|  | 819 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 820 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 821 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 822 | if (portalToDisplayId != ADISPLAY_ID_NONE && | 
|  | 823 | portalToDisplayId != displayId) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 824 | if (addPortalWindows) { | 
|  | 825 | // For the monitoring channels of the display. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 826 | touchState->addPortalWindow(windowHandle); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 827 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 828 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 829 | addOutsideTargets, addPortalWindows); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 830 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 831 | // Found window. | 
|  | 832 | return windowHandle; | 
|  | 833 | } | 
|  | 834 | } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 835 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 836 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 837 | touchState->addOrUpdateWindow(windowHandle, | 
|  | 838 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
|  | 839 | BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 840 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 841 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 842 | } | 
|  | 843 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 844 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 845 | } | 
|  | 846 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 847 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 848 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 849 | std::vector<TouchedMonitor> touchedMonitors; | 
|  | 850 |  | 
|  | 851 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
|  | 852 | addGestureMonitors(monitors, touchedMonitors); | 
|  | 853 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { | 
|  | 854 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); | 
|  | 855 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 856 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, | 
|  | 857 | -windowInfo->frameTop); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 858 | } | 
|  | 859 | return touchedMonitors; | 
|  | 860 | } | 
|  | 861 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 862 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 863 | const char* reason; | 
|  | 864 | switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | case DropReason::POLICY: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 867 | ALOGD("Dropped event because policy consumed it."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 868 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 | reason = "inbound event was dropped because the policy consumed it"; | 
|  | 870 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | case DropReason::DISABLED: | 
|  | 872 | if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 873 | ALOGI("Dropped event because input dispatch is disabled."); | 
|  | 874 | } | 
|  | 875 | reason = "inbound event was dropped because input dispatch is disabled"; | 
|  | 876 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | ALOGI("Dropped event because of pending overdue app switch."); | 
|  | 879 | reason = "inbound event was dropped because of pending overdue app switch"; | 
|  | 880 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 881 | case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 882 | ALOGI("Dropped event because the current application is not responding and the user " | 
|  | 883 | "has started interacting with a different application."); | 
|  | 884 | reason = "inbound event was dropped because the current application is not responding " | 
|  | 885 | "and the user has started interacting with a different application"; | 
|  | 886 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 887 | case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | ALOGI("Dropped event because it is stale."); | 
|  | 889 | reason = "inbound event was dropped because it is stale"; | 
|  | 890 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 891 | case DropReason::NOT_DROPPED: { | 
|  | 892 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 893 | return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 894 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | } | 
|  | 896 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 897 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 898 | case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 900 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 901 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 903 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 904 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 905 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 906 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
|  | 907 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 908 | } else { | 
|  | 909 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 910 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 911 | } | 
|  | 912 | break; | 
|  | 913 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 914 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 915 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 916 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 917 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); | 
|  | 918 | break; | 
|  | 919 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 920 | } | 
|  | 921 | } | 
|  | 922 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 923 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 924 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
|  | 925 | keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 926 | } | 
|  | 927 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 928 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
|  | 929 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
|  | 930 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 931 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | } | 
|  | 933 |  | 
|  | 934 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
|  | 935 | return mAppSwitchDueTime != LONG_LONG_MAX; | 
|  | 936 | } | 
|  | 937 |  | 
|  | 938 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
|  | 939 | mAppSwitchDueTime = LONG_LONG_MAX; | 
|  | 940 |  | 
|  | 941 | #if DEBUG_APP_SWITCH | 
|  | 942 | if (handled) { | 
|  | 943 | ALOGD("App switch has arrived."); | 
|  | 944 | } else { | 
|  | 945 | ALOGD("App switch was abandoned."); | 
|  | 946 | } | 
|  | 947 | #endif | 
|  | 948 | } | 
|  | 949 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 951 | return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 | } | 
|  | 953 |  | 
|  | 954 | bool InputDispatcher::runCommandsLockedInterruptible() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 955 | if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | return false; | 
|  | 957 | } | 
|  | 958 |  | 
|  | 959 | do { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 960 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 961 | mCommandQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 962 | Command command = commandEntry->command; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 963 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 |  | 
|  | 965 | commandEntry->connection.clear(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 966 | } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 967 | return true; | 
|  | 968 | } | 
|  | 969 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 970 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { | 
|  | 971 | mCommandQueue.push_back(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 972 | } | 
|  | 973 |  | 
|  | 974 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 975 | while (!mInboundQueue.empty()) { | 
|  | 976 | EventEntry* entry = mInboundQueue.front(); | 
|  | 977 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 978 | releaseInboundEventLocked(entry); | 
|  | 979 | } | 
|  | 980 | traceInboundQueueLengthLocked(); | 
|  | 981 | } | 
|  | 982 |  | 
|  | 983 | void InputDispatcher::releasePendingEventLocked() { | 
|  | 984 | if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 985 | releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 986 | mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 987 | } | 
|  | 988 | } | 
|  | 989 |  | 
|  | 990 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { | 
|  | 991 | InjectionState* injectionState = entry->injectionState; | 
|  | 992 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
|  | 993 | #if DEBUG_DISPATCH_CYCLE | 
|  | 994 | ALOGD("Injected inbound event was dropped."); | 
|  | 995 | #endif | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 996 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | } | 
|  | 998 | if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 999 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1000 | } | 
|  | 1001 | addRecentEventLocked(entry); | 
|  | 1002 | entry->release(); | 
|  | 1003 | } | 
|  | 1004 |  | 
|  | 1005 | void InputDispatcher::resetKeyRepeatLocked() { | 
|  | 1006 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 1007 | mKeyRepeatState.lastKeyEntry->release(); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1008 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1009 | } | 
|  | 1010 | } | 
|  | 1011 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1012 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1013 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; | 
|  | 1014 |  | 
|  | 1015 | // Reuse the repeated key entry if it is otherwise unreferenced. | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1016 | uint32_t policyFlags = entry->policyFlags & | 
|  | 1017 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | if (entry->refCount == 1) { | 
|  | 1019 | entry->recycle(); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1020 | entry->id = mIdGenerator.nextId(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1021 | entry->eventTime = currentTime; | 
|  | 1022 | entry->policyFlags = policyFlags; | 
|  | 1023 | entry->repeatCount += 1; | 
|  | 1024 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1025 | KeyEntry* newEntry = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1026 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1027 | entry->displayId, policyFlags, entry->action, entry->flags, | 
|  | 1028 | entry->keyCode, entry->scanCode, entry->metaState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1029 | entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 |  | 
|  | 1031 | mKeyRepeatState.lastKeyEntry = newEntry; | 
|  | 1032 | entry->release(); | 
|  | 1033 |  | 
|  | 1034 | entry = newEntry; | 
|  | 1035 | } | 
|  | 1036 | entry->syntheticRepeat = true; | 
|  | 1037 |  | 
|  | 1038 | // Increment reference count since we keep a reference to the event in | 
|  | 1039 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. | 
|  | 1040 | entry->refCount += 1; | 
|  | 1041 |  | 
|  | 1042 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
|  | 1043 | return entry; | 
|  | 1044 | } | 
|  | 1045 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1046 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
|  | 1047 | ConfigurationChangedEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1049 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | #endif | 
|  | 1051 |  | 
|  | 1052 | // Reset key repeating in case a keyboard device was added or removed or something. | 
|  | 1053 | resetKeyRepeatLocked(); | 
|  | 1054 |  | 
|  | 1055 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1056 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 1057 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | commandEntry->eventTime = entry->eventTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1059 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 | return true; | 
|  | 1061 | } | 
|  | 1062 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1063 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1065 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1066 | entry->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | #endif | 
|  | 1068 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1069 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1070 | options.deviceId = entry->deviceId; | 
|  | 1071 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1072 | return true; | 
|  | 1073 | } | 
|  | 1074 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1075 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1076 | std::string_view reason) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1077 | if (mPendingEvent != nullptr) { | 
|  | 1078 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 1079 | // for the pending event to get dispatched to the newly focused window | 
|  | 1080 | mInboundQueue.push_front(mPendingEvent); | 
|  | 1081 | mPendingEvent = nullptr; | 
|  | 1082 | } | 
|  | 1083 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1084 | FocusEntry* focusEntry = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1085 | new FocusEntry(mIdGenerator.nextId(), now(), windowToken, hasFocus, reason); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1086 |  | 
|  | 1087 | // This event should go to the front of the queue, but behind all other focus events | 
|  | 1088 | // Find the last focus event, and insert right after it | 
|  | 1089 | std::deque<EventEntry*>::reverse_iterator it = | 
|  | 1090 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
|  | 1091 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); | 
|  | 1092 |  | 
|  | 1093 | // Maintain the order of focus events. Insert the entry after all other focus events. | 
|  | 1094 | mInboundQueue.insert(it.base(), focusEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1095 | } | 
|  | 1096 |  | 
|  | 1097 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1098 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1099 | if (channel == nullptr) { | 
|  | 1100 | return; // Window has gone away | 
|  | 1101 | } | 
|  | 1102 | InputTarget target; | 
|  | 1103 | target.inputChannel = channel; | 
|  | 1104 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1105 | entry->dispatchInProgress = true; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1106 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + | 
|  | 1107 | channel->getName(); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1108 | std::string reason = std::string("reason=").append(entry->reason); | 
|  | 1109 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1110 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1111 | } | 
|  | 1112 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1113 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1115 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1116 | if (!entry->dispatchInProgress) { | 
|  | 1117 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
|  | 1118 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1119 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
|  | 1120 | if (mKeyRepeatState.lastKeyEntry && | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1121 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | // We have seen two identical key downs in a row which indicates that the device | 
|  | 1123 | // driver is automatically generating key repeats itself.  We take note of the | 
|  | 1124 | // repeat here, but we disable our own next key repeat timer since it is clear that | 
|  | 1125 | // we will not need to synthesize key repeats ourselves. | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1126 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { | 
|  | 1127 | // Make sure we don't get key down from a different device. If a different | 
|  | 1128 | // device Id has same key pressed down, the new device Id will replace the | 
|  | 1129 | // current one to hold the key repeat with repeat count reset. | 
|  | 1130 | // In the future when got a KEY_UP on the device id, drop it and do not | 
|  | 1131 | // stop the key repeat on current device. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
|  | 1133 | resetKeyRepeatLocked(); | 
|  | 1134 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
|  | 1135 | } else { | 
|  | 1136 | // Not a repeat.  Save key down state in case we do see a repeat later. | 
|  | 1137 | resetKeyRepeatLocked(); | 
|  | 1138 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
|  | 1139 | } | 
|  | 1140 | mKeyRepeatState.lastKeyEntry = entry; | 
|  | 1141 | entry->refCount += 1; | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1142 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && | 
|  | 1143 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { | 
|  | 1144 | // The stale device releases the key, reset staleDeviceId. | 
|  | 1145 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 1146 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); | 
|  | 1147 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1148 | } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1149 | resetKeyRepeatLocked(); | 
|  | 1150 | } | 
|  | 1151 |  | 
|  | 1152 | if (entry->repeatCount == 1) { | 
|  | 1153 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1154 | } else { | 
|  | 1155 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1156 | } | 
|  | 1157 |  | 
|  | 1158 | entry->dispatchInProgress = true; | 
|  | 1159 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1160 | logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 | } | 
|  | 1162 |  | 
|  | 1163 | // Handle case where the policy asked us to try again later last time. | 
|  | 1164 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1165 | if (currentTime < entry->interceptKeyWakeupTime) { | 
|  | 1166 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
|  | 1167 | *nextWakeupTime = entry->interceptKeyWakeupTime; | 
|  | 1168 | } | 
|  | 1169 | return false; // wait until next wakeup | 
|  | 1170 | } | 
|  | 1171 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1172 | entry->interceptKeyWakeupTime = 0; | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | // Give the policy a chance to intercept the key. | 
|  | 1176 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
|  | 1177 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1178 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
| Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1179 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1180 | sp<IBinder> focusedWindowToken = | 
|  | 1181 | getValueByKey(mFocusedWindowTokenByDisplay, getTargetDisplayId(*entry)); | 
|  | 1182 | if (focusedWindowToken != nullptr) { | 
|  | 1183 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | } | 
|  | 1185 | commandEntry->keyEntry = entry; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1186 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | entry->refCount += 1; | 
|  | 1188 | return false; // wait for the command to run | 
|  | 1189 | } else { | 
|  | 1190 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 1191 | } | 
|  | 1192 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1193 | if (*dropReason == DropReason::NOT_DROPPED) { | 
|  | 1194 | *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1195 | } | 
|  | 1196 | } | 
|  | 1197 |  | 
|  | 1198 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1199 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1200 | setInjectionResult(entry, | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1201 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1202 | : INPUT_EVENT_INJECTION_FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1203 | mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | return true; | 
|  | 1205 | } | 
|  | 1206 |  | 
|  | 1207 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1208 | std::vector<InputTarget> inputTargets; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1209 | int32_t injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1210 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
|  | 1212 | return false; | 
|  | 1213 | } | 
|  | 1214 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1215 | setInjectionResult(entry, injectionResult); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { | 
|  | 1217 | return true; | 
|  | 1218 | } | 
|  | 1219 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1220 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1221 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 |  | 
|  | 1223 | // Dispatch the key. | 
|  | 1224 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1225 | return true; | 
|  | 1226 | } | 
|  | 1227 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1228 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1230 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1231 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
|  | 1232 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1233 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1234 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 1235 | entry.repeatCount, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | #endif | 
|  | 1237 | } | 
|  | 1238 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1239 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, | 
|  | 1240 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1241 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1242 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1243 | if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | entry->dispatchInProgress = true; | 
|  | 1245 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1246 | logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1250 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1251 | setInjectionResult(entry, | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1252 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1253 | : INPUT_EVENT_INJECTION_FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | return true; | 
|  | 1255 | } | 
|  | 1256 |  | 
|  | 1257 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
|  | 1258 |  | 
|  | 1259 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1260 | std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 |  | 
|  | 1262 | bool conflictingPointerActions = false; | 
|  | 1263 | int32_t injectionResult; | 
|  | 1264 | if (isPointerEvent) { | 
|  | 1265 | // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1266 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1267 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1268 | &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 | } else { | 
|  | 1270 | // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1271 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1272 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1273 | } | 
|  | 1274 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
|  | 1275 | return false; | 
|  | 1276 | } | 
|  | 1277 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1278 | setInjectionResult(entry, injectionResult); | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1279 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { | 
|  | 1280 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
|  | 1281 | return true; | 
|  | 1282 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1284 | CancelationOptions::Mode mode(isPointerEvent | 
|  | 1285 | ? CancelationOptions::CANCEL_POINTER_EVENTS | 
|  | 1286 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
|  | 1287 | CancelationOptions options(mode, "input event injection failed"); | 
|  | 1288 | synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1289 | return true; | 
|  | 1290 | } | 
|  | 1291 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1292 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1293 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1294 |  | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1295 | if (isPointerEvent) { | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1296 | std::unordered_map<int32_t, TouchState>::iterator it = | 
|  | 1297 | mTouchStatesByDisplay.find(entry->displayId); | 
|  | 1298 | if (it != mTouchStatesByDisplay.end()) { | 
|  | 1299 | const TouchState& state = it->second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1300 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1301 | // The event has gone through these portal windows, so we add monitoring targets of | 
|  | 1302 | // the corresponding displays as well. | 
|  | 1303 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1304 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1305 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1306 | -windowInfo->frameLeft, -windowInfo->frameTop); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1307 | } | 
|  | 1308 | } | 
|  | 1309 | } | 
|  | 1310 | } | 
|  | 1311 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1312 | // Dispatch the motion. | 
|  | 1313 | if (conflictingPointerActions) { | 
|  | 1314 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1315 | "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1316 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1317 | } | 
|  | 1318 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1319 | return true; | 
|  | 1320 | } | 
|  | 1321 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1322 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1323 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1324 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1325 | ", policyFlags=0x%x, " | 
|  | 1326 | "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
|  | 1327 | "metaState=0x%x, buttonState=0x%x," | 
|  | 1328 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1329 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1330 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, | 
|  | 1331 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1332 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1333 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1334 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1335 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 1336 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 1337 | "orientation=%f", | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1338 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
|  | 1339 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 1340 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 1341 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 1342 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 1343 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 1344 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 1345 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 1346 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 1347 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 | } | 
|  | 1349 | #endif | 
|  | 1350 | } | 
|  | 1351 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1352 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, | 
|  | 1353 | const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1354 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1355 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1356 | ALOGD("dispatchEventToCurrentInputTargets"); | 
|  | 1357 | #endif | 
|  | 1358 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1359 | updateInteractionTokensLocked(*eventEntry, inputTargets); | 
|  | 1360 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1361 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
|  | 1362 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1363 | pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1365 | for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1366 | sp<Connection> connection = | 
|  | 1367 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1368 | if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1369 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1370 | } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1371 | if (DEBUG_FOCUS) { | 
|  | 1372 | ALOGD("Dropping event delivery to target with channel '%s' because it " | 
|  | 1373 | "is no longer registered with the input dispatcher.", | 
|  | 1374 | inputTarget.inputChannel->getName().c_str()); | 
|  | 1375 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 | } | 
|  | 1377 | } | 
|  | 1378 | } | 
|  | 1379 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1380 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
|  | 1381 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
|  | 1382 | // If the policy decides to close the app, we will get a channel removal event via | 
|  | 1383 | // unregisterInputChannel, and will clean up the connection that way. We are already not | 
|  | 1384 | // sending new pointers to the connection when it blocked, but focused events will continue to | 
|  | 1385 | // pile up. | 
|  | 1386 | ALOGW("Canceling events for %s because it is unresponsive", | 
|  | 1387 | connection->inputChannel->getName().c_str()); | 
|  | 1388 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 1389 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
|  | 1390 | "application not responding"); | 
|  | 1391 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1392 | } | 
|  | 1393 | } | 
|  | 1394 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1395 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1396 | if (DEBUG_FOCUS) { | 
|  | 1397 | ALOGD("Resetting ANR timeouts."); | 
|  | 1398 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1399 |  | 
|  | 1400 | // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1401 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1402 | mAwaitedFocusedApplication.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1403 | } | 
|  | 1404 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1405 | /** | 
|  | 1406 | * Get the display id that the given event should go to. If this event specifies a valid display id, | 
|  | 1407 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
|  | 1408 | * Focused display is the display that the user most recently interacted with. | 
|  | 1409 | */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1410 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1411 | int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1412 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1413 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1414 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 1415 | displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1416 | break; | 
|  | 1417 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1418 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1419 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1420 | displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1421 | break; | 
|  | 1422 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1423 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1424 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 1425 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 1426 | 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] | 1427 | return ADISPLAY_ID_NONE; | 
|  | 1428 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1429 | } | 
|  | 1430 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
|  | 1431 | } | 
|  | 1432 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1433 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
|  | 1434 | const char* focusedWindowName) { | 
|  | 1435 | if (mAnrTracker.empty()) { | 
|  | 1436 | // already processed all events that we waited for | 
|  | 1437 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1438 | return false; | 
|  | 1439 | } | 
|  | 1440 |  | 
|  | 1441 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
|  | 1442 | // Start the timer | 
|  | 1443 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " | 
|  | 1444 | "focus to change", | 
|  | 1445 | focusedWindowName); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1446 | mKeyIsWaitingForEventsTimeout = currentTime + | 
|  | 1447 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) | 
|  | 1448 | .count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1449 | return true; | 
|  | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | // We still have pending events, and already started the timer | 
|  | 1453 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
|  | 1454 | return true; // Still waiting | 
|  | 1455 | } | 
|  | 1456 |  | 
|  | 1457 | // Waited too long, and some connection still hasn't processed all motions | 
|  | 1458 | // Just send the key to the focused window | 
|  | 1459 | ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
|  | 1460 | focusedWindowName); | 
|  | 1461 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1462 | return false; | 
|  | 1463 | } | 
|  | 1464 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1466 | const EventEntry& entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1467 | std::vector<InputTarget>& inputTargets, | 
|  | 1468 | nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1469 | std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1470 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1471 | int32_t displayId = getTargetDisplayId(entry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1472 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1473 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1474 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 1475 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1476 | // If there is no currently focused window and no focused application | 
|  | 1477 | // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1478 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
|  | 1479 | ALOGI("Dropping %s event because there is no focused window or focused application in " | 
|  | 1480 | "display %" PRId32 ".", | 
|  | 1481 | EventEntry::typeToString(entry.type), displayId); | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1482 | return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1483 | } | 
|  | 1484 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1485 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
|  | 1486 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
|  | 1487 | // start interacting with another application via touch (app switch). This code can be removed | 
|  | 1488 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
|  | 1489 | // an app is expected to have a focused window. | 
|  | 1490 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
|  | 1491 | if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
|  | 1492 | // 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] | 1493 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( | 
|  | 1494 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
|  | 1495 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1496 | mAwaitedFocusedApplication = focusedApplicationHandle; | 
|  | 1497 | ALOGW("Waiting because no window has focus but %s may eventually add a " | 
|  | 1498 | "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1499 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1500 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
|  | 1501 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1502 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
|  | 1503 | // Already raised ANR. Drop the event | 
|  | 1504 | ALOGE("Dropping %s event because there is no focused window", | 
|  | 1505 | EventEntry::typeToString(entry.type)); | 
|  | 1506 | return INPUT_EVENT_INJECTION_FAILED; | 
|  | 1507 | } else { | 
|  | 1508 | // Still waiting for the focused window | 
|  | 1509 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1510 | } | 
|  | 1511 | } | 
|  | 1512 |  | 
|  | 1513 | // we have a valid, non-null focused window | 
|  | 1514 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 1515 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1516 | // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1517 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1518 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 | } | 
|  | 1520 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1521 | if (focusedWindowHandle->getInfo()->paused) { | 
|  | 1522 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
|  | 1523 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1524 | } | 
|  | 1525 |  | 
|  | 1526 | // If the event is a key event, then we must wait for all previous events to | 
|  | 1527 | // complete before delivering it because previous events may have the | 
|  | 1528 | // side-effect of transferring focus to a different window and we want to | 
|  | 1529 | // ensure that the following keys are sent to the new window. | 
|  | 1530 | // | 
|  | 1531 | // Suppose the user touches a button in a window then immediately presses "A". | 
|  | 1532 | // If the button causes a pop-up window to appear then we want to ensure that | 
|  | 1533 | // the "A" key is delivered to the new pop-up window.  This is because users | 
|  | 1534 | // often anticipate pending UI changes when typing on a keyboard. | 
|  | 1535 | // To obtain this behavior, we must serialize key events with respect to all | 
|  | 1536 | // prior input events. | 
|  | 1537 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 1538 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
|  | 1539 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
|  | 1540 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1541 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 | } | 
|  | 1543 |  | 
|  | 1544 | // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1545 | addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1546 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1547 | BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1548 |  | 
|  | 1549 | // Done. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1550 | return INPUT_EVENT_INJECTION_SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1551 | } | 
|  | 1552 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1553 | /** | 
|  | 1554 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
|  | 1555 | * that are currently unresponsive. | 
|  | 1556 | */ | 
|  | 1557 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
|  | 1558 | const std::vector<TouchedMonitor>& monitors) const { | 
|  | 1559 | std::vector<TouchedMonitor> responsiveMonitors; | 
|  | 1560 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
|  | 1561 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
|  | 1562 | sp<Connection> connection = getConnectionLocked( | 
|  | 1563 | monitor.monitor.inputChannel->getConnectionToken()); | 
|  | 1564 | if (connection == nullptr) { | 
|  | 1565 | ALOGE("Could not find connection for monitor %s", | 
|  | 1566 | monitor.monitor.inputChannel->getName().c_str()); | 
|  | 1567 | return false; | 
|  | 1568 | } | 
|  | 1569 | if (!connection->responsive) { | 
|  | 1570 | ALOGW("Unresponsive monitor %s will not get the new gesture", | 
|  | 1571 | connection->inputChannel->getName().c_str()); | 
|  | 1572 | return false; | 
|  | 1573 | } | 
|  | 1574 | return true; | 
|  | 1575 | }); | 
|  | 1576 | return responsiveMonitors; | 
|  | 1577 | } | 
|  | 1578 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1580 | const MotionEntry& entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1581 | std::vector<InputTarget>& inputTargets, | 
|  | 1582 | nsecs_t* nextWakeupTime, | 
|  | 1583 | bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1584 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1585 | enum InjectionPermission { | 
|  | 1586 | INJECTION_PERMISSION_UNKNOWN, | 
|  | 1587 | INJECTION_PERMISSION_GRANTED, | 
|  | 1588 | INJECTION_PERMISSION_DENIED | 
|  | 1589 | }; | 
|  | 1590 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | // For security reasons, we defer updating the touch state until we are sure that | 
|  | 1592 | // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1593 | int32_t displayId = entry.displayId; | 
|  | 1594 | int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1595 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
|  | 1596 |  | 
|  | 1597 | // Update the touch state as needed based on the properties of the touch event. | 
|  | 1598 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; | 
|  | 1599 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1600 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); | 
|  | 1601 | sp<InputWindowHandle> newTouchedWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1602 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1603 | // Copy current touch state into tempTouchState. | 
|  | 1604 | // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
|  | 1605 | // 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] | 1606 | const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1607 | TouchState tempTouchState; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1608 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
|  | 1609 | mTouchStatesByDisplay.find(displayId); | 
|  | 1610 | if (oldStateIt != mTouchStatesByDisplay.end()) { | 
|  | 1611 | oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1612 | tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1613 | } | 
|  | 1614 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1615 | bool isSplit = tempTouchState.split; | 
|  | 1616 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
|  | 1617 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
|  | 1618 | tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1619 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
|  | 1620 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1621 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
|  | 1622 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
|  | 1623 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1624 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1625 | bool wrongDevice = false; | 
|  | 1626 | if (newGesture) { | 
|  | 1627 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1628 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1629 | ALOGI("Dropping event because a pointer for a different device is already down " | 
|  | 1630 | "in display %" PRId32, | 
|  | 1631 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1632 | // TODO: test multiple simultaneous input streams. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1633 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1634 | switchedDevice = false; | 
|  | 1635 | wrongDevice = true; | 
|  | 1636 | goto Failed; | 
|  | 1637 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1638 | tempTouchState.reset(); | 
|  | 1639 | tempTouchState.down = down; | 
|  | 1640 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1641 | tempTouchState.source = entry.source; | 
|  | 1642 | tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1644 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1645 | ALOGI("Dropping move event because a pointer for a different device is already active " | 
|  | 1646 | "in display %" PRId32, | 
|  | 1647 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1648 | // TODO: test multiple simultaneous input streams. | 
|  | 1649 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
|  | 1650 | switchedDevice = false; | 
|  | 1651 | wrongDevice = true; | 
|  | 1652 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1653 | } | 
|  | 1654 |  | 
|  | 1655 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
|  | 1656 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
|  | 1657 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1658 | int32_t x; | 
|  | 1659 | int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1660 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1661 | // Always dispatch mouse events to cursor position. | 
|  | 1662 | if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1663 | x = int32_t(entry.xCursorPosition); | 
|  | 1664 | y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1665 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1666 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1667 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1668 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1669 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1670 | newTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1671 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
|  | 1672 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1673 |  | 
|  | 1674 | std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1675 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1676 | : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1677 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1679 | if (newTouchedWindowHandle != nullptr && | 
|  | 1680 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1681 | // New window supports splitting, but we should never split mouse events. | 
|  | 1682 | isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1683 | } else if (isSplit) { | 
|  | 1684 | // New window does not support splitting but we have already split events. | 
|  | 1685 | // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1686 | newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1687 | } | 
|  | 1688 |  | 
|  | 1689 | // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1690 | if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1691 | // 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] | 1692 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1693 | } | 
|  | 1694 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1695 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
|  | 1696 | ALOGI("Not sending touch event to %s because it is paused", | 
|  | 1697 | newTouchedWindowHandle->getName().c_str()); | 
|  | 1698 | newTouchedWindowHandle = nullptr; | 
|  | 1699 | } | 
|  | 1700 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1701 | // Ensure the window has a connection and the connection is responsive | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1702 | if (newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1703 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); | 
|  | 1704 | if (!isResponsive) { | 
|  | 1705 | ALOGW("%s will not receive the new gesture at %" PRIu64, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1706 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
|  | 1707 | newTouchedWindowHandle = nullptr; | 
|  | 1708 | } | 
|  | 1709 | } | 
|  | 1710 |  | 
|  | 1711 | // Also don't send the new touch event to unresponsive gesture monitors | 
|  | 1712 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
|  | 1713 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1714 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
|  | 1715 | 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] | 1716 | "(%d, %d) in display %" PRId32 ".", | 
|  | 1717 | x, y, displayId); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1718 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1719 | goto Failed; | 
|  | 1720 | } | 
|  | 1721 |  | 
|  | 1722 | if (newTouchedWindowHandle != nullptr) { | 
|  | 1723 | // Set target flags. | 
|  | 1724 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1725 | if (isSplit) { | 
|  | 1726 | targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1727 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1728 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 1729 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 1730 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 1731 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1732 | } | 
|  | 1733 |  | 
|  | 1734 | // Update hover state. | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1735 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 1736 | newHoverWindowHandle = nullptr; | 
|  | 1737 | } else if (isHoverAction) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1738 | newHoverWindowHandle = newTouchedWindowHandle; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1739 | } | 
|  | 1740 |  | 
|  | 1741 | // Update the temporary touch state. | 
|  | 1742 | BitSet32 pointerIds; | 
|  | 1743 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1744 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1745 | pointerIds.markBit(pointerId); | 
|  | 1746 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1747 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1748 | } | 
|  | 1749 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1750 | tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1751 | } else { | 
|  | 1752 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
|  | 1753 |  | 
|  | 1754 | // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1755 | if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1756 | if (DEBUG_FOCUS) { | 
|  | 1757 | ALOGD("Dropping event because the pointer is not down or we previously " | 
|  | 1758 | "dropped the pointer down event in display %" PRId32, | 
|  | 1759 | displayId); | 
|  | 1760 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1761 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1762 | goto Failed; | 
|  | 1763 | } | 
|  | 1764 |  | 
|  | 1765 | // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1766 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1767 | tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1768 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1769 | 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] | 1770 |  | 
|  | 1771 | sp<InputWindowHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1772 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1773 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1774 | if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
|  | 1775 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1776 | if (DEBUG_FOCUS) { | 
|  | 1777 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
|  | 1778 | oldTouchedWindowHandle->getName().c_str(), | 
|  | 1779 | newTouchedWindowHandle->getName().c_str(), displayId); | 
|  | 1780 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1782 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
|  | 1783 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
|  | 1784 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1785 |  | 
|  | 1786 | // Make a slippery entrance into the new window. | 
|  | 1787 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
|  | 1788 | isSplit = true; | 
|  | 1789 | } | 
|  | 1790 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1791 | int32_t targetFlags = | 
|  | 1792 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | if (isSplit) { | 
|  | 1794 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 1795 | } | 
|  | 1796 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 1797 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 1798 | } | 
|  | 1799 |  | 
|  | 1800 | BitSet32 pointerIds; | 
|  | 1801 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1802 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1803 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1804 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1805 | } | 
|  | 1806 | } | 
|  | 1807 | } | 
|  | 1808 |  | 
|  | 1809 | if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1810 | // Let the previous window know that the hover sequence is over, unless we already did it | 
|  | 1811 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 1812 | if (mLastHoverWindowHandle != nullptr && | 
|  | 1813 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || | 
|  | 1814 | mLastHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1815 | #if DEBUG_HOVER | 
|  | 1816 | ALOGD("Sending hover exit event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1817 | mLastHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1818 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1819 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
|  | 1820 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | } | 
|  | 1822 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1823 | // Let the new window know that the hover sequence is starting, unless we already did it | 
|  | 1824 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 1825 | if (newHoverWindowHandle != nullptr && | 
|  | 1826 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1827 | newHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | #if DEBUG_HOVER | 
|  | 1829 | ALOGD("Sending hover enter event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1830 | newHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1832 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
|  | 1833 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
|  | 1834 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | } | 
|  | 1836 | } | 
|  | 1837 |  | 
|  | 1838 | // Check permission to inject into all touched foreground windows and ensure there | 
|  | 1839 | // is at least one touched foreground window. | 
|  | 1840 | { | 
|  | 1841 | bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1842 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1843 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
|  | 1844 | haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1845 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1846 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
|  | 1847 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 1848 | goto Failed; | 
|  | 1849 | } | 
|  | 1850 | } | 
|  | 1851 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1852 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1853 | if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1854 | ALOGI("Dropping event because there is no touched foreground window in display " | 
|  | 1855 | "%" PRId32 " or gesture monitor to receive it.", | 
|  | 1856 | displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1857 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1858 | goto Failed; | 
|  | 1859 | } | 
|  | 1860 |  | 
|  | 1861 | // Permission granted to injection into all touched foreground windows. | 
|  | 1862 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 1863 | } | 
|  | 1864 |  | 
|  | 1865 | // Check whether windows listening for outside touches are owned by the same UID. If it is | 
|  | 1866 | // set the policy flag that we will not reveal coordinate information to this window. | 
|  | 1867 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1868 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1869 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1870 | if (foregroundWindowHandle) { | 
|  | 1871 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1872 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1873 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 1874 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; | 
|  | 1875 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1876 | tempTouchState.addOrUpdateWindow(inputWindowHandle, | 
|  | 1877 | InputTarget::FLAG_ZERO_COORDS, | 
|  | 1878 | BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1879 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | } | 
|  | 1881 | } | 
|  | 1882 | } | 
|  | 1883 | } | 
|  | 1884 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1885 | // If this is the first pointer going down and the touched window has a wallpaper | 
|  | 1886 | // then also add the touched wallpaper windows so they are locked in for the duration | 
|  | 1887 | // of the touch gesture. | 
|  | 1888 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
|  | 1889 | // engine only supports touch events.  We would need to add a mechanism similar | 
|  | 1890 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
|  | 1891 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1892 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1893 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1894 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1895 | const std::vector<sp<InputWindowHandle>>& windowHandles = | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1896 | getWindowHandlesLocked(displayId); | 
|  | 1897 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1898 | const InputWindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1899 | if (info->displayId == displayId && | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1900 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1901 | tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1902 | .addOrUpdateWindow(windowHandle, | 
|  | 1903 | InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
|  | 1904 | InputTarget:: | 
|  | 1905 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
|  | 1906 | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1907 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | } | 
|  | 1909 | } | 
|  | 1910 | } | 
|  | 1911 | } | 
|  | 1912 |  | 
|  | 1913 | // Success!  Output targets. | 
|  | 1914 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; | 
|  | 1915 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1916 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1917 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1918 | touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | } | 
|  | 1920 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1921 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1922 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1923 | touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1924 | } | 
|  | 1925 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | // Drop the outside or hover touch windows since we will not care about them | 
|  | 1927 | // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1928 | tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 |  | 
|  | 1930 | Failed: | 
|  | 1931 | // Check injection permission once and for all. | 
|  | 1932 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1933 | if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1934 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 1935 | } else { | 
|  | 1936 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 1937 | } | 
|  | 1938 | } | 
|  | 1939 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1940 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
|  | 1941 | return injectionResult; | 
|  | 1942 | } | 
|  | 1943 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1944 | // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1945 | if (!wrongDevice) { | 
|  | 1946 | if (switchedDevice) { | 
|  | 1947 | if (DEBUG_FOCUS) { | 
|  | 1948 | ALOGD("Conflicting pointer actions: Switched to a different device."); | 
|  | 1949 | } | 
|  | 1950 | *outConflictingPointerActions = true; | 
|  | 1951 | } | 
|  | 1952 |  | 
|  | 1953 | if (isHoverAction) { | 
|  | 1954 | // Started hovering, therefore no longer down. | 
|  | 1955 | if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1956 | if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1957 | ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
|  | 1958 | "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1959 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | *outConflictingPointerActions = true; | 
|  | 1961 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1962 | tempTouchState.reset(); | 
|  | 1963 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1964 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
|  | 1965 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1966 | tempTouchState.source = entry.source; | 
|  | 1967 | tempTouchState.displayId = displayId; | 
|  | 1968 | } | 
|  | 1969 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
|  | 1970 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 1971 | // All pointers up or canceled. | 
|  | 1972 | tempTouchState.reset(); | 
|  | 1973 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1974 | // First pointer went down. | 
|  | 1975 | if (oldState && oldState->down) { | 
|  | 1976 | if (DEBUG_FOCUS) { | 
|  | 1977 | ALOGD("Conflicting pointer actions: Down received while already down."); | 
|  | 1978 | } | 
|  | 1979 | *outConflictingPointerActions = true; | 
|  | 1980 | } | 
|  | 1981 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 1982 | // One pointer went up. | 
|  | 1983 | if (isSplit) { | 
|  | 1984 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 1985 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1986 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1987 | for (size_t i = 0; i < tempTouchState.windows.size();) { | 
|  | 1988 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
|  | 1989 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
|  | 1990 | touchedWindow.pointerIds.clearBit(pointerId); | 
|  | 1991 | if (touchedWindow.pointerIds.isEmpty()) { | 
|  | 1992 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
|  | 1993 | continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1995 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1996 | i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1997 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1998 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1999 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2000 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2001 | // Save changes unless the action was scroll in which case the temporary touch | 
|  | 2002 | // state was only valid for this one action. | 
|  | 2003 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
|  | 2004 | if (tempTouchState.displayId >= 0) { | 
|  | 2005 | mTouchStatesByDisplay[displayId] = tempTouchState; | 
|  | 2006 | } else { | 
|  | 2007 | mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2009 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2010 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2011 | // Update hover state. | 
|  | 2012 | mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2013 | } | 
|  | 2014 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2015 | return injectionResult; | 
|  | 2016 | } | 
|  | 2017 |  | 
|  | 2018 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2019 | int32_t targetFlags, BitSet32 pointerIds, | 
|  | 2020 | std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2021 | std::vector<InputTarget>::iterator it = | 
|  | 2022 | std::find_if(inputTargets.begin(), inputTargets.end(), | 
|  | 2023 | [&windowHandle](const InputTarget& inputTarget) { | 
|  | 2024 | return inputTarget.inputChannel->getConnectionToken() == | 
|  | 2025 | windowHandle->getToken(); | 
|  | 2026 | }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2027 |  | 
| Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2028 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2029 |  | 
|  | 2030 | if (it == inputTargets.end()) { | 
|  | 2031 | InputTarget inputTarget; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2032 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 2033 | getInputChannelLocked(windowHandle->getToken()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2034 | if (inputChannel == nullptr) { | 
|  | 2035 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
|  | 2036 | return; | 
|  | 2037 | } | 
|  | 2038 | inputTarget.inputChannel = inputChannel; | 
|  | 2039 | inputTarget.flags = targetFlags; | 
|  | 2040 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2041 | inputTargets.push_back(inputTarget); | 
|  | 2042 | it = inputTargets.end() - 1; | 
|  | 2043 | } | 
|  | 2044 |  | 
|  | 2045 | ALOG_ASSERT(it->flags == targetFlags); | 
|  | 2046 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
|  | 2047 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2048 | it->addPointers(pointerIds, windowInfo->transform); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2049 | } | 
|  | 2050 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2051 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2052 | int32_t displayId, float xOffset, | 
|  | 2053 | float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2054 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
|  | 2055 | mGlobalMonitorsByDisplay.find(displayId); | 
|  | 2056 |  | 
|  | 2057 | if (it != mGlobalMonitorsByDisplay.end()) { | 
|  | 2058 | const std::vector<Monitor>& monitors = it->second; | 
|  | 2059 | for (const Monitor& monitor : monitors) { | 
|  | 2060 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2061 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2062 | } | 
|  | 2063 | } | 
|  | 2064 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2065 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
|  | 2066 | float yOffset, | 
|  | 2067 | std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2068 | InputTarget target; | 
|  | 2069 | target.inputChannel = monitor.inputChannel; | 
|  | 2070 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2071 | ui::Transform t; | 
|  | 2072 | t.set(xOffset, yOffset); | 
|  | 2073 | target.setDefaultPointerTransform(t); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2074 | inputTargets.push_back(target); | 
|  | 2075 | } | 
|  | 2076 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2078 | const InjectionState* injectionState) { | 
|  | 2079 | if (injectionState && | 
|  | 2080 | (windowHandle == nullptr || | 
|  | 2081 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
|  | 2082 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2083 | if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2084 | 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] | 2085 | "owned by uid %d", | 
|  | 2086 | injectionState->injectorPid, injectionState->injectorUid, | 
|  | 2087 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2088 | } else { | 
|  | 2089 | ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2090 | injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2091 | } | 
|  | 2092 | return false; | 
|  | 2093 | } | 
|  | 2094 | return true; | 
|  | 2095 | } | 
|  | 2096 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2097 | /** | 
|  | 2098 | * Indicate whether one window handle should be considered as obscuring | 
|  | 2099 | * another window handle. We only check a few preconditions. Actually | 
|  | 2100 | * checking the bounds is left to the caller. | 
|  | 2101 | */ | 
|  | 2102 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, | 
|  | 2103 | const sp<InputWindowHandle>& otherHandle) { | 
|  | 2104 | // Compare by token so cloned layers aren't counted | 
|  | 2105 | if (haveSameToken(windowHandle, otherHandle)) { | 
|  | 2106 | return false; | 
|  | 2107 | } | 
|  | 2108 | auto info = windowHandle->getInfo(); | 
|  | 2109 | auto otherInfo = otherHandle->getInfo(); | 
|  | 2110 | if (!otherInfo->visible) { | 
|  | 2111 | return false; | 
| Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2112 | } else if (info->ownerPid == otherInfo->ownerPid) { | 
|  | 2113 | // If ownerPid is the same we don't generate occlusion events as there | 
|  | 2114 | // is no in-process security boundary. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2115 | return false; | 
| Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2116 | } else if (otherInfo->trustedOverlay) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2117 | return false; | 
|  | 2118 | } else if (otherInfo->displayId != info->displayId) { | 
|  | 2119 | return false; | 
|  | 2120 | } | 
|  | 2121 | return true; | 
|  | 2122 | } | 
|  | 2123 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2124 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, | 
|  | 2125 | int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2126 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2127 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2128 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2129 | if (windowHandle == otherHandle) { | 
|  | 2130 | break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2131 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2132 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2133 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2134 | otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2135 | return true; | 
|  | 2136 | } | 
|  | 2137 | } | 
|  | 2138 | return false; | 
|  | 2139 | } | 
|  | 2140 |  | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2141 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 2142 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2143 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2144 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2145 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2146 | if (windowHandle == otherHandle) { | 
|  | 2147 | break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2148 | } | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2149 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2150 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2151 | otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2152 | return true; | 
|  | 2153 | } | 
|  | 2154 | } | 
|  | 2155 | return false; | 
|  | 2156 | } | 
|  | 2157 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2158 | std::string InputDispatcher::getApplicationWindowLabel( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2159 | const std::shared_ptr<InputApplicationHandle>& applicationHandle, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2160 | const sp<InputWindowHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2161 | if (applicationHandle != nullptr) { | 
|  | 2162 | if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2163 | return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2164 | } else { | 
|  | 2165 | return applicationHandle->getName(); | 
|  | 2166 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2167 | } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2168 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2169 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2170 | return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2171 | } | 
|  | 2172 | } | 
|  | 2173 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2174 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2175 | if (eventEntry.type == EventEntry::Type::FOCUS) { | 
|  | 2176 | // Focus events are passed to apps, but do not represent user activity. | 
|  | 2177 | return; | 
|  | 2178 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2179 | int32_t displayId = getTargetDisplayId(eventEntry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2180 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2181 | if (focusedWindowHandle != nullptr) { | 
|  | 2182 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2183 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2184 | #if DEBUG_DISPATCH_CYCLE | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2185 | 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] | 2186 | #endif | 
|  | 2187 | return; | 
|  | 2188 | } | 
|  | 2189 | } | 
|  | 2190 |  | 
|  | 2191 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2192 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2193 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2194 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 2195 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2196 | return; | 
|  | 2197 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2198 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2199 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2200 | eventType = USER_ACTIVITY_EVENT_TOUCH; | 
|  | 2201 | } | 
|  | 2202 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2204 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2205 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2206 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2207 | return; | 
|  | 2208 | } | 
|  | 2209 | eventType = USER_ACTIVITY_EVENT_BUTTON; | 
|  | 2210 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2211 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2212 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2213 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2214 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2215 | LOG_ALWAYS_FATAL("%s events are not user activity", | 
|  | 2216 | EventEntry::typeToString(eventEntry.type)); | 
|  | 2217 | break; | 
|  | 2218 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | } | 
|  | 2220 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2221 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 2222 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2223 | commandEntry->eventTime = eventEntry.eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2224 | commandEntry->userActivityEventType = eventType; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2225 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2226 | } | 
|  | 2227 |  | 
|  | 2228 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2229 | const sp<Connection>& connection, | 
|  | 2230 | EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2231 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2232 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2233 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2234 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2235 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2236 | ATRACE_NAME(message.c_str()); | 
|  | 2237 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2238 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2239 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2240 | "globalScaleFactor=%f, pointerIds=0x%x %s", | 
|  | 2241 | connection->getInputChannelName().c_str(), inputTarget.flags, | 
|  | 2242 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
|  | 2243 | inputTarget.getPointerInfoString().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2244 | #endif | 
|  | 2245 |  | 
|  | 2246 | // Skip this event if the connection status is not normal. | 
|  | 2247 | // We don't want to enqueue additional outbound events if the connection is broken. | 
|  | 2248 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 2249 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2250 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2251 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | #endif | 
|  | 2253 | return; | 
|  | 2254 | } | 
|  | 2255 |  | 
|  | 2256 | // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2257 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
|  | 2258 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
|  | 2259 | "Entry type %s should not have FLAG_SPLIT", | 
|  | 2260 | EventEntry::typeToString(eventEntry->type)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2261 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2262 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2263 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2264 | MotionEntry* splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2265 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2266 | if (!splitMotionEntry) { | 
|  | 2267 | return; // split event was dropped | 
|  | 2268 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2269 | if (DEBUG_FOCUS) { | 
|  | 2270 | ALOGD("channel '%s' ~ Split motion event.", | 
|  | 2271 | connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2272 | logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2273 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2274 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2275 | splitMotionEntry->release(); | 
|  | 2276 | return; | 
|  | 2277 | } | 
|  | 2278 | } | 
|  | 2279 |  | 
|  | 2280 | // Not splitting.  Enqueue dispatch entries for the event as is. | 
|  | 2281 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
|  | 2282 | } | 
|  | 2283 |  | 
|  | 2284 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2285 | const sp<Connection>& connection, | 
|  | 2286 | EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2287 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2288 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2289 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2290 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2291 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2292 | ATRACE_NAME(message.c_str()); | 
|  | 2293 | } | 
|  | 2294 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2295 | bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2296 |  | 
|  | 2297 | // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2298 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2299 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2300 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2301 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2302 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2303 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2304 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2305 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2306 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2307 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2308 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2309 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2310 |  | 
|  | 2311 | // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2312 | if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2314 | } | 
|  | 2315 | } | 
|  | 2316 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2317 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
|  | 2318 | EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2319 | const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2320 | int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2321 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2322 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
|  | 2323 | connection->getInputChannelName().c_str(), | 
|  | 2324 | dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2325 | ATRACE_NAME(message.c_str()); | 
|  | 2326 | } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2327 | int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2328 | if (!(inputTargetFlags & dispatchMode)) { | 
|  | 2329 | return; | 
|  | 2330 | } | 
|  | 2331 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
|  | 2332 |  | 
|  | 2333 | // This is a new event. | 
|  | 2334 | // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2335 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2336 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2337 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2338 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
|  | 2339 | // different EventEntry than what was passed in. | 
|  | 2340 | EventEntry* newEntry = dispatchEntry->eventEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2341 | // Apply target flags and update the connection's input state. | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2342 | switch (newEntry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2343 | case EventEntry::Type::KEY: { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2344 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2345 | dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2346 | dispatchEntry->resolvedAction = keyEntry.action; | 
|  | 2347 | dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2348 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2349 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
|  | 2350 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2351 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2352 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", | 
|  | 2353 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2354 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2355 | return; // skip the inconsistent event | 
|  | 2356 | } | 
|  | 2357 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2359 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2360 | case EventEntry::Type::MOTION: { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2361 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2362 | // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
|  | 2363 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
|  | 2364 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
|  | 2365 | static_cast<int32_t>(IdGenerator::Source::OTHER); | 
|  | 2366 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2367 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2368 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
|  | 2369 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
|  | 2370 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
|  | 2371 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
|  | 2372 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2373 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
|  | 2374 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
|  | 2375 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
|  | 2376 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
|  | 2377 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2378 | dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2379 | dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2380 | } | 
|  | 2381 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2382 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
|  | 2383 | motionEntry.displayId)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2385 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " | 
|  | 2386 | "event", | 
|  | 2387 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2388 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2389 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2390 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2391 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2392 | dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2393 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
|  | 2394 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 2395 | } | 
|  | 2396 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
|  | 2397 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2398 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2399 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2400 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
|  | 2401 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2402 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2403 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
|  | 2404 | "event", | 
|  | 2405 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2406 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2407 | return; // skip the inconsistent event | 
|  | 2408 | } | 
|  | 2409 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2410 | dispatchEntry->resolvedEventId = | 
|  | 2411 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
|  | 2412 | ? mIdGenerator.nextId() | 
|  | 2413 | : motionEntry.id; | 
|  | 2414 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
|  | 2415 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
|  | 2416 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 2417 | motionEntry.id, dispatchEntry->resolvedEventId); | 
|  | 2418 | ATRACE_NAME(message.c_str()); | 
|  | 2419 | } | 
|  | 2420 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2421 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2422 | inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2423 |  | 
|  | 2424 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2425 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2426 | case EventEntry::Type::FOCUS: { | 
|  | 2427 | break; | 
|  | 2428 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2429 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2430 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2431 | LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2432 | EventEntry::typeToString(newEntry->type)); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2433 | break; | 
|  | 2434 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2435 | } | 
|  | 2436 |  | 
|  | 2437 | // Remember that we are waiting for this dispatch to complete. | 
|  | 2438 | if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2439 | incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2440 | } | 
|  | 2441 |  | 
|  | 2442 | // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2443 | connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2444 | traceOutboundQueueLength(connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2445 | } | 
|  | 2446 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2447 | /** | 
|  | 2448 | * This function is purely for debugging. It helps us understand where the user interaction | 
|  | 2449 | * was taking place. For example, if user is touching launcher, we will see a log that user | 
|  | 2450 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. | 
|  | 2451 | * We will see both launcher and wallpaper in that list. | 
|  | 2452 | * Once the interaction with a particular set of connections starts, no new logs will be printed | 
|  | 2453 | * until the set of interacted connections changes. | 
|  | 2454 | * | 
|  | 2455 | * The following items are skipped, to reduce the logspam: | 
|  | 2456 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged | 
|  | 2457 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). | 
|  | 2458 | * This includes situations like the soft BACK button key. When the user releases (lifts up the | 
|  | 2459 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. | 
|  | 2460 | * Both of those ACTION_UP events would not be logged | 
|  | 2461 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events | 
|  | 2462 | * will not be logged. This is omitted to reduce the amount of data printed. | 
|  | 2463 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore | 
|  | 2464 | * gesture monitor is the only connection receiving the remainder of the gesture. | 
|  | 2465 | */ | 
|  | 2466 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, | 
|  | 2467 | const std::vector<InputTarget>& targets) { | 
|  | 2468 | // Skip ACTION_UP events, and all events other than keys and motions | 
|  | 2469 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 2470 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 2471 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
|  | 2472 | return; | 
|  | 2473 | } | 
|  | 2474 | } else if (entry.type == EventEntry::Type::MOTION) { | 
|  | 2475 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 2476 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || | 
|  | 2477 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2478 | return; | 
|  | 2479 | } | 
|  | 2480 | } else { | 
|  | 2481 | return; // Not a key or a motion | 
|  | 2482 | } | 
|  | 2483 |  | 
|  | 2484 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; | 
|  | 2485 | std::vector<sp<Connection>> newConnections; | 
|  | 2486 | for (const InputTarget& target : targets) { | 
|  | 2487 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == | 
|  | 2488 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2489 | continue; // Skip windows that receive ACTION_OUTSIDE | 
|  | 2490 | } | 
|  | 2491 |  | 
|  | 2492 | sp<IBinder> token = target.inputChannel->getConnectionToken(); | 
|  | 2493 | sp<Connection> connection = getConnectionLocked(token); | 
|  | 2494 | if (connection == nullptr || connection->monitor) { | 
|  | 2495 | continue; // We only need to keep track of the non-monitor connections. | 
|  | 2496 | } | 
|  | 2497 | newConnectionTokens.insert(std::move(token)); | 
|  | 2498 | newConnections.emplace_back(connection); | 
|  | 2499 | } | 
|  | 2500 | if (newConnectionTokens == mInteractionConnectionTokens) { | 
|  | 2501 | return; // no change | 
|  | 2502 | } | 
|  | 2503 | mInteractionConnectionTokens = newConnectionTokens; | 
|  | 2504 |  | 
|  | 2505 | std::string windowList; | 
|  | 2506 | for (const sp<Connection>& connection : newConnections) { | 
|  | 2507 | windowList += connection->getWindowName() + ", "; | 
|  | 2508 | } | 
|  | 2509 | std::string message = "Interaction with windows: " + windowList; | 
|  | 2510 | if (windowList.empty()) { | 
|  | 2511 | message += "<none>"; | 
|  | 2512 | } | 
|  | 2513 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; | 
|  | 2514 | } | 
|  | 2515 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2516 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2517 | const sp<IBinder>& token) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2518 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2519 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
|  | 2520 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2521 | return; | 
|  | 2522 | } | 
|  | 2523 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2524 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
|  | 2525 | if (focusedToken == token) { | 
|  | 2526 | // ignore since token is focused | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2527 | return; | 
|  | 2528 | } | 
|  | 2529 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2530 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 2531 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2532 | commandEntry->newToken = token; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2533 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2534 | } | 
|  | 2535 |  | 
|  | 2536 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2537 | const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2538 | if (ATRACE_ENABLED()) { | 
|  | 2539 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2540 | connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2541 | ATRACE_NAME(message.c_str()); | 
|  | 2542 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2543 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2544 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2545 | #endif | 
|  | 2546 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2547 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
|  | 2548 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2549 | dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2550 | const std::chrono::nanoseconds timeout = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2551 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2552 | dispatchEntry->timeoutTime = currentTime + timeout.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2553 |  | 
|  | 2554 | // Publish the event. | 
|  | 2555 | status_t status; | 
|  | 2556 | EventEntry* eventEntry = dispatchEntry->eventEntry; | 
|  | 2557 | switch (eventEntry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2558 | case EventEntry::Type::KEY: { | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2559 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); | 
|  | 2560 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); | 
| 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 | // Publish the key event. | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2563 | status = | 
|  | 2564 | connection->inputPublisher | 
|  | 2565 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, | 
|  | 2566 | keyEntry->deviceId, keyEntry->source, | 
|  | 2567 | keyEntry->displayId, std::move(hmac), | 
|  | 2568 | dispatchEntry->resolvedAction, | 
|  | 2569 | dispatchEntry->resolvedFlags, keyEntry->keyCode, | 
|  | 2570 | keyEntry->scanCode, keyEntry->metaState, | 
|  | 2571 | keyEntry->repeatCount, keyEntry->downTime, | 
|  | 2572 | keyEntry->eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2573 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2574 | } | 
|  | 2575 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2576 | case EventEntry::Type::MOTION: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2577 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2578 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2579 | PointerCoords scaledCoords[MAX_POINTERS]; | 
|  | 2580 | const PointerCoords* usingCoords = motionEntry->pointerCoords; | 
|  | 2581 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2582 | // 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] | 2583 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && | 
|  | 2584 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
|  | 2585 | float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2586 | if (globalScaleFactor != 1.0f) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2587 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { | 
|  | 2588 | scaledCoords[i] = motionEntry->pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2589 | // Don't apply window scale here since we don't want scale to affect raw | 
|  | 2590 | // coordinates. The scale will be sent back to the client and applied | 
|  | 2591 | // later when requesting relative coordinates. | 
|  | 2592 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
|  | 2593 | 1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2594 | } | 
|  | 2595 | usingCoords = scaledCoords; | 
|  | 2596 | } | 
|  | 2597 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2598 | // We don't want the dispatch target to know. | 
|  | 2599 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
|  | 2600 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { | 
|  | 2601 | scaledCoords[i].clear(); | 
|  | 2602 | } | 
|  | 2603 | usingCoords = scaledCoords; | 
|  | 2604 | } | 
|  | 2605 | } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2606 |  | 
|  | 2607 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2608 |  | 
|  | 2609 | // Publish the motion event. | 
|  | 2610 | status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2611 | .publishMotionEvent(dispatchEntry->seq, | 
|  | 2612 | dispatchEntry->resolvedEventId, | 
|  | 2613 | motionEntry->deviceId, motionEntry->source, | 
|  | 2614 | motionEntry->displayId, std::move(hmac), | 
|  | 2615 | dispatchEntry->resolvedAction, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2616 | motionEntry->actionButton, | 
|  | 2617 | dispatchEntry->resolvedFlags, | 
|  | 2618 | motionEntry->edgeFlags, motionEntry->metaState, | 
|  | 2619 | motionEntry->buttonState, | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2620 | motionEntry->classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2621 | dispatchEntry->transform, | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2622 | motionEntry->xPrecision, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2623 | motionEntry->yPrecision, | 
|  | 2624 | motionEntry->xCursorPosition, | 
|  | 2625 | motionEntry->yCursorPosition, | 
|  | 2626 | motionEntry->downTime, motionEntry->eventTime, | 
|  | 2627 | motionEntry->pointerCount, | 
|  | 2628 | motionEntry->pointerProperties, usingCoords); | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2629 | reportTouchEventForStatistics(*motionEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2630 | break; | 
|  | 2631 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2632 | case EventEntry::Type::FOCUS: { | 
|  | 2633 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); | 
|  | 2634 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2635 | focusEntry->id, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2636 | focusEntry->hasFocus, | 
|  | 2637 | mInTouchMode); | 
|  | 2638 | break; | 
|  | 2639 | } | 
|  | 2640 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2641 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2642 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2643 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
|  | 2644 | EventEntry::typeToString(eventEntry->type)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2645 | return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2646 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2647 | } | 
|  | 2648 |  | 
|  | 2649 | // Check the result. | 
|  | 2650 | if (status) { | 
|  | 2651 | if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2652 | if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2653 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2654 | "This is unexpected because the wait queue is empty, so the pipe " | 
|  | 2655 | "should be empty and we shouldn't have any problems writing an " | 
|  | 2656 | "event to it, status=%d", | 
|  | 2657 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2658 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 2659 | } else { | 
|  | 2660 | // Pipe is full and we are waiting for the app to finish process some events | 
|  | 2661 | // before sending more events to it. | 
|  | 2662 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2663 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2664 | "waiting for the application to catch up", | 
|  | 2665 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2666 | #endif | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2667 | } | 
|  | 2668 | } else { | 
|  | 2669 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2670 | "status=%d", | 
|  | 2671 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2672 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 2673 | } | 
|  | 2674 | return; | 
|  | 2675 | } | 
|  | 2676 |  | 
|  | 2677 | // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2678 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
|  | 2679 | connection->outboundQueue.end(), | 
|  | 2680 | dispatchEntry)); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2681 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2682 | connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2683 | if (connection->responsive) { | 
|  | 2684 | mAnrTracker.insert(dispatchEntry->timeoutTime, | 
|  | 2685 | connection->inputChannel->getConnectionToken()); | 
|  | 2686 | } | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2687 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2688 | } | 
|  | 2689 | } | 
|  | 2690 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2691 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { | 
|  | 2692 | size_t size; | 
|  | 2693 | switch (event.type) { | 
|  | 2694 | case VerifiedInputEvent::Type::KEY: { | 
|  | 2695 | size = sizeof(VerifiedKeyEvent); | 
|  | 2696 | break; | 
|  | 2697 | } | 
|  | 2698 | case VerifiedInputEvent::Type::MOTION: { | 
|  | 2699 | size = sizeof(VerifiedMotionEvent); | 
|  | 2700 | break; | 
|  | 2701 | } | 
|  | 2702 | } | 
|  | 2703 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
|  | 2704 | return mHmacKeyManager.sign(start, size); | 
|  | 2705 | } | 
|  | 2706 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2707 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 2708 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 2709 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
|  | 2710 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
|  | 2711 | // Only sign events up and down events as the purely move events | 
|  | 2712 | // are tied to their up/down counterparts so signing would be redundant. | 
|  | 2713 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
|  | 2714 | verifiedEvent.actionMasked = actionMasked; | 
|  | 2715 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2716 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2717 | } | 
|  | 2718 | return INVALID_HMAC; | 
|  | 2719 | } | 
|  | 2720 |  | 
|  | 2721 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 2722 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 2723 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
|  | 2724 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
|  | 2725 | verifiedEvent.action = dispatchEntry.resolvedAction; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2726 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2727 | } | 
|  | 2728 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2729 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2730 | const sp<Connection>& connection, uint32_t seq, | 
|  | 2731 | bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2732 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2733 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2734 | connection->getInputChannelName().c_str(), seq, toString(handled)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2735 | #endif | 
|  | 2736 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2737 | if (connection->status == Connection::STATUS_BROKEN || | 
|  | 2738 | connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2739 | return; | 
|  | 2740 | } | 
|  | 2741 |  | 
|  | 2742 | // Notify other system components and prepare to start the next dispatch cycle. | 
|  | 2743 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); | 
|  | 2744 | } | 
|  | 2745 |  | 
|  | 2746 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2747 | const sp<Connection>& connection, | 
|  | 2748 | bool notify) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2749 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2750 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2751 | connection->getInputChannelName().c_str(), toString(notify)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2752 | #endif | 
|  | 2753 |  | 
|  | 2754 | // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2755 | drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2756 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2757 | drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2758 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2759 |  | 
|  | 2760 | // The connection appears to be unrecoverably broken. | 
|  | 2761 | // Ignore already broken or zombie connections. | 
|  | 2762 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 2763 | connection->status = Connection::STATUS_BROKEN; | 
|  | 2764 |  | 
|  | 2765 | if (notify) { | 
|  | 2766 | // Notify other system components. | 
|  | 2767 | onDispatchCycleBrokenLocked(currentTime, connection); | 
|  | 2768 | } | 
|  | 2769 | } | 
|  | 2770 | } | 
|  | 2771 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2772 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
|  | 2773 | while (!queue.empty()) { | 
|  | 2774 | DispatchEntry* dispatchEntry = queue.front(); | 
|  | 2775 | queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2776 | releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2777 | } | 
|  | 2778 | } | 
|  | 2779 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2780 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2781 | if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2782 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2783 | } | 
|  | 2784 | delete dispatchEntry; | 
|  | 2785 | } | 
|  | 2786 |  | 
|  | 2787 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { | 
|  | 2788 | InputDispatcher* d = static_cast<InputDispatcher*>(data); | 
|  | 2789 |  | 
|  | 2790 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2791 | std::scoped_lock _l(d->mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2793 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2794 | ALOGE("Received spurious receive callback for unknown input channel.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2795 | "fd=%d, events=0x%x", | 
|  | 2796 | fd, events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2797 | return 0; // remove the callback | 
|  | 2798 | } | 
|  | 2799 |  | 
|  | 2800 | bool notify; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2801 | sp<Connection> connection = d->mConnectionsByFd[fd]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2802 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
|  | 2803 | if (!(events & ALOOPER_EVENT_INPUT)) { | 
|  | 2804 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2805 | "events=0x%x", | 
|  | 2806 | connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2807 | return 1; | 
|  | 2808 | } | 
|  | 2809 |  | 
|  | 2810 | nsecs_t currentTime = now(); | 
|  | 2811 | bool gotOne = false; | 
|  | 2812 | status_t status; | 
|  | 2813 | for (;;) { | 
|  | 2814 | uint32_t seq; | 
|  | 2815 | bool handled; | 
|  | 2816 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); | 
|  | 2817 | if (status) { | 
|  | 2818 | break; | 
|  | 2819 | } | 
|  | 2820 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); | 
|  | 2821 | gotOne = true; | 
|  | 2822 | } | 
|  | 2823 | if (gotOne) { | 
|  | 2824 | d->runCommandsLockedInterruptible(); | 
|  | 2825 | if (status == WOULD_BLOCK) { | 
|  | 2826 | return 1; | 
|  | 2827 | } | 
|  | 2828 | } | 
|  | 2829 |  | 
|  | 2830 | notify = status != DEAD_OBJECT || !connection->monitor; | 
|  | 2831 | if (notify) { | 
|  | 2832 | ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2833 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2834 | } | 
|  | 2835 | } else { | 
|  | 2836 | // Monitor channels are never explicitly unregistered. | 
|  | 2837 | // We do it automatically when the remote endpoint is closed so don't warn | 
|  | 2838 | // about them. | 
| arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2839 | const bool stillHaveWindowHandle = | 
|  | 2840 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != | 
|  | 2841 | nullptr; | 
|  | 2842 | notify = !connection->monitor && stillHaveWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2843 | if (notify) { | 
|  | 2844 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2845 | "events=0x%x", | 
|  | 2846 | connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2847 | } | 
|  | 2848 | } | 
|  | 2849 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2850 | // Remove the channel. | 
|  | 2851 | d->removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2852 | return 0; // remove the callback | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2853 | }             // release lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 | } | 
|  | 2855 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2856 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2857 | const CancelationOptions& options) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2858 | for (const auto& pair : mConnectionsByFd) { | 
|  | 2859 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2860 | } | 
|  | 2861 | } | 
|  | 2862 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2863 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2864 | const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2865 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
|  | 2866 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
|  | 2867 | } | 
|  | 2868 |  | 
|  | 2869 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
|  | 2870 | const CancelationOptions& options, | 
|  | 2871 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
|  | 2872 | for (const auto& it : monitorsByDisplay) { | 
|  | 2873 | const std::vector<Monitor>& monitors = it.second; | 
|  | 2874 | for (const Monitor& monitor : monitors) { | 
|  | 2875 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2876 | } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2877 | } | 
|  | 2878 | } | 
|  | 2879 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2880 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2881 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2882 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2883 | if (connection == nullptr) { | 
|  | 2884 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2885 | } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2886 |  | 
|  | 2887 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2888 | } | 
|  | 2889 |  | 
|  | 2890 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
|  | 2891 | const sp<Connection>& connection, const CancelationOptions& options) { | 
|  | 2892 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 2893 | return; | 
|  | 2894 | } | 
|  | 2895 |  | 
|  | 2896 | nsecs_t currentTime = now(); | 
|  | 2897 |  | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2898 | std::vector<EventEntry*> cancelationEvents = | 
|  | 2899 | connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2900 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2901 | if (cancelationEvents.empty()) { | 
|  | 2902 | return; | 
|  | 2903 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2904 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2905 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
|  | 2906 | "with reality: %s, mode=%d.", | 
|  | 2907 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
|  | 2908 | options.mode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2909 | #endif | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2910 |  | 
|  | 2911 | InputTarget target; | 
|  | 2912 | sp<InputWindowHandle> windowHandle = | 
|  | 2913 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 2914 | if (windowHandle != nullptr) { | 
|  | 2915 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2916 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2917 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2918 | } | 
|  | 2919 | target.inputChannel = connection->inputChannel; | 
|  | 2920 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2921 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2922 | for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
|  | 2923 | EventEntry* cancelationEventEntry = cancelationEvents[i]; | 
|  | 2924 | switch (cancelationEventEntry->type) { | 
|  | 2925 | case EventEntry::Type::KEY: { | 
|  | 2926 | logOutboundKeyDetails("cancel - ", | 
|  | 2927 | static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
|  | 2928 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2929 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2930 | case EventEntry::Type::MOTION: { | 
|  | 2931 | logOutboundMotionDetails("cancel - ", | 
|  | 2932 | static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
|  | 2933 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2934 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2935 | case EventEntry::Type::FOCUS: { | 
|  | 2936 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); | 
|  | 2937 | break; | 
|  | 2938 | } | 
|  | 2939 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2940 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2941 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
|  | 2942 | EventEntry::typeToString(cancelationEventEntry->type)); | 
|  | 2943 | break; | 
|  | 2944 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | } | 
|  | 2946 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2947 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref | 
|  | 2948 | target, InputTarget::FLAG_DISPATCH_AS_IS); | 
|  | 2949 |  | 
|  | 2950 | cancelationEventEntry->release(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2951 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2952 |  | 
|  | 2953 | startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2954 | } | 
|  | 2955 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2956 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
|  | 2957 | const sp<Connection>& connection) { | 
|  | 2958 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 2959 | return; | 
|  | 2960 | } | 
|  | 2961 |  | 
|  | 2962 | nsecs_t currentTime = now(); | 
|  | 2963 |  | 
|  | 2964 | std::vector<EventEntry*> downEvents = | 
|  | 2965 | connection->inputState.synthesizePointerDownEvents(currentTime); | 
|  | 2966 |  | 
|  | 2967 | if (downEvents.empty()) { | 
|  | 2968 | return; | 
|  | 2969 | } | 
|  | 2970 |  | 
|  | 2971 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 2972 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
|  | 2973 | connection->getInputChannelName().c_str(), downEvents.size()); | 
|  | 2974 | #endif | 
|  | 2975 |  | 
|  | 2976 | InputTarget target; | 
|  | 2977 | sp<InputWindowHandle> windowHandle = | 
|  | 2978 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 2979 | if (windowHandle != nullptr) { | 
|  | 2980 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2981 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2982 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2983 | } | 
|  | 2984 | target.inputChannel = connection->inputChannel; | 
|  | 2985 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2986 |  | 
|  | 2987 | for (EventEntry* downEventEntry : downEvents) { | 
|  | 2988 | switch (downEventEntry->type) { | 
|  | 2989 | case EventEntry::Type::MOTION: { | 
|  | 2990 | logOutboundMotionDetails("down - ", | 
|  | 2991 | static_cast<const MotionEntry&>(*downEventEntry)); | 
|  | 2992 | break; | 
|  | 2993 | } | 
|  | 2994 |  | 
|  | 2995 | case EventEntry::Type::KEY: | 
|  | 2996 | case EventEntry::Type::FOCUS: | 
|  | 2997 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2998 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2999 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
|  | 3000 | EventEntry::typeToString(downEventEntry->type)); | 
|  | 3001 | break; | 
|  | 3002 | } | 
|  | 3003 | } | 
|  | 3004 |  | 
|  | 3005 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref | 
|  | 3006 | target, InputTarget::FLAG_DISPATCH_AS_IS); | 
|  | 3007 |  | 
|  | 3008 | downEventEntry->release(); | 
|  | 3009 | } | 
|  | 3010 |  | 
|  | 3011 | startDispatchCycleLocked(currentTime, connection); | 
|  | 3012 | } | 
|  | 3013 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3014 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 3015 | BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3016 | ALOG_ASSERT(pointerIds.value != 0); | 
|  | 3017 |  | 
|  | 3018 | uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
|  | 3019 | PointerProperties splitPointerProperties[MAX_POINTERS]; | 
|  | 3020 | PointerCoords splitPointerCoords[MAX_POINTERS]; | 
|  | 3021 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3022 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3023 | uint32_t splitPointerCount = 0; | 
|  | 3024 |  | 
|  | 3025 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3026 | originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3027 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3028 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3029 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3030 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3031 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
|  | 3032 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
|  | 3033 | splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3034 | originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | splitPointerCount += 1; | 
|  | 3036 | } | 
|  | 3037 | } | 
|  | 3038 |  | 
|  | 3039 | if (splitPointerCount != pointerIds.count()) { | 
|  | 3040 | // This is bad.  We are missing some of the pointers that we expected to deliver. | 
|  | 3041 | // Most likely this indicates that we received an ACTION_MOVE events that has | 
|  | 3042 | // different pointer ids than we expected based on the previous ACTION_DOWN | 
|  | 3043 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
|  | 3044 | // in this way. | 
|  | 3045 | ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3046 | "we expected there to be %d pointers.  This probably means we received " | 
|  | 3047 | "a broken sequence of pointer ids from the input device.", | 
|  | 3048 | splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3049 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3050 | } | 
|  | 3051 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3052 | int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3054 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
|  | 3055 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3056 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 3057 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3058 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3059 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3060 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3061 | if (pointerIds.count() == 1) { | 
|  | 3062 | // The first/last pointer went down/up. | 
|  | 3063 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3064 | ? AMOTION_EVENT_ACTION_DOWN | 
|  | 3065 | : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | } else { | 
|  | 3067 | // A secondary pointer went down/up. | 
|  | 3068 | uint32_t splitPointerIndex = 0; | 
|  | 3069 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
|  | 3070 | splitPointerIndex += 1; | 
|  | 3071 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3072 | action = maskedAction | | 
|  | 3073 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3074 | } | 
|  | 3075 | } else { | 
|  | 3076 | // An unrelated pointer changed. | 
|  | 3077 | action = AMOTION_EVENT_ACTION_MOVE; | 
|  | 3078 | } | 
|  | 3079 | } | 
|  | 3080 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3081 | int32_t newId = mIdGenerator.nextId(); | 
|  | 3082 | if (ATRACE_ENABLED()) { | 
|  | 3083 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
|  | 3084 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3085 | originalMotionEntry.id, newId); | 
|  | 3086 | ATRACE_NAME(message.c_str()); | 
|  | 3087 | } | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3088 | MotionEntry* splitMotionEntry = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3089 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, | 
|  | 3090 | originalMotionEntry.source, originalMotionEntry.displayId, | 
|  | 3091 | originalMotionEntry.policyFlags, action, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3092 | originalMotionEntry.actionButton, originalMotionEntry.flags, | 
|  | 3093 | originalMotionEntry.metaState, originalMotionEntry.buttonState, | 
|  | 3094 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, | 
|  | 3095 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, | 
|  | 3096 | originalMotionEntry.xCursorPosition, | 
|  | 3097 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3098 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3100 | if (originalMotionEntry.injectionState) { | 
|  | 3101 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3102 | splitMotionEntry->injectionState->refCount += 1; | 
|  | 3103 | } | 
|  | 3104 |  | 
|  | 3105 | return splitMotionEntry; | 
|  | 3106 | } | 
|  | 3107 |  | 
|  | 3108 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
|  | 3109 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3110 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3111 | #endif | 
|  | 3112 |  | 
|  | 3113 | bool needWake; | 
|  | 3114 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3115 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3116 |  | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3117 | ConfigurationChangedEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3118 | new ConfigurationChangedEntry(args->id, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3119 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3120 | } // release lock | 
|  | 3121 |  | 
|  | 3122 | if (needWake) { | 
|  | 3123 | mLooper->wake(); | 
|  | 3124 | } | 
|  | 3125 | } | 
|  | 3126 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3127 | /** | 
|  | 3128 | * If one of the meta shortcuts is detected, process them here: | 
|  | 3129 | *     Meta + Backspace -> generate BACK | 
|  | 3130 | *     Meta + Enter -> generate HOME | 
|  | 3131 | * This will potentially overwrite keyCode and metaState. | 
|  | 3132 | */ | 
|  | 3133 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3134 | int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3135 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
|  | 3136 | int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
|  | 3137 | if (keyCode == AKEYCODE_DEL) { | 
|  | 3138 | newKeyCode = AKEYCODE_BACK; | 
|  | 3139 | } else if (keyCode == AKEYCODE_ENTER) { | 
|  | 3140 | newKeyCode = AKEYCODE_HOME; | 
|  | 3141 | } | 
|  | 3142 | if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3143 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3144 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3145 | mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3146 | keyCode = newKeyCode; | 
|  | 3147 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3148 | } | 
|  | 3149 | } else if (action == AKEY_EVENT_ACTION_UP) { | 
|  | 3150 | // In order to maintain a consistent stream of up and down events, check to see if the key | 
|  | 3151 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
|  | 3152 | // 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] | 3153 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3154 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3155 | auto replacementIt = mReplacedKeys.find(replacement); | 
|  | 3156 | if (replacementIt != mReplacedKeys.end()) { | 
|  | 3157 | keyCode = replacementIt->second; | 
|  | 3158 | mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3159 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3160 | } | 
|  | 3161 | } | 
|  | 3162 | } | 
|  | 3163 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3164 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
|  | 3165 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3166 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 3167 | "policyFlags=0x%x, action=0x%x, " | 
|  | 3168 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
|  | 3169 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
|  | 3170 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
|  | 3171 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3172 | #endif | 
|  | 3173 | if (!validateKeyEvent(args->action)) { | 
|  | 3174 | return; | 
|  | 3175 | } | 
|  | 3176 |  | 
|  | 3177 | uint32_t policyFlags = args->policyFlags; | 
|  | 3178 | int32_t flags = args->flags; | 
|  | 3179 | int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3180 | // InputDispatcher tracks and generates key repeats on behalf of | 
|  | 3181 | // whatever notifies it, so repeatCount should always be set to 0 | 
|  | 3182 | constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3183 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
|  | 3184 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 3185 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
|  | 3186 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3187 | if (policyFlags & POLICY_FLAG_FUNCTION) { | 
|  | 3188 | metaState |= AMETA_FUNCTION_ON; | 
|  | 3189 | } | 
|  | 3190 |  | 
|  | 3191 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3192 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3193 | int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3194 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3195 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3196 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3197 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3198 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3199 | args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3200 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3201 | android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3202 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3203 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3204 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3205 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3206 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3207 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3208 | bool needWake; | 
|  | 3209 | { // acquire lock | 
|  | 3210 | mLock.lock(); | 
|  | 3211 |  | 
|  | 3212 | if (shouldSendKeyToInputFilterLocked(args)) { | 
|  | 3213 | mLock.unlock(); | 
|  | 3214 |  | 
|  | 3215 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3216 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3217 | return; // event was consumed by the filter | 
|  | 3218 | } | 
|  | 3219 |  | 
|  | 3220 | mLock.lock(); | 
|  | 3221 | } | 
|  | 3222 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3223 | KeyEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3224 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3225 | args->displayId, policyFlags, args->action, flags, keyCode, | 
|  | 3226 | args->scanCode, metaState, repeatCount, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 |  | 
|  | 3228 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3229 | mLock.unlock(); | 
|  | 3230 | } // release lock | 
|  | 3231 |  | 
|  | 3232 | if (needWake) { | 
|  | 3233 | mLooper->wake(); | 
|  | 3234 | } | 
|  | 3235 | } | 
|  | 3236 |  | 
|  | 3237 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
|  | 3238 | return mInputFilterEnabled; | 
|  | 3239 | } | 
|  | 3240 |  | 
|  | 3241 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
|  | 3242 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3243 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3244 | "displayId=%" PRId32 ", policyFlags=0x%x, " | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3245 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
|  | 3246 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
| Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3247 | "yCursorPosition=%f, downTime=%" PRId64, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3248 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
|  | 3249 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
|  | 3250 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
|  | 3251 | args->xCursorPosition, args->yCursorPosition, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3252 | for (uint32_t i = 0; i < args->pointerCount; i++) { | 
|  | 3253 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3254 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 3255 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 3256 | "orientation=%f", | 
|  | 3257 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
|  | 3258 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3259 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3260 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 3261 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 3262 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 3263 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 3264 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 3265 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 3266 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3267 | } | 
|  | 3268 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3269 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
|  | 3270 | args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3271 | return; | 
|  | 3272 | } | 
|  | 3273 |  | 
|  | 3274 | uint32_t policyFlags = args->policyFlags; | 
|  | 3275 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3276 |  | 
|  | 3277 | android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3278 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3279 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3280 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3281 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3282 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3283 |  | 
|  | 3284 | bool needWake; | 
|  | 3285 | { // acquire lock | 
|  | 3286 | mLock.lock(); | 
|  | 3287 |  | 
|  | 3288 | if (shouldSendMotionToInputFilterLocked(args)) { | 
|  | 3289 | mLock.unlock(); | 
|  | 3290 |  | 
|  | 3291 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3292 | ui::Transform transform; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3293 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
|  | 3294 | args->action, args->actionButton, args->flags, args->edgeFlags, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3295 | args->metaState, args->buttonState, args->classification, transform, | 
|  | 3296 | args->xPrecision, args->yPrecision, args->xCursorPosition, | 
|  | 3297 | args->yCursorPosition, args->downTime, args->eventTime, | 
|  | 3298 | args->pointerCount, args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3299 |  | 
|  | 3300 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3301 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3302 | return; // event was consumed by the filter | 
|  | 3303 | } | 
|  | 3304 |  | 
|  | 3305 | mLock.lock(); | 
|  | 3306 | } | 
|  | 3307 |  | 
|  | 3308 | // Just enqueue a new motion event. | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3309 | MotionEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3310 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3311 | args->displayId, policyFlags, args->action, args->actionButton, | 
|  | 3312 | args->flags, args->metaState, args->buttonState, | 
|  | 3313 | args->classification, args->edgeFlags, args->xPrecision, | 
|  | 3314 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, | 
|  | 3315 | args->downTime, args->pointerCount, args->pointerProperties, | 
|  | 3316 | args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 |  | 
|  | 3318 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3319 | mLock.unlock(); | 
|  | 3320 | } // release lock | 
|  | 3321 |  | 
|  | 3322 | if (needWake) { | 
|  | 3323 | mLooper->wake(); | 
|  | 3324 | } | 
|  | 3325 | } | 
|  | 3326 |  | 
|  | 3327 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3328 | return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3329 | } | 
|  | 3330 |  | 
|  | 3331 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
|  | 3332 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3333 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3334 | "switchMask=0x%08x", | 
|  | 3335 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3336 | #endif | 
|  | 3337 |  | 
|  | 3338 | uint32_t policyFlags = args->policyFlags; | 
|  | 3339 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3340 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3341 | } | 
|  | 3342 |  | 
|  | 3343 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
|  | 3344 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3345 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
|  | 3346 | args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | #endif | 
|  | 3348 |  | 
|  | 3349 | bool needWake; | 
|  | 3350 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3351 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 |  | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3353 | DeviceResetEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3354 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3355 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3356 | } // release lock | 
|  | 3357 |  | 
|  | 3358 | if (needWake) { | 
|  | 3359 | mLooper->wake(); | 
|  | 3360 | } | 
|  | 3361 | } | 
|  | 3362 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3363 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, | 
|  | 3364 | int32_t injectorUid, int32_t syncMode, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3365 | std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3366 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3367 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3368 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
|  | 3369 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | #endif | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3371 | 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] | 3372 |  | 
|  | 3373 | policyFlags |= POLICY_FLAG_INJECTED; | 
|  | 3374 | if (hasInjectionPermission(injectorPid, injectorUid)) { | 
|  | 3375 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3376 | } | 
|  | 3377 |  | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3378 | std::queue<EventEntry*> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3379 | switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3380 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3381 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
|  | 3382 | int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3383 | if (!validateKeyEvent(action)) { | 
|  | 3384 | return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3385 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3387 | int32_t flags = incomingKey.getFlags(); | 
|  | 3388 | int32_t keyCode = incomingKey.getKeyCode(); | 
|  | 3389 | int32_t metaState = incomingKey.getMetaState(); | 
|  | 3390 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3391 | /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3392 | KeyEvent keyEvent; | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3393 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3394 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
|  | 3395 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
|  | 3396 | incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3397 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3398 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
|  | 3399 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3400 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3401 |  | 
|  | 3402 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3403 | android::base::Timer t; | 
|  | 3404 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
|  | 3405 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3406 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
|  | 3407 | std::to_string(t.duration().count()).c_str()); | 
|  | 3408 | } | 
|  | 3409 | } | 
|  | 3410 |  | 
|  | 3411 | mLock.lock(); | 
|  | 3412 | KeyEntry* injectedEntry = | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3413 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), | 
|  | 3414 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3415 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, | 
|  | 3416 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3417 | incomingKey.getDownTime()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3418 | injectedEntries.push(injectedEntry); | 
|  | 3419 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3420 | } | 
|  | 3421 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3422 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3423 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); | 
|  | 3424 | int32_t action = motionEvent->getAction(); | 
|  | 3425 | size_t pointerCount = motionEvent->getPointerCount(); | 
|  | 3426 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); | 
|  | 3427 | int32_t actionButton = motionEvent->getActionButton(); | 
|  | 3428 | int32_t displayId = motionEvent->getDisplayId(); | 
|  | 3429 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
|  | 3430 | return INPUT_EVENT_INJECTION_FAILED; | 
|  | 3431 | } | 
|  | 3432 |  | 
|  | 3433 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3434 | nsecs_t eventTime = motionEvent->getEventTime(); | 
|  | 3435 | android::base::Timer t; | 
|  | 3436 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
|  | 3437 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3438 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
|  | 3439 | std::to_string(t.duration().count()).c_str()); | 
|  | 3440 | } | 
|  | 3441 | } | 
|  | 3442 |  | 
|  | 3443 | mLock.lock(); | 
|  | 3444 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); | 
|  | 3445 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); | 
|  | 3446 | MotionEntry* injectedEntry = | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3447 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, | 
|  | 3448 | motionEvent->getSource(), motionEvent->getDisplayId(), | 
|  | 3449 | policyFlags, action, actionButton, motionEvent->getFlags(), | 
|  | 3450 | motionEvent->getMetaState(), motionEvent->getButtonState(), | 
|  | 3451 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), | 
|  | 3452 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3453 | motionEvent->getRawXCursorPosition(), | 
|  | 3454 | motionEvent->getRawYCursorPosition(), | 
|  | 3455 | motionEvent->getDownTime(), uint32_t(pointerCount), | 
|  | 3456 | pointerProperties, samplePointerCoords, | 
|  | 3457 | motionEvent->getXOffset(), motionEvent->getYOffset()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3458 | injectedEntries.push(injectedEntry); | 
|  | 3459 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { | 
|  | 3460 | sampleEventTimes += 1; | 
|  | 3461 | samplePointerCoords += pointerCount; | 
|  | 3462 | MotionEntry* nextInjectedEntry = | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3463 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3464 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3465 | motionEvent->getDisplayId(), policyFlags, action, | 
|  | 3466 | actionButton, motionEvent->getFlags(), | 
|  | 3467 | motionEvent->getMetaState(), motionEvent->getButtonState(), | 
|  | 3468 | motionEvent->getClassification(), | 
|  | 3469 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), | 
|  | 3470 | motionEvent->getYPrecision(), | 
|  | 3471 | motionEvent->getRawXCursorPosition(), | 
|  | 3472 | motionEvent->getRawYCursorPosition(), | 
|  | 3473 | motionEvent->getDownTime(), uint32_t(pointerCount), | 
|  | 3474 | pointerProperties, samplePointerCoords, | 
|  | 3475 | motionEvent->getXOffset(), motionEvent->getYOffset()); | 
|  | 3476 | injectedEntries.push(nextInjectedEntry); | 
|  | 3477 | } | 
|  | 3478 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3480 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3481 | default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3482 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3483 | return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3484 | } | 
|  | 3485 |  | 
|  | 3486 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
|  | 3487 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { | 
|  | 3488 | injectionState->injectionIsAsync = true; | 
|  | 3489 | } | 
|  | 3490 |  | 
|  | 3491 | injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3492 | injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3493 |  | 
|  | 3494 | bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3495 | while (!injectedEntries.empty()) { | 
|  | 3496 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); | 
|  | 3497 | injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3498 | } | 
|  | 3499 |  | 
|  | 3500 | mLock.unlock(); | 
|  | 3501 |  | 
|  | 3502 | if (needWake) { | 
|  | 3503 | mLooper->wake(); | 
|  | 3504 | } | 
|  | 3505 |  | 
|  | 3506 | int32_t injectionResult; | 
|  | 3507 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3508 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 |  | 
|  | 3510 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { | 
|  | 3511 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; | 
|  | 3512 | } else { | 
|  | 3513 | for (;;) { | 
|  | 3514 | injectionResult = injectionState->injectionResult; | 
|  | 3515 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { | 
|  | 3516 | break; | 
|  | 3517 | } | 
|  | 3518 |  | 
|  | 3519 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 3520 | if (remainingTimeout <= 0) { | 
|  | 3521 | #if DEBUG_INJECTION | 
|  | 3522 | ALOGD("injectInputEvent - Timed out waiting for injection result " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3523 | "to become available."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3524 | #endif | 
|  | 3525 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; | 
|  | 3526 | break; | 
|  | 3527 | } | 
|  | 3528 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3529 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | } | 
|  | 3531 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3532 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && | 
|  | 3533 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | while (injectionState->pendingForegroundDispatches != 0) { | 
|  | 3535 | #if DEBUG_INJECTION | 
|  | 3536 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3537 | injectionState->pendingForegroundDispatches); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3538 | #endif | 
|  | 3539 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 3540 | if (remainingTimeout <= 0) { | 
|  | 3541 | #if DEBUG_INJECTION | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3542 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
|  | 3543 | "dispatches to finish."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3544 | #endif | 
|  | 3545 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; | 
|  | 3546 | break; | 
|  | 3547 | } | 
|  | 3548 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3549 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3550 | } | 
|  | 3551 | } | 
|  | 3552 | } | 
|  | 3553 |  | 
|  | 3554 | injectionState->release(); | 
|  | 3555 | } // release lock | 
|  | 3556 |  | 
|  | 3557 | #if DEBUG_INJECTION | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3558 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3559 | injectionResult, injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3560 | #endif | 
|  | 3561 |  | 
|  | 3562 | return injectionResult; | 
|  | 3563 | } | 
|  | 3564 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3565 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3566 | std::array<uint8_t, 32> calculatedHmac; | 
|  | 3567 | std::unique_ptr<VerifiedInputEvent> result; | 
|  | 3568 | switch (event.getType()) { | 
|  | 3569 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 3570 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 3571 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
|  | 3572 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3573 | calculatedHmac = sign(verifiedKeyEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3574 | break; | 
|  | 3575 | } | 
|  | 3576 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3577 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 3578 | VerifiedMotionEvent verifiedMotionEvent = | 
|  | 3579 | verifiedMotionEventFromMotionEvent(motionEvent); | 
|  | 3580 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3581 | calculatedHmac = sign(verifiedMotionEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3582 | break; | 
|  | 3583 | } | 
|  | 3584 | default: { | 
|  | 3585 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
|  | 3586 | return nullptr; | 
|  | 3587 | } | 
|  | 3588 | } | 
|  | 3589 | if (calculatedHmac == INVALID_HMAC) { | 
|  | 3590 | return nullptr; | 
|  | 3591 | } | 
|  | 3592 | if (calculatedHmac != event.getHmac()) { | 
|  | 3593 | return nullptr; | 
|  | 3594 | } | 
|  | 3595 | return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3596 | } | 
|  | 3597 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3599 | return injectorUid == 0 || | 
|  | 3600 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3601 | } | 
|  | 3602 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3603 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | InjectionState* injectionState = entry->injectionState; | 
|  | 3605 | if (injectionState) { | 
|  | 3606 | #if DEBUG_INJECTION | 
|  | 3607 | ALOGD("Setting input event injection result to %d.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3608 | "injectorPid=%d, injectorUid=%d", | 
|  | 3609 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3610 | #endif | 
|  | 3611 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3612 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3613 | // Log the outcome since the injector did not wait for the injection result. | 
|  | 3614 | switch (injectionResult) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3615 | case INPUT_EVENT_INJECTION_SUCCEEDED: | 
|  | 3616 | ALOGV("Asynchronous input event injection succeeded."); | 
|  | 3617 | break; | 
|  | 3618 | case INPUT_EVENT_INJECTION_FAILED: | 
|  | 3619 | ALOGW("Asynchronous input event injection failed."); | 
|  | 3620 | break; | 
|  | 3621 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: | 
|  | 3622 | ALOGW("Asynchronous input event injection permission denied."); | 
|  | 3623 | break; | 
|  | 3624 | case INPUT_EVENT_INJECTION_TIMED_OUT: | 
|  | 3625 | ALOGW("Asynchronous input event injection timed out."); | 
|  | 3626 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3627 | } | 
|  | 3628 | } | 
|  | 3629 |  | 
|  | 3630 | injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3631 | mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | } | 
|  | 3633 | } | 
|  | 3634 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3635 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3636 | InjectionState* injectionState = entry->injectionState; | 
|  | 3637 | if (injectionState) { | 
|  | 3638 | injectionState->pendingForegroundDispatches += 1; | 
|  | 3639 | } | 
|  | 3640 | } | 
|  | 3641 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3642 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3643 | InjectionState* injectionState = entry->injectionState; | 
|  | 3644 | if (injectionState) { | 
|  | 3645 | injectionState->pendingForegroundDispatches -= 1; | 
|  | 3646 |  | 
|  | 3647 | if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3648 | mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3649 | } | 
|  | 3650 | } | 
|  | 3651 | } | 
|  | 3652 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3653 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3654 | int32_t displayId) const { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3655 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; | 
|  | 3656 | auto it = mWindowHandlesByDisplay.find(displayId); | 
|  | 3657 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3658 | } | 
|  | 3659 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3660 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3661 | const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3662 | if (windowHandleToken == nullptr) { | 
|  | 3663 | return nullptr; | 
|  | 3664 | } | 
|  | 3665 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3666 | for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3667 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3668 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3669 | if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3670 | return windowHandle; | 
|  | 3671 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3672 | } | 
|  | 3673 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3674 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3675 | } | 
|  | 3676 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3677 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, | 
|  | 3678 | int displayId) const { | 
|  | 3679 | if (windowHandleToken == nullptr) { | 
|  | 3680 | return nullptr; | 
|  | 3681 | } | 
|  | 3682 |  | 
|  | 3683 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
|  | 3684 | if (windowHandle->getToken() == windowHandleToken) { | 
|  | 3685 | return windowHandle; | 
|  | 3686 | } | 
|  | 3687 | } | 
|  | 3688 | return nullptr; | 
|  | 3689 | } | 
|  | 3690 |  | 
|  | 3691 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
|  | 3692 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 3693 | return getWindowHandleLocked(focusedToken, displayId); | 
|  | 3694 | } | 
|  | 3695 |  | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3696 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 3697 | for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3698 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3699 | for (const sp<InputWindowHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3700 | if (handle->getId() == windowHandle->getId() && | 
|  | 3701 | handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3702 | if (windowHandle->getInfo()->displayId != it.first) { | 
|  | 3703 | ALOGE("Found window %s in display %" PRId32 | 
|  | 3704 | ", but it should belong to display %" PRId32, | 
|  | 3705 | windowHandle->getName().c_str(), it.first, | 
|  | 3706 | windowHandle->getInfo()->displayId); | 
|  | 3707 | } | 
|  | 3708 | return true; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3709 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3710 | } | 
|  | 3711 | } | 
|  | 3712 | return false; | 
|  | 3713 | } | 
|  | 3714 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3715 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { | 
|  | 3716 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); | 
|  | 3717 | const bool noInputChannel = | 
|  | 3718 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 3719 | if (connection != nullptr && noInputChannel) { | 
|  | 3720 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", | 
|  | 3721 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); | 
|  | 3722 | return false; | 
|  | 3723 | } | 
|  | 3724 |  | 
|  | 3725 | if (connection == nullptr) { | 
|  | 3726 | if (!noInputChannel) { | 
|  | 3727 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); | 
|  | 3728 | } | 
|  | 3729 | return false; | 
|  | 3730 | } | 
|  | 3731 | if (!connection->responsive) { | 
|  | 3732 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); | 
|  | 3733 | return false; | 
|  | 3734 | } | 
|  | 3735 | return true; | 
|  | 3736 | } | 
|  | 3737 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3738 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( | 
|  | 3739 | const sp<IBinder>& token) const { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3740 | size_t count = mInputChannelsByToken.count(token); | 
|  | 3741 | if (count == 0) { | 
|  | 3742 | return nullptr; | 
|  | 3743 | } | 
|  | 3744 | return mInputChannelsByToken.at(token); | 
|  | 3745 | } | 
|  | 3746 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3747 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
|  | 3748 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
|  | 3749 | if (inputWindowHandles.empty()) { | 
|  | 3750 | // Remove all handles on a display if there are no windows left. | 
|  | 3751 | mWindowHandlesByDisplay.erase(displayId); | 
|  | 3752 | return; | 
|  | 3753 | } | 
|  | 3754 |  | 
|  | 3755 | // Since we compare the pointer of input window handles across window updates, we need | 
|  | 3756 | // to make sure the handle object for the same window stays unchanged across updates. | 
|  | 3757 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3758 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3759 | for (const sp<InputWindowHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3760 | oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3761 | } | 
|  | 3762 |  | 
|  | 3763 | std::vector<sp<InputWindowHandle>> newHandles; | 
|  | 3764 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { | 
|  | 3765 | if (!handle->updateInfo()) { | 
|  | 3766 | // handle no longer valid | 
|  | 3767 | continue; | 
|  | 3768 | } | 
|  | 3769 |  | 
|  | 3770 | const InputWindowInfo* info = handle->getInfo(); | 
|  | 3771 | if ((getInputChannelLocked(handle->getToken()) == nullptr && | 
|  | 3772 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { | 
|  | 3773 | const bool noInputChannel = | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3774 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 3775 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || | 
|  | 3776 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3777 | if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3778 | ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3779 | handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3780 | continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3781 | } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3782 | } | 
|  | 3783 |  | 
|  | 3784 | if (info->displayId != displayId) { | 
|  | 3785 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
|  | 3786 | handle->getName().c_str(), displayId, info->displayId); | 
|  | 3787 | continue; | 
|  | 3788 | } | 
|  | 3789 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3790 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
|  | 3791 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3792 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3793 | oldHandle->updateFrom(handle); | 
|  | 3794 | newHandles.push_back(oldHandle); | 
|  | 3795 | } else { | 
|  | 3796 | newHandles.push_back(handle); | 
|  | 3797 | } | 
|  | 3798 | } | 
|  | 3799 |  | 
|  | 3800 | // Insert or replace | 
|  | 3801 | mWindowHandlesByDisplay[displayId] = newHandles; | 
|  | 3802 | } | 
|  | 3803 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3804 | void InputDispatcher::setInputWindows( | 
|  | 3805 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { | 
|  | 3806 | { // acquire lock | 
|  | 3807 | std::scoped_lock _l(mLock); | 
|  | 3808 | for (auto const& i : handlesPerDisplay) { | 
|  | 3809 | setInputWindowsLocked(i.second, i.first); | 
|  | 3810 | } | 
|  | 3811 | } | 
|  | 3812 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3813 | mLooper->wake(); | 
|  | 3814 | } | 
|  | 3815 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3816 | /** | 
|  | 3817 | * Called from InputManagerService, update window handle list by displayId that can receive input. | 
|  | 3818 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
|  | 3819 | * If set an empty list, remove all handles from the specific display. | 
|  | 3820 | * For focused handle, check if need to change and send a cancel event to previous one. | 
|  | 3821 | * For removed handle, check if need to send a cancel event if already in touch. | 
|  | 3822 | */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3823 | void InputDispatcher::setInputWindowsLocked( | 
|  | 3824 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3825 | if (DEBUG_FOCUS) { | 
|  | 3826 | std::string windowList; | 
|  | 3827 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { | 
|  | 3828 | windowList += iwh->getName() + " "; | 
|  | 3829 | } | 
|  | 3830 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
|  | 3831 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3832 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3833 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL | 
|  | 3834 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { | 
|  | 3835 | const bool noInputWindow = | 
|  | 3836 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 3837 | if (noInputWindow && window->getToken() != nullptr) { | 
|  | 3838 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", | 
|  | 3839 | window->getName().c_str()); | 
|  | 3840 | window->releaseChannel(); | 
|  | 3841 | } | 
|  | 3842 | } | 
|  | 3843 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3844 | // Copy old handles for release if they are no longer present. | 
|  | 3845 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3846 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3847 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3848 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3849 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 3850 | if (mLastHoverWindowHandle && | 
|  | 3851 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == | 
|  | 3852 | windowHandles.end()) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3853 | mLastHoverWindowHandle = nullptr; | 
|  | 3854 | } | 
|  | 3855 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3856 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 3857 | if (focusedToken) { | 
|  | 3858 | FocusResult result = checkTokenFocusableLocked(focusedToken, displayId); | 
|  | 3859 | if (result != FocusResult::OK) { | 
|  | 3860 | onFocusChangedLocked(focusedToken, nullptr, displayId, typeToString(result)); | 
|  | 3861 | } | 
|  | 3862 | } | 
|  | 3863 |  | 
|  | 3864 | std::optional<FocusRequest> focusRequest = | 
|  | 3865 | getOptionalValueByKey(mPendingFocusRequests, displayId); | 
|  | 3866 | if (focusRequest) { | 
|  | 3867 | // If the window from the pending request is now visible, provide it focus. | 
|  | 3868 | FocusResult result = handleFocusRequestLocked(*focusRequest); | 
|  | 3869 | if (result != FocusResult::NOT_VISIBLE) { | 
|  | 3870 | // Drop the request if we were able to change the focus or we cannot change | 
|  | 3871 | // it for another reason. | 
|  | 3872 | mPendingFocusRequests.erase(displayId); | 
|  | 3873 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3874 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3876 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 3877 | mTouchStatesByDisplay.find(displayId); | 
|  | 3878 | if (stateIt != mTouchStatesByDisplay.end()) { | 
|  | 3879 | TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3880 | for (size_t i = 0; i < state.windows.size();) { | 
|  | 3881 | TouchedWindow& touchedWindow = state.windows[i]; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3882 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3883 | if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3884 | ALOGD("Touched window was removed: %s in display %" PRId32, | 
|  | 3885 | touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3886 | } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3887 | std::shared_ptr<InputChannel> touchedInputChannel = | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3888 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
|  | 3889 | if (touchedInputChannel != nullptr) { | 
|  | 3890 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 3891 | "touched window was removed"); | 
|  | 3892 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3894 | state.windows.erase(state.windows.begin() + i); | 
|  | 3895 | } else { | 
|  | 3896 | ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3897 | } | 
|  | 3898 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3899 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3900 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3901 | // Release information for windows that are no longer present. | 
|  | 3902 | // This ensures that unused input channels are released promptly. | 
|  | 3903 | // Otherwise, they might stick around until the window handle is destroyed | 
|  | 3904 | // which might not happen until the next GC. | 
|  | 3905 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3906 | if (!hasWindowHandleLocked(oldWindowHandle)) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3907 | if (DEBUG_FOCUS) { | 
|  | 3908 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3909 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3910 | oldWindowHandle->releaseChannel(); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3911 | } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3912 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3913 | } | 
|  | 3914 |  | 
|  | 3915 | void InputDispatcher::setFocusedApplication( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3916 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3917 | if (DEBUG_FOCUS) { | 
|  | 3918 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
|  | 3919 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
|  | 3920 | } | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3921 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3922 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3924 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3925 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3926 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3927 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { | 
|  | 3928 | 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] | 3929 | } | 
|  | 3930 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3931 | // Set the new application handle. | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3932 | if (inputApplicationHandle != nullptr) { | 
|  | 3933 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
|  | 3934 | } else { | 
|  | 3935 | mFocusedApplicationHandlesByDisplay.erase(displayId); | 
|  | 3936 | } | 
|  | 3937 |  | 
|  | 3938 | // No matter what the old focused application was, stop waiting on it because it is | 
|  | 3939 | // no longer focused. | 
|  | 3940 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3941 | } // release lock | 
|  | 3942 |  | 
|  | 3943 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3944 | mLooper->wake(); | 
|  | 3945 | } | 
|  | 3946 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3947 | /** | 
|  | 3948 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
|  | 3949 | * the display not specified. | 
|  | 3950 | * | 
|  | 3951 | * We track any unreleased events for each window. If a window loses the ability to receive the | 
|  | 3952 | * released event, we will send a cancel event to it. So when the focused display is changed, we | 
|  | 3953 | * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
|  | 3954 | * display. The display-specified events won't be affected. | 
|  | 3955 | */ | 
|  | 3956 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3957 | if (DEBUG_FOCUS) { | 
|  | 3958 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
|  | 3959 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3960 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3961 | std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3962 |  | 
|  | 3963 | if (mFocusedDisplayId != displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3964 | sp<IBinder> oldFocusedWindowToken = | 
|  | 3965 | getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
|  | 3966 | if (oldFocusedWindowToken != nullptr) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3967 | std::shared_ptr<InputChannel> inputChannel = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3968 | getInputChannelLocked(oldFocusedWindowToken); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3969 | if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3970 | CancelationOptions | 
|  | 3971 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 3972 | "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3973 | options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3974 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 3975 | } | 
|  | 3976 | } | 
|  | 3977 | mFocusedDisplayId = displayId; | 
|  | 3978 |  | 
| Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 3979 | // Find new focused window and validate | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3980 | sp<IBinder> newFocusedWindowToken = | 
|  | 3981 | getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 3982 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3983 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3984 | if (newFocusedWindowToken == nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3985 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3986 | if (!mFocusedWindowTokenByDisplay.empty()) { | 
|  | 3987 | ALOGE("But another display has a focused window\n%s", | 
|  | 3988 | dumpFocusedWindowsLocked().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3989 | } | 
|  | 3990 | } | 
|  | 3991 | } | 
|  | 3992 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3993 | if (DEBUG_FOCUS) { | 
|  | 3994 | logDispatchStateLocked(); | 
|  | 3995 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3996 | } // release lock | 
|  | 3997 |  | 
|  | 3998 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3999 | mLooper->wake(); | 
|  | 4000 | } | 
|  | 4001 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4003 | if (DEBUG_FOCUS) { | 
|  | 4004 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
|  | 4005 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4006 |  | 
|  | 4007 | bool changed; | 
|  | 4008 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4009 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4010 |  | 
|  | 4011 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
|  | 4012 | if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4013 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4014 | } | 
|  | 4015 |  | 
|  | 4016 | if (mDispatchEnabled && !enabled) { | 
|  | 4017 | resetAndDropEverythingLocked("dispatcher is being disabled"); | 
|  | 4018 | } | 
|  | 4019 |  | 
|  | 4020 | mDispatchEnabled = enabled; | 
|  | 4021 | mDispatchFrozen = frozen; | 
|  | 4022 | changed = true; | 
|  | 4023 | } else { | 
|  | 4024 | changed = false; | 
|  | 4025 | } | 
|  | 4026 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4027 | if (DEBUG_FOCUS) { | 
|  | 4028 | logDispatchStateLocked(); | 
|  | 4029 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4030 | } // release lock | 
|  | 4031 |  | 
|  | 4032 | if (changed) { | 
|  | 4033 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4034 | mLooper->wake(); | 
|  | 4035 | } | 
|  | 4036 | } | 
|  | 4037 |  | 
|  | 4038 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4039 | if (DEBUG_FOCUS) { | 
|  | 4040 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
|  | 4041 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4042 |  | 
|  | 4043 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4044 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 |  | 
|  | 4046 | if (mInputFilterEnabled == enabled) { | 
|  | 4047 | return; | 
|  | 4048 | } | 
|  | 4049 |  | 
|  | 4050 | mInputFilterEnabled = enabled; | 
|  | 4051 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
|  | 4052 | } // release lock | 
|  | 4053 |  | 
|  | 4054 | // Wake up poll loop since there might be work to do to drop everything. | 
|  | 4055 | mLooper->wake(); | 
|  | 4056 | } | 
|  | 4057 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4058 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
|  | 4059 | std::scoped_lock lock(mLock); | 
|  | 4060 | mInTouchMode = inTouchMode; | 
|  | 4061 | } | 
|  | 4062 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4063 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { | 
|  | 4064 | if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4065 | if (DEBUG_FOCUS) { | 
|  | 4066 | ALOGD("Trivial transfer to same window."); | 
|  | 4067 | } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4068 | return true; | 
|  | 4069 | } | 
|  | 4070 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4072 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4073 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4074 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
|  | 4075 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4076 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4077 | ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4078 | return false; | 
|  | 4079 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4080 | if (DEBUG_FOCUS) { | 
|  | 4081 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
|  | 4082 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
|  | 4083 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4084 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4085 | if (DEBUG_FOCUS) { | 
|  | 4086 | ALOGD("Cannot transfer focus because windows are on different displays."); | 
|  | 4087 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4088 | return false; | 
|  | 4089 | } | 
|  | 4090 |  | 
|  | 4091 | bool found = false; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4092 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4093 | TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4094 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4095 | const TouchedWindow& touchedWindow = state.windows[i]; | 
|  | 4096 | if (touchedWindow.windowHandle == fromWindowHandle) { | 
|  | 4097 | int32_t oldTargetFlags = touchedWindow.targetFlags; | 
|  | 4098 | BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4099 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4100 | state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4101 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4102 | int32_t newTargetFlags = oldTargetFlags & | 
|  | 4103 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
|  | 4104 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4105 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4107 | found = true; | 
|  | 4108 | goto Found; | 
|  | 4109 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | } | 
|  | 4111 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4112 | Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4114 | if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4115 | if (DEBUG_FOCUS) { | 
|  | 4116 | ALOGD("Focus transfer failed because from window did not have focus."); | 
|  | 4117 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4118 | return false; | 
|  | 4119 | } | 
|  | 4120 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4121 | sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
|  | 4122 | sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4123 | if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4124 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4125 | CancelationOptions | 
|  | 4126 | options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4127 | "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4128 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4129 | synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | } | 
|  | 4131 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4132 | if (DEBUG_FOCUS) { | 
|  | 4133 | logDispatchStateLocked(); | 
|  | 4134 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | } // release lock | 
|  | 4136 |  | 
|  | 4137 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4138 | mLooper->wake(); | 
|  | 4139 | return true; | 
|  | 4140 | } | 
|  | 4141 |  | 
|  | 4142 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4143 | if (DEBUG_FOCUS) { | 
|  | 4144 | ALOGD("Resetting and dropping all events (%s).", reason); | 
|  | 4145 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4146 |  | 
|  | 4147 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
|  | 4148 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 4149 |  | 
|  | 4150 | resetKeyRepeatLocked(); | 
|  | 4151 | releasePendingEventLocked(); | 
|  | 4152 | drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4153 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4154 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4155 | mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4156 | mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4158 | mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4159 | } | 
|  | 4160 |  | 
|  | 4161 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4162 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4163 | dumpDispatchStateLocked(dump); | 
|  | 4164 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4165 | std::istringstream stream(dump); | 
|  | 4166 | std::string line; | 
|  | 4167 |  | 
|  | 4168 | while (std::getline(stream, line, '\n')) { | 
|  | 4169 | ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4170 | } | 
|  | 4171 | } | 
|  | 4172 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4173 | std::string InputDispatcher::dumpFocusedWindowsLocked() { | 
|  | 4174 | if (mFocusedWindowTokenByDisplay.empty()) { | 
|  | 4175 | return INDENT "FocusedWindows: <none>\n"; | 
|  | 4176 | } | 
|  | 4177 |  | 
|  | 4178 | std::string dump; | 
|  | 4179 | dump += INDENT "FocusedWindows:\n"; | 
|  | 4180 | for (auto& it : mFocusedWindowTokenByDisplay) { | 
|  | 4181 | const int32_t displayId = it.first; | 
|  | 4182 | const sp<InputWindowHandle> windowHandle = getFocusedWindowHandleLocked(displayId); | 
|  | 4183 | if (windowHandle) { | 
|  | 4184 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, | 
|  | 4185 | windowHandle->getName().c_str()); | 
|  | 4186 | } else { | 
|  | 4187 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
|  | 4188 | " has focused token without a window'\n", | 
|  | 4189 | displayId); | 
|  | 4190 | } | 
|  | 4191 | } | 
|  | 4192 | return dump; | 
|  | 4193 | } | 
|  | 4194 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4195 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4196 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
|  | 4197 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
|  | 4198 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4199 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4200 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4201 | if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
|  | 4202 | dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
|  | 4203 | for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
|  | 4204 | const int32_t displayId = it.first; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4205 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4206 | const std::chrono::duration timeout = | 
|  | 4207 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4208 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4209 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4210 | displayId, applicationHandle->getName().c_str(), millis(timeout)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4211 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4213 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4214 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4215 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4216 | dump += dumpFocusedWindowsLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4217 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4218 | if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4219 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4220 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4221 | const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4222 | 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] | 4223 | state.displayId, toString(state.down), toString(state.split), | 
|  | 4224 | state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4225 | if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4226 | dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4227 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4228 | const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4229 | dump += StringPrintf(INDENT4 | 
|  | 4230 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
|  | 4231 | i, touchedWindow.windowHandle->getName().c_str(), | 
|  | 4232 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4233 | } | 
|  | 4234 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4235 | dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4236 | } | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4237 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4238 | dump += INDENT3 "Portal windows:\n"; | 
|  | 4239 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4240 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4241 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, | 
|  | 4242 | portalWindowHandle->getName().c_str()); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4243 | } | 
|  | 4244 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4245 | } | 
|  | 4246 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4247 | dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4248 | } | 
|  | 4249 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4250 | if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4251 | for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4252 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4253 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4254 | if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4255 | dump += INDENT2 "Windows:\n"; | 
|  | 4256 | for (size_t i = 0; i < windowHandles.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4257 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4258 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4260 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4261 | "portalToDisplayId=%d, paused=%s, focusable=%s, " | 
|  | 4262 | "hasWallpaper=%s, visible=%s, " | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4263 | "flags=%s, type=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4264 | "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4265 | "applicationInfo=%s, " | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4266 | "touchableRegion=", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4267 | i, windowInfo->name.c_str(), windowInfo->displayId, | 
|  | 4268 | windowInfo->portalToDisplayId, | 
|  | 4269 | toString(windowInfo->paused), | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4270 | toString(windowInfo->focusable), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4271 | toString(windowInfo->hasWallpaper), | 
|  | 4272 | toString(windowInfo->visible), | 
| Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4273 | windowInfo->flags.string().c_str(), | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4274 | static_cast<int32_t>(windowInfo->type), | 
|  | 4275 | windowInfo->frameLeft, windowInfo->frameTop, | 
|  | 4276 | windowInfo->frameRight, windowInfo->frameBottom, | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4277 | windowInfo->globalScaleFactor, | 
|  | 4278 | windowInfo->applicationInfo.name.c_str()); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4279 | dumpRegion(dump, windowInfo->touchableRegion); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4280 | dump += StringPrintf(", inputFeatures=%s", | 
|  | 4281 | windowInfo->inputFeatures.string().c_str()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4282 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
|  | 4283 | "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4284 | windowInfo->ownerPid, windowInfo->ownerUid, | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4285 | millis(windowInfo->dispatchingTimeout)); | 
| chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4286 | windowInfo->transform.dump(dump, "transform", INDENT4); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4287 | } | 
|  | 4288 | } else { | 
|  | 4289 | dump += INDENT2 "Windows: <none>\n"; | 
|  | 4290 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4291 | } | 
|  | 4292 | } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4293 | dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | } | 
|  | 4295 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4296 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4297 | for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4298 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4299 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
|  | 4300 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4301 | } | 
|  | 4302 | for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4303 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4304 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
|  | 4305 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4306 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4308 | dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4309 | } | 
|  | 4310 |  | 
|  | 4311 | nsecs_t currentTime = now(); | 
|  | 4312 |  | 
|  | 4313 | // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4314 | if (!mRecentQueue.empty()) { | 
|  | 4315 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
|  | 4316 | for (EventEntry* entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4317 | dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4318 | entry->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4319 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } | 
|  | 4321 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4322 | dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4323 | } | 
|  | 4324 |  | 
|  | 4325 | // Dump event currently being dispatched. | 
|  | 4326 | if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4327 | dump += INDENT "PendingEvent:\n"; | 
|  | 4328 | dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | mPendingEvent->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4330 | dump += StringPrintf(", age=%" PRId64 "ms\n", | 
|  | 4331 | ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4333 | dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | } | 
|  | 4335 |  | 
|  | 4336 | // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4337 | if (!mInboundQueue.empty()) { | 
|  | 4338 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
|  | 4339 | for (EventEntry* entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4340 | dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4341 | entry->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4342 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4343 | } | 
|  | 4344 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4345 | dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | } | 
|  | 4347 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4348 | if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4349 | dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4350 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
|  | 4351 | const KeyReplacement& replacement = pair.first; | 
|  | 4352 | int32_t newKeyCode = pair.second; | 
|  | 4353 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4354 | replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4355 | } | 
|  | 4356 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4357 | dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4358 | } | 
|  | 4359 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4360 | if (!mConnectionsByFd.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4361 | dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4362 | for (const auto& pair : mConnectionsByFd) { | 
|  | 4363 | const sp<Connection>& connection = pair.second; | 
|  | 4364 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4365 | "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4366 | pair.first, connection->getInputChannelName().c_str(), | 
|  | 4367 | connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4368 | toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4370 | if (!connection->outboundQueue.empty()) { | 
|  | 4371 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
|  | 4372 | connection->outboundQueue.size()); | 
|  | 4373 | for (DispatchEntry* entry : connection->outboundQueue) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | dump.append(INDENT4); | 
|  | 4375 | entry->eventEntry->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4376 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 | 
|  | 4377 | "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4378 | entry->targetFlags, entry->resolvedAction, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4379 | ns2ms(currentTime - entry->eventEntry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4380 | } | 
|  | 4381 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4382 | dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | } | 
|  | 4384 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4385 | if (!connection->waitQueue.empty()) { | 
|  | 4386 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
|  | 4387 | connection->waitQueue.size()); | 
|  | 4388 | for (DispatchEntry* entry : connection->waitQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4389 | dump += INDENT4; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4390 | entry->eventEntry->appendDescription(dump); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4391 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4392 | "age=%" PRId64 "ms, wait=%" PRId64 "ms seq=%" PRIu32 "\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4393 | entry->targetFlags, entry->resolvedAction, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4394 | ns2ms(currentTime - entry->eventEntry->eventTime), | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 4395 | ns2ms(currentTime - entry->deliveryTime), entry->seq); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | } | 
|  | 4397 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4398 | dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | } | 
|  | 4400 | } | 
|  | 4401 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4402 | dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4403 | } | 
|  | 4404 |  | 
|  | 4405 | if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4406 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
|  | 4407 | ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4408 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4409 | dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4410 | } | 
|  | 4411 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4412 | dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4413 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
|  | 4414 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
|  | 4415 | ns2ms(mConfig.keyRepeatTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4416 | } | 
|  | 4417 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4418 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
|  | 4419 | const size_t numMonitors = monitors.size(); | 
|  | 4420 | for (size_t i = 0; i < numMonitors; i++) { | 
|  | 4421 | const Monitor& monitor = monitors[i]; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4422 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4423 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
|  | 4424 | dump += "\n"; | 
|  | 4425 | } | 
|  | 4426 | } | 
|  | 4427 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4428 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel( | 
|  | 4429 | const std::string& name) { | 
|  | 4430 | #if DEBUG_CHANNEL_CREATION | 
|  | 4431 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4432 | #endif | 
|  | 4433 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4434 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 4435 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 4436 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 4437 |  | 
|  | 4438 | if (result) { | 
|  | 4439 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 4440 | } | 
|  | 4441 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4442 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4443 | std::scoped_lock _l(mLock); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4444 | sp<Connection> connection = new Connection(serverChannel, false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4445 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4446 | int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4447 | mConnectionsByFd[fd] = connection; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4448 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4449 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4450 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
|  | 4451 | } // release lock | 
|  | 4452 |  | 
|  | 4453 | // Wake the looper because some connections have changed. | 
|  | 4454 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4455 | return clientChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4456 | } | 
|  | 4457 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4458 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor( | 
|  | 4459 | int32_t displayId, bool isGestureMonitor, const std::string& name) { | 
|  | 4460 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 4461 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 4462 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 4463 | if (result) { | 
|  | 4464 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 4465 | } | 
|  | 4466 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4467 | { // acquire lock | 
|  | 4468 | std::scoped_lock _l(mLock); | 
|  | 4469 |  | 
|  | 4470 | if (displayId < 0) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4471 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name | 
|  | 4472 | << " without a specified display."; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4473 | } | 
|  | 4474 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4475 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4476 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4477 | const int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4478 | mConnectionsByFd[fd] = connection; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4479 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4480 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4481 | auto& monitorsByDisplay = | 
|  | 4482 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4483 | monitorsByDisplay[displayId].emplace_back(serverChannel); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4484 |  | 
|  | 4485 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4486 | } | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4487 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4488 | // Wake the looper because some connections have changed. | 
|  | 4489 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4490 | return clientChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4491 | } | 
|  | 4492 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4493 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4494 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4495 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4496 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4497 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4498 | if (status) { | 
|  | 4499 | return status; | 
|  | 4500 | } | 
|  | 4501 | } // release lock | 
|  | 4502 |  | 
|  | 4503 | // Wake the poll loop because removing the connection may have changed the current | 
|  | 4504 | // synchronization state. | 
|  | 4505 | mLooper->wake(); | 
|  | 4506 | return OK; | 
|  | 4507 | } | 
|  | 4508 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4509 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, | 
|  | 4510 | bool notify) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4511 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4512 | if (connection == nullptr) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4513 | ALOGW("Attempted to unregister already unregistered input channel"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | return BAD_VALUE; | 
|  | 4515 | } | 
|  | 4516 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4517 | removeConnectionLocked(connection); | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4518 | mInputChannelsByToken.erase(connectionToken); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4519 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4520 | if (connection->monitor) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4521 | removeMonitorChannelLocked(connectionToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4522 | } | 
|  | 4523 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4524 | mLooper->removeFd(connection->inputChannel->getFd()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4525 |  | 
|  | 4526 | nsecs_t currentTime = now(); | 
|  | 4527 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
|  | 4528 |  | 
|  | 4529 | connection->status = Connection::STATUS_ZOMBIE; | 
|  | 4530 | return OK; | 
|  | 4531 | } | 
|  | 4532 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4533 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { | 
|  | 4534 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); | 
|  | 4535 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4536 | } | 
|  | 4537 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4538 | void InputDispatcher::removeMonitorChannelLocked( | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4539 | const sp<IBinder>& connectionToken, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4540 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4541 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4542 | std::vector<Monitor>& monitors = it->second; | 
|  | 4543 | const size_t numMonitors = monitors.size(); | 
|  | 4544 | for (size_t i = 0; i < numMonitors; i++) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4545 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4546 | monitors.erase(monitors.begin() + i); | 
|  | 4547 | break; | 
|  | 4548 | } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4549 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4550 | if (monitors.empty()) { | 
|  | 4551 | it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4552 | } else { | 
|  | 4553 | ++it; | 
|  | 4554 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4555 | } | 
|  | 4556 | } | 
|  | 4557 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4558 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
|  | 4559 | { // acquire lock | 
|  | 4560 | std::scoped_lock _l(mLock); | 
|  | 4561 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
|  | 4562 |  | 
|  | 4563 | if (!foundDisplayId) { | 
|  | 4564 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
|  | 4565 | return BAD_VALUE; | 
|  | 4566 | } | 
|  | 4567 | int32_t displayId = foundDisplayId.value(); | 
|  | 4568 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4569 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4570 | mTouchStatesByDisplay.find(displayId); | 
|  | 4571 | if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4572 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
|  | 4573 | return BAD_VALUE; | 
|  | 4574 | } | 
|  | 4575 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4576 | TouchState& state = stateIt->second; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4577 | std::optional<int32_t> foundDeviceId; | 
|  | 4578 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4579 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4580 | foundDeviceId = state.deviceId; | 
|  | 4581 | } | 
|  | 4582 | } | 
|  | 4583 | if (!foundDeviceId || !state.down) { | 
|  | 4584 | 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] | 4585 | " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4586 | return BAD_VALUE; | 
|  | 4587 | } | 
|  | 4588 | int32_t deviceId = foundDeviceId.value(); | 
|  | 4589 |  | 
|  | 4590 | // Send cancel events to all the input channels we're stealing from. | 
|  | 4591 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4592 | "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4593 | options.deviceId = deviceId; | 
|  | 4594 | options.displayId = displayId; | 
|  | 4595 | for (const TouchedWindow& window : state.windows) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4596 | std::shared_ptr<InputChannel> channel = | 
|  | 4597 | getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4598 | if (channel != nullptr) { | 
|  | 4599 | synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
|  | 4600 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4601 | } | 
|  | 4602 | // Then clear the current touch state so we stop dispatching to them as well. | 
|  | 4603 | state.filterNonMonitors(); | 
|  | 4604 | } | 
|  | 4605 | return OK; | 
|  | 4606 | } | 
|  | 4607 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4608 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
|  | 4609 | const sp<IBinder>& token) { | 
|  | 4610 | for (const auto& it : mGestureMonitorsByDisplay) { | 
|  | 4611 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4612 | for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4613 | if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4614 | return it.first; | 
|  | 4615 | } | 
|  | 4616 | } | 
|  | 4617 | } | 
|  | 4618 | return std::nullopt; | 
|  | 4619 | } | 
|  | 4620 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4621 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4622 | if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4623 | return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4624 | } | 
|  | 4625 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4626 | for (const auto& pair : mConnectionsByFd) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4627 | const sp<Connection>& connection = pair.second; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4628 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4629 | return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4630 | } | 
|  | 4631 | } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4632 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4633 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4634 | } | 
|  | 4635 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4636 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4637 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4638 | removeByValue(mConnectionsByFd, connection); | 
|  | 4639 | } | 
|  | 4640 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4641 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, | 
|  | 4642 | const sp<Connection>& connection, uint32_t seq, | 
|  | 4643 | bool handled) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4644 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4645 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4646 | commandEntry->connection = connection; | 
|  | 4647 | commandEntry->eventTime = currentTime; | 
|  | 4648 | commandEntry->seq = seq; | 
|  | 4649 | commandEntry->handled = handled; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4650 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4651 | } | 
|  | 4652 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4653 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, | 
|  | 4654 | const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4655 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4656 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4657 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4658 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4659 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4660 | commandEntry->connection = connection; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4661 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4662 | } | 
|  | 4663 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4664 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, | 
|  | 4665 | const sp<IBinder>& newToken) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4666 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4667 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4668 | commandEntry->oldToken = oldToken; | 
|  | 4669 | commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4670 | postCommandLocked(std::move(commandEntry)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4671 | } | 
|  | 4672 |  | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4673 | void InputDispatcher::onAnrLocked(const Connection& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4674 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
|  | 4675 | // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4676 | if (connection.waitQueue.empty()) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4677 | ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4678 | connection.inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4679 | return; | 
|  | 4680 | } | 
|  | 4681 | /** | 
|  | 4682 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
|  | 4683 | * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
|  | 4684 | * has changed. This could cause newer entries to time out before the already dispatched | 
|  | 4685 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
|  | 4686 | * processes the events linearly. So providing information about the oldest entry seems to be | 
|  | 4687 | * most useful. | 
|  | 4688 | */ | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4689 | DispatchEntry* oldestEntry = *connection.waitQueue.begin(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4690 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
|  | 4691 | std::string reason = | 
|  | 4692 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4693 | connection.inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4694 | ns2ms(currentWait), | 
|  | 4695 | oldestEntry->eventEntry->getDescription().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4696 |  | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4697 | updateLastAnrStateLocked(getWindowHandleLocked(connection.inputChannel->getConnectionToken()), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4698 | reason); | 
|  | 4699 |  | 
|  | 4700 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 4701 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
|  | 4702 | commandEntry->inputApplicationHandle = nullptr; | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4703 | commandEntry->inputChannel = connection.inputChannel; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4704 | commandEntry->reason = std::move(reason); | 
|  | 4705 | postCommandLocked(std::move(commandEntry)); | 
|  | 4706 | } | 
|  | 4707 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4708 | void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4709 | std::string reason = android::base::StringPrintf("%s does not have a focused window", | 
|  | 4710 | application->getName().c_str()); | 
|  | 4711 |  | 
|  | 4712 | updateLastAnrStateLocked(application, reason); | 
|  | 4713 |  | 
|  | 4714 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 4715 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
|  | 4716 | commandEntry->inputApplicationHandle = application; | 
|  | 4717 | commandEntry->inputChannel = nullptr; | 
|  | 4718 | commandEntry->reason = std::move(reason); | 
|  | 4719 | postCommandLocked(std::move(commandEntry)); | 
|  | 4720 | } | 
|  | 4721 |  | 
|  | 4722 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, | 
|  | 4723 | const std::string& reason) { | 
|  | 4724 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
|  | 4725 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 4726 | } | 
|  | 4727 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4728 | void InputDispatcher::updateLastAnrStateLocked( | 
|  | 4729 | const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4730 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); | 
|  | 4731 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 4732 | } | 
|  | 4733 |  | 
|  | 4734 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
|  | 4735 | const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | // 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] | 4737 | time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | struct tm tm; | 
|  | 4739 | localtime_r(&t, &tm); | 
|  | 4740 | char timestr[64]; | 
|  | 4741 | strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4742 | mLastAnrState.clear(); | 
|  | 4743 | mLastAnrState += INDENT "ANR:\n"; | 
|  | 4744 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4745 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
|  | 4746 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4747 | dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4748 | } | 
|  | 4749 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4750 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4751 | mLock.unlock(); | 
|  | 4752 |  | 
|  | 4753 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); | 
|  | 4754 |  | 
|  | 4755 | mLock.lock(); | 
|  | 4756 | } | 
|  | 4757 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4758 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4759 | sp<Connection> connection = commandEntry->connection; | 
|  | 4760 |  | 
|  | 4761 | if (connection->status != Connection::STATUS_ZOMBIE) { | 
|  | 4762 | mLock.unlock(); | 
|  | 4763 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4764 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4765 |  | 
|  | 4766 | mLock.lock(); | 
|  | 4767 | } | 
|  | 4768 | } | 
|  | 4769 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4770 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4771 | sp<IBinder> oldToken = commandEntry->oldToken; | 
|  | 4772 | sp<IBinder> newToken = commandEntry->newToken; | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4773 | mLock.unlock(); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4774 | mPolicy->notifyFocusChanged(oldToken, newToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4775 | mLock.lock(); | 
|  | 4776 | } | 
|  | 4777 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4778 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4779 | sp<IBinder> token = | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4780 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4781 | mLock.unlock(); | 
|  | 4782 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4783 | const std::chrono::nanoseconds timeoutExtension = | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4784 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4785 |  | 
|  | 4786 | mLock.lock(); | 
|  | 4787 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4788 | if (timeoutExtension > 0s) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4789 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); | 
|  | 4790 | } else { | 
|  | 4791 | // stop waking up for events in this connection, it is already not responding | 
|  | 4792 | sp<Connection> connection = getConnectionLocked(token); | 
|  | 4793 | if (connection == nullptr) { | 
|  | 4794 | return; | 
|  | 4795 | } | 
|  | 4796 | cancelEventsForAnrLocked(connection); | 
|  | 4797 | } | 
|  | 4798 | } | 
|  | 4799 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4800 | void InputDispatcher::extendAnrTimeoutsLocked( | 
|  | 4801 | const std::shared_ptr<InputApplicationHandle>& application, | 
|  | 4802 | const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) { | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4803 | if (connectionToken == nullptr && application != nullptr) { | 
|  | 4804 | // The ANR happened because there's no focused window | 
|  | 4805 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); | 
|  | 4806 | mAwaitedFocusedApplication = application; | 
|  | 4807 | } | 
|  | 4808 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4809 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 4810 | if (connection == nullptr) { | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4811 | // It's possible that the connection already disappeared. No action necessary. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4812 | return; | 
|  | 4813 | } | 
|  | 4814 |  | 
|  | 4815 | 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] | 4816 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4817 |  | 
|  | 4818 | connection->responsive = true; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 4819 | const nsecs_t newTimeout = now() + timeoutExtension.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4820 | for (DispatchEntry* entry : connection->waitQueue) { | 
|  | 4821 | if (newTimeout >= entry->timeoutTime) { | 
|  | 4822 | // Already removed old entries when connection was marked unresponsive | 
|  | 4823 | entry->timeoutTime = newTimeout; | 
|  | 4824 | mAnrTracker.insert(entry->timeoutTime, connectionToken); | 
|  | 4825 | } | 
|  | 4826 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | } | 
|  | 4828 |  | 
|  | 4829 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( | 
|  | 4830 | CommandEntry* commandEntry) { | 
|  | 4831 | KeyEntry* entry = commandEntry->keyEntry; | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4832 | KeyEvent event = createKeyEvent(*entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4833 |  | 
|  | 4834 | mLock.unlock(); | 
|  | 4835 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4836 | android::base::Timer t; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4837 | sp<IBinder> token = commandEntry->inputChannel != nullptr | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4838 | ? commandEntry->inputChannel->getConnectionToken() | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4839 | : nullptr; | 
|  | 4840 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4841 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4842 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4843 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4844 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4845 |  | 
|  | 4846 | mLock.lock(); | 
|  | 4847 |  | 
|  | 4848 | if (delay < 0) { | 
|  | 4849 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
|  | 4850 | } else if (!delay) { | 
|  | 4851 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 4852 | } else { | 
|  | 4853 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
|  | 4854 | entry->interceptKeyWakeupTime = now() + delay; | 
|  | 4855 | } | 
|  | 4856 | entry->release(); | 
|  | 4857 | } | 
|  | 4858 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4859 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 4860 | mLock.unlock(); | 
|  | 4861 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); | 
|  | 4862 | mLock.lock(); | 
|  | 4863 | } | 
|  | 4864 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4865 | /** | 
|  | 4866 | * Connection is responsive if it has no events in the waitQueue that are older than the | 
|  | 4867 | * current time. | 
|  | 4868 | */ | 
|  | 4869 | static bool isConnectionResponsive(const Connection& connection) { | 
|  | 4870 | const nsecs_t currentTime = now(); | 
|  | 4871 | for (const DispatchEntry* entry : connection.waitQueue) { | 
|  | 4872 | if (entry->timeoutTime < currentTime) { | 
|  | 4873 | return false; | 
|  | 4874 | } | 
|  | 4875 | } | 
|  | 4876 | return true; | 
|  | 4877 | } | 
|  | 4878 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4879 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4880 | sp<Connection> connection = commandEntry->connection; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4881 | const nsecs_t finishTime = commandEntry->eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4882 | uint32_t seq = commandEntry->seq; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4883 | const bool handled = commandEntry->handled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4884 |  | 
|  | 4885 | // Handle post-event policy actions. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4886 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4887 | if (dispatchEntryIt == connection->waitQueue.end()) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4888 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4889 | } | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4890 | DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4891 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4892 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4893 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
|  | 4894 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4895 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4896 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4897 |  | 
|  | 4898 | bool restartEvent; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4899 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4900 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); | 
|  | 4901 | restartEvent = | 
|  | 4902 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4903 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4904 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); | 
|  | 4905 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, | 
|  | 4906 | handled); | 
|  | 4907 | } else { | 
|  | 4908 | restartEvent = false; | 
|  | 4909 | } | 
|  | 4910 |  | 
|  | 4911 | // Dequeue the event and start the next cycle. | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4912 | // Because the lock might have been released, it is possible that the | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4913 | // contents of the wait queue to have been drained, so we need to double-check | 
|  | 4914 | // a few things. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4915 | dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 4916 | if (dispatchEntryIt != connection->waitQueue.end()) { | 
|  | 4917 | dispatchEntry = *dispatchEntryIt; | 
|  | 4918 | connection->waitQueue.erase(dispatchEntryIt); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4919 | mAnrTracker.erase(dispatchEntry->timeoutTime, | 
|  | 4920 | connection->inputChannel->getConnectionToken()); | 
|  | 4921 | if (!connection->responsive) { | 
|  | 4922 | connection->responsive = isConnectionResponsive(*connection); | 
|  | 4923 | } | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4924 | traceWaitQueueLength(connection); | 
|  | 4925 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4926 | connection->outboundQueue.push_front(dispatchEntry); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4927 | traceOutboundQueueLength(connection); | 
|  | 4928 | } else { | 
|  | 4929 | releaseDispatchEntry(dispatchEntry); | 
|  | 4930 | } | 
|  | 4931 | } | 
|  | 4932 |  | 
|  | 4933 | // Start the next dispatch cycle for this connection. | 
|  | 4934 | startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | } | 
|  | 4936 |  | 
|  | 4937 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4938 | DispatchEntry* dispatchEntry, | 
|  | 4939 | KeyEntry* keyEntry, bool handled) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4940 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4941 | if (!handled) { | 
|  | 4942 | // Report the key as unhandled, since the fallback was not handled. | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4943 | mReporter->reportUnhandledKey(keyEntry->id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4944 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4945 | return false; | 
|  | 4946 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4947 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4948 | // Get the fallback key state. | 
|  | 4949 | // Clear it out after dispatching the UP. | 
|  | 4950 | int32_t originalKeyCode = keyEntry->keyCode; | 
|  | 4951 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
|  | 4952 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { | 
|  | 4953 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 4954 | } | 
|  | 4955 |  | 
|  | 4956 | if (handled || !dispatchEntry->hasForegroundTarget()) { | 
|  | 4957 | // If the application handles the original key for which we previously | 
|  | 4958 | // generated a fallback or if the window is not a foreground window, | 
|  | 4959 | // then cancel the associated fallback key, if any. | 
|  | 4960 | if (fallbackKeyCode != -1) { | 
|  | 4961 | // Dispatch the unhandled key to the policy with the cancel flag. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4962 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4963 | ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4964 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 4965 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, | 
|  | 4966 | keyEntry->policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4967 | #endif | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4968 | KeyEvent event = createKeyEvent(*keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4969 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4970 |  | 
|  | 4971 | mLock.unlock(); | 
|  | 4972 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4973 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4974 | keyEntry->policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4975 |  | 
|  | 4976 | mLock.lock(); | 
|  | 4977 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4978 | // Cancel the fallback key. | 
|  | 4979 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4980 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4981 | "application handled the original non-fallback key " | 
|  | 4982 | "or is no longer a foreground target, " | 
|  | 4983 | "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4984 | options.keyCode = fallbackKeyCode; | 
|  | 4985 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4986 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4987 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 4988 | } | 
|  | 4989 | } else { | 
|  | 4990 | // If the application did not handle a non-fallback key, first check | 
|  | 4991 | // that we are in a good state to perform unhandled key event processing | 
|  | 4992 | // Then ask the policy what to do with it. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4993 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4994 | if (fallbackKeyCode == -1 && !initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4995 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4996 | ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4997 | "since this is not an initial down.  " | 
|  | 4998 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 4999 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5000 | #endif | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5001 | return false; | 
|  | 5002 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5003 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5004 | // Dispatch the unhandled key to the policy. | 
|  | 5005 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5006 | ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5007 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 5008 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5009 | #endif | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5010 | KeyEvent event = createKeyEvent(*keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5011 |  | 
|  | 5012 | mLock.unlock(); | 
|  | 5013 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5014 | bool fallback = | 
|  | 5015 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
|  | 5016 | &event, keyEntry->policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5017 |  | 
|  | 5018 | mLock.lock(); | 
|  | 5019 |  | 
|  | 5020 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 5021 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5022 | return false; | 
|  | 5023 | } | 
|  | 5024 |  | 
|  | 5025 | // Latch the fallback keycode for this key on an initial down. | 
|  | 5026 | // The fallback keycode cannot change at any other point in the lifecycle. | 
|  | 5027 | if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5028 | if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5029 | fallbackKeyCode = event.getKeyCode(); | 
|  | 5030 | } else { | 
|  | 5031 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 5032 | } | 
|  | 5033 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
|  | 5034 | } | 
|  | 5035 |  | 
|  | 5036 | ALOG_ASSERT(fallbackKeyCode != -1); | 
|  | 5037 |  | 
|  | 5038 | // Cancel the fallback key if the policy decides not to send it anymore. | 
|  | 5039 | // We will continue to dispatch the key to the policy but we will no | 
|  | 5040 | // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5041 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
|  | 5042 | (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5043 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5044 | if (fallback) { | 
|  | 5045 | ALOGD("Unhandled key event: Policy requested to send key %d" | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5046 | "as a fallback for %d, but on the DOWN it had requested " | 
|  | 5047 | "to send %d instead.  Fallback canceled.", | 
|  | 5048 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5049 | } else { | 
|  | 5050 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5051 | "but on the DOWN it had requested to send %d.  " | 
|  | 5052 | "Fallback canceled.", | 
|  | 5053 | originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5054 | } | 
|  | 5055 | #endif | 
|  | 5056 |  | 
|  | 5057 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
|  | 5058 | "canceling fallback, policy no longer desires it"); | 
|  | 5059 | options.keyCode = fallbackKeyCode; | 
|  | 5060 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
|  | 5061 |  | 
|  | 5062 | fallback = false; | 
|  | 5063 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 5064 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5065 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5066 | } | 
|  | 5067 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 |  | 
|  | 5069 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5070 | { | 
|  | 5071 | std::string msg; | 
|  | 5072 | const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
|  | 5073 | connection->inputState.getFallbackKeys(); | 
|  | 5074 | for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5075 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5076 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5077 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5078 | fallbackKeys.size(), msg.c_str()); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5079 | } | 
|  | 5080 | #endif | 
|  | 5081 |  | 
|  | 5082 | if (fallback) { | 
|  | 5083 | // Restart the dispatch cycle using the fallback key. | 
|  | 5084 | keyEntry->eventTime = event.getEventTime(); | 
|  | 5085 | keyEntry->deviceId = event.getDeviceId(); | 
|  | 5086 | keyEntry->source = event.getSource(); | 
|  | 5087 | keyEntry->displayId = event.getDisplayId(); | 
|  | 5088 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
|  | 5089 | keyEntry->keyCode = fallbackKeyCode; | 
|  | 5090 | keyEntry->scanCode = event.getScanCode(); | 
|  | 5091 | keyEntry->metaState = event.getMetaState(); | 
|  | 5092 | keyEntry->repeatCount = event.getRepeatCount(); | 
|  | 5093 | keyEntry->downTime = event.getDownTime(); | 
|  | 5094 | keyEntry->syntheticRepeat = false; | 
|  | 5095 |  | 
|  | 5096 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5097 | ALOGD("Unhandled key event: Dispatching fallback key.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5098 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
|  | 5099 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5100 | #endif | 
|  | 5101 | return true; // restart the event | 
|  | 5102 | } else { | 
|  | 5103 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5104 | ALOGD("Unhandled key event: No fallback key."); | 
|  | 5105 | #endif | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5106 |  | 
|  | 5107 | // Report the key as unhandled, since there is no fallback key. | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5108 | mReporter->reportUnhandledKey(keyEntry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5109 | } | 
|  | 5110 | } | 
|  | 5111 | return false; | 
|  | 5112 | } | 
|  | 5113 |  | 
|  | 5114 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5115 | DispatchEntry* dispatchEntry, | 
|  | 5116 | MotionEntry* motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | return false; | 
|  | 5118 | } | 
|  | 5119 |  | 
|  | 5120 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5121 | mLock.unlock(); | 
|  | 5122 |  | 
|  | 5123 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); | 
|  | 5124 |  | 
|  | 5125 | mLock.lock(); | 
|  | 5126 | } | 
|  | 5127 |  | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5128 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { | 
|  | 5129 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5130 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5131 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 5132 | entry.repeatCount, entry.downTime, entry.eventTime); | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5133 | return event; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5134 | } | 
|  | 5135 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5136 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, | 
|  | 5137 | const Connection& connection, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5138 | // TODO Write some statistics about how long we spend waiting. | 
|  | 5139 | } | 
|  | 5140 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5141 | /** | 
|  | 5142 | * Report the touch event latency to the statsd server. | 
|  | 5143 | * Input events are reported for statistics if: | 
|  | 5144 | * - This is a touchscreen event | 
|  | 5145 | * - InputFilter is not enabled | 
|  | 5146 | * - Event is not injected or synthesized | 
|  | 5147 | * | 
|  | 5148 | * Statistics should be reported before calling addValue, to prevent a fresh new sample | 
|  | 5149 | * from getting aggregated with the "old" data. | 
|  | 5150 | */ | 
|  | 5151 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) | 
|  | 5152 | REQUIRES(mLock) { | 
|  | 5153 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && | 
|  | 5154 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; | 
|  | 5155 | if (!reportForStatistics) { | 
|  | 5156 | return; | 
|  | 5157 | } | 
|  | 5158 |  | 
|  | 5159 | if (mTouchStatistics.shouldReport()) { | 
|  | 5160 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), | 
|  | 5161 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), | 
|  | 5162 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); | 
|  | 5163 | mTouchStatistics.reset(); | 
|  | 5164 | } | 
|  | 5165 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); | 
|  | 5166 | mTouchStatistics.addValue(latencyMicros); | 
|  | 5167 | } | 
|  | 5168 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
|  | 5170 | if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5171 | ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | } | 
|  | 5173 | } | 
|  | 5174 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5175 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | if (ATRACE_ENABLED()) { | 
|  | 5177 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5178 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5179 | ATRACE_INT(counterName, connection->outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5180 | } | 
|  | 5181 | } | 
|  | 5182 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5183 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5184 | if (ATRACE_ENABLED()) { | 
|  | 5185 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5186 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5187 | ATRACE_INT(counterName, connection->waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5188 | } | 
|  | 5189 | } | 
|  | 5190 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5191 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5192 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5193 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5194 | dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5195 | dumpDispatchStateLocked(dump); | 
|  | 5196 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5197 | if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5198 | dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5199 | dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | } | 
|  | 5201 | } | 
|  | 5202 |  | 
|  | 5203 | void InputDispatcher::monitor() { | 
|  | 5204 | // 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] | 5205 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5206 | mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5207 | mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5208 | } | 
|  | 5209 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5210 | /** | 
|  | 5211 | * Wake up the dispatcher and wait until it processes all events and commands. | 
|  | 5212 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
|  | 5213 | * this method can be safely called from any thread, as long as you've ensured that | 
|  | 5214 | * the work you are interested in completing has already been queued. | 
|  | 5215 | */ | 
|  | 5216 | bool InputDispatcher::waitForIdle() { | 
|  | 5217 | /** | 
|  | 5218 | * Timeout should represent the longest possible time that a device might spend processing | 
|  | 5219 | * events and commands. | 
|  | 5220 | */ | 
|  | 5221 | constexpr std::chrono::duration TIMEOUT = 100ms; | 
|  | 5222 | std::unique_lock lock(mLock); | 
|  | 5223 | mLooper->wake(); | 
|  | 5224 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
|  | 5225 | return result == std::cv_status::no_timeout; | 
|  | 5226 | } | 
|  | 5227 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5228 | /** | 
|  | 5229 | * Sets focus to the window identified by the token. This must be called | 
|  | 5230 | * after updating any input window handles. | 
|  | 5231 | * | 
|  | 5232 | * Params: | 
|  | 5233 | *  request.token - input channel token used to identify the window that should gain focus. | 
|  | 5234 | *  request.focusedToken - the token that the caller expects currently to be focused. If the | 
|  | 5235 | *  specified token does not match the currently focused window, this request will be dropped. | 
|  | 5236 | *  If the specified focused token matches the currently focused window, the call will succeed. | 
|  | 5237 | *  Set this to "null" if this call should succeed no matter what the currently focused token is. | 
|  | 5238 | *  request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) | 
|  | 5239 | *  when requesting the focus change. This determines which request gets | 
|  | 5240 | *  precedence if there is a focus change request from another source such as pointer down. | 
|  | 5241 | */ | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5242 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { | 
|  | 5243 | { // acquire lock | 
|  | 5244 | std::scoped_lock _l(mLock); | 
|  | 5245 |  | 
|  | 5246 | const int32_t displayId = request.displayId; | 
|  | 5247 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 5248 | if (request.focusedToken && oldFocusedToken != request.focusedToken) { | 
|  | 5249 | ALOGD_IF(DEBUG_FOCUS, | 
|  | 5250 | "setFocusedWindow on display %" PRId32 | 
|  | 5251 | " ignored, reason: focusedToken is not focused", | 
|  | 5252 | displayId); | 
|  | 5253 | return; | 
|  | 5254 | } | 
|  | 5255 |  | 
|  | 5256 | mPendingFocusRequests.erase(displayId); | 
|  | 5257 | FocusResult result = handleFocusRequestLocked(request); | 
|  | 5258 | if (result == FocusResult::NOT_VISIBLE) { | 
|  | 5259 | // The requested window is not currently visible. Wait for the window to become visible | 
|  | 5260 | // and then provide it focus. This is to handle situations where a user action triggers | 
|  | 5261 | // a new window to appear. We want to be able to queue any key events after the user | 
|  | 5262 | // action and deliver it to the newly focused window. In order for this to happen, we | 
|  | 5263 | // take focus from the currently focused window so key events can be queued. | 
|  | 5264 | ALOGD_IF(DEBUG_FOCUS, | 
|  | 5265 | "setFocusedWindow on display %" PRId32 | 
|  | 5266 | " pending, reason: window is not visible", | 
|  | 5267 | displayId); | 
|  | 5268 | mPendingFocusRequests[displayId] = request; | 
|  | 5269 | onFocusChangedLocked(oldFocusedToken, nullptr, displayId, | 
|  | 5270 | "setFocusedWindow_AwaitingWindowVisibility"); | 
|  | 5271 | } else if (result != FocusResult::OK) { | 
|  | 5272 | ALOGW("setFocusedWindow on display %" PRId32 " ignored, reason:%s", displayId, | 
|  | 5273 | typeToString(result)); | 
|  | 5274 | } | 
|  | 5275 | } // release lock | 
|  | 5276 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 5277 | mLooper->wake(); | 
|  | 5278 | } | 
|  | 5279 |  | 
|  | 5280 | InputDispatcher::FocusResult InputDispatcher::handleFocusRequestLocked( | 
|  | 5281 | const FocusRequest& request) { | 
|  | 5282 | const int32_t displayId = request.displayId; | 
|  | 5283 | const sp<IBinder> newFocusedToken = request.token; | 
|  | 5284 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 5285 |  | 
|  | 5286 | if (oldFocusedToken == request.token) { | 
|  | 5287 | ALOGD_IF(DEBUG_FOCUS, | 
|  | 5288 | "setFocusedWindow on display %" PRId32 " ignored, reason: already focused", | 
|  | 5289 | displayId); | 
|  | 5290 | return FocusResult::OK; | 
|  | 5291 | } | 
|  | 5292 |  | 
|  | 5293 | FocusResult result = checkTokenFocusableLocked(newFocusedToken, displayId); | 
|  | 5294 | if (result != FocusResult::OK) { | 
|  | 5295 | return result; | 
|  | 5296 | } | 
|  | 5297 |  | 
|  | 5298 | std::string_view reason = | 
|  | 5299 | (request.focusedToken) ? "setFocusedWindow_FocusCheck" : "setFocusedWindow"; | 
|  | 5300 | onFocusChangedLocked(oldFocusedToken, newFocusedToken, displayId, reason); | 
|  | 5301 | return FocusResult::OK; | 
|  | 5302 | } | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5303 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5304 | void InputDispatcher::onFocusChangedLocked(const sp<IBinder>& oldFocusedToken, | 
|  | 5305 | const sp<IBinder>& newFocusedToken, int32_t displayId, | 
|  | 5306 | std::string_view reason) { | 
|  | 5307 | if (oldFocusedToken) { | 
|  | 5308 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(oldFocusedToken); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5309 | if (focusedInputChannel) { | 
|  | 5310 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 5311 | "focus left window"); | 
|  | 5312 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5313 | enqueueFocusEventLocked(oldFocusedToken, false /*hasFocus*/, reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5314 | } | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5315 | mFocusedWindowTokenByDisplay.erase(displayId); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5316 | } | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5317 | if (newFocusedToken) { | 
|  | 5318 | mFocusedWindowTokenByDisplay[displayId] = newFocusedToken; | 
|  | 5319 | enqueueFocusEventLocked(newFocusedToken, true /*hasFocus*/, reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5320 | } | 
|  | 5321 |  | 
|  | 5322 | if (mFocusedDisplayId == displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5323 | notifyFocusChangedLocked(oldFocusedToken, newFocusedToken); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5324 | } | 
|  | 5325 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5326 |  | 
|  | 5327 | /** | 
|  | 5328 | * Checks if the window token can be focused on a display. The token can be focused if there is | 
|  | 5329 | * at least one window handle that is visible with the same token and all window handles with the | 
|  | 5330 | * same token are focusable. | 
|  | 5331 | * | 
|  | 5332 | * In the case of mirroring, two windows may share the same window token and their visibility | 
|  | 5333 | * might be different. Example, the mirrored window can cover the window its mirroring. However, | 
|  | 5334 | * we expect the focusability of the windows to match since its hard to reason why one window can | 
|  | 5335 | * receive focus events and the other cannot when both are backed by the same input channel. | 
|  | 5336 | */ | 
|  | 5337 | InputDispatcher::FocusResult InputDispatcher::checkTokenFocusableLocked(const sp<IBinder>& token, | 
|  | 5338 | int32_t displayId) const { | 
|  | 5339 | bool allWindowsAreFocusable = true; | 
|  | 5340 | bool visibleWindowFound = false; | 
|  | 5341 | bool windowFound = false; | 
|  | 5342 | for (const sp<InputWindowHandle>& window : getWindowHandlesLocked(displayId)) { | 
|  | 5343 | if (window->getToken() != token) { | 
|  | 5344 | continue; | 
|  | 5345 | } | 
|  | 5346 | windowFound = true; | 
|  | 5347 | if (window->getInfo()->visible) { | 
|  | 5348 | // Check if at least a single window is visible. | 
|  | 5349 | visibleWindowFound = true; | 
|  | 5350 | } | 
|  | 5351 | if (!window->getInfo()->focusable) { | 
|  | 5352 | // Check if all windows with the window token are focusable. | 
|  | 5353 | allWindowsAreFocusable = false; | 
|  | 5354 | break; | 
|  | 5355 | } | 
|  | 5356 | } | 
|  | 5357 |  | 
|  | 5358 | if (!windowFound) { | 
|  | 5359 | return FocusResult::NO_WINDOW; | 
|  | 5360 | } | 
|  | 5361 | if (!allWindowsAreFocusable) { | 
|  | 5362 | return FocusResult::NOT_FOCUSABLE; | 
|  | 5363 | } | 
|  | 5364 | if (!visibleWindowFound) { | 
|  | 5365 | return FocusResult::NOT_VISIBLE; | 
|  | 5366 | } | 
|  | 5367 |  | 
|  | 5368 | return FocusResult::OK; | 
|  | 5369 | } | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5370 | } // namespace android::inputdispatcher |