| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #define LOG_TAG "InputDispatcher" | 
|  | 18 | #define ATRACE_TAG ATRACE_TAG_INPUT | 
|  | 19 |  | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 |  | 
|  | 22 | // Log detailed debug messages about each inbound event notification to the dispatcher. | 
|  | 23 | #define DEBUG_INBOUND_EVENT_DETAILS 0 | 
|  | 24 |  | 
|  | 25 | // Log detailed debug messages about each outbound event processed by the dispatcher. | 
|  | 26 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 | 
|  | 27 |  | 
|  | 28 | // Log debug messages about the dispatch cycle. | 
|  | 29 | #define DEBUG_DISPATCH_CYCLE 0 | 
|  | 30 |  | 
|  | 31 | // Log debug messages about registrations. | 
|  | 32 | #define DEBUG_REGISTRATION 0 | 
|  | 33 |  | 
|  | 34 | // Log debug messages about input event injection. | 
|  | 35 | #define DEBUG_INJECTION 0 | 
|  | 36 |  | 
|  | 37 | // Log debug messages about input focus tracking. | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 38 | static constexpr bool DEBUG_FOCUS = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 |  | 
|  | 40 | // Log debug messages about the app switch latency optimization. | 
|  | 41 | #define DEBUG_APP_SWITCH 0 | 
|  | 42 |  | 
|  | 43 | // Log debug messages about hover events. | 
|  | 44 | #define DEBUG_HOVER 0 | 
|  | 45 |  | 
|  | 46 | #include "InputDispatcher.h" | 
|  | 47 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 48 | #include "Connection.h" | 
|  | 49 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | #include <errno.h> | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 51 | #include <inttypes.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | #include <limits.h> | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 53 | #include <statslog.h> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 54 | #include <stddef.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | #include <time.h> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 56 | #include <unistd.h> | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 57 | #include <queue> | 
|  | 58 | #include <sstream> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 59 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 60 | #include <android-base/chrono_utils.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 61 | #include <android-base/stringprintf.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 62 | #include <binder/Binder.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 63 | #include <input/InputDevice.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 64 | #include <log/log.h> | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 65 | #include <openssl/hmac.h> | 
|  | 66 | #include <openssl/rand.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 67 | #include <powermanager/PowerManager.h> | 
|  | 68 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 |  | 
|  | 70 | #define INDENT "  " | 
|  | 71 | #define INDENT2 "    " | 
|  | 72 | #define INDENT3 "      " | 
|  | 73 | #define INDENT4 "        " | 
|  | 74 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 75 | using android::base::StringPrintf; | 
|  | 76 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 77 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 80 | // from which to determine an appropriate dispatching timeout. | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 81 | constexpr std::chrono::nanoseconds DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5s; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | // Amount of time to allow for all pending events to be processed when an app switch | 
|  | 84 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
|  | 85 | // 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] | 86 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 |  | 
|  | 88 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) | 
|  | 89 | // before considering it stale and dropping it. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 90 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | // 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] | 93 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
|  | 94 |  | 
|  | 95 | // Log a warning when an interception call takes longer than this to process. | 
|  | 96 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 98 | // Additional key latency in case a connection is still processing some motion events. | 
|  | 99 | // This will help with the case when a user touched a button that opens a new window, | 
|  | 100 | // and gives us the chance to dispatch the key to this new window. | 
|  | 101 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
|  | 102 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 104 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
|  | 105 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 106 | static inline nsecs_t now() { | 
|  | 107 | return systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | static inline const char* toString(bool value) { | 
|  | 111 | return value ? "true" : "false"; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 115 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
|  | 116 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | static bool isValidKeyAction(int32_t action) { | 
|  | 120 | switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 121 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 122 | case AKEY_EVENT_ACTION_UP: | 
|  | 123 | return true; | 
|  | 124 | default: | 
|  | 125 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | } | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | static bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 130 | if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | ALOGE("Key event has invalid action code 0x%x", action); | 
|  | 132 | return false; | 
|  | 133 | } | 
|  | 134 | return true; | 
|  | 135 | } | 
|  | 136 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 137 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 138 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 139 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 140 | case AMOTION_EVENT_ACTION_UP: | 
|  | 141 | case AMOTION_EVENT_ACTION_CANCEL: | 
|  | 142 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 143 | case AMOTION_EVENT_ACTION_OUTSIDE: | 
|  | 144 | case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
|  | 145 | case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
|  | 146 | case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
|  | 147 | case AMOTION_EVENT_ACTION_SCROLL: | 
|  | 148 | return true; | 
|  | 149 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 150 | case AMOTION_EVENT_ACTION_POINTER_UP: { | 
|  | 151 | int32_t index = getMotionEventActionPointerIndex(action); | 
|  | 152 | return index >= 0 && index < pointerCount; | 
|  | 153 | } | 
|  | 154 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
|  | 155 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
|  | 156 | return actionButton != 0; | 
|  | 157 | default: | 
|  | 158 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 159 | } | 
|  | 160 | } | 
|  | 161 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 162 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 163 | const PointerProperties* pointerProperties) { | 
|  | 164 | if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | ALOGE("Motion event has invalid action code 0x%x", action); | 
|  | 166 | return false; | 
|  | 167 | } | 
|  | 168 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 169 | 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] | 170 | pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | return false; | 
|  | 172 | } | 
|  | 173 | BitSet32 pointerIdBits; | 
|  | 174 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 175 | int32_t id = pointerProperties[i].id; | 
|  | 176 | if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 177 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
|  | 178 | MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 179 | return false; | 
|  | 180 | } | 
|  | 181 | if (pointerIdBits.hasBit(id)) { | 
|  | 182 | ALOGE("Motion event has duplicate pointer id %d", id); | 
|  | 183 | return false; | 
|  | 184 | } | 
|  | 185 | pointerIdBits.markBit(id); | 
|  | 186 | } | 
|  | 187 | return true; | 
|  | 188 | } | 
|  | 189 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 190 | static void dumpRegion(std::string& dump, const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | if (region.isEmpty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 192 | dump += "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | return; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | bool first = true; | 
|  | 197 | Region::const_iterator cur = region.begin(); | 
|  | 198 | Region::const_iterator const tail = region.end(); | 
|  | 199 | while (cur != tail) { | 
|  | 200 | if (first) { | 
|  | 201 | first = false; | 
|  | 202 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 203 | dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 205 | 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] | 206 | cur++; | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 210 | /** | 
|  | 211 | * Find the entry in std::unordered_map by key, and return it. | 
|  | 212 | * If the entry is not found, return a default constructed entry. | 
|  | 213 | * | 
|  | 214 | * Useful when the entries are vectors, since an empty vector will be returned | 
|  | 215 | * if the entry is not found. | 
|  | 216 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
|  | 217 | */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 218 | template <typename K, typename V> | 
|  | 219 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 220 | auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 221 | return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 224 | /** | 
|  | 225 | * Find the entry in std::unordered_map by value, and remove it. | 
|  | 226 | * If more than one entry has the same value, then all matching | 
|  | 227 | * key-value pairs will be removed. | 
|  | 228 | * | 
|  | 229 | * Return true if at least one value has been removed. | 
|  | 230 | */ | 
|  | 231 | template <typename K, typename V> | 
|  | 232 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { | 
|  | 233 | bool removed = false; | 
|  | 234 | for (auto it = map.begin(); it != map.end();) { | 
|  | 235 | if (it->second == value) { | 
|  | 236 | it = map.erase(it); | 
|  | 237 | removed = true; | 
|  | 238 | } else { | 
|  | 239 | it++; | 
|  | 240 | } | 
|  | 241 | } | 
|  | 242 | return removed; | 
|  | 243 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 245 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { | 
|  | 246 | if (first == second) { | 
|  | 247 | return true; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | if (first == nullptr || second == nullptr) { | 
|  | 251 | return false; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | return first->getToken() == second->getToken(); | 
|  | 255 | } | 
|  | 256 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 257 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
|  | 258 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; | 
|  | 259 | } | 
|  | 260 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 261 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
|  | 262 | EventEntry* eventEntry, | 
|  | 263 | int32_t inputTargetFlags) { | 
|  | 264 | if (inputTarget.useDefaultPointerInfo()) { | 
|  | 265 | const PointerInfo& pointerInfo = inputTarget.getDefaultPointerInfo(); | 
|  | 266 | return std::make_unique<DispatchEntry>(eventEntry, // increments ref | 
|  | 267 | inputTargetFlags, pointerInfo.xOffset, | 
|  | 268 | pointerInfo.yOffset, inputTarget.globalScaleFactor, | 
|  | 269 | pointerInfo.windowXScale, pointerInfo.windowYScale); | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
|  | 273 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
|  | 274 |  | 
|  | 275 | PointerCoords pointerCoords[motionEntry.pointerCount]; | 
|  | 276 |  | 
|  | 277 | // Use the first pointer information to normalize all other pointers. This could be any pointer | 
|  | 278 | // as long as all other pointers are normalized to the same value and the final DispatchEntry | 
|  | 279 | // uses the offset and scale for the normalized pointer. | 
|  | 280 | const PointerInfo& firstPointerInfo = | 
|  | 281 | inputTarget.pointerInfos[inputTarget.pointerIds.firstMarkedBit()]; | 
|  | 282 |  | 
|  | 283 | // Iterate through all pointers in the event to normalize against the first. | 
|  | 284 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
|  | 285 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
|  | 286 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 287 | const PointerInfo& currPointerInfo = inputTarget.pointerInfos[pointerId]; | 
|  | 288 |  | 
|  | 289 | // The scale factor is the ratio of the current pointers scale to the normalized scale. | 
|  | 290 | float scaleXDiff = currPointerInfo.windowXScale / firstPointerInfo.windowXScale; | 
|  | 291 | float scaleYDiff = currPointerInfo.windowYScale / firstPointerInfo.windowYScale; | 
|  | 292 |  | 
|  | 293 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
|  | 294 | // First apply the current pointers offset to set the window at 0,0 | 
|  | 295 | pointerCoords[pointerIndex].applyOffset(currPointerInfo.xOffset, currPointerInfo.yOffset); | 
|  | 296 | // Next scale the coordinates. | 
|  | 297 | pointerCoords[pointerIndex].scale(1, scaleXDiff, scaleYDiff); | 
|  | 298 | // Lastly, offset the coordinates so they're in the normalized pointer's frame. | 
|  | 299 | pointerCoords[pointerIndex].applyOffset(-firstPointerInfo.xOffset, | 
|  | 300 | -firstPointerInfo.yOffset); | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | MotionEntry* combinedMotionEntry = | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 304 | new MotionEntry(motionEntry.id, motionEntry.eventTime, motionEntry.deviceId, | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 305 | motionEntry.source, motionEntry.displayId, motionEntry.policyFlags, | 
|  | 306 | motionEntry.action, motionEntry.actionButton, motionEntry.flags, | 
|  | 307 | motionEntry.metaState, motionEntry.buttonState, | 
|  | 308 | motionEntry.classification, motionEntry.edgeFlags, | 
|  | 309 | motionEntry.xPrecision, motionEntry.yPrecision, | 
|  | 310 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, | 
|  | 311 | motionEntry.downTime, motionEntry.pointerCount, | 
|  | 312 | motionEntry.pointerProperties, pointerCoords, 0 /* xOffset */, | 
|  | 313 | 0 /* yOffset */); | 
|  | 314 |  | 
|  | 315 | if (motionEntry.injectionState) { | 
|  | 316 | combinedMotionEntry->injectionState = motionEntry.injectionState; | 
|  | 317 | combinedMotionEntry->injectionState->refCount += 1; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
|  | 321 | std::make_unique<DispatchEntry>(combinedMotionEntry, // increments ref | 
|  | 322 | inputTargetFlags, firstPointerInfo.xOffset, | 
|  | 323 | firstPointerInfo.yOffset, inputTarget.globalScaleFactor, | 
|  | 324 | firstPointerInfo.windowXScale, | 
|  | 325 | firstPointerInfo.windowYScale); | 
|  | 326 | combinedMotionEntry->release(); | 
|  | 327 | return dispatchEntry; | 
|  | 328 | } | 
|  | 329 |  | 
| Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 330 | static void addGestureMonitors(const std::vector<Monitor>& monitors, | 
|  | 331 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, | 
|  | 332 | float yOffset = 0) { | 
|  | 333 | if (monitors.empty()) { | 
|  | 334 | return; | 
|  | 335 | } | 
|  | 336 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); | 
|  | 337 | for (const Monitor& monitor : monitors) { | 
|  | 338 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); | 
|  | 339 | } | 
|  | 340 | } | 
|  | 341 |  | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 342 | static std::array<uint8_t, 128> getRandomKey() { | 
|  | 343 | std::array<uint8_t, 128> key; | 
|  | 344 | if (RAND_bytes(key.data(), key.size()) != 1) { | 
|  | 345 | LOG_ALWAYS_FATAL("Can't generate HMAC key"); | 
|  | 346 | } | 
|  | 347 | return key; | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | // --- HmacKeyManager --- | 
|  | 351 |  | 
|  | 352 | HmacKeyManager::HmacKeyManager() : mHmacKey(getRandomKey()) {} | 
|  | 353 |  | 
|  | 354 | std::array<uint8_t, 32> HmacKeyManager::sign(const VerifiedInputEvent& event) const { | 
|  | 355 | size_t size; | 
|  | 356 | switch (event.type) { | 
|  | 357 | case VerifiedInputEvent::Type::KEY: { | 
|  | 358 | size = sizeof(VerifiedKeyEvent); | 
|  | 359 | break; | 
|  | 360 | } | 
|  | 361 | case VerifiedInputEvent::Type::MOTION: { | 
|  | 362 | size = sizeof(VerifiedMotionEvent); | 
|  | 363 | break; | 
|  | 364 | } | 
|  | 365 | } | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 366 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
| Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 367 | return sign(start, size); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 368 | } | 
|  | 369 |  | 
| Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 370 | std::array<uint8_t, 32> HmacKeyManager::sign(const uint8_t* data, size_t size) const { | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 371 | // SHA256 always generates 32-bytes result | 
|  | 372 | std::array<uint8_t, 32> hash; | 
|  | 373 | unsigned int hashLen = 0; | 
| Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 374 | uint8_t* result = | 
|  | 375 | HMAC(EVP_sha256(), mHmacKey.data(), mHmacKey.size(), data, size, hash.data(), &hashLen); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 376 | if (result == nullptr) { | 
|  | 377 | ALOGE("Could not sign the data using HMAC"); | 
|  | 378 | return INVALID_HMAC; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | if (hashLen != hash.size()) { | 
|  | 382 | ALOGE("HMAC-SHA256 has unexpected length"); | 
|  | 383 | return INVALID_HMAC; | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | return hash; | 
|  | 387 | } | 
|  | 388 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 389 | // --- InputDispatcher --- | 
|  | 390 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 391 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
|  | 392 | : mPolicy(policy), | 
|  | 393 | mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 394 | mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 395 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 396 | mAppSwitchSawKeyDown(false), | 
|  | 397 | mAppSwitchDueTime(LONG_LONG_MAX), | 
|  | 398 | mNextUnblockedEvent(nullptr), | 
|  | 399 | mDispatchEnabled(false), | 
|  | 400 | mDispatchFrozen(false), | 
|  | 401 | mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 402 | // mInTouchMode will be initialized by the WindowManager to the default device config. | 
|  | 403 | // To avoid leaking stack in case that call never comes, and for tests, | 
|  | 404 | // initialize it here anyways. | 
|  | 405 | mInTouchMode(true), | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 406 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 407 | mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 408 | mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 409 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 410 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 411 |  | 
|  | 412 | policy->getDispatcherConfiguration(&mConfig); | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | InputDispatcher::~InputDispatcher() { | 
|  | 416 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 417 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 418 |  | 
|  | 419 | resetKeyRepeatLocked(); | 
|  | 420 | releasePendingEventLocked(); | 
|  | 421 | drainInboundQueueLocked(); | 
|  | 422 | } | 
|  | 423 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 424 | while (!mConnectionsByFd.empty()) { | 
|  | 425 | sp<Connection> connection = mConnectionsByFd.begin()->second; | 
|  | 426 | unregisterInputChannel(connection->inputChannel); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 427 | } | 
|  | 428 | } | 
|  | 429 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 430 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 431 | if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 432 | return ALREADY_EXISTS; | 
|  | 433 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 434 | mThread = std::make_unique<InputThread>( | 
|  | 435 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
|  | 436 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
|  | 439 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 440 | if (mThread && mThread->isCallingThread()) { | 
|  | 441 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 442 | return INVALID_OPERATION; | 
|  | 443 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 444 | mThread.reset(); | 
|  | 445 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 448 | void InputDispatcher::dispatchOnce() { | 
|  | 449 | nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
|  | 450 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 451 | std::scoped_lock _l(mLock); | 
|  | 452 | mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 |  | 
|  | 454 | // Run a dispatch loop if there are no pending commands. | 
|  | 455 | // The dispatch loop might enqueue commands to run afterwards. | 
|  | 456 | if (!haveCommandsLocked()) { | 
|  | 457 | dispatchOnceInnerLocked(&nextWakeupTime); | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | // Run all pending commands if there are any. | 
|  | 461 | // If any commands were run then force the next poll to wake up immediately. | 
|  | 462 | if (runCommandsLockedInterruptible()) { | 
|  | 463 | nextWakeupTime = LONG_LONG_MIN; | 
|  | 464 | } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 465 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 466 | // If we are still waiting for ack on some events, | 
|  | 467 | // we might have to wake up earlier to check if an app is anr'ing. | 
|  | 468 | const nsecs_t nextAnrCheck = processAnrsLocked(); | 
|  | 469 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
|  | 470 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 471 | // We are about to enter an infinitely long sleep, because we have no commands or | 
|  | 472 | // pending or queued events | 
|  | 473 | if (nextWakeupTime == LONG_LONG_MAX) { | 
|  | 474 | mDispatcherEnteredIdle.notify_all(); | 
|  | 475 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 476 | } // release lock | 
|  | 477 |  | 
|  | 478 | // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
|  | 479 | nsecs_t currentTime = now(); | 
|  | 480 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
|  | 481 | mLooper->pollOnce(timeoutMillis); | 
|  | 482 | } | 
|  | 483 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 484 | /** | 
| Siarhei Vishniakou | 265ab01 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 485 | * Raise ANR if there is no focused window. | 
|  | 486 | * Before the ANR is raised, do a final state check: | 
|  | 487 | * 1. The currently focused application must be the same one we are waiting for. | 
|  | 488 | * 2. Ensure we still don't have a focused window. | 
|  | 489 | */ | 
|  | 490 | void InputDispatcher::processNoFocusedWindowAnrLocked() { | 
|  | 491 | // Check if the application that we are waiting for is still focused. | 
|  | 492 | sp<InputApplicationHandle> focusedApplication = | 
|  | 493 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); | 
|  | 494 | if (focusedApplication == nullptr || | 
|  | 495 | focusedApplication->getApplicationToken() != | 
|  | 496 | mAwaitedFocusedApplication->getApplicationToken()) { | 
|  | 497 | // Unexpected because we should have reset the ANR timer when focused application changed | 
|  | 498 | ALOGE("Waited for a focused window, but focused application has already changed to %s", | 
|  | 499 | focusedApplication->getName().c_str()); | 
|  | 500 | return; // The focused application has changed. | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | const sp<InputWindowHandle>& focusedWindowHandle = | 
|  | 504 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); | 
|  | 505 | if (focusedWindowHandle != nullptr) { | 
|  | 506 | return; // We now have a focused window. No need for ANR. | 
|  | 507 | } | 
|  | 508 | onAnrLocked(mAwaitedFocusedApplication); | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | /** | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 512 | * Check if any of the connections' wait queues have events that are too old. | 
|  | 513 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
|  | 514 | * Return the time at which we should wake up next. | 
|  | 515 | */ | 
|  | 516 | nsecs_t InputDispatcher::processAnrsLocked() { | 
|  | 517 | const nsecs_t currentTime = now(); | 
|  | 518 | nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
|  | 519 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
|  | 520 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
|  | 521 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
| Siarhei Vishniakou | 265ab01 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 522 | processNoFocusedWindowAnrLocked(); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 523 | mAwaitedFocusedApplication.clear(); | 
| Siarhei Vishniakou | 265ab01 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 524 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 525 | return LONG_LONG_MIN; | 
|  | 526 | } else { | 
|  | 527 | // Keep waiting | 
|  | 528 | const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); | 
|  | 529 | ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); | 
|  | 530 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
|  | 531 | } | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | // Check if any connection ANRs are due | 
|  | 535 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
|  | 536 | if (currentTime < nextAnrCheck) { // most likely scenario | 
|  | 537 | return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
|  | 538 | } | 
|  | 539 |  | 
|  | 540 | // If we reached here, we have an unresponsive connection. | 
|  | 541 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
|  | 542 | if (connection == nullptr) { | 
|  | 543 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
|  | 544 | return nextAnrCheck; | 
|  | 545 | } | 
|  | 546 | connection->responsive = false; | 
|  | 547 | // Stop waking up for this unresponsive connection | 
|  | 548 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 549 | onAnrLocked(*connection); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 550 | return LONG_LONG_MIN; | 
|  | 551 | } | 
|  | 552 |  | 
|  | 553 | nsecs_t InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
|  | 554 | sp<InputWindowHandle> window = getWindowHandleLocked(token); | 
|  | 555 | if (window != nullptr) { | 
|  | 556 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT).count(); | 
|  | 557 | } | 
|  | 558 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT.count(); | 
|  | 559 | } | 
|  | 560 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 561 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
|  | 562 | nsecs_t currentTime = now(); | 
|  | 563 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 564 | // Reset the key repeat timer whenever normal dispatch is suspended while the | 
|  | 565 | // device is in a non-interactive state.  This is to ensure that we abort a key | 
|  | 566 | // repeat if the device is just coming out of sleep. | 
|  | 567 | if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | resetKeyRepeatLocked(); | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
|  | 572 | if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 573 | if (DEBUG_FOCUS) { | 
|  | 574 | ALOGD("Dispatch frozen.  Waiting some more."); | 
|  | 575 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | return; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | // Optimize latency of app switches. | 
|  | 580 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
|  | 581 | // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
|  | 582 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
|  | 583 | if (mAppSwitchDueTime < *nextWakeupTime) { | 
|  | 584 | *nextWakeupTime = mAppSwitchDueTime; | 
|  | 585 | } | 
|  | 586 |  | 
|  | 587 | // Ready to start a new event. | 
|  | 588 | // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 589 | if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 590 | if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | if (isAppSwitchDue) { | 
|  | 592 | // The inbound queue is empty so the app switch key we were waiting | 
|  | 593 | // for will never arrive.  Stop waiting for it. | 
|  | 594 | resetPendingAppSwitchLocked(false); | 
|  | 595 | isAppSwitchDue = false; | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | // Synthesize a key repeat if appropriate. | 
|  | 599 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 600 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
|  | 601 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
|  | 602 | } else { | 
|  | 603 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
|  | 604 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
|  | 605 | } | 
|  | 606 | } | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | // Nothing to do if there is no pending event. | 
|  | 610 | if (!mPendingEvent) { | 
|  | 611 | return; | 
|  | 612 | } | 
|  | 613 | } else { | 
|  | 614 | // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 615 | mPendingEvent = mInboundQueue.front(); | 
|  | 616 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | traceInboundQueueLengthLocked(); | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | // Poke user activity for this event. | 
|  | 621 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 622 | pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | } | 
|  | 625 |  | 
|  | 626 | // Now we have an event to dispatch. | 
|  | 627 | // 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] | 628 | ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 630 | DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 631 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 632 | dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 633 | } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 634 | dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 635 | } | 
|  | 636 |  | 
|  | 637 | if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 638 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | } | 
|  | 640 |  | 
|  | 641 | switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 642 | case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 643 | ConfigurationChangedEntry* typedEntry = | 
|  | 644 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); | 
|  | 645 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 646 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 647 | break; | 
|  | 648 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 649 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 650 | case EventEntry::Type::DEVICE_RESET: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 651 | DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); | 
|  | 652 | done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 653 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 654 | break; | 
|  | 655 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 657 | case EventEntry::Type::FOCUS: { | 
|  | 658 | FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); | 
|  | 659 | dispatchFocusLocked(currentTime, typedEntry); | 
|  | 660 | done = true; | 
|  | 661 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
|  | 662 | break; | 
|  | 663 | } | 
|  | 664 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 665 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 666 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); | 
|  | 667 | if (isAppSwitchDue) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 668 | if (isAppSwitchKeyEvent(*typedEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 669 | resetPendingAppSwitchLocked(true); | 
|  | 670 | isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 671 | } else if (dropReason == DropReason::NOT_DROPPED) { | 
|  | 672 | dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 673 | } | 
|  | 674 | } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 675 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 676 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 677 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 678 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 679 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 680 | } | 
|  | 681 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); | 
|  | 682 | break; | 
|  | 683 | } | 
|  | 684 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 685 | case EventEntry::Type::MOTION: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 686 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 687 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 688 | dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 690 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 691 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 692 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 693 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 694 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 695 | } | 
|  | 696 | done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); | 
|  | 697 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 698 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 699 | } | 
|  | 700 |  | 
|  | 701 | if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 702 | if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 703 | dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 704 | } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 705 | mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 |  | 
|  | 707 | releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 708 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 709 | } | 
|  | 710 | } | 
|  | 711 |  | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 712 | /** | 
|  | 713 | * Return true if the events preceding this incoming motion event should be dropped | 
|  | 714 | * Return false otherwise (the default behaviour) | 
|  | 715 | */ | 
|  | 716 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 717 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 718 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 719 |  | 
|  | 720 | // Optimize case where the current application is unresponsive and the user | 
|  | 721 | // decides to touch a window in a different application. | 
|  | 722 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 723 | // the touch into the other window. | 
|  | 724 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 725 | int32_t displayId = motionEntry.displayId; | 
|  | 726 | int32_t x = static_cast<int32_t>( | 
|  | 727 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 728 | int32_t y = static_cast<int32_t>( | 
|  | 729 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 730 | sp<InputWindowHandle> touchedWindowHandle = | 
|  | 731 | findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 732 | if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 733 | touchedWindowHandle->getApplicationToken() != | 
|  | 734 | mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 735 | // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 736 | ALOGI("Pruning input queue because user touched a different application while waiting " | 
|  | 737 | "for %s", | 
|  | 738 | mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 739 | return true; | 
|  | 740 | } | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 741 |  | 
|  | 742 | // Alternatively, maybe there's a gesture monitor that could handle this event | 
|  | 743 | std::vector<TouchedMonitor> gestureMonitors = | 
|  | 744 | findTouchedGestureMonitorsLocked(displayId, {}); | 
|  | 745 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
|  | 746 | sp<Connection> connection = | 
|  | 747 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 748 | if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 749 | // This monitor could take more input. Drop all events preceding this | 
|  | 750 | // event, so that gesture monitor could get a chance to receive the stream | 
|  | 751 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
|  | 752 | "responsive gesture monitor that may handle the event", | 
|  | 753 | mAwaitedFocusedApplication->getName().c_str()); | 
|  | 754 | return true; | 
|  | 755 | } | 
|  | 756 | } | 
|  | 757 | } | 
|  | 758 |  | 
|  | 759 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
|  | 760 | // yet been processed by some connections, the dispatcher will wait for these motion | 
|  | 761 | // events to be processed before dispatching the key event. This is because these motion events | 
|  | 762 | // may cause a new window to be launched, which the user might expect to receive focus. | 
|  | 763 | // To prevent waiting forever for such events, just send the key to the currently focused window | 
|  | 764 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
|  | 765 | ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
|  | 766 | "just send the pending key event to the focused window."); | 
|  | 767 | mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 768 | } | 
|  | 769 | return false; | 
|  | 770 | } | 
|  | 771 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 772 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 773 | bool needWake = mInboundQueue.empty(); | 
|  | 774 | mInboundQueue.push_back(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | traceInboundQueueLengthLocked(); | 
|  | 776 |  | 
|  | 777 | switch (entry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 778 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 779 | // Optimize app switch latency. | 
|  | 780 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 781 | // the app switch key. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 782 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 783 | if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 784 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 785 | mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 786 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 787 | if (mAppSwitchSawKeyDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | #if DEBUG_APP_SWITCH | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 789 | ALOGD("App switch is pending!"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | #endif | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 791 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 792 | mAppSwitchSawKeyDown = false; | 
|  | 793 | needWake = true; | 
|  | 794 | } | 
|  | 795 | } | 
|  | 796 | } | 
|  | 797 | break; | 
|  | 798 | } | 
|  | 799 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 800 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 801 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { | 
|  | 802 | mNextUnblockedEvent = entry; | 
|  | 803 | needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 805 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 806 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 807 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 808 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
|  | 809 | break; | 
|  | 810 | } | 
|  | 811 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 812 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 813 | // nothing to do | 
|  | 814 | break; | 
|  | 815 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 816 | } | 
|  | 817 |  | 
|  | 818 | return needWake; | 
|  | 819 | } | 
|  | 820 |  | 
|  | 821 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { | 
|  | 822 | entry->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 823 | mRecentQueue.push_back(entry); | 
|  | 824 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
|  | 825 | mRecentQueue.front()->release(); | 
|  | 826 | mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 827 | } | 
|  | 828 | } | 
|  | 829 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 830 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 831 | int32_t y, TouchState* touchState, | 
|  | 832 | bool addOutsideTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 833 | bool addPortalWindows) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 834 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { | 
|  | 835 | LOG_ALWAYS_FATAL( | 
|  | 836 | "Must provide a valid touch state if adding portal windows or outside targets"); | 
|  | 837 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 838 | // Traverse windows from front to back to find touched window. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 839 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); | 
|  | 840 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 841 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 842 | if (windowInfo->displayId == displayId) { | 
|  | 843 | int32_t flags = windowInfo->layoutParamsFlags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 844 |  | 
|  | 845 | if (windowInfo->visible) { | 
|  | 846 | if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 847 | bool isTouchModal = (flags & | 
|  | 848 | (InputWindowInfo::FLAG_NOT_FOCUSABLE | | 
|  | 849 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 850 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 851 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 852 | if (portalToDisplayId != ADISPLAY_ID_NONE && | 
|  | 853 | portalToDisplayId != displayId) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 854 | if (addPortalWindows) { | 
|  | 855 | // For the monitoring channels of the display. | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 856 | touchState->addPortalWindow(windowHandle); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 857 | } | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 858 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | addOutsideTargets, addPortalWindows); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 860 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 861 | // Found window. | 
|  | 862 | return windowHandle; | 
|  | 863 | } | 
|  | 864 | } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 865 |  | 
|  | 866 | if (addOutsideTargets && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 867 | touchState->addOrUpdateWindow(windowHandle, | 
|  | 868 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
|  | 869 | BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 870 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 871 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | } | 
|  | 873 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 874 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | } | 
|  | 876 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 877 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 878 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 879 | std::vector<TouchedMonitor> touchedMonitors; | 
|  | 880 |  | 
|  | 881 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
|  | 882 | addGestureMonitors(monitors, touchedMonitors); | 
|  | 883 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { | 
|  | 884 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); | 
|  | 885 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 886 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, | 
|  | 887 | -windowInfo->frameTop); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 888 | } | 
|  | 889 | return touchedMonitors; | 
|  | 890 | } | 
|  | 891 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 892 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 893 | const char* reason; | 
|  | 894 | switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 895 | case DropReason::POLICY: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 896 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 897 | ALOGD("Dropped event because policy consumed it."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 899 | reason = "inbound event was dropped because the policy consumed it"; | 
|  | 900 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 901 | case DropReason::DISABLED: | 
|  | 902 | if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 903 | ALOGI("Dropped event because input dispatch is disabled."); | 
|  | 904 | } | 
|  | 905 | reason = "inbound event was dropped because input dispatch is disabled"; | 
|  | 906 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 907 | case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 | ALOGI("Dropped event because of pending overdue app switch."); | 
|  | 909 | reason = "inbound event was dropped because of pending overdue app switch"; | 
|  | 910 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 911 | case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 912 | ALOGI("Dropped event because the current application is not responding and the user " | 
|  | 913 | "has started interacting with a different application."); | 
|  | 914 | reason = "inbound event was dropped because the current application is not responding " | 
|  | 915 | "and the user has started interacting with a different application"; | 
|  | 916 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 917 | case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 918 | ALOGI("Dropped event because it is stale."); | 
|  | 919 | reason = "inbound event was dropped because it is stale"; | 
|  | 920 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 921 | case DropReason::NOT_DROPPED: { | 
|  | 922 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 923 | return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 924 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | } | 
|  | 926 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 927 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 928 | case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 929 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 930 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 931 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 933 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 934 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 935 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 936 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
|  | 937 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 938 | } else { | 
|  | 939 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 940 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 941 | } | 
|  | 942 | break; | 
|  | 943 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 944 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 945 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 946 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 947 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); | 
|  | 948 | break; | 
|  | 949 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | } | 
|  | 951 | } | 
|  | 952 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 953 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 954 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
|  | 955 | keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | } | 
|  | 957 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 958 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
|  | 959 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
|  | 960 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 961 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 962 | } | 
|  | 963 |  | 
|  | 964 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
|  | 965 | return mAppSwitchDueTime != LONG_LONG_MAX; | 
|  | 966 | } | 
|  | 967 |  | 
|  | 968 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
|  | 969 | mAppSwitchDueTime = LONG_LONG_MAX; | 
|  | 970 |  | 
|  | 971 | #if DEBUG_APP_SWITCH | 
|  | 972 | if (handled) { | 
|  | 973 | ALOGD("App switch has arrived."); | 
|  | 974 | } else { | 
|  | 975 | ALOGD("App switch was abandoned."); | 
|  | 976 | } | 
|  | 977 | #endif | 
|  | 978 | } | 
|  | 979 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 980 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 981 | return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 982 | } | 
|  | 983 |  | 
|  | 984 | bool InputDispatcher::runCommandsLockedInterruptible() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 985 | if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 | return false; | 
|  | 987 | } | 
|  | 988 |  | 
|  | 989 | do { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 990 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 991 | mCommandQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 992 | Command command = commandEntry->command; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 993 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 994 |  | 
|  | 995 | commandEntry->connection.clear(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 996 | } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | return true; | 
|  | 998 | } | 
|  | 999 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1000 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { | 
|  | 1001 | mCommandQueue.push_back(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | } | 
|  | 1003 |  | 
|  | 1004 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1005 | while (!mInboundQueue.empty()) { | 
|  | 1006 | EventEntry* entry = mInboundQueue.front(); | 
|  | 1007 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1008 | releaseInboundEventLocked(entry); | 
|  | 1009 | } | 
|  | 1010 | traceInboundQueueLengthLocked(); | 
|  | 1011 | } | 
|  | 1012 |  | 
|  | 1013 | void InputDispatcher::releasePendingEventLocked() { | 
|  | 1014 | if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 | releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1016 | mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1017 | } | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { | 
|  | 1021 | InjectionState* injectionState = entry->injectionState; | 
|  | 1022 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
|  | 1023 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1024 | ALOGD("Injected inbound event was dropped."); | 
|  | 1025 | #endif | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1026 | setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1027 | } | 
|  | 1028 | if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1029 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | } | 
|  | 1031 | addRecentEventLocked(entry); | 
|  | 1032 | entry->release(); | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | void InputDispatcher::resetKeyRepeatLocked() { | 
|  | 1036 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 1037 | mKeyRepeatState.lastKeyEntry->release(); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1038 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | } | 
|  | 1040 | } | 
|  | 1041 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1042 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1043 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; | 
|  | 1044 |  | 
|  | 1045 | // Reuse the repeated key entry if it is otherwise unreferenced. | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1046 | uint32_t policyFlags = entry->policyFlags & | 
|  | 1047 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 | if (entry->refCount == 1) { | 
|  | 1049 | entry->recycle(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1050 | entry->id = mIdGenerator.nextId(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | entry->eventTime = currentTime; | 
|  | 1052 | entry->policyFlags = policyFlags; | 
|  | 1053 | entry->repeatCount += 1; | 
|  | 1054 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1055 | KeyEntry* newEntry = | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1056 | new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1057 | entry->displayId, policyFlags, entry->action, entry->flags, | 
|  | 1058 | entry->keyCode, entry->scanCode, entry->metaState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1059 | entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 |  | 
|  | 1061 | mKeyRepeatState.lastKeyEntry = newEntry; | 
|  | 1062 | entry->release(); | 
|  | 1063 |  | 
|  | 1064 | entry = newEntry; | 
|  | 1065 | } | 
|  | 1066 | entry->syntheticRepeat = true; | 
|  | 1067 |  | 
|  | 1068 | // Increment reference count since we keep a reference to the event in | 
|  | 1069 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. | 
|  | 1070 | entry->refCount += 1; | 
|  | 1071 |  | 
|  | 1072 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
|  | 1073 | return entry; | 
|  | 1074 | } | 
|  | 1075 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1076 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
|  | 1077 | ConfigurationChangedEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1078 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1079 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | #endif | 
|  | 1081 |  | 
|  | 1082 | // Reset key repeating in case a keyboard device was added or removed or something. | 
|  | 1083 | resetKeyRepeatLocked(); | 
|  | 1084 |  | 
|  | 1085 | // 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] | 1086 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 1087 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1088 | commandEntry->eventTime = entry->eventTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1089 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1090 | return true; | 
|  | 1091 | } | 
|  | 1092 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1093 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1094 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1095 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | entry->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1097 | #endif | 
|  | 1098 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1099 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | options.deviceId = entry->deviceId; | 
|  | 1101 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1102 | return true; | 
|  | 1103 | } | 
|  | 1104 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1105 | void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1106 | if (mPendingEvent != nullptr) { | 
|  | 1107 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 1108 | // for the pending event to get dispatched to the newly focused window | 
|  | 1109 | mInboundQueue.push_front(mPendingEvent); | 
|  | 1110 | mPendingEvent = nullptr; | 
|  | 1111 | } | 
|  | 1112 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1113 | FocusEntry* focusEntry = | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1114 | new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus); | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1115 |  | 
|  | 1116 | // This event should go to the front of the queue, but behind all other focus events | 
|  | 1117 | // Find the last focus event, and insert right after it | 
|  | 1118 | std::deque<EventEntry*>::reverse_iterator it = | 
|  | 1119 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
|  | 1120 | [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); | 
|  | 1121 |  | 
|  | 1122 | // Maintain the order of focus events. Insert the entry after all other focus events. | 
|  | 1123 | mInboundQueue.insert(it.base(), focusEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1124 | } | 
|  | 1125 |  | 
|  | 1126 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { | 
|  | 1127 | sp<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
|  | 1128 | if (channel == nullptr) { | 
|  | 1129 | return; // Window has gone away | 
|  | 1130 | } | 
|  | 1131 | InputTarget target; | 
|  | 1132 | target.inputChannel = channel; | 
|  | 1133 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1134 | entry->dispatchInProgress = true; | 
| Selim Cinek | 3d989c2 | 2020-06-17 21:42:12 +0000 | [diff] [blame] | 1135 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1136 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1137 | } | 
|  | 1138 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1140 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1141 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1142 | if (!entry->dispatchInProgress) { | 
|  | 1143 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
|  | 1144 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1145 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
|  | 1146 | if (mKeyRepeatState.lastKeyEntry && | 
|  | 1147 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | // We have seen two identical key downs in a row which indicates that the device | 
|  | 1149 | // driver is automatically generating key repeats itself.  We take note of the | 
|  | 1150 | // repeat here, but we disable our own next key repeat timer since it is clear that | 
|  | 1151 | // we will not need to synthesize key repeats ourselves. | 
|  | 1152 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
|  | 1153 | resetKeyRepeatLocked(); | 
|  | 1154 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
|  | 1155 | } else { | 
|  | 1156 | // Not a repeat.  Save key down state in case we do see a repeat later. | 
|  | 1157 | resetKeyRepeatLocked(); | 
|  | 1158 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
|  | 1159 | } | 
|  | 1160 | mKeyRepeatState.lastKeyEntry = entry; | 
|  | 1161 | entry->refCount += 1; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1162 | } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | resetKeyRepeatLocked(); | 
|  | 1164 | } | 
|  | 1165 |  | 
|  | 1166 | if (entry->repeatCount == 1) { | 
|  | 1167 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1168 | } else { | 
|  | 1169 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1170 | } | 
|  | 1171 |  | 
|  | 1172 | entry->dispatchInProgress = true; | 
|  | 1173 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1174 | logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 | } | 
|  | 1176 |  | 
|  | 1177 | // Handle case where the policy asked us to try again later last time. | 
|  | 1178 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1179 | if (currentTime < entry->interceptKeyWakeupTime) { | 
|  | 1180 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
|  | 1181 | *nextWakeupTime = entry->interceptKeyWakeupTime; | 
|  | 1182 | } | 
|  | 1183 | return false; // wait until next wakeup | 
|  | 1184 | } | 
|  | 1185 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1186 | entry->interceptKeyWakeupTime = 0; | 
|  | 1187 | } | 
|  | 1188 |  | 
|  | 1189 | // Give the policy a chance to intercept the key. | 
|  | 1190 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
|  | 1191 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1192 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
| Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1193 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1194 | sp<InputWindowHandle> focusedWindowHandle = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1195 | getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(*entry)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1196 | if (focusedWindowHandle != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1197 | commandEntry->inputChannel = getInputChannelLocked(focusedWindowHandle->getToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | } | 
|  | 1199 | commandEntry->keyEntry = entry; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1200 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1201 | entry->refCount += 1; | 
|  | 1202 | return false; // wait for the command to run | 
|  | 1203 | } else { | 
|  | 1204 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 1205 | } | 
|  | 1206 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1207 | if (*dropReason == DropReason::NOT_DROPPED) { | 
|  | 1208 | *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | } | 
|  | 1210 | } | 
|  | 1211 |  | 
|  | 1212 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1213 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1214 | setInjectionResult(entry, | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1215 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1216 | : INPUT_EVENT_INJECTION_FAILED); | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1217 | mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1218 | return true; | 
|  | 1219 | } | 
|  | 1220 |  | 
|  | 1221 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1222 | std::vector<InputTarget> inputTargets; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1223 | int32_t injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1224 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1225 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
|  | 1226 | return false; | 
|  | 1227 | } | 
|  | 1228 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1229 | setInjectionResult(entry, injectionResult); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { | 
|  | 1231 | return true; | 
|  | 1232 | } | 
|  | 1233 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1234 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1235 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 |  | 
|  | 1237 | // Dispatch the key. | 
|  | 1238 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1239 | return true; | 
|  | 1240 | } | 
|  | 1241 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1242 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1244 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1245 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
|  | 1246 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1247 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1248 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 1249 | entry.repeatCount, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | #endif | 
|  | 1251 | } | 
|  | 1252 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1253 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, | 
|  | 1254 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1255 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1256 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1257 | if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1258 | entry->dispatchInProgress = true; | 
|  | 1259 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1260 | logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | } | 
|  | 1262 |  | 
|  | 1263 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1264 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1265 | setInjectionResult(entry, | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1266 | *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1267 | : INPUT_EVENT_INJECTION_FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | return true; | 
|  | 1269 | } | 
|  | 1270 |  | 
|  | 1271 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
|  | 1272 |  | 
|  | 1273 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1274 | std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 |  | 
|  | 1276 | bool conflictingPointerActions = false; | 
|  | 1277 | int32_t injectionResult; | 
|  | 1278 | if (isPointerEvent) { | 
|  | 1279 | // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1280 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1281 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1282 | &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | } else { | 
|  | 1284 | // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1285 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1286 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1287 | } | 
|  | 1288 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
|  | 1289 | return false; | 
|  | 1290 | } | 
|  | 1291 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1292 | setInjectionResult(entry, injectionResult); | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1293 | if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { | 
|  | 1294 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
|  | 1295 | return true; | 
|  | 1296 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1297 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1298 | CancelationOptions::Mode mode(isPointerEvent | 
|  | 1299 | ? CancelationOptions::CANCEL_POINTER_EVENTS | 
|  | 1300 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
|  | 1301 | CancelationOptions options(mode, "input event injection failed"); | 
|  | 1302 | synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | return true; | 
|  | 1304 | } | 
|  | 1305 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1306 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1307 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1308 |  | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1309 | if (isPointerEvent) { | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1310 | std::unordered_map<int32_t, TouchState>::iterator it = | 
|  | 1311 | mTouchStatesByDisplay.find(entry->displayId); | 
|  | 1312 | if (it != mTouchStatesByDisplay.end()) { | 
|  | 1313 | const TouchState& state = it->second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1314 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1315 | // The event has gone through these portal windows, so we add monitoring targets of | 
|  | 1316 | // the corresponding displays as well. | 
|  | 1317 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1318 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1319 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1320 | -windowInfo->frameLeft, -windowInfo->frameTop); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1321 | } | 
|  | 1322 | } | 
|  | 1323 | } | 
|  | 1324 | } | 
|  | 1325 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1326 | // Dispatch the motion. | 
|  | 1327 | if (conflictingPointerActions) { | 
|  | 1328 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1329 | "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1330 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1331 | } | 
|  | 1332 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1333 | return true; | 
|  | 1334 | } | 
|  | 1335 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1336 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1338 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1339 | ", policyFlags=0x%x, " | 
|  | 1340 | "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
|  | 1341 | "metaState=0x%x, buttonState=0x%x," | 
|  | 1342 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1343 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1344 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, | 
|  | 1345 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1346 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1347 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1349 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 1350 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 1351 | "orientation=%f", | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1352 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
|  | 1353 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 1354 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 1355 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 1356 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 1357 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 1358 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 1359 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 1360 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 1361 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1362 | } | 
|  | 1363 | #endif | 
|  | 1364 | } | 
|  | 1365 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1366 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, | 
|  | 1367 | const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1368 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1369 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1370 | ALOGD("dispatchEventToCurrentInputTargets"); | 
|  | 1371 | #endif | 
|  | 1372 |  | 
|  | 1373 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
|  | 1374 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1375 | pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1377 | for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1378 | sp<Connection> connection = | 
|  | 1379 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1380 | if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1381 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1382 | } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1383 | if (DEBUG_FOCUS) { | 
|  | 1384 | ALOGD("Dropping event delivery to target with channel '%s' because it " | 
|  | 1385 | "is no longer registered with the input dispatcher.", | 
|  | 1386 | inputTarget.inputChannel->getName().c_str()); | 
|  | 1387 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1388 | } | 
|  | 1389 | } | 
|  | 1390 | } | 
|  | 1391 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1392 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
|  | 1393 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
|  | 1394 | // If the policy decides to close the app, we will get a channel removal event via | 
|  | 1395 | // unregisterInputChannel, and will clean up the connection that way. We are already not | 
|  | 1396 | // sending new pointers to the connection when it blocked, but focused events will continue to | 
|  | 1397 | // pile up. | 
|  | 1398 | ALOGW("Canceling events for %s because it is unresponsive", | 
|  | 1399 | connection->inputChannel->getName().c_str()); | 
|  | 1400 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 1401 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
|  | 1402 | "application not responding"); | 
|  | 1403 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1404 | } | 
|  | 1405 | } | 
|  | 1406 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1407 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1408 | if (DEBUG_FOCUS) { | 
|  | 1409 | ALOGD("Resetting ANR timeouts."); | 
|  | 1410 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1411 |  | 
|  | 1412 | // Reset input target wait timeout. | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1413 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
|  | 1414 | mAwaitedFocusedApplication.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1415 | } | 
|  | 1416 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1417 | /** | 
|  | 1418 | * Get the display id that the given event should go to. If this event specifies a valid display id, | 
|  | 1419 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
|  | 1420 | * Focused display is the display that the user most recently interacted with. | 
|  | 1421 | */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1422 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1423 | int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1424 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1425 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1426 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 1427 | displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1428 | break; | 
|  | 1429 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1430 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1431 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1432 | displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1433 | break; | 
|  | 1434 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1435 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1436 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 1437 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 1438 | 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] | 1439 | return ADISPLAY_ID_NONE; | 
|  | 1440 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1441 | } | 
|  | 1442 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
|  | 1443 | } | 
|  | 1444 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1445 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
|  | 1446 | const char* focusedWindowName) { | 
|  | 1447 | if (mAnrTracker.empty()) { | 
|  | 1448 | // already processed all events that we waited for | 
|  | 1449 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1450 | return false; | 
|  | 1451 | } | 
|  | 1452 |  | 
|  | 1453 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
|  | 1454 | // Start the timer | 
|  | 1455 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " | 
|  | 1456 | "focus to change", | 
|  | 1457 | focusedWindowName); | 
|  | 1458 | mKeyIsWaitingForEventsTimeout = currentTime + KEY_WAITING_FOR_EVENTS_TIMEOUT.count(); | 
|  | 1459 | return true; | 
|  | 1460 | } | 
|  | 1461 |  | 
|  | 1462 | // We still have pending events, and already started the timer | 
|  | 1463 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
|  | 1464 | return true; // Still waiting | 
|  | 1465 | } | 
|  | 1466 |  | 
|  | 1467 | // Waited too long, and some connection still hasn't processed all motions | 
|  | 1468 | // Just send the key to the focused window | 
|  | 1469 | ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
|  | 1470 | focusedWindowName); | 
|  | 1471 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1472 | return false; | 
|  | 1473 | } | 
|  | 1474 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1475 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1476 | const EventEntry& entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1477 | std::vector<InputTarget>& inputTargets, | 
|  | 1478 | nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1479 | std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1480 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1481 | int32_t displayId = getTargetDisplayId(entry); | 
|  | 1482 | sp<InputWindowHandle> focusedWindowHandle = | 
|  | 1483 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
|  | 1484 | sp<InputApplicationHandle> focusedApplicationHandle = | 
|  | 1485 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 1486 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1487 | // If there is no currently focused window and no focused application | 
|  | 1488 | // then drop the event. | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1489 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
|  | 1490 | ALOGI("Dropping %s event because there is no focused window or focused application in " | 
|  | 1491 | "display %" PRId32 ".", | 
|  | 1492 | EventEntry::typeToString(entry.type), displayId); | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1493 | return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1494 | } | 
|  | 1495 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1496 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
|  | 1497 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
|  | 1498 | // start interacting with another application via touch (app switch). This code can be removed | 
|  | 1499 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
|  | 1500 | // an app is expected to have a focused window. | 
|  | 1501 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
|  | 1502 | if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
|  | 1503 | // We just discovered that there's no focused window. Start the ANR timer | 
|  | 1504 | const nsecs_t timeout = focusedApplicationHandle->getDispatchingTimeout( | 
|  | 1505 | DEFAULT_INPUT_DISPATCHING_TIMEOUT.count()); | 
|  | 1506 | mNoFocusedWindowTimeoutTime = currentTime + timeout; | 
|  | 1507 | mAwaitedFocusedApplication = focusedApplicationHandle; | 
| Siarhei Vishniakou | 265ab01 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1508 | mAwaitedApplicationDisplayId = displayId; | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1509 | ALOGW("Waiting because no window has focus but %s may eventually add a " | 
|  | 1510 | "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
|  | 1511 | mAwaitedFocusedApplication->getName().c_str(), ns2ms(timeout)); | 
|  | 1512 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
|  | 1513 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1514 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
|  | 1515 | // Already raised ANR. Drop the event | 
|  | 1516 | ALOGE("Dropping %s event because there is no focused window", | 
|  | 1517 | EventEntry::typeToString(entry.type)); | 
|  | 1518 | return INPUT_EVENT_INJECTION_FAILED; | 
|  | 1519 | } else { | 
|  | 1520 | // Still waiting for the focused window | 
|  | 1521 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1522 | } | 
|  | 1523 | } | 
|  | 1524 |  | 
|  | 1525 | // we have a valid, non-null focused window | 
|  | 1526 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 1527 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1528 | // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1529 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1530 | return INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | } | 
|  | 1532 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1533 | if (focusedWindowHandle->getInfo()->paused) { | 
|  | 1534 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
|  | 1535 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1536 | } | 
|  | 1537 |  | 
|  | 1538 | // If the event is a key event, then we must wait for all previous events to | 
|  | 1539 | // complete before delivering it because previous events may have the | 
|  | 1540 | // side-effect of transferring focus to a different window and we want to | 
|  | 1541 | // ensure that the following keys are sent to the new window. | 
|  | 1542 | // | 
|  | 1543 | // Suppose the user touches a button in a window then immediately presses "A". | 
|  | 1544 | // If the button causes a pop-up window to appear then we want to ensure that | 
|  | 1545 | // the "A" key is delivered to the new pop-up window.  This is because users | 
|  | 1546 | // often anticipate pending UI changes when typing on a keyboard. | 
|  | 1547 | // To obtain this behavior, we must serialize key events with respect to all | 
|  | 1548 | // prior input events. | 
|  | 1549 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 1550 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
|  | 1551 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
|  | 1552 | return INPUT_EVENT_INJECTION_PENDING; | 
|  | 1553 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | } | 
|  | 1555 |  | 
|  | 1556 | // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1557 | addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1558 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1559 | BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1560 |  | 
|  | 1561 | // Done. | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1562 | return INPUT_EVENT_INJECTION_SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | } | 
|  | 1564 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1565 | /** | 
|  | 1566 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
|  | 1567 | * that are currently unresponsive. | 
|  | 1568 | */ | 
|  | 1569 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
|  | 1570 | const std::vector<TouchedMonitor>& monitors) const { | 
|  | 1571 | std::vector<TouchedMonitor> responsiveMonitors; | 
|  | 1572 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
|  | 1573 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
|  | 1574 | sp<Connection> connection = getConnectionLocked( | 
|  | 1575 | monitor.monitor.inputChannel->getConnectionToken()); | 
|  | 1576 | if (connection == nullptr) { | 
|  | 1577 | ALOGE("Could not find connection for monitor %s", | 
|  | 1578 | monitor.monitor.inputChannel->getName().c_str()); | 
|  | 1579 | return false; | 
|  | 1580 | } | 
|  | 1581 | if (!connection->responsive) { | 
|  | 1582 | ALOGW("Unresponsive monitor %s will not get the new gesture", | 
|  | 1583 | connection->inputChannel->getName().c_str()); | 
|  | 1584 | return false; | 
|  | 1585 | } | 
|  | 1586 | return true; | 
|  | 1587 | }); | 
|  | 1588 | return responsiveMonitors; | 
|  | 1589 | } | 
|  | 1590 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1592 | const MotionEntry& entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1593 | std::vector<InputTarget>& inputTargets, | 
|  | 1594 | nsecs_t* nextWakeupTime, | 
|  | 1595 | bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1596 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 | enum InjectionPermission { | 
|  | 1598 | INJECTION_PERMISSION_UNKNOWN, | 
|  | 1599 | INJECTION_PERMISSION_GRANTED, | 
|  | 1600 | INJECTION_PERMISSION_DENIED | 
|  | 1601 | }; | 
|  | 1602 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | // For security reasons, we defer updating the touch state until we are sure that | 
|  | 1604 | // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1605 | int32_t displayId = entry.displayId; | 
|  | 1606 | int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
|  | 1608 |  | 
|  | 1609 | // Update the touch state as needed based on the properties of the touch event. | 
|  | 1610 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; | 
|  | 1611 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
|  | 1612 | sp<InputWindowHandle> newHoverWindowHandle; | 
|  | 1613 |  | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1614 | // Copy current touch state into tempTouchState. | 
|  | 1615 | // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
|  | 1616 | // 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] | 1617 | const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1618 | TouchState tempTouchState; | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1619 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
|  | 1620 | mTouchStatesByDisplay.find(displayId); | 
|  | 1621 | if (oldStateIt != mTouchStatesByDisplay.end()) { | 
|  | 1622 | oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1623 | tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1624 | } | 
|  | 1625 |  | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1626 | bool isSplit = tempTouchState.split; | 
|  | 1627 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
|  | 1628 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
|  | 1629 | tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1630 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
|  | 1631 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1632 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
|  | 1633 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
|  | 1634 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1635 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1636 | bool wrongDevice = false; | 
|  | 1637 | if (newGesture) { | 
|  | 1638 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1639 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1640 | ALOGI("Dropping event because a pointer for a different device is already down " | 
|  | 1641 | "in display %" PRId32, | 
|  | 1642 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1643 | // TODO: test multiple simultaneous input streams. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1645 | switchedDevice = false; | 
|  | 1646 | wrongDevice = true; | 
|  | 1647 | goto Failed; | 
|  | 1648 | } | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1649 | tempTouchState.reset(); | 
|  | 1650 | tempTouchState.down = down; | 
|  | 1651 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1652 | tempTouchState.source = entry.source; | 
|  | 1653 | tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1654 | isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1655 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1656 | ALOGI("Dropping move event because a pointer for a different device is already active " | 
|  | 1657 | "in display %" PRId32, | 
|  | 1658 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1659 | // TODO: test multiple simultaneous input streams. | 
|  | 1660 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
|  | 1661 | switchedDevice = false; | 
|  | 1662 | wrongDevice = true; | 
|  | 1663 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | } | 
|  | 1665 |  | 
|  | 1666 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
|  | 1667 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
|  | 1668 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1669 | int32_t x; | 
|  | 1670 | int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1671 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1672 | // Always dispatch mouse events to cursor position. | 
|  | 1673 | if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1674 | x = int32_t(entry.xCursorPosition); | 
|  | 1675 | y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1676 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1677 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1678 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1679 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1680 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1681 | sp<InputWindowHandle> newTouchedWindowHandle = | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1682 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
|  | 1683 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1684 |  | 
|  | 1685 | std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1686 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1687 | : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1688 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1689 | // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1690 | if (newTouchedWindowHandle != nullptr && | 
|  | 1691 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1692 | // New window supports splitting, but we should never split mouse events. | 
|  | 1693 | isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1694 | } else if (isSplit) { | 
|  | 1695 | // New window does not support splitting but we have already split events. | 
|  | 1696 | // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1697 | newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1698 | } | 
|  | 1699 |  | 
|  | 1700 | // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1701 | if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1702 | // Try to assign the pointer to the first foreground window we find, if there is one. | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1703 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1704 | } | 
|  | 1705 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1706 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
|  | 1707 | ALOGI("Not sending touch event to %s because it is paused", | 
|  | 1708 | newTouchedWindowHandle->getName().c_str()); | 
|  | 1709 | newTouchedWindowHandle = nullptr; | 
|  | 1710 | } | 
|  | 1711 |  | 
|  | 1712 | if (newTouchedWindowHandle != nullptr) { | 
|  | 1713 | sp<Connection> connection = getConnectionLocked(newTouchedWindowHandle->getToken()); | 
|  | 1714 | if (connection == nullptr) { | 
|  | 1715 | ALOGI("Could not find connection for %s", | 
|  | 1716 | newTouchedWindowHandle->getName().c_str()); | 
|  | 1717 | newTouchedWindowHandle = nullptr; | 
|  | 1718 | } else if (!connection->responsive) { | 
|  | 1719 | // don't send the new touch to an unresponsive window | 
|  | 1720 | ALOGW("Unresponsive window %s will not get the new gesture at %" PRIu64, | 
|  | 1721 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
|  | 1722 | newTouchedWindowHandle = nullptr; | 
|  | 1723 | } | 
|  | 1724 | } | 
|  | 1725 |  | 
|  | 1726 | // Also don't send the new touch event to unresponsive gesture monitors | 
|  | 1727 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
|  | 1728 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1729 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
|  | 1730 | 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] | 1731 | "(%d, %d) in display %" PRId32 ".", | 
|  | 1732 | x, y, displayId); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1733 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1734 | goto Failed; | 
|  | 1735 | } | 
|  | 1736 |  | 
|  | 1737 | if (newTouchedWindowHandle != nullptr) { | 
|  | 1738 | // Set target flags. | 
|  | 1739 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1740 | if (isSplit) { | 
|  | 1741 | targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1742 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1743 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 1744 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 1745 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 1746 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1747 | } | 
|  | 1748 |  | 
|  | 1749 | // Update hover state. | 
|  | 1750 | if (isHoverAction) { | 
|  | 1751 | newHoverWindowHandle = newTouchedWindowHandle; | 
|  | 1752 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { | 
|  | 1753 | newHoverWindowHandle = mLastHoverWindowHandle; | 
|  | 1754 | } | 
|  | 1755 |  | 
|  | 1756 | // Update the temporary touch state. | 
|  | 1757 | BitSet32 pointerIds; | 
|  | 1758 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1759 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1760 | pointerIds.markBit(pointerId); | 
|  | 1761 | } | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1762 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1763 | } | 
|  | 1764 |  | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1765 | tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1766 | } else { | 
|  | 1767 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
|  | 1768 |  | 
|  | 1769 | // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1770 | if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1771 | if (DEBUG_FOCUS) { | 
|  | 1772 | ALOGD("Dropping event because the pointer is not down or we previously " | 
|  | 1773 | "dropped the pointer down event in display %" PRId32, | 
|  | 1774 | displayId); | 
|  | 1775 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1776 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1777 | goto Failed; | 
|  | 1778 | } | 
|  | 1779 |  | 
|  | 1780 | // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1781 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1782 | tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1783 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1784 | 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] | 1785 |  | 
|  | 1786 | sp<InputWindowHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1787 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | sp<InputWindowHandle> newTouchedWindowHandle = | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1789 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1790 | if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
|  | 1791 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1792 | if (DEBUG_FOCUS) { | 
|  | 1793 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
|  | 1794 | oldTouchedWindowHandle->getName().c_str(), | 
|  | 1795 | newTouchedWindowHandle->getName().c_str(), displayId); | 
|  | 1796 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1797 | // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1798 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
|  | 1799 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
|  | 1800 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 |  | 
|  | 1802 | // Make a slippery entrance into the new window. | 
|  | 1803 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
|  | 1804 | isSplit = true; | 
|  | 1805 | } | 
|  | 1806 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1807 | int32_t targetFlags = | 
|  | 1808 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | if (isSplit) { | 
|  | 1810 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 1811 | } | 
|  | 1812 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 1813 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 1814 | } | 
|  | 1815 |  | 
|  | 1816 | BitSet32 pointerIds; | 
|  | 1817 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1818 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | } | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1820 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | } | 
|  | 1822 | } | 
|  | 1823 | } | 
|  | 1824 |  | 
|  | 1825 | if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
|  | 1826 | // Let the previous window know that the hover sequence is over. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1827 | if (mLastHoverWindowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | #if DEBUG_HOVER | 
|  | 1829 | ALOGD("Sending hover exit event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1830 | mLastHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | #endif | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1832 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
|  | 1833 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | } | 
|  | 1835 |  | 
|  | 1836 | // Let the new window know that the hover sequence is starting. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1837 | if (newHoverWindowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | #if DEBUG_HOVER | 
|  | 1839 | ALOGD("Sending hover enter event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1840 | newHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1841 | #endif | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1842 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
|  | 1843 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
|  | 1844 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | } | 
|  | 1846 | } | 
|  | 1847 |  | 
|  | 1848 | // Check permission to inject into all touched foreground windows and ensure there | 
|  | 1849 | // is at least one touched foreground window. | 
|  | 1850 | { | 
|  | 1851 | bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1852 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
|  | 1854 | haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1855 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
|  | 1857 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 1858 | goto Failed; | 
|  | 1859 | } | 
|  | 1860 | } | 
|  | 1861 | } | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1862 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1863 | if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | e0fb6bd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1864 | ALOGI("Dropping event because there is no touched foreground window in display " | 
|  | 1865 | "%" PRId32 " or gesture monitor to receive it.", | 
|  | 1866 | displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1867 | injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
|  | 1868 | goto Failed; | 
|  | 1869 | } | 
|  | 1870 |  | 
|  | 1871 | // Permission granted to injection into all touched foreground windows. | 
|  | 1872 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 1873 | } | 
|  | 1874 |  | 
|  | 1875 | // Check whether windows listening for outside touches are owned by the same UID. If it is | 
|  | 1876 | // set the policy flag that we will not reveal coordinate information to this window. | 
|  | 1877 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1878 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1879 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1880 | if (foregroundWindowHandle) { | 
|  | 1881 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1882 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1883 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 1884 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; | 
|  | 1885 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1886 | tempTouchState.addOrUpdateWindow(inputWindowHandle, | 
|  | 1887 | InputTarget::FLAG_ZERO_COORDS, | 
|  | 1888 | BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1889 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1890 | } | 
|  | 1891 | } | 
|  | 1892 | } | 
|  | 1893 | } | 
|  | 1894 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1895 | // If this is the first pointer going down and the touched window has a wallpaper | 
|  | 1896 | // then also add the touched wallpaper windows so they are locked in for the duration | 
|  | 1897 | // of the touch gesture. | 
|  | 1898 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
|  | 1899 | // engine only supports touch events.  We would need to add a mechanism similar | 
|  | 1900 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
|  | 1901 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1902 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1903 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1904 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1905 | const std::vector<sp<InputWindowHandle>> windowHandles = | 
|  | 1906 | getWindowHandlesLocked(displayId); | 
|  | 1907 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | const InputWindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1909 | if (info->displayId == displayId && | 
|  | 1910 | windowHandle->getInfo()->layoutParamsType == InputWindowInfo::TYPE_WALLPAPER) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1911 | tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1912 | .addOrUpdateWindow(windowHandle, | 
|  | 1913 | InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
|  | 1914 | InputTarget:: | 
|  | 1915 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
|  | 1916 | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1917 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 | } | 
|  | 1919 | } | 
|  | 1920 | } | 
|  | 1921 | } | 
|  | 1922 |  | 
|  | 1923 | // Success!  Output targets. | 
|  | 1924 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; | 
|  | 1925 |  | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1926 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1927 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1928 | touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | } | 
|  | 1930 |  | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1931 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1932 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1933 | touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1934 | } | 
|  | 1935 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1936 | // Drop the outside or hover touch windows since we will not care about them | 
|  | 1937 | // in the next iteration. | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1938 | tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1939 |  | 
|  | 1940 | Failed: | 
|  | 1941 | // Check injection permission once and for all. | 
|  | 1942 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1943 | if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1944 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 1945 | } else { | 
|  | 1946 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 1947 | } | 
|  | 1948 | } | 
|  | 1949 |  | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1950 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
|  | 1951 | return injectionResult; | 
|  | 1952 | } | 
|  | 1953 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1954 | // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1955 | if (!wrongDevice) { | 
|  | 1956 | if (switchedDevice) { | 
|  | 1957 | if (DEBUG_FOCUS) { | 
|  | 1958 | ALOGD("Conflicting pointer actions: Switched to a different device."); | 
|  | 1959 | } | 
|  | 1960 | *outConflictingPointerActions = true; | 
|  | 1961 | } | 
|  | 1962 |  | 
|  | 1963 | if (isHoverAction) { | 
|  | 1964 | // Started hovering, therefore no longer down. | 
|  | 1965 | if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1966 | if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1967 | ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
|  | 1968 | "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1969 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1970 | *outConflictingPointerActions = true; | 
|  | 1971 | } | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1972 | tempTouchState.reset(); | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1973 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1974 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1975 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1976 | tempTouchState.source = entry.source; | 
|  | 1977 | tempTouchState.displayId = displayId; | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1978 | } | 
|  | 1979 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
|  | 1980 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 1981 | // All pointers up or canceled. | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1982 | tempTouchState.reset(); | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1983 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1984 | // First pointer went down. | 
|  | 1985 | if (oldState && oldState->down) { | 
|  | 1986 | if (DEBUG_FOCUS) { | 
|  | 1987 | ALOGD("Conflicting pointer actions: Down received while already down."); | 
|  | 1988 | } | 
|  | 1989 | *outConflictingPointerActions = true; | 
|  | 1990 | } | 
|  | 1991 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 1992 | // One pointer went up. | 
|  | 1993 | if (isSplit) { | 
|  | 1994 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 1995 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1996 |  | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1997 | for (size_t i = 0; i < tempTouchState.windows.size();) { | 
|  | 1998 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1999 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
|  | 2000 | touchedWindow.pointerIds.clearBit(pointerId); | 
|  | 2001 | if (touchedWindow.pointerIds.isEmpty()) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2002 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2003 | continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 | } | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2006 | i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2008 | } | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2009 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2010 |  | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2011 | // Save changes unless the action was scroll in which case the temporary touch | 
|  | 2012 | // state was only valid for this one action. | 
|  | 2013 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
| Siarhei Vishniakou | 1ea3cf1 | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2014 | if (tempTouchState.displayId >= 0) { | 
|  | 2015 | mTouchStatesByDisplay[displayId] = tempTouchState; | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 2016 | } else { | 
|  | 2017 | mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2018 | } | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2019 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2020 |  | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2021 | // Update hover state. | 
|  | 2022 | mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2023 | } | 
|  | 2024 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2025 | return injectionResult; | 
|  | 2026 | } | 
|  | 2027 |  | 
|  | 2028 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2029 | int32_t targetFlags, BitSet32 pointerIds, | 
|  | 2030 | std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2031 | std::vector<InputTarget>::iterator it = | 
|  | 2032 | std::find_if(inputTargets.begin(), inputTargets.end(), | 
|  | 2033 | [&windowHandle](const InputTarget& inputTarget) { | 
|  | 2034 | return inputTarget.inputChannel->getConnectionToken() == | 
|  | 2035 | windowHandle->getToken(); | 
|  | 2036 | }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2037 |  | 
| Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2038 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2039 |  | 
|  | 2040 | if (it == inputTargets.end()) { | 
|  | 2041 | InputTarget inputTarget; | 
|  | 2042 | sp<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); | 
|  | 2043 | if (inputChannel == nullptr) { | 
|  | 2044 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
|  | 2045 | return; | 
|  | 2046 | } | 
|  | 2047 | inputTarget.inputChannel = inputChannel; | 
|  | 2048 | inputTarget.flags = targetFlags; | 
|  | 2049 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2050 | inputTargets.push_back(inputTarget); | 
|  | 2051 | it = inputTargets.end() - 1; | 
|  | 2052 | } | 
|  | 2053 |  | 
|  | 2054 | ALOG_ASSERT(it->flags == targetFlags); | 
|  | 2055 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
|  | 2056 |  | 
|  | 2057 | it->addPointers(pointerIds, -windowInfo->frameLeft, -windowInfo->frameTop, | 
|  | 2058 | windowInfo->windowXScale, windowInfo->windowYScale); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2059 | } | 
|  | 2060 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2061 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2062 | int32_t displayId, float xOffset, | 
|  | 2063 | float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2064 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
|  | 2065 | mGlobalMonitorsByDisplay.find(displayId); | 
|  | 2066 |  | 
|  | 2067 | if (it != mGlobalMonitorsByDisplay.end()) { | 
|  | 2068 | const std::vector<Monitor>& monitors = it->second; | 
|  | 2069 | for (const Monitor& monitor : monitors) { | 
|  | 2070 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2071 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | } | 
|  | 2073 | } | 
|  | 2074 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2075 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
|  | 2076 | float yOffset, | 
|  | 2077 | std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2078 | InputTarget target; | 
|  | 2079 | target.inputChannel = monitor.inputChannel; | 
|  | 2080 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2081 | target.setDefaultPointerInfo(xOffset, yOffset, 1 /* windowXScale */, 1 /* windowYScale */); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2082 | inputTargets.push_back(target); | 
|  | 2083 | } | 
|  | 2084 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2085 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2086 | const InjectionState* injectionState) { | 
|  | 2087 | if (injectionState && | 
|  | 2088 | (windowHandle == nullptr || | 
|  | 2089 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
|  | 2090 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2091 | if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2092 | 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] | 2093 | "owned by uid %d", | 
|  | 2094 | injectionState->injectorPid, injectionState->injectorUid, | 
|  | 2095 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2096 | } else { | 
|  | 2097 | ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2098 | injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2099 | } | 
|  | 2100 | return false; | 
|  | 2101 | } | 
|  | 2102 | return true; | 
|  | 2103 | } | 
|  | 2104 |  | 
| Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2105 | /** | 
|  | 2106 | * Indicate whether one window handle should be considered as obscuring | 
|  | 2107 | * another window handle. We only check a few preconditions. Actually | 
|  | 2108 | * checking the bounds is left to the caller. | 
|  | 2109 | */ | 
|  | 2110 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, | 
|  | 2111 | const sp<InputWindowHandle>& otherHandle) { | 
|  | 2112 | // Compare by token so cloned layers aren't counted | 
|  | 2113 | if (haveSameToken(windowHandle, otherHandle)) { | 
|  | 2114 | return false; | 
|  | 2115 | } | 
|  | 2116 | auto info = windowHandle->getInfo(); | 
|  | 2117 | auto otherInfo = otherHandle->getInfo(); | 
|  | 2118 | if (!otherInfo->visible) { | 
|  | 2119 | return false; | 
| Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2120 | } else if (info->ownerPid == otherInfo->ownerPid) { | 
|  | 2121 | // If ownerPid is the same we don't generate occlusion events as there | 
|  | 2122 | // is no in-process security boundary. | 
| Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2123 | return false; | 
|  | 2124 | } else if (otherInfo->isTrustedOverlay()) { | 
|  | 2125 | return false; | 
|  | 2126 | } else if (otherInfo->displayId != info->displayId) { | 
|  | 2127 | return false; | 
|  | 2128 | } | 
|  | 2129 | return true; | 
|  | 2130 | } | 
|  | 2131 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2132 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, | 
|  | 2133 | int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2134 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2135 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2136 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2137 | if (windowHandle == otherHandle) { | 
|  | 2138 | break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2139 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2140 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2141 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
|  | 2142 | otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2143 | return true; | 
|  | 2144 | } | 
|  | 2145 | } | 
|  | 2146 | return false; | 
|  | 2147 | } | 
|  | 2148 |  | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2149 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 2150 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2151 | const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2152 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2153 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2154 | if (windowHandle == otherHandle) { | 
|  | 2155 | break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2156 | } | 
|  | 2157 |  | 
|  | 2158 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | 9cada03 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2159 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
|  | 2160 | otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2161 | return true; | 
|  | 2162 | } | 
|  | 2163 | } | 
|  | 2164 | return false; | 
|  | 2165 | } | 
|  | 2166 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2167 | std::string InputDispatcher::getApplicationWindowLabel( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2168 | const sp<InputApplicationHandle>& applicationHandle, | 
|  | 2169 | const sp<InputWindowHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2170 | if (applicationHandle != nullptr) { | 
|  | 2171 | if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2172 | return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2173 | } else { | 
|  | 2174 | return applicationHandle->getName(); | 
|  | 2175 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2176 | } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2177 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2178 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2179 | return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2180 | } | 
|  | 2181 | } | 
|  | 2182 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2183 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2184 | if (eventEntry.type == EventEntry::Type::FOCUS) { | 
|  | 2185 | // Focus events are passed to apps, but do not represent user activity. | 
|  | 2186 | return; | 
|  | 2187 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2188 | int32_t displayId = getTargetDisplayId(eventEntry); | 
|  | 2189 | sp<InputWindowHandle> focusedWindowHandle = | 
|  | 2190 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
|  | 2191 | if (focusedWindowHandle != nullptr) { | 
|  | 2192 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { | 
|  | 2194 | #if DEBUG_DISPATCH_CYCLE | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2195 | 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] | 2196 | #endif | 
|  | 2197 | return; | 
|  | 2198 | } | 
|  | 2199 | } | 
|  | 2200 |  | 
|  | 2201 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2202 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2203 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2204 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 2205 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2206 | return; | 
|  | 2207 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2209 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2210 | eventType = USER_ACTIVITY_EVENT_TOUCH; | 
|  | 2211 | } | 
|  | 2212 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2214 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2215 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2216 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2217 | return; | 
|  | 2218 | } | 
|  | 2219 | eventType = USER_ACTIVITY_EVENT_BUTTON; | 
|  | 2220 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2221 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2222 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2223 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2224 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2225 | LOG_ALWAYS_FATAL("%s events are not user activity", | 
|  | 2226 | EventEntry::typeToString(eventEntry.type)); | 
|  | 2227 | break; | 
|  | 2228 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | } | 
|  | 2230 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2231 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 2232 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2233 | commandEntry->eventTime = eventEntry.eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | commandEntry->userActivityEventType = eventType; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2235 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2236 | } | 
|  | 2237 |  | 
|  | 2238 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2239 | const sp<Connection>& connection, | 
|  | 2240 | EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2241 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2242 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2243 | std::string message = | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2244 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2245 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2246 | ATRACE_NAME(message.c_str()); | 
|  | 2247 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2248 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2249 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2250 | "globalScaleFactor=%f, pointerIds=0x%x %s", | 
|  | 2251 | connection->getInputChannelName().c_str(), inputTarget.flags, | 
|  | 2252 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
|  | 2253 | inputTarget.getPointerInfoString().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2254 | #endif | 
|  | 2255 |  | 
|  | 2256 | // Skip this event if the connection status is not normal. | 
|  | 2257 | // We don't want to enqueue additional outbound events if the connection is broken. | 
|  | 2258 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 2259 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2260 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2261 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2262 | #endif | 
|  | 2263 | return; | 
|  | 2264 | } | 
|  | 2265 |  | 
|  | 2266 | // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2267 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
|  | 2268 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
|  | 2269 | "Entry type %s should not have FLAG_SPLIT", | 
|  | 2270 | EventEntry::typeToString(eventEntry->type)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2271 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2272 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2273 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2274 | MotionEntry* splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2275 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 | if (!splitMotionEntry) { | 
|  | 2277 | return; // split event was dropped | 
|  | 2278 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2279 | if (DEBUG_FOCUS) { | 
|  | 2280 | ALOGD("channel '%s' ~ Split motion event.", | 
|  | 2281 | connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2282 | logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2283 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2284 | enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2285 | splitMotionEntry->release(); | 
|  | 2286 | return; | 
|  | 2287 | } | 
|  | 2288 | } | 
|  | 2289 |  | 
|  | 2290 | // Not splitting.  Enqueue dispatch entries for the event as is. | 
|  | 2291 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
|  | 2292 | } | 
|  | 2293 |  | 
|  | 2294 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2295 | const sp<Connection>& connection, | 
|  | 2296 | EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2297 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2298 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2299 | std::string message = | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2300 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2301 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2302 | ATRACE_NAME(message.c_str()); | 
|  | 2303 | } | 
|  | 2304 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2305 | bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2306 |  | 
|  | 2307 | // Enqueue dispatch entries for the requested modes. | 
| 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_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2310 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2311 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2312 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2313 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2314 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2315 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2316 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2317 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2318 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2319 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2320 |  | 
|  | 2321 | // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2322 | if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2323 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2324 | } | 
|  | 2325 | } | 
|  | 2326 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2327 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
|  | 2328 | EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2329 | const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2330 | int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2331 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2332 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
|  | 2333 | connection->getInputChannelName().c_str(), | 
|  | 2334 | dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2335 | ATRACE_NAME(message.c_str()); | 
|  | 2336 | } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2337 | int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 | if (!(inputTargetFlags & dispatchMode)) { | 
|  | 2339 | return; | 
|  | 2340 | } | 
|  | 2341 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
|  | 2342 |  | 
|  | 2343 | // This is a new event. | 
|  | 2344 | // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2345 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2346 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2348 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
|  | 2349 | // different EventEntry than what was passed in. | 
|  | 2350 | EventEntry* newEntry = dispatchEntry->eventEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2351 | // Apply target flags and update the connection's input state. | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2352 | switch (newEntry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2353 | case EventEntry::Type::KEY: { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2354 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2355 | dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2356 | dispatchEntry->resolvedAction = keyEntry.action; | 
|  | 2357 | dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2359 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
|  | 2360 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2361 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2362 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", | 
|  | 2363 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2365 | return; // skip the inconsistent event | 
|  | 2366 | } | 
|  | 2367 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2368 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2369 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2370 | case EventEntry::Type::MOTION: { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2371 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2372 | // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
|  | 2373 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
|  | 2374 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
|  | 2375 | static_cast<int32_t>(IdGenerator::Source::OTHER); | 
|  | 2376 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2377 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2378 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
|  | 2379 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
|  | 2380 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
|  | 2381 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
|  | 2382 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2383 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
|  | 2384 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
|  | 2385 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
|  | 2386 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
|  | 2387 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2388 | dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2389 | dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2390 | } | 
|  | 2391 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2392 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
|  | 2393 | motionEntry.displayId)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2394 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2395 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " | 
|  | 2396 | "event", | 
|  | 2397 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2398 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2399 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2400 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2401 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2402 | dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2403 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
|  | 2404 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 2405 | } | 
|  | 2406 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
|  | 2407 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2408 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2409 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2410 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
|  | 2411 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2412 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2413 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
|  | 2414 | "event", | 
|  | 2415 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2416 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2417 | return; // skip the inconsistent event | 
|  | 2418 | } | 
|  | 2419 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2420 | dispatchEntry->resolvedEventId = | 
|  | 2421 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
|  | 2422 | ? mIdGenerator.nextId() | 
|  | 2423 | : motionEntry.id; | 
|  | 2424 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
|  | 2425 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
|  | 2426 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 2427 | motionEntry.id, dispatchEntry->resolvedEventId); | 
|  | 2428 | ATRACE_NAME(message.c_str()); | 
|  | 2429 | } | 
|  | 2430 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2431 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2432 | inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2433 |  | 
|  | 2434 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2435 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2436 | case EventEntry::Type::FOCUS: { | 
|  | 2437 | break; | 
|  | 2438 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2439 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2440 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2441 | LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2442 | EventEntry::typeToString(newEntry->type)); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2443 | break; | 
|  | 2444 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2445 | } | 
|  | 2446 |  | 
|  | 2447 | // Remember that we are waiting for this dispatch to complete. | 
|  | 2448 | if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2449 | incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2450 | } | 
|  | 2451 |  | 
|  | 2452 | // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2453 | connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2454 | traceOutboundQueueLength(connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2455 | } | 
|  | 2456 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2457 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2458 | const sp<IBinder>& newToken) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2459 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2460 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
|  | 2461 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2462 | return; | 
|  | 2463 | } | 
|  | 2464 |  | 
|  | 2465 | sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); | 
|  | 2466 | if (inputWindowHandle == nullptr) { | 
|  | 2467 | return; | 
|  | 2468 | } | 
|  | 2469 |  | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2470 | sp<InputWindowHandle> focusedWindowHandle = | 
| Tiger Huang | 0683fe7 | 2019-06-03 21:50:55 +0800 | [diff] [blame] | 2471 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2472 |  | 
|  | 2473 | bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; | 
|  | 2474 |  | 
|  | 2475 | if (!hasFocusChanged) { | 
|  | 2476 | return; | 
|  | 2477 | } | 
|  | 2478 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2479 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 2480 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2481 | commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2482 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2483 | } | 
|  | 2484 |  | 
|  | 2485 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2486 | const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2487 | if (ATRACE_ENABLED()) { | 
|  | 2488 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2489 | connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2490 | ATRACE_NAME(message.c_str()); | 
|  | 2491 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2492 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2493 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2494 | #endif | 
|  | 2495 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2496 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
|  | 2497 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2498 | dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2499 | const nsecs_t timeout = | 
|  | 2500 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
|  | 2501 | dispatchEntry->timeoutTime = currentTime + timeout; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2502 |  | 
|  | 2503 | // Publish the event. | 
|  | 2504 | status_t status; | 
|  | 2505 | EventEntry* eventEntry = dispatchEntry->eventEntry; | 
|  | 2506 | switch (eventEntry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2507 | case EventEntry::Type::KEY: { | 
| Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2508 | const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); | 
|  | 2509 | std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2510 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2511 | // Publish the key event. | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2512 | status = | 
|  | 2513 | connection->inputPublisher | 
|  | 2514 | .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, | 
|  | 2515 | keyEntry->deviceId, keyEntry->source, | 
|  | 2516 | keyEntry->displayId, std::move(hmac), | 
|  | 2517 | dispatchEntry->resolvedAction, | 
|  | 2518 | dispatchEntry->resolvedFlags, keyEntry->keyCode, | 
|  | 2519 | keyEntry->scanCode, keyEntry->metaState, | 
|  | 2520 | keyEntry->repeatCount, keyEntry->downTime, | 
|  | 2521 | keyEntry->eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2522 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2523 | } | 
|  | 2524 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2525 | case EventEntry::Type::MOTION: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2526 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2527 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2528 | PointerCoords scaledCoords[MAX_POINTERS]; | 
|  | 2529 | const PointerCoords* usingCoords = motionEntry->pointerCoords; | 
|  | 2530 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2531 | // Set the X and Y offset and X and Y scale depending on the input source. | 
|  | 2532 | float xOffset = 0.0f, yOffset = 0.0f; | 
|  | 2533 | float xScale = 1.0f, yScale = 1.0f; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2534 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && | 
|  | 2535 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
|  | 2536 | float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2537 | xScale = dispatchEntry->windowXScale; | 
|  | 2538 | yScale = dispatchEntry->windowYScale; | 
|  | 2539 | xOffset = dispatchEntry->xOffset * xScale; | 
|  | 2540 | yOffset = dispatchEntry->yOffset * yScale; | 
|  | 2541 | if (globalScaleFactor != 1.0f) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2542 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { | 
|  | 2543 | scaledCoords[i] = motionEntry->pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2544 | // Don't apply window scale here since we don't want scale to affect raw | 
|  | 2545 | // coordinates. The scale will be sent back to the client and applied | 
|  | 2546 | // later when requesting relative coordinates. | 
|  | 2547 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
|  | 2548 | 1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2549 | } | 
|  | 2550 | usingCoords = scaledCoords; | 
|  | 2551 | } | 
|  | 2552 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2553 | // We don't want the dispatch target to know. | 
|  | 2554 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
|  | 2555 | for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { | 
|  | 2556 | scaledCoords[i].clear(); | 
|  | 2557 | } | 
|  | 2558 | usingCoords = scaledCoords; | 
|  | 2559 | } | 
|  | 2560 | } | 
| Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2561 |  | 
|  | 2562 | std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2563 |  | 
|  | 2564 | // Publish the motion event. | 
|  | 2565 | status = connection->inputPublisher | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2566 | .publishMotionEvent(dispatchEntry->seq, | 
|  | 2567 | dispatchEntry->resolvedEventId, | 
|  | 2568 | motionEntry->deviceId, motionEntry->source, | 
|  | 2569 | motionEntry->displayId, std::move(hmac), | 
|  | 2570 | dispatchEntry->resolvedAction, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2571 | motionEntry->actionButton, | 
|  | 2572 | dispatchEntry->resolvedFlags, | 
|  | 2573 | motionEntry->edgeFlags, motionEntry->metaState, | 
|  | 2574 | motionEntry->buttonState, | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2575 | motionEntry->classification, xScale, yScale, | 
|  | 2576 | xOffset, yOffset, motionEntry->xPrecision, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2577 | motionEntry->yPrecision, | 
|  | 2578 | motionEntry->xCursorPosition, | 
|  | 2579 | motionEntry->yCursorPosition, | 
|  | 2580 | motionEntry->downTime, motionEntry->eventTime, | 
|  | 2581 | motionEntry->pointerCount, | 
|  | 2582 | motionEntry->pointerProperties, usingCoords); | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2583 | reportTouchEventForStatistics(*motionEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2584 | break; | 
|  | 2585 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2586 | case EventEntry::Type::FOCUS: { | 
|  | 2587 | FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); | 
|  | 2588 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2589 | focusEntry->id, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2590 | focusEntry->hasFocus, | 
|  | 2591 | mInTouchMode); | 
|  | 2592 | break; | 
|  | 2593 | } | 
|  | 2594 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2595 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2596 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2597 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
|  | 2598 | EventEntry::typeToString(eventEntry->type)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2599 | return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2600 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2601 | } | 
|  | 2602 |  | 
|  | 2603 | // Check the result. | 
|  | 2604 | if (status) { | 
|  | 2605 | if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2606 | if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2607 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2608 | "This is unexpected because the wait queue is empty, so the pipe " | 
|  | 2609 | "should be empty and we shouldn't have any problems writing an " | 
|  | 2610 | "event to it, status=%d", | 
|  | 2611 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2612 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 2613 | } else { | 
|  | 2614 | // Pipe is full and we are waiting for the app to finish process some events | 
|  | 2615 | // before sending more events to it. | 
|  | 2616 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2617 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2618 | "waiting for the application to catch up", | 
|  | 2619 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | #endif | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2621 | } | 
|  | 2622 | } else { | 
|  | 2623 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2624 | "status=%d", | 
|  | 2625 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2626 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 2627 | } | 
|  | 2628 | return; | 
|  | 2629 | } | 
|  | 2630 |  | 
|  | 2631 | // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2632 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
|  | 2633 | connection->outboundQueue.end(), | 
|  | 2634 | dispatchEntry)); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2635 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2636 | connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2637 | if (connection->responsive) { | 
|  | 2638 | mAnrTracker.insert(dispatchEntry->timeoutTime, | 
|  | 2639 | connection->inputChannel->getConnectionToken()); | 
|  | 2640 | } | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2641 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2642 | } | 
|  | 2643 | } | 
|  | 2644 |  | 
| Edgar Arriaga | 3d61bc1 | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2645 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 2646 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 2647 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
|  | 2648 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
|  | 2649 | // Only sign events up and down events as the purely move events | 
|  | 2650 | // are tied to their up/down counterparts so signing would be redundant. | 
|  | 2651 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
|  | 2652 | verifiedEvent.actionMasked = actionMasked; | 
|  | 2653 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
|  | 2654 | return mHmacKeyManager.sign(verifiedEvent); | 
|  | 2655 | } | 
|  | 2656 | return INVALID_HMAC; | 
|  | 2657 | } | 
|  | 2658 |  | 
|  | 2659 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 2660 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 2661 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
|  | 2662 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
|  | 2663 | verifiedEvent.action = dispatchEntry.resolvedAction; | 
|  | 2664 | return mHmacKeyManager.sign(verifiedEvent); | 
|  | 2665 | } | 
|  | 2666 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2667 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2668 | const sp<Connection>& connection, uint32_t seq, | 
|  | 2669 | bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2670 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2671 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2672 | connection->getInputChannelName().c_str(), seq, toString(handled)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2673 | #endif | 
|  | 2674 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2675 | if (connection->status == Connection::STATUS_BROKEN || | 
|  | 2676 | connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2677 | return; | 
|  | 2678 | } | 
|  | 2679 |  | 
|  | 2680 | // Notify other system components and prepare to start the next dispatch cycle. | 
|  | 2681 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); | 
|  | 2682 | } | 
|  | 2683 |  | 
|  | 2684 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2685 | const sp<Connection>& connection, | 
|  | 2686 | bool notify) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2687 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2688 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2689 | connection->getInputChannelName().c_str(), toString(notify)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2690 | #endif | 
|  | 2691 |  | 
|  | 2692 | // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2693 | drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2694 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2695 | drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2696 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2697 |  | 
|  | 2698 | // The connection appears to be unrecoverably broken. | 
|  | 2699 | // Ignore already broken or zombie connections. | 
|  | 2700 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 2701 | connection->status = Connection::STATUS_BROKEN; | 
|  | 2702 |  | 
|  | 2703 | if (notify) { | 
|  | 2704 | // Notify other system components. | 
|  | 2705 | onDispatchCycleBrokenLocked(currentTime, connection); | 
|  | 2706 | } | 
|  | 2707 | } | 
|  | 2708 | } | 
|  | 2709 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2710 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
|  | 2711 | while (!queue.empty()) { | 
|  | 2712 | DispatchEntry* dispatchEntry = queue.front(); | 
|  | 2713 | queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2714 | releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2715 | } | 
|  | 2716 | } | 
|  | 2717 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2718 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2719 | if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2720 | decrementPendingForegroundDispatches(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2721 | } | 
|  | 2722 | delete dispatchEntry; | 
|  | 2723 | } | 
|  | 2724 |  | 
|  | 2725 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { | 
|  | 2726 | InputDispatcher* d = static_cast<InputDispatcher*>(data); | 
|  | 2727 |  | 
|  | 2728 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2729 | std::scoped_lock _l(d->mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2730 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2731 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2732 | ALOGE("Received spurious receive callback for unknown input channel.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2733 | "fd=%d, events=0x%x", | 
|  | 2734 | fd, events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2735 | return 0; // remove the callback | 
|  | 2736 | } | 
|  | 2737 |  | 
|  | 2738 | bool notify; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2739 | sp<Connection> connection = d->mConnectionsByFd[fd]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2740 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
|  | 2741 | if (!(events & ALOOPER_EVENT_INPUT)) { | 
|  | 2742 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2743 | "events=0x%x", | 
|  | 2744 | connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2745 | return 1; | 
|  | 2746 | } | 
|  | 2747 |  | 
|  | 2748 | nsecs_t currentTime = now(); | 
|  | 2749 | bool gotOne = false; | 
|  | 2750 | status_t status; | 
|  | 2751 | for (;;) { | 
|  | 2752 | uint32_t seq; | 
|  | 2753 | bool handled; | 
|  | 2754 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); | 
|  | 2755 | if (status) { | 
|  | 2756 | break; | 
|  | 2757 | } | 
|  | 2758 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); | 
|  | 2759 | gotOne = true; | 
|  | 2760 | } | 
|  | 2761 | if (gotOne) { | 
|  | 2762 | d->runCommandsLockedInterruptible(); | 
|  | 2763 | if (status == WOULD_BLOCK) { | 
|  | 2764 | return 1; | 
|  | 2765 | } | 
|  | 2766 | } | 
|  | 2767 |  | 
|  | 2768 | notify = status != DEAD_OBJECT || !connection->monitor; | 
|  | 2769 | if (notify) { | 
|  | 2770 | ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2771 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | } | 
|  | 2773 | } else { | 
|  | 2774 | // Monitor channels are never explicitly unregistered. | 
|  | 2775 | // We do it automatically when the remote endpoint is closed so don't warn | 
|  | 2776 | // about them. | 
| arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2777 | const bool stillHaveWindowHandle = | 
|  | 2778 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != | 
|  | 2779 | nullptr; | 
|  | 2780 | notify = !connection->monitor && stillHaveWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2781 | if (notify) { | 
|  | 2782 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2783 | "events=0x%x", | 
|  | 2784 | connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2785 | } | 
|  | 2786 | } | 
|  | 2787 |  | 
|  | 2788 | // Unregister the channel. | 
|  | 2789 | d->unregisterInputChannelLocked(connection->inputChannel, notify); | 
|  | 2790 | return 0; // remove the callback | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2791 | }             // release lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | } | 
|  | 2793 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2794 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2795 | const CancelationOptions& options) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2796 | for (const auto& pair : mConnectionsByFd) { | 
|  | 2797 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2798 | } | 
|  | 2799 | } | 
|  | 2800 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2801 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2802 | const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2803 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
|  | 2804 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
|  | 2805 | } | 
|  | 2806 |  | 
|  | 2807 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
|  | 2808 | const CancelationOptions& options, | 
|  | 2809 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
|  | 2810 | for (const auto& it : monitorsByDisplay) { | 
|  | 2811 | const std::vector<Monitor>& monitors = it.second; | 
|  | 2812 | for (const Monitor& monitor : monitors) { | 
|  | 2813 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2814 | } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2815 | } | 
|  | 2816 | } | 
|  | 2817 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2818 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
|  | 2819 | const sp<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2820 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2821 | if (connection == nullptr) { | 
|  | 2822 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2823 | } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2824 |  | 
|  | 2825 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | } | 
|  | 2827 |  | 
|  | 2828 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
|  | 2829 | const sp<Connection>& connection, const CancelationOptions& options) { | 
|  | 2830 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 2831 | return; | 
|  | 2832 | } | 
|  | 2833 |  | 
|  | 2834 | nsecs_t currentTime = now(); | 
|  | 2835 |  | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2836 | std::vector<EventEntry*> cancelationEvents = | 
|  | 2837 | connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2839 | if (cancelationEvents.empty()) { | 
|  | 2840 | return; | 
|  | 2841 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2842 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2843 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
|  | 2844 | "with reality: %s, mode=%d.", | 
|  | 2845 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
|  | 2846 | options.mode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2847 | #endif | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2848 |  | 
|  | 2849 | InputTarget target; | 
|  | 2850 | sp<InputWindowHandle> windowHandle = | 
|  | 2851 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 2852 | if (windowHandle != nullptr) { | 
|  | 2853 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 2854 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, | 
|  | 2855 | windowInfo->windowXScale, windowInfo->windowYScale); | 
|  | 2856 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2857 | } | 
|  | 2858 | target.inputChannel = connection->inputChannel; | 
|  | 2859 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2860 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2861 | for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
|  | 2862 | EventEntry* cancelationEventEntry = cancelationEvents[i]; | 
|  | 2863 | switch (cancelationEventEntry->type) { | 
|  | 2864 | case EventEntry::Type::KEY: { | 
|  | 2865 | logOutboundKeyDetails("cancel - ", | 
|  | 2866 | static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
|  | 2867 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2869 | case EventEntry::Type::MOTION: { | 
|  | 2870 | logOutboundMotionDetails("cancel - ", | 
|  | 2871 | static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
|  | 2872 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2874 | case EventEntry::Type::FOCUS: { | 
|  | 2875 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); | 
|  | 2876 | break; | 
|  | 2877 | } | 
|  | 2878 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2879 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2880 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
|  | 2881 | EventEntry::typeToString(cancelationEventEntry->type)); | 
|  | 2882 | break; | 
|  | 2883 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2884 | } | 
|  | 2885 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2886 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref | 
|  | 2887 | target, InputTarget::FLAG_DISPATCH_AS_IS); | 
|  | 2888 |  | 
|  | 2889 | cancelationEventEntry->release(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2891 |  | 
|  | 2892 | startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2893 | } | 
|  | 2894 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2895 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
|  | 2896 | const sp<Connection>& connection) { | 
|  | 2897 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 2898 | return; | 
|  | 2899 | } | 
|  | 2900 |  | 
|  | 2901 | nsecs_t currentTime = now(); | 
|  | 2902 |  | 
|  | 2903 | std::vector<EventEntry*> downEvents = | 
|  | 2904 | connection->inputState.synthesizePointerDownEvents(currentTime); | 
|  | 2905 |  | 
|  | 2906 | if (downEvents.empty()) { | 
|  | 2907 | return; | 
|  | 2908 | } | 
|  | 2909 |  | 
|  | 2910 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 2911 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
|  | 2912 | connection->getInputChannelName().c_str(), downEvents.size()); | 
|  | 2913 | #endif | 
|  | 2914 |  | 
|  | 2915 | InputTarget target; | 
|  | 2916 | sp<InputWindowHandle> windowHandle = | 
|  | 2917 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 2918 | if (windowHandle != nullptr) { | 
|  | 2919 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 2920 | target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, | 
|  | 2921 | windowInfo->windowXScale, windowInfo->windowYScale); | 
|  | 2922 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2923 | } | 
|  | 2924 | target.inputChannel = connection->inputChannel; | 
|  | 2925 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2926 |  | 
|  | 2927 | for (EventEntry* downEventEntry : downEvents) { | 
|  | 2928 | switch (downEventEntry->type) { | 
|  | 2929 | case EventEntry::Type::MOTION: { | 
|  | 2930 | logOutboundMotionDetails("down - ", | 
|  | 2931 | static_cast<const MotionEntry&>(*downEventEntry)); | 
|  | 2932 | break; | 
|  | 2933 | } | 
|  | 2934 |  | 
|  | 2935 | case EventEntry::Type::KEY: | 
|  | 2936 | case EventEntry::Type::FOCUS: | 
|  | 2937 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2938 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2939 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
|  | 2940 | EventEntry::typeToString(downEventEntry->type)); | 
|  | 2941 | break; | 
|  | 2942 | } | 
|  | 2943 | } | 
|  | 2944 |  | 
|  | 2945 | enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref | 
|  | 2946 | target, InputTarget::FLAG_DISPATCH_AS_IS); | 
|  | 2947 |  | 
|  | 2948 | downEventEntry->release(); | 
|  | 2949 | } | 
|  | 2950 |  | 
|  | 2951 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2952 | } | 
|  | 2953 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2954 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2955 | BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2956 | ALOG_ASSERT(pointerIds.value != 0); | 
|  | 2957 |  | 
|  | 2958 | uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
|  | 2959 | PointerProperties splitPointerProperties[MAX_POINTERS]; | 
|  | 2960 | PointerCoords splitPointerCoords[MAX_POINTERS]; | 
|  | 2961 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2962 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2963 | uint32_t splitPointerCount = 0; | 
|  | 2964 |  | 
|  | 2965 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2966 | originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2967 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2968 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2969 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 2970 | if (pointerIds.hasBit(pointerId)) { | 
|  | 2971 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
|  | 2972 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
|  | 2973 | splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2974 | originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2975 | splitPointerCount += 1; | 
|  | 2976 | } | 
|  | 2977 | } | 
|  | 2978 |  | 
|  | 2979 | if (splitPointerCount != pointerIds.count()) { | 
|  | 2980 | // This is bad.  We are missing some of the pointers that we expected to deliver. | 
|  | 2981 | // Most likely this indicates that we received an ACTION_MOVE events that has | 
|  | 2982 | // different pointer ids than we expected based on the previous ACTION_DOWN | 
|  | 2983 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
|  | 2984 | // in this way. | 
|  | 2985 | ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2986 | "we expected there to be %d pointers.  This probably means we received " | 
|  | 2987 | "a broken sequence of pointer ids from the input device.", | 
|  | 2988 | splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2989 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2990 | } | 
|  | 2991 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2992 | int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2993 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2994 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
|  | 2995 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2996 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 2997 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2998 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2999 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3000 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3001 | if (pointerIds.count() == 1) { | 
|  | 3002 | // The first/last pointer went down/up. | 
|  | 3003 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3004 | ? AMOTION_EVENT_ACTION_DOWN | 
|  | 3005 | : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3006 | } else { | 
|  | 3007 | // A secondary pointer went down/up. | 
|  | 3008 | uint32_t splitPointerIndex = 0; | 
|  | 3009 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
|  | 3010 | splitPointerIndex += 1; | 
|  | 3011 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3012 | action = maskedAction | | 
|  | 3013 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3014 | } | 
|  | 3015 | } else { | 
|  | 3016 | // An unrelated pointer changed. | 
|  | 3017 | action = AMOTION_EVENT_ACTION_MOVE; | 
|  | 3018 | } | 
|  | 3019 | } | 
|  | 3020 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3021 | int32_t newId = mIdGenerator.nextId(); | 
|  | 3022 | if (ATRACE_ENABLED()) { | 
|  | 3023 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
|  | 3024 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3025 | originalMotionEntry.id, newId); | 
|  | 3026 | ATRACE_NAME(message.c_str()); | 
|  | 3027 | } | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3028 | MotionEntry* splitMotionEntry = | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3029 | new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, | 
|  | 3030 | originalMotionEntry.source, originalMotionEntry.displayId, | 
|  | 3031 | originalMotionEntry.policyFlags, action, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3032 | originalMotionEntry.actionButton, originalMotionEntry.flags, | 
|  | 3033 | originalMotionEntry.metaState, originalMotionEntry.buttonState, | 
|  | 3034 | originalMotionEntry.classification, originalMotionEntry.edgeFlags, | 
|  | 3035 | originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, | 
|  | 3036 | originalMotionEntry.xCursorPosition, | 
|  | 3037 | originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3038 | splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3039 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3040 | if (originalMotionEntry.injectionState) { | 
|  | 3041 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | splitMotionEntry->injectionState->refCount += 1; | 
|  | 3043 | } | 
|  | 3044 |  | 
|  | 3045 | return splitMotionEntry; | 
|  | 3046 | } | 
|  | 3047 |  | 
|  | 3048 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
|  | 3049 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3050 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3051 | #endif | 
|  | 3052 |  | 
|  | 3053 | bool needWake; | 
|  | 3054 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3055 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3056 |  | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3057 | ConfigurationChangedEntry* newEntry = | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3058 | new ConfigurationChangedEntry(args->id, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3059 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3060 | } // release lock | 
|  | 3061 |  | 
|  | 3062 | if (needWake) { | 
|  | 3063 | mLooper->wake(); | 
|  | 3064 | } | 
|  | 3065 | } | 
|  | 3066 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3067 | /** | 
|  | 3068 | * If one of the meta shortcuts is detected, process them here: | 
|  | 3069 | *     Meta + Backspace -> generate BACK | 
|  | 3070 | *     Meta + Enter -> generate HOME | 
|  | 3071 | * This will potentially overwrite keyCode and metaState. | 
|  | 3072 | */ | 
|  | 3073 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3074 | int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3075 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
|  | 3076 | int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
|  | 3077 | if (keyCode == AKEYCODE_DEL) { | 
|  | 3078 | newKeyCode = AKEYCODE_BACK; | 
|  | 3079 | } else if (keyCode == AKEYCODE_ENTER) { | 
|  | 3080 | newKeyCode = AKEYCODE_HOME; | 
|  | 3081 | } | 
|  | 3082 | if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3083 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3084 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3085 | mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3086 | keyCode = newKeyCode; | 
|  | 3087 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3088 | } | 
|  | 3089 | } else if (action == AKEY_EVENT_ACTION_UP) { | 
|  | 3090 | // In order to maintain a consistent stream of up and down events, check to see if the key | 
|  | 3091 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
|  | 3092 | // 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] | 3093 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3094 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3095 | auto replacementIt = mReplacedKeys.find(replacement); | 
|  | 3096 | if (replacementIt != mReplacedKeys.end()) { | 
|  | 3097 | keyCode = replacementIt->second; | 
|  | 3098 | mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3099 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3100 | } | 
|  | 3101 | } | 
|  | 3102 | } | 
|  | 3103 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3104 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
|  | 3105 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3106 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 3107 | "policyFlags=0x%x, action=0x%x, " | 
|  | 3108 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
|  | 3109 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
|  | 3110 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
|  | 3111 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3112 | #endif | 
|  | 3113 | if (!validateKeyEvent(args->action)) { | 
|  | 3114 | return; | 
|  | 3115 | } | 
|  | 3116 |  | 
|  | 3117 | uint32_t policyFlags = args->policyFlags; | 
|  | 3118 | int32_t flags = args->flags; | 
|  | 3119 | int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3120 | // InputDispatcher tracks and generates key repeats on behalf of | 
|  | 3121 | // whatever notifies it, so repeatCount should always be set to 0 | 
|  | 3122 | constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3123 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
|  | 3124 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 3125 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
|  | 3126 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3127 | if (policyFlags & POLICY_FLAG_FUNCTION) { | 
|  | 3128 | metaState |= AMETA_FUNCTION_ON; | 
|  | 3129 | } | 
|  | 3130 |  | 
|  | 3131 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3132 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3133 | int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3134 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3135 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3136 | KeyEvent event; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3137 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3138 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3139 | args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3140 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3141 | android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3142 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3143 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3144 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3145 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3146 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3147 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3148 | bool needWake; | 
|  | 3149 | { // acquire lock | 
|  | 3150 | mLock.lock(); | 
|  | 3151 |  | 
|  | 3152 | if (shouldSendKeyToInputFilterLocked(args)) { | 
|  | 3153 | mLock.unlock(); | 
|  | 3154 |  | 
|  | 3155 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3156 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3157 | return; // event was consumed by the filter | 
|  | 3158 | } | 
|  | 3159 |  | 
|  | 3160 | mLock.lock(); | 
|  | 3161 | } | 
|  | 3162 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3163 | KeyEntry* newEntry = | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3164 | new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3165 | args->displayId, policyFlags, args->action, flags, keyCode, | 
|  | 3166 | args->scanCode, metaState, repeatCount, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3167 |  | 
|  | 3168 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3169 | mLock.unlock(); | 
|  | 3170 | } // release lock | 
|  | 3171 |  | 
|  | 3172 | if (needWake) { | 
|  | 3173 | mLooper->wake(); | 
|  | 3174 | } | 
|  | 3175 | } | 
|  | 3176 |  | 
|  | 3177 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
|  | 3178 | return mInputFilterEnabled; | 
|  | 3179 | } | 
|  | 3180 |  | 
|  | 3181 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
|  | 3182 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3183 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3184 | "displayId=%" PRId32 ", policyFlags=0x%x, " | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3185 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
|  | 3186 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
| Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3187 | "yCursorPosition=%f, downTime=%" PRId64, | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3188 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
|  | 3189 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
|  | 3190 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
|  | 3191 | args->xCursorPosition, args->yCursorPosition, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3192 | for (uint32_t i = 0; i < args->pointerCount; i++) { | 
|  | 3193 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3194 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 3195 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 3196 | "orientation=%f", | 
|  | 3197 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
|  | 3198 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3199 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3200 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 3201 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 3202 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 3203 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 3204 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 3205 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 3206 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3207 | } | 
|  | 3208 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3209 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
|  | 3210 | args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3211 | return; | 
|  | 3212 | } | 
|  | 3213 |  | 
|  | 3214 | uint32_t policyFlags = args->policyFlags; | 
|  | 3215 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3216 |  | 
|  | 3217 | android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3218 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3219 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3220 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3221 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3222 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3223 |  | 
|  | 3224 | bool needWake; | 
|  | 3225 | { // acquire lock | 
|  | 3226 | mLock.lock(); | 
|  | 3227 |  | 
|  | 3228 | if (shouldSendMotionToInputFilterLocked(args)) { | 
|  | 3229 | mLock.unlock(); | 
|  | 3230 |  | 
|  | 3231 | MotionEvent event; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3232 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
|  | 3233 | args->action, args->actionButton, args->flags, args->edgeFlags, | 
|  | 3234 | args->metaState, args->buttonState, args->classification, 1 /*xScale*/, | 
|  | 3235 | 1 /*yScale*/, 0 /* xOffset */, 0 /* yOffset */, args->xPrecision, | 
|  | 3236 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, | 
|  | 3237 | args->downTime, args->eventTime, args->pointerCount, | 
|  | 3238 | args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3239 |  | 
|  | 3240 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3241 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3242 | return; // event was consumed by the filter | 
|  | 3243 | } | 
|  | 3244 |  | 
|  | 3245 | mLock.lock(); | 
|  | 3246 | } | 
|  | 3247 |  | 
|  | 3248 | // Just enqueue a new motion event. | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3249 | MotionEntry* newEntry = | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3250 | new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3251 | args->displayId, policyFlags, args->action, args->actionButton, | 
|  | 3252 | args->flags, args->metaState, args->buttonState, | 
|  | 3253 | args->classification, args->edgeFlags, args->xPrecision, | 
|  | 3254 | args->yPrecision, args->xCursorPosition, args->yCursorPosition, | 
|  | 3255 | args->downTime, args->pointerCount, args->pointerProperties, | 
|  | 3256 | args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 |  | 
|  | 3258 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3259 | mLock.unlock(); | 
|  | 3260 | } // release lock | 
|  | 3261 |  | 
|  | 3262 | if (needWake) { | 
|  | 3263 | mLooper->wake(); | 
|  | 3264 | } | 
|  | 3265 | } | 
|  | 3266 |  | 
|  | 3267 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3268 | return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3269 | } | 
|  | 3270 |  | 
|  | 3271 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
|  | 3272 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3273 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3274 | "switchMask=0x%08x", | 
|  | 3275 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3276 | #endif | 
|  | 3277 |  | 
|  | 3278 | uint32_t policyFlags = args->policyFlags; | 
|  | 3279 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3280 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | } | 
|  | 3282 |  | 
|  | 3283 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
|  | 3284 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3285 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
|  | 3286 | args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3287 | #endif | 
|  | 3288 |  | 
|  | 3289 | bool needWake; | 
|  | 3290 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3291 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3292 |  | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3293 | DeviceResetEntry* newEntry = | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3294 | new DeviceResetEntry(args->id, args->eventTime, args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3295 | needWake = enqueueInboundEventLocked(newEntry); | 
|  | 3296 | } // release lock | 
|  | 3297 |  | 
|  | 3298 | if (needWake) { | 
|  | 3299 | mLooper->wake(); | 
|  | 3300 | } | 
|  | 3301 | } | 
|  | 3302 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3303 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, | 
|  | 3304 | int32_t injectorUid, int32_t syncMode, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3305 | std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3306 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3307 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3308 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
|  | 3309 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3310 | #endif | 
|  | 3311 |  | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3312 | 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] | 3313 |  | 
|  | 3314 | policyFlags |= POLICY_FLAG_INJECTED; | 
|  | 3315 | if (hasInjectionPermission(injectorPid, injectorUid)) { | 
|  | 3316 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3317 | } | 
|  | 3318 |  | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3319 | std::queue<EventEntry*> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3320 | switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3321 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3322 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
|  | 3323 | int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3324 | if (!validateKeyEvent(action)) { | 
|  | 3325 | return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3326 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3328 | int32_t flags = incomingKey.getFlags(); | 
|  | 3329 | int32_t keyCode = incomingKey.getKeyCode(); | 
|  | 3330 | int32_t metaState = incomingKey.getMetaState(); | 
|  | 3331 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3332 | /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3333 | KeyEvent keyEvent; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3334 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3335 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
|  | 3336 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
|  | 3337 | incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3338 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3339 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
|  | 3340 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3341 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3342 |  | 
|  | 3343 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3344 | android::base::Timer t; | 
|  | 3345 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
|  | 3346 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3347 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
|  | 3348 | std::to_string(t.duration().count()).c_str()); | 
|  | 3349 | } | 
|  | 3350 | } | 
|  | 3351 |  | 
|  | 3352 | mLock.lock(); | 
|  | 3353 | KeyEntry* injectedEntry = | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3354 | new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), | 
|  | 3355 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3356 | incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, | 
|  | 3357 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3358 | incomingKey.getDownTime()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3359 | injectedEntries.push(injectedEntry); | 
|  | 3360 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3361 | } | 
|  | 3362 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3363 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3364 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); | 
|  | 3365 | int32_t action = motionEvent->getAction(); | 
|  | 3366 | size_t pointerCount = motionEvent->getPointerCount(); | 
|  | 3367 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); | 
|  | 3368 | int32_t actionButton = motionEvent->getActionButton(); | 
|  | 3369 | int32_t displayId = motionEvent->getDisplayId(); | 
|  | 3370 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
|  | 3371 | return INPUT_EVENT_INJECTION_FAILED; | 
|  | 3372 | } | 
|  | 3373 |  | 
|  | 3374 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3375 | nsecs_t eventTime = motionEvent->getEventTime(); | 
|  | 3376 | android::base::Timer t; | 
|  | 3377 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
|  | 3378 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3379 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
|  | 3380 | std::to_string(t.duration().count()).c_str()); | 
|  | 3381 | } | 
|  | 3382 | } | 
|  | 3383 |  | 
|  | 3384 | mLock.lock(); | 
|  | 3385 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); | 
|  | 3386 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); | 
|  | 3387 | MotionEntry* injectedEntry = | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3388 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, | 
|  | 3389 | motionEvent->getSource(), motionEvent->getDisplayId(), | 
|  | 3390 | policyFlags, action, actionButton, motionEvent->getFlags(), | 
|  | 3391 | motionEvent->getMetaState(), motionEvent->getButtonState(), | 
|  | 3392 | motionEvent->getClassification(), motionEvent->getEdgeFlags(), | 
|  | 3393 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3394 | motionEvent->getRawXCursorPosition(), | 
|  | 3395 | motionEvent->getRawYCursorPosition(), | 
|  | 3396 | motionEvent->getDownTime(), uint32_t(pointerCount), | 
|  | 3397 | pointerProperties, samplePointerCoords, | 
|  | 3398 | motionEvent->getXOffset(), motionEvent->getYOffset()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3399 | injectedEntries.push(injectedEntry); | 
|  | 3400 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { | 
|  | 3401 | sampleEventTimes += 1; | 
|  | 3402 | samplePointerCoords += pointerCount; | 
|  | 3403 | MotionEntry* nextInjectedEntry = | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3404 | new MotionEntry(motionEvent->getId(), *sampleEventTimes, | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3405 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3406 | motionEvent->getDisplayId(), policyFlags, action, | 
|  | 3407 | actionButton, motionEvent->getFlags(), | 
|  | 3408 | motionEvent->getMetaState(), motionEvent->getButtonState(), | 
|  | 3409 | motionEvent->getClassification(), | 
|  | 3410 | motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), | 
|  | 3411 | motionEvent->getYPrecision(), | 
|  | 3412 | motionEvent->getRawXCursorPosition(), | 
|  | 3413 | motionEvent->getRawYCursorPosition(), | 
|  | 3414 | motionEvent->getDownTime(), uint32_t(pointerCount), | 
|  | 3415 | pointerProperties, samplePointerCoords, | 
|  | 3416 | motionEvent->getXOffset(), motionEvent->getYOffset()); | 
|  | 3417 | injectedEntries.push(nextInjectedEntry); | 
|  | 3418 | } | 
|  | 3419 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3420 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3421 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3422 | default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3423 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3424 | return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3425 | } | 
|  | 3426 |  | 
|  | 3427 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
|  | 3428 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { | 
|  | 3429 | injectionState->injectionIsAsync = true; | 
|  | 3430 | } | 
|  | 3431 |  | 
|  | 3432 | injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3433 | injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3434 |  | 
|  | 3435 | bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3436 | while (!injectedEntries.empty()) { | 
|  | 3437 | needWake |= enqueueInboundEventLocked(injectedEntries.front()); | 
|  | 3438 | injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3439 | } | 
|  | 3440 |  | 
|  | 3441 | mLock.unlock(); | 
|  | 3442 |  | 
|  | 3443 | if (needWake) { | 
|  | 3444 | mLooper->wake(); | 
|  | 3445 | } | 
|  | 3446 |  | 
|  | 3447 | int32_t injectionResult; | 
|  | 3448 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3449 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3450 |  | 
|  | 3451 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { | 
|  | 3452 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; | 
|  | 3453 | } else { | 
|  | 3454 | for (;;) { | 
|  | 3455 | injectionResult = injectionState->injectionResult; | 
|  | 3456 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { | 
|  | 3457 | break; | 
|  | 3458 | } | 
|  | 3459 |  | 
|  | 3460 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 3461 | if (remainingTimeout <= 0) { | 
|  | 3462 | #if DEBUG_INJECTION | 
|  | 3463 | ALOGD("injectInputEvent - Timed out waiting for injection result " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3464 | "to become available."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3465 | #endif | 
|  | 3466 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; | 
|  | 3467 | break; | 
|  | 3468 | } | 
|  | 3469 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3470 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3471 | } | 
|  | 3472 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3473 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && | 
|  | 3474 | syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3475 | while (injectionState->pendingForegroundDispatches != 0) { | 
|  | 3476 | #if DEBUG_INJECTION | 
|  | 3477 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3478 | injectionState->pendingForegroundDispatches); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | #endif | 
|  | 3480 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 3481 | if (remainingTimeout <= 0) { | 
|  | 3482 | #if DEBUG_INJECTION | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3483 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
|  | 3484 | "dispatches to finish."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3485 | #endif | 
|  | 3486 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; | 
|  | 3487 | break; | 
|  | 3488 | } | 
|  | 3489 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3490 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | } | 
|  | 3492 | } | 
|  | 3493 | } | 
|  | 3494 |  | 
|  | 3495 | injectionState->release(); | 
|  | 3496 | } // release lock | 
|  | 3497 |  | 
|  | 3498 | #if DEBUG_INJECTION | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3499 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3500 | injectionResult, injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3501 | #endif | 
|  | 3502 |  | 
|  | 3503 | return injectionResult; | 
|  | 3504 | } | 
|  | 3505 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3506 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3507 | std::array<uint8_t, 32> calculatedHmac; | 
|  | 3508 | std::unique_ptr<VerifiedInputEvent> result; | 
|  | 3509 | switch (event.getType()) { | 
|  | 3510 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 3511 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 3512 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
|  | 3513 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
|  | 3514 | calculatedHmac = mHmacKeyManager.sign(verifiedKeyEvent); | 
|  | 3515 | break; | 
|  | 3516 | } | 
|  | 3517 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3518 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 3519 | VerifiedMotionEvent verifiedMotionEvent = | 
|  | 3520 | verifiedMotionEventFromMotionEvent(motionEvent); | 
|  | 3521 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
|  | 3522 | calculatedHmac = mHmacKeyManager.sign(verifiedMotionEvent); | 
|  | 3523 | break; | 
|  | 3524 | } | 
|  | 3525 | default: { | 
|  | 3526 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
|  | 3527 | return nullptr; | 
|  | 3528 | } | 
|  | 3529 | } | 
|  | 3530 | if (calculatedHmac == INVALID_HMAC) { | 
|  | 3531 | return nullptr; | 
|  | 3532 | } | 
|  | 3533 | if (calculatedHmac != event.getHmac()) { | 
|  | 3534 | return nullptr; | 
|  | 3535 | } | 
|  | 3536 | return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3537 | } | 
|  | 3538 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3539 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3540 | return injectorUid == 0 || | 
|  | 3541 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | } | 
|  | 3543 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3544 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3545 | InjectionState* injectionState = entry->injectionState; | 
|  | 3546 | if (injectionState) { | 
|  | 3547 | #if DEBUG_INJECTION | 
|  | 3548 | ALOGD("Setting input event injection result to %d.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3549 | "injectorPid=%d, injectorUid=%d", | 
|  | 3550 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3551 | #endif | 
|  | 3552 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3553 | if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 | // Log the outcome since the injector did not wait for the injection result. | 
|  | 3555 | switch (injectionResult) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3556 | case INPUT_EVENT_INJECTION_SUCCEEDED: | 
|  | 3557 | ALOGV("Asynchronous input event injection succeeded."); | 
|  | 3558 | break; | 
|  | 3559 | case INPUT_EVENT_INJECTION_FAILED: | 
|  | 3560 | ALOGW("Asynchronous input event injection failed."); | 
|  | 3561 | break; | 
|  | 3562 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: | 
|  | 3563 | ALOGW("Asynchronous input event injection permission denied."); | 
|  | 3564 | break; | 
|  | 3565 | case INPUT_EVENT_INJECTION_TIMED_OUT: | 
|  | 3566 | ALOGW("Asynchronous input event injection timed out."); | 
|  | 3567 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3568 | } | 
|  | 3569 | } | 
|  | 3570 |  | 
|  | 3571 | injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3572 | mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3573 | } | 
|  | 3574 | } | 
|  | 3575 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3576 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3577 | InjectionState* injectionState = entry->injectionState; | 
|  | 3578 | if (injectionState) { | 
|  | 3579 | injectionState->pendingForegroundDispatches += 1; | 
|  | 3580 | } | 
|  | 3581 | } | 
|  | 3582 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3583 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3584 | InjectionState* injectionState = entry->injectionState; | 
|  | 3585 | if (injectionState) { | 
|  | 3586 | injectionState->pendingForegroundDispatches -= 1; | 
|  | 3587 |  | 
|  | 3588 | if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3589 | mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3590 | } | 
|  | 3591 | } | 
|  | 3592 | } | 
|  | 3593 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3594 | std::vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked( | 
|  | 3595 | int32_t displayId) const { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3596 | return getValueByKey(mWindowHandlesByDisplay, displayId); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3597 | } | 
|  | 3598 |  | 
| Siarhei Vishniakou | 265ab01 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3599 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
|  | 3600 | return getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
|  | 3601 | } | 
|  | 3602 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3603 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3604 | const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3605 | if (windowHandleToken == nullptr) { | 
|  | 3606 | return nullptr; | 
|  | 3607 | } | 
|  | 3608 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3609 | for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3610 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
|  | 3611 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3612 | if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3613 | return windowHandle; | 
|  | 3614 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3615 | } | 
|  | 3616 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3617 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3618 | } | 
|  | 3619 |  | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3620 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 3621 | for (auto& it : mWindowHandlesByDisplay) { | 
|  | 3622 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
|  | 3623 | for (const sp<InputWindowHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3624 | if (handle->getId() == windowHandle->getId() && | 
|  | 3625 | handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3626 | if (windowHandle->getInfo()->displayId != it.first) { | 
|  | 3627 | ALOGE("Found window %s in display %" PRId32 | 
|  | 3628 | ", but it should belong to display %" PRId32, | 
|  | 3629 | windowHandle->getName().c_str(), it.first, | 
|  | 3630 | windowHandle->getInfo()->displayId); | 
|  | 3631 | } | 
|  | 3632 | return true; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3633 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3634 | } | 
|  | 3635 | } | 
|  | 3636 | return false; | 
|  | 3637 | } | 
|  | 3638 |  | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3639 | sp<InputChannel> InputDispatcher::getInputChannelLocked(const sp<IBinder>& token) const { | 
|  | 3640 | size_t count = mInputChannelsByToken.count(token); | 
|  | 3641 | if (count == 0) { | 
|  | 3642 | return nullptr; | 
|  | 3643 | } | 
|  | 3644 | return mInputChannelsByToken.at(token); | 
|  | 3645 | } | 
|  | 3646 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3647 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
|  | 3648 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
|  | 3649 | if (inputWindowHandles.empty()) { | 
|  | 3650 | // Remove all handles on a display if there are no windows left. | 
|  | 3651 | mWindowHandlesByDisplay.erase(displayId); | 
|  | 3652 | return; | 
|  | 3653 | } | 
|  | 3654 |  | 
|  | 3655 | // Since we compare the pointer of input window handles across window updates, we need | 
|  | 3656 | // to make sure the handle object for the same window stays unchanged across updates. | 
|  | 3657 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3658 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3659 | for (const sp<InputWindowHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3660 | oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3661 | } | 
|  | 3662 |  | 
|  | 3663 | std::vector<sp<InputWindowHandle>> newHandles; | 
|  | 3664 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { | 
|  | 3665 | if (!handle->updateInfo()) { | 
|  | 3666 | // handle no longer valid | 
|  | 3667 | continue; | 
|  | 3668 | } | 
|  | 3669 |  | 
|  | 3670 | const InputWindowInfo* info = handle->getInfo(); | 
|  | 3671 | if ((getInputChannelLocked(handle->getToken()) == nullptr && | 
|  | 3672 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { | 
|  | 3673 | const bool noInputChannel = | 
|  | 3674 | info->inputFeatures & InputWindowInfo::INPUT_FEATURE_NO_INPUT_CHANNEL; | 
|  | 3675 | const bool canReceiveInput = | 
|  | 3676 | !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_TOUCHABLE) || | 
|  | 3677 | !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_FOCUSABLE); | 
|  | 3678 | if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3679 | ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3680 | handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3681 | continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3682 | } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3683 | } | 
|  | 3684 |  | 
|  | 3685 | if (info->displayId != displayId) { | 
|  | 3686 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
|  | 3687 | handle->getName().c_str(), displayId, info->displayId); | 
|  | 3688 | continue; | 
|  | 3689 | } | 
|  | 3690 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3691 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
|  | 3692 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3693 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3694 | oldHandle->updateFrom(handle); | 
|  | 3695 | newHandles.push_back(oldHandle); | 
|  | 3696 | } else { | 
|  | 3697 | newHandles.push_back(handle); | 
|  | 3698 | } | 
|  | 3699 | } | 
|  | 3700 |  | 
|  | 3701 | // Insert or replace | 
|  | 3702 | mWindowHandlesByDisplay[displayId] = newHandles; | 
|  | 3703 | } | 
|  | 3704 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3705 | void InputDispatcher::setInputWindows( | 
|  | 3706 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { | 
|  | 3707 | { // acquire lock | 
|  | 3708 | std::scoped_lock _l(mLock); | 
|  | 3709 | for (auto const& i : handlesPerDisplay) { | 
|  | 3710 | setInputWindowsLocked(i.second, i.first); | 
|  | 3711 | } | 
|  | 3712 | } | 
|  | 3713 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3714 | mLooper->wake(); | 
|  | 3715 | } | 
|  | 3716 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3717 | /** | 
|  | 3718 | * Called from InputManagerService, update window handle list by displayId that can receive input. | 
|  | 3719 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
|  | 3720 | * If set an empty list, remove all handles from the specific display. | 
|  | 3721 | * For focused handle, check if need to change and send a cancel event to previous one. | 
|  | 3722 | * For removed handle, check if need to send a cancel event if already in touch. | 
|  | 3723 | */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3724 | void InputDispatcher::setInputWindowsLocked( | 
|  | 3725 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3726 | if (DEBUG_FOCUS) { | 
|  | 3727 | std::string windowList; | 
|  | 3728 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { | 
|  | 3729 | windowList += iwh->getName() + " "; | 
|  | 3730 | } | 
|  | 3731 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
|  | 3732 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3733 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3734 | // Copy old handles for release if they are no longer present. | 
|  | 3735 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3737 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3738 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3739 | sp<InputWindowHandle> newFocusedWindowHandle = nullptr; | 
|  | 3740 | bool foundHoveredWindow = false; | 
|  | 3741 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
|  | 3742 | // Set newFocusedWindowHandle to the top most focused window instead of the last one | 
|  | 3743 | if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus && | 
|  | 3744 | windowHandle->getInfo()->visible) { | 
|  | 3745 | newFocusedWindowHandle = windowHandle; | 
|  | 3746 | } | 
|  | 3747 | if (windowHandle == mLastHoverWindowHandle) { | 
|  | 3748 | foundHoveredWindow = true; | 
|  | 3749 | } | 
|  | 3750 | } | 
|  | 3751 |  | 
|  | 3752 | if (!foundHoveredWindow) { | 
|  | 3753 | mLastHoverWindowHandle = nullptr; | 
|  | 3754 | } | 
|  | 3755 |  | 
|  | 3756 | sp<InputWindowHandle> oldFocusedWindowHandle = | 
|  | 3757 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
|  | 3758 |  | 
|  | 3759 | if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) { | 
|  | 3760 | if (oldFocusedWindowHandle != nullptr) { | 
|  | 3761 | if (DEBUG_FOCUS) { | 
|  | 3762 | ALOGD("Focus left window: %s in display %" PRId32, | 
|  | 3763 | oldFocusedWindowHandle->getName().c_str(), displayId); | 
| Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3764 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3765 | sp<InputChannel> focusedInputChannel = | 
|  | 3766 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); | 
|  | 3767 | if (focusedInputChannel != nullptr) { | 
|  | 3768 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 3769 | "focus left window"); | 
|  | 3770 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
|  | 3771 | enqueueFocusEventLocked(*oldFocusedWindowHandle, false /*hasFocus*/); | 
| Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3772 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3773 | mFocusedWindowHandlesByDisplay.erase(displayId); | 
|  | 3774 | } | 
|  | 3775 | if (newFocusedWindowHandle != nullptr) { | 
|  | 3776 | if (DEBUG_FOCUS) { | 
|  | 3777 | ALOGD("Focus entered window: %s in display %" PRId32, | 
|  | 3778 | newFocusedWindowHandle->getName().c_str(), displayId); | 
|  | 3779 | } | 
|  | 3780 | mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; | 
|  | 3781 | enqueueFocusEventLocked(*newFocusedWindowHandle, true /*hasFocus*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3782 | } | 
|  | 3783 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3784 | if (mFocusedDisplayId == displayId) { | 
|  | 3785 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3786 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3787 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3788 |  | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3789 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 3790 | mTouchStatesByDisplay.find(displayId); | 
|  | 3791 | if (stateIt != mTouchStatesByDisplay.end()) { | 
|  | 3792 | TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3793 | for (size_t i = 0; i < state.windows.size();) { | 
|  | 3794 | TouchedWindow& touchedWindow = state.windows[i]; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3795 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3796 | if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3797 | ALOGD("Touched window was removed: %s in display %" PRId32, | 
|  | 3798 | touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3799 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3800 | sp<InputChannel> touchedInputChannel = | 
|  | 3801 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
|  | 3802 | if (touchedInputChannel != nullptr) { | 
|  | 3803 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 3804 | "touched window was removed"); | 
|  | 3805 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3806 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3807 | state.windows.erase(state.windows.begin() + i); | 
|  | 3808 | } else { | 
|  | 3809 | ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3810 | } | 
|  | 3811 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3812 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3813 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3814 | // Release information for windows that are no longer present. | 
|  | 3815 | // This ensures that unused input channels are released promptly. | 
|  | 3816 | // Otherwise, they might stick around until the window handle is destroyed | 
|  | 3817 | // which might not happen until the next GC. | 
|  | 3818 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3819 | if (!hasWindowHandleLocked(oldWindowHandle)) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3820 | if (DEBUG_FOCUS) { | 
|  | 3821 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3822 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3823 | oldWindowHandle->releaseChannel(); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3824 | } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3825 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3826 | } | 
|  | 3827 |  | 
|  | 3828 | void InputDispatcher::setFocusedApplication( | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3829 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3830 | if (DEBUG_FOCUS) { | 
|  | 3831 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
|  | 3832 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
|  | 3833 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3835 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3836 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3837 | sp<InputApplicationHandle> oldFocusedApplicationHandle = | 
|  | 3838 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3839 |  | 
|  | 3840 | if (oldFocusedApplicationHandle == mAwaitedFocusedApplication && | 
|  | 3841 | inputApplicationHandle != oldFocusedApplicationHandle) { | 
|  | 3842 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 3843 | } | 
|  | 3844 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3845 | if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3846 | if (oldFocusedApplicationHandle != inputApplicationHandle) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3847 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3848 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3849 | } else if (oldFocusedApplicationHandle != nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3850 | oldFocusedApplicationHandle.clear(); | 
|  | 3851 | mFocusedApplicationHandlesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3852 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3853 | } // release lock | 
|  | 3854 |  | 
|  | 3855 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3856 | mLooper->wake(); | 
|  | 3857 | } | 
|  | 3858 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3859 | /** | 
|  | 3860 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
|  | 3861 | * the display not specified. | 
|  | 3862 | * | 
|  | 3863 | * We track any unreleased events for each window. If a window loses the ability to receive the | 
|  | 3864 | * released event, we will send a cancel event to it. So when the focused display is changed, we | 
|  | 3865 | * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
|  | 3866 | * display. The display-specified events won't be affected. | 
|  | 3867 | */ | 
|  | 3868 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3869 | if (DEBUG_FOCUS) { | 
|  | 3870 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
|  | 3871 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3872 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3873 | std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3874 |  | 
|  | 3875 | if (mFocusedDisplayId != displayId) { | 
|  | 3876 | sp<InputWindowHandle> oldFocusedWindowHandle = | 
|  | 3877 | getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); | 
|  | 3878 | if (oldFocusedWindowHandle != nullptr) { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3879 | sp<InputChannel> inputChannel = | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3880 | getInputChannelLocked(oldFocusedWindowHandle->getToken()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3881 | if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3882 | CancelationOptions | 
|  | 3883 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 3884 | "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3885 | options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3886 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 3887 | } | 
|  | 3888 | } | 
|  | 3889 | mFocusedDisplayId = displayId; | 
|  | 3890 |  | 
|  | 3891 | // Sanity check | 
|  | 3892 | sp<InputWindowHandle> newFocusedWindowHandle = | 
|  | 3893 | getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 3894 | onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3895 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3896 | if (newFocusedWindowHandle == nullptr) { | 
|  | 3897 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
|  | 3898 | if (!mFocusedWindowHandlesByDisplay.empty()) { | 
|  | 3899 | ALOGE("But another display has a focused window:"); | 
|  | 3900 | for (auto& it : mFocusedWindowHandlesByDisplay) { | 
|  | 3901 | const int32_t displayId = it.first; | 
|  | 3902 | const sp<InputWindowHandle>& windowHandle = it.second; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3903 | ALOGE("Display #%" PRId32 " has focused window: '%s'\n", displayId, | 
|  | 3904 | windowHandle->getName().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3905 | } | 
|  | 3906 | } | 
|  | 3907 | } | 
|  | 3908 | } | 
|  | 3909 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3910 | if (DEBUG_FOCUS) { | 
|  | 3911 | logDispatchStateLocked(); | 
|  | 3912 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3913 | } // release lock | 
|  | 3914 |  | 
|  | 3915 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3916 | mLooper->wake(); | 
|  | 3917 | } | 
|  | 3918 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3919 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3920 | if (DEBUG_FOCUS) { | 
|  | 3921 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
|  | 3922 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 |  | 
|  | 3924 | bool changed; | 
|  | 3925 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3926 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3927 |  | 
|  | 3928 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
|  | 3929 | if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3930 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | } | 
|  | 3932 |  | 
|  | 3933 | if (mDispatchEnabled && !enabled) { | 
|  | 3934 | resetAndDropEverythingLocked("dispatcher is being disabled"); | 
|  | 3935 | } | 
|  | 3936 |  | 
|  | 3937 | mDispatchEnabled = enabled; | 
|  | 3938 | mDispatchFrozen = frozen; | 
|  | 3939 | changed = true; | 
|  | 3940 | } else { | 
|  | 3941 | changed = false; | 
|  | 3942 | } | 
|  | 3943 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3944 | if (DEBUG_FOCUS) { | 
|  | 3945 | logDispatchStateLocked(); | 
|  | 3946 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3947 | } // release lock | 
|  | 3948 |  | 
|  | 3949 | if (changed) { | 
|  | 3950 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 3951 | mLooper->wake(); | 
|  | 3952 | } | 
|  | 3953 | } | 
|  | 3954 |  | 
|  | 3955 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3956 | if (DEBUG_FOCUS) { | 
|  | 3957 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
|  | 3958 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 |  | 
|  | 3960 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3961 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3962 |  | 
|  | 3963 | if (mInputFilterEnabled == enabled) { | 
|  | 3964 | return; | 
|  | 3965 | } | 
|  | 3966 |  | 
|  | 3967 | mInputFilterEnabled = enabled; | 
|  | 3968 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
|  | 3969 | } // release lock | 
|  | 3970 |  | 
|  | 3971 | // Wake up poll loop since there might be work to do to drop everything. | 
|  | 3972 | mLooper->wake(); | 
|  | 3973 | } | 
|  | 3974 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 3975 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
|  | 3976 | std::scoped_lock lock(mLock); | 
|  | 3977 | mInTouchMode = inTouchMode; | 
|  | 3978 | } | 
|  | 3979 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3980 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { | 
|  | 3981 | if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3982 | if (DEBUG_FOCUS) { | 
|  | 3983 | ALOGD("Trivial transfer to same window."); | 
|  | 3984 | } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3985 | return true; | 
|  | 3986 | } | 
|  | 3987 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3989 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3991 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
|  | 3992 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3993 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3994 | ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3995 | return false; | 
|  | 3996 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3997 | if (DEBUG_FOCUS) { | 
|  | 3998 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
|  | 3999 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
|  | 4000 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4002 | if (DEBUG_FOCUS) { | 
|  | 4003 | ALOGD("Cannot transfer focus because windows are on different displays."); | 
|  | 4004 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | return false; | 
|  | 4006 | } | 
|  | 4007 |  | 
|  | 4008 | bool found = false; | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4009 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4010 | TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4011 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4012 | const TouchedWindow& touchedWindow = state.windows[i]; | 
|  | 4013 | if (touchedWindow.windowHandle == fromWindowHandle) { | 
|  | 4014 | int32_t oldTargetFlags = touchedWindow.targetFlags; | 
|  | 4015 | BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4016 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4017 | state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4018 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4019 | int32_t newTargetFlags = oldTargetFlags & | 
|  | 4020 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
|  | 4021 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4022 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4023 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4024 | found = true; | 
|  | 4025 | goto Found; | 
|  | 4026 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4027 | } | 
|  | 4028 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4029 | Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4030 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4031 | if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4032 | if (DEBUG_FOCUS) { | 
|  | 4033 | ALOGD("Focus transfer failed because from window did not have focus."); | 
|  | 4034 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4035 | return false; | 
|  | 4036 | } | 
|  | 4037 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4038 | sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
|  | 4039 | sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4040 | if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4041 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4042 | CancelationOptions | 
|  | 4043 | options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4044 | "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4046 | synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4047 | } | 
|  | 4048 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4049 | if (DEBUG_FOCUS) { | 
|  | 4050 | logDispatchStateLocked(); | 
|  | 4051 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 | } // release lock | 
|  | 4053 |  | 
|  | 4054 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4055 | mLooper->wake(); | 
|  | 4056 | return true; | 
|  | 4057 | } | 
|  | 4058 |  | 
|  | 4059 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4060 | if (DEBUG_FOCUS) { | 
|  | 4061 | ALOGD("Resetting and dropping all events (%s).", reason); | 
|  | 4062 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 |  | 
|  | 4064 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
|  | 4065 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 4066 |  | 
|  | 4067 | resetKeyRepeatLocked(); | 
|  | 4068 | releasePendingEventLocked(); | 
|  | 4069 | drainInboundQueueLocked(); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4070 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4072 | mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4073 | mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4075 | mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4076 | } | 
|  | 4077 |  | 
|  | 4078 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4079 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4080 | dumpDispatchStateLocked(dump); | 
|  | 4081 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4082 | std::istringstream stream(dump); | 
|  | 4083 | std::string line; | 
|  | 4084 |  | 
|  | 4085 | while (std::getline(stream, line, '\n')) { | 
|  | 4086 | ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4087 | } | 
|  | 4088 | } | 
|  | 4089 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4090 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4091 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
|  | 4092 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
|  | 4093 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4094 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4095 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4096 | if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
|  | 4097 | dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
|  | 4098 | for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
|  | 4099 | const int32_t displayId = it.first; | 
|  | 4100 | const sp<InputApplicationHandle>& applicationHandle = it.second; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4101 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4102 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4103 | displayId, applicationHandle->getName().c_str(), | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4104 | ns2ms(applicationHandle | 
|  | 4105 | ->getDispatchingTimeout( | 
|  | 4106 | DEFAULT_INPUT_DISPATCHING_TIMEOUT) | 
|  | 4107 | .count())); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4108 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4109 | } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4110 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4111 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4112 |  | 
|  | 4113 | if (!mFocusedWindowHandlesByDisplay.empty()) { | 
|  | 4114 | dump += StringPrintf(INDENT "FocusedWindows:\n"); | 
|  | 4115 | for (auto& it : mFocusedWindowHandlesByDisplay) { | 
|  | 4116 | const int32_t displayId = it.first; | 
|  | 4117 | const sp<InputWindowHandle>& windowHandle = it.second; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4118 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, | 
|  | 4119 | windowHandle->getName().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4120 | } | 
|  | 4121 | } else { | 
|  | 4122 | dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); | 
|  | 4123 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4124 |  | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4125 | if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4126 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4127 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4128 | const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4129 | 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] | 4130 | state.displayId, toString(state.down), toString(state.split), | 
|  | 4131 | state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4132 | if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4133 | dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4134 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4135 | const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4136 | dump += StringPrintf(INDENT4 | 
|  | 4137 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
|  | 4138 | i, touchedWindow.windowHandle->getName().c_str(), | 
|  | 4139 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4140 | } | 
|  | 4141 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4142 | dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4143 | } | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4144 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4145 | dump += INDENT3 "Portal windows:\n"; | 
|  | 4146 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4147 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4148 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, | 
|  | 4149 | portalWindowHandle->getName().c_str()); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4150 | } | 
|  | 4151 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4152 | } | 
|  | 4153 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4154 | dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4155 | } | 
|  | 4156 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4157 | if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4158 | for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4159 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4160 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4161 | if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4162 | dump += INDENT2 "Windows:\n"; | 
|  | 4163 | for (size_t i = 0; i < windowHandles.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4164 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4165 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4167 | dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4168 | "portalToDisplayId=%d, paused=%s, hasFocus=%s, " | 
| chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4169 | "hasWallpaper=%s, visible=%s, canReceiveKeys=%s, " | 
|  | 4170 | "flags=0x%08x, type=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4171 | "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4172 | "windowScale=(%f,%f), touchableRegion=", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4173 | i, windowInfo->name.c_str(), windowInfo->displayId, | 
|  | 4174 | windowInfo->portalToDisplayId, | 
|  | 4175 | toString(windowInfo->paused), | 
|  | 4176 | toString(windowInfo->hasFocus), | 
|  | 4177 | toString(windowInfo->hasWallpaper), | 
|  | 4178 | toString(windowInfo->visible), | 
|  | 4179 | toString(windowInfo->canReceiveKeys), | 
|  | 4180 | windowInfo->layoutParamsFlags, | 
| chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4181 | windowInfo->layoutParamsType, windowInfo->frameLeft, | 
|  | 4182 | windowInfo->frameTop, windowInfo->frameRight, | 
|  | 4183 | windowInfo->frameBottom, windowInfo->globalScaleFactor, | 
|  | 4184 | windowInfo->windowXScale, windowInfo->windowYScale); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4185 | dumpRegion(dump, windowInfo->touchableRegion); | 
|  | 4186 | dump += StringPrintf(", inputFeatures=0x%08x", windowInfo->inputFeatures); | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4187 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
|  | 4188 | "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4189 | windowInfo->ownerPid, windowInfo->ownerUid, | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4190 | ns2ms(windowInfo->dispatchingTimeout)); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4191 | } | 
|  | 4192 | } else { | 
|  | 4193 | dump += INDENT2 "Windows: <none>\n"; | 
|  | 4194 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | } | 
|  | 4196 | } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4197 | dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4198 | } | 
|  | 4199 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4200 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4201 | for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4202 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4203 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
|  | 4204 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4205 | } | 
|  | 4206 | for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4207 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4208 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
|  | 4209 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4210 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4211 | } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4212 | dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4213 | } | 
|  | 4214 |  | 
|  | 4215 | nsecs_t currentTime = now(); | 
|  | 4216 |  | 
|  | 4217 | // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4218 | if (!mRecentQueue.empty()) { | 
|  | 4219 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
|  | 4220 | for (EventEntry* entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4221 | dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4222 | entry->appendDescription(dump); | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4223 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4224 | } | 
|  | 4225 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4226 | dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4227 | } | 
|  | 4228 |  | 
|  | 4229 | // Dump event currently being dispatched. | 
|  | 4230 | if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4231 | dump += INDENT "PendingEvent:\n"; | 
|  | 4232 | dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4233 | mPendingEvent->appendDescription(dump); | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4234 | dump += StringPrintf(", age=%" PRId64 "ms\n", | 
|  | 4235 | ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4236 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4237 | dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | } | 
|  | 4239 |  | 
|  | 4240 | // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4241 | if (!mInboundQueue.empty()) { | 
|  | 4242 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
|  | 4243 | for (EventEntry* entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4244 | dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4245 | entry->appendDescription(dump); | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4246 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4247 | } | 
|  | 4248 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4249 | dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4250 | } | 
|  | 4251 |  | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4252 | if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4253 | dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4254 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
|  | 4255 | const KeyReplacement& replacement = pair.first; | 
|  | 4256 | int32_t newKeyCode = pair.second; | 
|  | 4257 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4258 | replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4259 | } | 
|  | 4260 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4261 | dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4262 | } | 
|  | 4263 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4264 | if (!mConnectionsByFd.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4265 | dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4266 | for (const auto& pair : mConnectionsByFd) { | 
|  | 4267 | const sp<Connection>& connection = pair.second; | 
|  | 4268 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4269 | "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4270 | pair.first, connection->getInputChannelName().c_str(), | 
|  | 4271 | connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4272 | toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4273 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4274 | if (!connection->outboundQueue.empty()) { | 
|  | 4275 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
|  | 4276 | connection->outboundQueue.size()); | 
|  | 4277 | for (DispatchEntry* entry : connection->outboundQueue) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4278 | dump.append(INDENT4); | 
|  | 4279 | entry->eventEntry->appendDescription(dump); | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4280 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 | 
|  | 4281 | "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4282 | entry->targetFlags, entry->resolvedAction, | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4283 | ns2ms(currentTime - entry->eventEntry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4284 | } | 
|  | 4285 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4286 | dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | } | 
|  | 4288 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4289 | if (!connection->waitQueue.empty()) { | 
|  | 4290 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
|  | 4291 | connection->waitQueue.size()); | 
|  | 4292 | for (DispatchEntry* entry : connection->waitQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4293 | dump += INDENT4; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | entry->eventEntry->appendDescription(dump); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4295 | dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4296 | "age=%" PRId64 "ms, wait=%" PRId64 "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4297 | entry->targetFlags, entry->resolvedAction, | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4298 | ns2ms(currentTime - entry->eventEntry->eventTime), | 
|  | 4299 | ns2ms(currentTime - entry->deliveryTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4300 | } | 
|  | 4301 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4302 | dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | } | 
|  | 4304 | } | 
|  | 4305 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4306 | dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | } | 
|  | 4308 |  | 
|  | 4309 | if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4310 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
|  | 4311 | ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4313 | dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4314 | } | 
|  | 4315 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4316 | dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4317 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
|  | 4318 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
|  | 4319 | ns2ms(mConfig.keyRepeatTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } | 
|  | 4321 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4322 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
|  | 4323 | const size_t numMonitors = monitors.size(); | 
|  | 4324 | for (size_t i = 0; i < numMonitors; i++) { | 
|  | 4325 | const Monitor& monitor = monitors[i]; | 
|  | 4326 | const sp<InputChannel>& channel = monitor.inputChannel; | 
|  | 4327 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
|  | 4328 | dump += "\n"; | 
|  | 4329 | } | 
|  | 4330 | } | 
|  | 4331 |  | 
| Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4332 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4333 | #if DEBUG_REGISTRATION | 
| Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4334 | ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | #endif | 
|  | 4336 |  | 
|  | 4337 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4338 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4339 | sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4340 | if (existingConnection != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4341 | ALOGW("Attempted to register already registered input channel '%s'", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4342 | inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4343 | return BAD_VALUE; | 
|  | 4344 | } | 
|  | 4345 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4346 | sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4347 |  | 
|  | 4348 | int fd = inputChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4349 | mConnectionsByFd[fd] = connection; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4350 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4352 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
|  | 4353 | } // release lock | 
|  | 4354 |  | 
|  | 4355 | // Wake the looper because some connections have changed. | 
|  | 4356 | mLooper->wake(); | 
|  | 4357 | return OK; | 
|  | 4358 | } | 
|  | 4359 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4360 | status_t InputDispatcher::registerInputMonitor(const sp<InputChannel>& inputChannel, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4361 | int32_t displayId, bool isGestureMonitor) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4362 | { // acquire lock | 
|  | 4363 | std::scoped_lock _l(mLock); | 
|  | 4364 |  | 
|  | 4365 | if (displayId < 0) { | 
|  | 4366 | ALOGW("Attempted to register input monitor without a specified display."); | 
|  | 4367 | return BAD_VALUE; | 
|  | 4368 | } | 
|  | 4369 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4370 | if (inputChannel->getConnectionToken() == nullptr) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4371 | ALOGW("Attempted to register input monitor without an identifying token."); | 
|  | 4372 | return BAD_VALUE; | 
|  | 4373 | } | 
|  | 4374 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4375 | sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4376 |  | 
|  | 4377 | const int fd = inputChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4378 | mConnectionsByFd[fd] = connection; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4379 | mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4380 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4381 | auto& monitorsByDisplay = | 
|  | 4382 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4383 | monitorsByDisplay[displayId].emplace_back(inputChannel); | 
|  | 4384 |  | 
|  | 4385 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4386 | } | 
|  | 4387 | // Wake the looper because some connections have changed. | 
|  | 4388 | mLooper->wake(); | 
|  | 4389 | return OK; | 
|  | 4390 | } | 
|  | 4391 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { | 
|  | 4393 | #if DEBUG_REGISTRATION | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4394 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4395 | #endif | 
|  | 4396 |  | 
|  | 4397 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4398 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 |  | 
|  | 4400 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); | 
|  | 4401 | if (status) { | 
|  | 4402 | return status; | 
|  | 4403 | } | 
|  | 4404 | } // release lock | 
|  | 4405 |  | 
|  | 4406 | // Wake the poll loop because removing the connection may have changed the current | 
|  | 4407 | // synchronization state. | 
|  | 4408 | mLooper->wake(); | 
|  | 4409 | return OK; | 
|  | 4410 | } | 
|  | 4411 |  | 
|  | 4412 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4413 | bool notify) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4414 | sp<Connection> connection = getConnectionLocked(inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4415 | if (connection == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4416 | ALOGW("Attempted to unregister already unregistered input channel '%s'", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4417 | inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4418 | return BAD_VALUE; | 
|  | 4419 | } | 
|  | 4420 |  | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4421 | removeConnectionLocked(connection); | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4422 | mInputChannelsByToken.erase(inputChannel->getConnectionToken()); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4423 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | if (connection->monitor) { | 
|  | 4425 | removeMonitorChannelLocked(inputChannel); | 
|  | 4426 | } | 
|  | 4427 |  | 
|  | 4428 | mLooper->removeFd(inputChannel->getFd()); | 
|  | 4429 |  | 
|  | 4430 | nsecs_t currentTime = now(); | 
|  | 4431 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
|  | 4432 |  | 
|  | 4433 | connection->status = Connection::STATUS_ZOMBIE; | 
|  | 4434 | return OK; | 
|  | 4435 | } | 
|  | 4436 |  | 
|  | 4437 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4438 | removeMonitorChannelLocked(inputChannel, mGlobalMonitorsByDisplay); | 
|  | 4439 | removeMonitorChannelLocked(inputChannel, mGestureMonitorsByDisplay); | 
|  | 4440 | } | 
|  | 4441 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4442 | void InputDispatcher::removeMonitorChannelLocked( | 
|  | 4443 | const sp<InputChannel>& inputChannel, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4444 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4445 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4446 | std::vector<Monitor>& monitors = it->second; | 
|  | 4447 | const size_t numMonitors = monitors.size(); | 
|  | 4448 | for (size_t i = 0; i < numMonitors; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4449 | if (monitors[i].inputChannel == inputChannel) { | 
|  | 4450 | monitors.erase(monitors.begin() + i); | 
|  | 4451 | break; | 
|  | 4452 | } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4453 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4454 | if (monitors.empty()) { | 
|  | 4455 | it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4456 | } else { | 
|  | 4457 | ++it; | 
|  | 4458 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } | 
|  | 4460 | } | 
|  | 4461 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4462 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
|  | 4463 | { // acquire lock | 
|  | 4464 | std::scoped_lock _l(mLock); | 
|  | 4465 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
|  | 4466 |  | 
|  | 4467 | if (!foundDisplayId) { | 
|  | 4468 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
|  | 4469 | return BAD_VALUE; | 
|  | 4470 | } | 
|  | 4471 | int32_t displayId = foundDisplayId.value(); | 
|  | 4472 |  | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4473 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4474 | mTouchStatesByDisplay.find(displayId); | 
|  | 4475 | if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4476 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
|  | 4477 | return BAD_VALUE; | 
|  | 4478 | } | 
|  | 4479 |  | 
| Siarhei Vishniakou | bde3d9e | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4480 | TouchState& state = stateIt->second; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4481 | std::optional<int32_t> foundDeviceId; | 
|  | 4482 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4483 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4484 | foundDeviceId = state.deviceId; | 
|  | 4485 | } | 
|  | 4486 | } | 
|  | 4487 | if (!foundDeviceId || !state.down) { | 
|  | 4488 | 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] | 4489 | " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4490 | return BAD_VALUE; | 
|  | 4491 | } | 
|  | 4492 | int32_t deviceId = foundDeviceId.value(); | 
|  | 4493 |  | 
|  | 4494 | // Send cancel events to all the input channels we're stealing from. | 
|  | 4495 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4496 | "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4497 | options.deviceId = deviceId; | 
|  | 4498 | options.displayId = displayId; | 
|  | 4499 | for (const TouchedWindow& window : state.windows) { | 
|  | 4500 | sp<InputChannel> channel = getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4501 | if (channel != nullptr) { | 
|  | 4502 | synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
|  | 4503 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4504 | } | 
|  | 4505 | // Then clear the current touch state so we stop dispatching to them as well. | 
|  | 4506 | state.filterNonMonitors(); | 
|  | 4507 | } | 
|  | 4508 | return OK; | 
|  | 4509 | } | 
|  | 4510 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4511 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
|  | 4512 | const sp<IBinder>& token) { | 
|  | 4513 | for (const auto& it : mGestureMonitorsByDisplay) { | 
|  | 4514 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4515 | for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4516 | if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4517 | return it.first; | 
|  | 4518 | } | 
|  | 4519 | } | 
|  | 4520 | } | 
|  | 4521 | return std::nullopt; | 
|  | 4522 | } | 
|  | 4523 |  | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4524 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4525 | if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4526 | return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4527 | } | 
|  | 4528 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4529 | for (const auto& pair : mConnectionsByFd) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4530 | const sp<Connection>& connection = pair.second; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4531 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4532 | return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4533 | } | 
|  | 4534 | } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4535 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4536 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4537 | } | 
|  | 4538 |  | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4539 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4540 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4541 | removeByValue(mConnectionsByFd, connection); | 
|  | 4542 | } | 
|  | 4543 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4544 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, | 
|  | 4545 | const sp<Connection>& connection, uint32_t seq, | 
|  | 4546 | bool handled) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4547 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4548 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4549 | commandEntry->connection = connection; | 
|  | 4550 | commandEntry->eventTime = currentTime; | 
|  | 4551 | commandEntry->seq = seq; | 
|  | 4552 | commandEntry->handled = handled; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4553 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4554 | } | 
|  | 4555 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4556 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, | 
|  | 4557 | const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4558 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4559 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4560 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4561 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4562 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4563 | commandEntry->connection = connection; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4564 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4565 | } | 
|  | 4566 |  | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4567 | void InputDispatcher::onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4568 | const sp<InputWindowHandle>& newFocus) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4569 | sp<IBinder> oldToken = oldFocus != nullptr ? oldFocus->getToken() : nullptr; | 
|  | 4570 | sp<IBinder> newToken = newFocus != nullptr ? newFocus->getToken() : nullptr; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4571 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4572 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4573 | commandEntry->oldToken = oldToken; | 
|  | 4574 | commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4575 | postCommandLocked(std::move(commandEntry)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4576 | } | 
|  | 4577 |  | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4578 | void InputDispatcher::onAnrLocked(const Connection& connection) { | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4579 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
|  | 4580 | // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4581 | if (connection.waitQueue.empty()) { | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4582 | ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4583 | connection.inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4584 | return; | 
|  | 4585 | } | 
|  | 4586 | /** | 
|  | 4587 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
|  | 4588 | * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
|  | 4589 | * has changed. This could cause newer entries to time out before the already dispatched | 
|  | 4590 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
|  | 4591 | * processes the events linearly. So providing information about the oldest entry seems to be | 
|  | 4592 | * most useful. | 
|  | 4593 | */ | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4594 | DispatchEntry* oldestEntry = *connection.waitQueue.begin(); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4595 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
|  | 4596 | std::string reason = | 
|  | 4597 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4598 | connection.inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4599 | ns2ms(currentWait), | 
|  | 4600 | oldestEntry->eventEntry->getDescription().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 |  | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4602 | updateLastAnrStateLocked(getWindowHandleLocked(connection.inputChannel->getConnectionToken()), | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4603 | reason); | 
|  | 4604 |  | 
|  | 4605 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 4606 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
|  | 4607 | commandEntry->inputApplicationHandle = nullptr; | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4608 | commandEntry->inputChannel = connection.inputChannel; | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4609 | commandEntry->reason = std::move(reason); | 
|  | 4610 | postCommandLocked(std::move(commandEntry)); | 
|  | 4611 | } | 
|  | 4612 |  | 
|  | 4613 | void InputDispatcher::onAnrLocked(const sp<InputApplicationHandle>& application) { | 
|  | 4614 | std::string reason = android::base::StringPrintf("%s does not have a focused window", | 
|  | 4615 | application->getName().c_str()); | 
|  | 4616 |  | 
|  | 4617 | updateLastAnrStateLocked(application, reason); | 
|  | 4618 |  | 
|  | 4619 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 4620 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
|  | 4621 | commandEntry->inputApplicationHandle = application; | 
|  | 4622 | commandEntry->inputChannel = nullptr; | 
|  | 4623 | commandEntry->reason = std::move(reason); | 
|  | 4624 | postCommandLocked(std::move(commandEntry)); | 
|  | 4625 | } | 
|  | 4626 |  | 
|  | 4627 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, | 
|  | 4628 | const std::string& reason) { | 
|  | 4629 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
|  | 4630 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 4631 | } | 
|  | 4632 |  | 
|  | 4633 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputApplicationHandle>& application, | 
|  | 4634 | const std::string& reason) { | 
|  | 4635 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); | 
|  | 4636 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 4637 | } | 
|  | 4638 |  | 
|  | 4639 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
|  | 4640 | const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4641 | // 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] | 4642 | time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | struct tm tm; | 
|  | 4644 | localtime_r(&t, &tm); | 
|  | 4645 | char timestr[64]; | 
|  | 4646 | strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4647 | mLastAnrState.clear(); | 
|  | 4648 | mLastAnrState += INDENT "ANR:\n"; | 
|  | 4649 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4650 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
|  | 4651 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4652 | dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4653 | } | 
|  | 4654 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4655 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4656 | mLock.unlock(); | 
|  | 4657 |  | 
|  | 4658 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); | 
|  | 4659 |  | 
|  | 4660 | mLock.lock(); | 
|  | 4661 | } | 
|  | 4662 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4663 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4664 | sp<Connection> connection = commandEntry->connection; | 
|  | 4665 |  | 
|  | 4666 | if (connection->status != Connection::STATUS_ZOMBIE) { | 
|  | 4667 | mLock.unlock(); | 
|  | 4668 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4669 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4670 |  | 
|  | 4671 | mLock.lock(); | 
|  | 4672 | } | 
|  | 4673 | } | 
|  | 4674 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4675 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4676 | sp<IBinder> oldToken = commandEntry->oldToken; | 
|  | 4677 | sp<IBinder> newToken = commandEntry->newToken; | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4678 | mLock.unlock(); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4679 | mPolicy->notifyFocusChanged(oldToken, newToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4680 | mLock.lock(); | 
|  | 4681 | } | 
|  | 4682 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4683 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4684 | sp<IBinder> token = | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4685 | commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4686 | mLock.unlock(); | 
|  | 4687 |  | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4688 | const nsecs_t timeoutExtension = | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4689 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4690 |  | 
|  | 4691 | mLock.lock(); | 
|  | 4692 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4693 | if (timeoutExtension > 0) { | 
|  | 4694 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); | 
|  | 4695 | } else { | 
|  | 4696 | // stop waking up for events in this connection, it is already not responding | 
|  | 4697 | sp<Connection> connection = getConnectionLocked(token); | 
|  | 4698 | if (connection == nullptr) { | 
|  | 4699 | return; | 
|  | 4700 | } | 
|  | 4701 | cancelEventsForAnrLocked(connection); | 
|  | 4702 | } | 
|  | 4703 | } | 
|  | 4704 |  | 
|  | 4705 | void InputDispatcher::extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application, | 
|  | 4706 | const sp<IBinder>& connectionToken, | 
|  | 4707 | nsecs_t timeoutExtension) { | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4708 | if (connectionToken == nullptr && application != nullptr) { | 
|  | 4709 | // The ANR happened because there's no focused window | 
|  | 4710 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension; | 
|  | 4711 | mAwaitedFocusedApplication = application; | 
|  | 4712 | } | 
|  | 4713 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4714 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 4715 | if (connection == nullptr) { | 
| Siarhei Vishniakou | 4e21979 | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4716 | // It's possible that the connection already disappeared. No action necessary. | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4717 | return; | 
|  | 4718 | } | 
|  | 4719 |  | 
|  | 4720 | ALOGI("Raised ANR, but the policy wants to keep waiting on %s for %" PRId64 "ms longer", | 
|  | 4721 | connection->inputChannel->getName().c_str(), ns2ms(timeoutExtension)); | 
|  | 4722 |  | 
|  | 4723 | connection->responsive = true; | 
|  | 4724 | const nsecs_t newTimeout = now() + timeoutExtension; | 
|  | 4725 | for (DispatchEntry* entry : connection->waitQueue) { | 
|  | 4726 | if (newTimeout >= entry->timeoutTime) { | 
|  | 4727 | // Already removed old entries when connection was marked unresponsive | 
|  | 4728 | entry->timeoutTime = newTimeout; | 
|  | 4729 | mAnrTracker.insert(entry->timeoutTime, connectionToken); | 
|  | 4730 | } | 
|  | 4731 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4732 | } | 
|  | 4733 |  | 
|  | 4734 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( | 
|  | 4735 | CommandEntry* commandEntry) { | 
|  | 4736 | KeyEntry* entry = commandEntry->keyEntry; | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4737 | KeyEvent event = createKeyEvent(*entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 |  | 
|  | 4739 | mLock.unlock(); | 
|  | 4740 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4741 | android::base::Timer t; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4742 | sp<IBinder> token = commandEntry->inputChannel != nullptr | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4743 | ? commandEntry->inputChannel->getConnectionToken() | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4744 | : nullptr; | 
|  | 4745 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4746 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4747 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4748 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4749 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4750 |  | 
|  | 4751 | mLock.lock(); | 
|  | 4752 |  | 
|  | 4753 | if (delay < 0) { | 
|  | 4754 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
|  | 4755 | } else if (!delay) { | 
|  | 4756 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 4757 | } else { | 
|  | 4758 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
|  | 4759 | entry->interceptKeyWakeupTime = now() + delay; | 
|  | 4760 | } | 
|  | 4761 | entry->release(); | 
|  | 4762 | } | 
|  | 4763 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4764 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 4765 | mLock.unlock(); | 
|  | 4766 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); | 
|  | 4767 | mLock.lock(); | 
|  | 4768 | } | 
|  | 4769 |  | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4770 | /** | 
|  | 4771 | * Connection is responsive if it has no events in the waitQueue that are older than the | 
|  | 4772 | * current time. | 
|  | 4773 | */ | 
|  | 4774 | static bool isConnectionResponsive(const Connection& connection) { | 
|  | 4775 | const nsecs_t currentTime = now(); | 
|  | 4776 | for (const DispatchEntry* entry : connection.waitQueue) { | 
|  | 4777 | if (entry->timeoutTime < currentTime) { | 
|  | 4778 | return false; | 
|  | 4779 | } | 
|  | 4780 | } | 
|  | 4781 | return true; | 
|  | 4782 | } | 
|  | 4783 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4784 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4785 | sp<Connection> connection = commandEntry->connection; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4786 | const nsecs_t finishTime = commandEntry->eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4787 | uint32_t seq = commandEntry->seq; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4788 | const bool handled = commandEntry->handled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4789 |  | 
|  | 4790 | // Handle post-event policy actions. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4791 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4792 | if (dispatchEntryIt == connection->waitQueue.end()) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4793 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4794 | } | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4795 | DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4796 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4797 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
| Siarhei Vishniakou | ffaa2b1 | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4798 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
|  | 4799 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4800 | } | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4801 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4802 |  | 
|  | 4803 | bool restartEvent; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4804 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4805 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); | 
|  | 4806 | restartEvent = | 
|  | 4807 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4808 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4809 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); | 
|  | 4810 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, | 
|  | 4811 | handled); | 
|  | 4812 | } else { | 
|  | 4813 | restartEvent = false; | 
|  | 4814 | } | 
|  | 4815 |  | 
|  | 4816 | // Dequeue the event and start the next cycle. | 
| Siarhei Vishniakou | de1bc4a | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4817 | // Because the lock might have been released, it is possible that the | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4818 | // contents of the wait queue to have been drained, so we need to double-check | 
|  | 4819 | // a few things. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4820 | dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 4821 | if (dispatchEntryIt != connection->waitQueue.end()) { | 
|  | 4822 | dispatchEntry = *dispatchEntryIt; | 
|  | 4823 | connection->waitQueue.erase(dispatchEntryIt); | 
| Siarhei Vishniakou | e462304 | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4824 | mAnrTracker.erase(dispatchEntry->timeoutTime, | 
|  | 4825 | connection->inputChannel->getConnectionToken()); | 
|  | 4826 | if (!connection->responsive) { | 
|  | 4827 | connection->responsive = isConnectionResponsive(*connection); | 
|  | 4828 | } | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4829 | traceWaitQueueLength(connection); | 
|  | 4830 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4831 | connection->outboundQueue.push_front(dispatchEntry); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4832 | traceOutboundQueueLength(connection); | 
|  | 4833 | } else { | 
|  | 4834 | releaseDispatchEntry(dispatchEntry); | 
|  | 4835 | } | 
|  | 4836 | } | 
|  | 4837 |  | 
|  | 4838 | // Start the next dispatch cycle for this connection. | 
|  | 4839 | startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4840 | } | 
|  | 4841 |  | 
|  | 4842 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4843 | DispatchEntry* dispatchEntry, | 
|  | 4844 | KeyEntry* keyEntry, bool handled) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4845 | if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4846 | if (!handled) { | 
|  | 4847 | // Report the key as unhandled, since the fallback was not handled. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4848 | mReporter->reportUnhandledKey(keyEntry->id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4849 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4850 | return false; | 
|  | 4851 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4852 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4853 | // Get the fallback key state. | 
|  | 4854 | // Clear it out after dispatching the UP. | 
|  | 4855 | int32_t originalKeyCode = keyEntry->keyCode; | 
|  | 4856 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
|  | 4857 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { | 
|  | 4858 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 4859 | } | 
|  | 4860 |  | 
|  | 4861 | if (handled || !dispatchEntry->hasForegroundTarget()) { | 
|  | 4862 | // If the application handles the original key for which we previously | 
|  | 4863 | // generated a fallback or if the window is not a foreground window, | 
|  | 4864 | // then cancel the associated fallback key, if any. | 
|  | 4865 | if (fallbackKeyCode != -1) { | 
|  | 4866 | // Dispatch the unhandled key to the policy with the cancel flag. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4868 | ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4869 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 4870 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, | 
|  | 4871 | keyEntry->policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4872 | #endif | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4873 | KeyEvent event = createKeyEvent(*keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4874 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4875 |  | 
|  | 4876 | mLock.unlock(); | 
|  | 4877 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4878 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4879 | keyEntry->policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4880 |  | 
|  | 4881 | mLock.lock(); | 
|  | 4882 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4883 | // Cancel the fallback key. | 
|  | 4884 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4885 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4886 | "application handled the original non-fallback key " | 
|  | 4887 | "or is no longer a foreground target, " | 
|  | 4888 | "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4889 | options.keyCode = fallbackKeyCode; | 
|  | 4890 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4891 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4892 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 4893 | } | 
|  | 4894 | } else { | 
|  | 4895 | // If the application did not handle a non-fallback key, first check | 
|  | 4896 | // that we are in a good state to perform unhandled key event processing | 
|  | 4897 | // Then ask the policy what to do with it. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4898 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4899 | if (fallbackKeyCode == -1 && !initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4900 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4901 | ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4902 | "since this is not an initial down.  " | 
|  | 4903 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 4904 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4905 | #endif | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4906 | return false; | 
|  | 4907 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4909 | // Dispatch the unhandled key to the policy. | 
|  | 4910 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 4911 | ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4912 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 4913 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4914 | #endif | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4915 | KeyEvent event = createKeyEvent(*keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4916 |  | 
|  | 4917 | mLock.unlock(); | 
|  | 4918 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4919 | bool fallback = | 
|  | 4920 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
|  | 4921 | &event, keyEntry->policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4922 |  | 
|  | 4923 | mLock.lock(); | 
|  | 4924 |  | 
|  | 4925 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 4926 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 4927 | return false; | 
|  | 4928 | } | 
|  | 4929 |  | 
|  | 4930 | // Latch the fallback keycode for this key on an initial down. | 
|  | 4931 | // The fallback keycode cannot change at any other point in the lifecycle. | 
|  | 4932 | if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4933 | if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4934 | fallbackKeyCode = event.getKeyCode(); | 
|  | 4935 | } else { | 
|  | 4936 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 4937 | } | 
|  | 4938 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
|  | 4939 | } | 
|  | 4940 |  | 
|  | 4941 | ALOG_ASSERT(fallbackKeyCode != -1); | 
|  | 4942 |  | 
|  | 4943 | // Cancel the fallback key if the policy decides not to send it anymore. | 
|  | 4944 | // We will continue to dispatch the key to the policy but we will no | 
|  | 4945 | // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4946 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
|  | 4947 | (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4948 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 4949 | if (fallback) { | 
|  | 4950 | ALOGD("Unhandled key event: Policy requested to send key %d" | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4951 | "as a fallback for %d, but on the DOWN it had requested " | 
|  | 4952 | "to send %d instead.  Fallback canceled.", | 
|  | 4953 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4954 | } else { | 
|  | 4955 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4956 | "but on the DOWN it had requested to send %d.  " | 
|  | 4957 | "Fallback canceled.", | 
|  | 4958 | originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4959 | } | 
|  | 4960 | #endif | 
|  | 4961 |  | 
|  | 4962 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
|  | 4963 | "canceling fallback, policy no longer desires it"); | 
|  | 4964 | options.keyCode = fallbackKeyCode; | 
|  | 4965 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
|  | 4966 |  | 
|  | 4967 | fallback = false; | 
|  | 4968 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 4969 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4970 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4971 | } | 
|  | 4972 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4973 |  | 
|  | 4974 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4975 | { | 
|  | 4976 | std::string msg; | 
|  | 4977 | const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
|  | 4978 | connection->inputState.getFallbackKeys(); | 
|  | 4979 | for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4980 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4981 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4982 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4983 | fallbackKeys.size(), msg.c_str()); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4984 | } | 
|  | 4985 | #endif | 
|  | 4986 |  | 
|  | 4987 | if (fallback) { | 
|  | 4988 | // Restart the dispatch cycle using the fallback key. | 
|  | 4989 | keyEntry->eventTime = event.getEventTime(); | 
|  | 4990 | keyEntry->deviceId = event.getDeviceId(); | 
|  | 4991 | keyEntry->source = event.getSource(); | 
|  | 4992 | keyEntry->displayId = event.getDisplayId(); | 
|  | 4993 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
|  | 4994 | keyEntry->keyCode = fallbackKeyCode; | 
|  | 4995 | keyEntry->scanCode = event.getScanCode(); | 
|  | 4996 | keyEntry->metaState = event.getMetaState(); | 
|  | 4997 | keyEntry->repeatCount = event.getRepeatCount(); | 
|  | 4998 | keyEntry->downTime = event.getDownTime(); | 
|  | 4999 | keyEntry->syntheticRepeat = false; | 
|  | 5000 |  | 
|  | 5001 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5002 | ALOGD("Unhandled key event: Dispatching fallback key.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5003 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
|  | 5004 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5005 | #endif | 
|  | 5006 | return true; // restart the event | 
|  | 5007 | } else { | 
|  | 5008 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5009 | ALOGD("Unhandled key event: No fallback key."); | 
|  | 5010 | #endif | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5011 |  | 
|  | 5012 | // Report the key as unhandled, since there is no fallback key. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5013 | mReporter->reportUnhandledKey(keyEntry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5014 | } | 
|  | 5015 | } | 
|  | 5016 | return false; | 
|  | 5017 | } | 
|  | 5018 |  | 
|  | 5019 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5020 | DispatchEntry* dispatchEntry, | 
|  | 5021 | MotionEntry* motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5022 | return false; | 
|  | 5023 | } | 
|  | 5024 |  | 
|  | 5025 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5026 | mLock.unlock(); | 
|  | 5027 |  | 
|  | 5028 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); | 
|  | 5029 |  | 
|  | 5030 | mLock.lock(); | 
|  | 5031 | } | 
|  | 5032 |  | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5033 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { | 
|  | 5034 | KeyEvent event; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5035 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5036 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 5037 | entry.repeatCount, entry.downTime, entry.eventTime); | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5038 | return event; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5039 | } | 
|  | 5040 |  | 
| Siarhei Vishniakou | 7f0a439 | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5041 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, | 
|  | 5042 | const Connection& connection, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5043 | // TODO Write some statistics about how long we spend waiting. | 
|  | 5044 | } | 
|  | 5045 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5046 | /** | 
|  | 5047 | * Report the touch event latency to the statsd server. | 
|  | 5048 | * Input events are reported for statistics if: | 
|  | 5049 | * - This is a touchscreen event | 
|  | 5050 | * - InputFilter is not enabled | 
|  | 5051 | * - Event is not injected or synthesized | 
|  | 5052 | * | 
|  | 5053 | * Statistics should be reported before calling addValue, to prevent a fresh new sample | 
|  | 5054 | * from getting aggregated with the "old" data. | 
|  | 5055 | */ | 
|  | 5056 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) | 
|  | 5057 | REQUIRES(mLock) { | 
|  | 5058 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && | 
|  | 5059 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; | 
|  | 5060 | if (!reportForStatistics) { | 
|  | 5061 | return; | 
|  | 5062 | } | 
|  | 5063 |  | 
|  | 5064 | if (mTouchStatistics.shouldReport()) { | 
|  | 5065 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), | 
|  | 5066 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), | 
|  | 5067 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); | 
|  | 5068 | mTouchStatistics.reset(); | 
|  | 5069 | } | 
|  | 5070 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); | 
|  | 5071 | mTouchStatistics.addValue(latencyMicros); | 
|  | 5072 | } | 
|  | 5073 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5074 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
|  | 5075 | if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5076 | ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5077 | } | 
|  | 5078 | } | 
|  | 5079 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5080 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5081 | if (ATRACE_ENABLED()) { | 
|  | 5082 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5083 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5084 | ATRACE_INT(counterName, connection->outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5085 | } | 
|  | 5086 | } | 
|  | 5087 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5088 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5089 | if (ATRACE_ENABLED()) { | 
|  | 5090 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5091 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5092 | ATRACE_INT(counterName, connection->waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5093 | } | 
|  | 5094 | } | 
|  | 5095 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5096 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5097 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5098 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5099 | dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5100 | dumpDispatchStateLocked(dump); | 
|  | 5101 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5102 | if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5103 | dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5104 | dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5105 | } | 
|  | 5106 | } | 
|  | 5107 |  | 
|  | 5108 | void InputDispatcher::monitor() { | 
|  | 5109 | // 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] | 5110 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5111 | mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5112 | mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5113 | } | 
|  | 5114 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5115 | /** | 
|  | 5116 | * Wake up the dispatcher and wait until it processes all events and commands. | 
|  | 5117 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
|  | 5118 | * this method can be safely called from any thread, as long as you've ensured that | 
|  | 5119 | * the work you are interested in completing has already been queued. | 
|  | 5120 | */ | 
|  | 5121 | bool InputDispatcher::waitForIdle() { | 
|  | 5122 | /** | 
|  | 5123 | * Timeout should represent the longest possible time that a device might spend processing | 
|  | 5124 | * events and commands. | 
|  | 5125 | */ | 
|  | 5126 | constexpr std::chrono::duration TIMEOUT = 100ms; | 
|  | 5127 | std::unique_lock lock(mLock); | 
|  | 5128 | mLooper->wake(); | 
|  | 5129 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
|  | 5130 | return result == std::cv_status::no_timeout; | 
|  | 5131 | } | 
|  | 5132 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5133 | } // namespace android::inputdispatcher |