| 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 | 4cb50ca | 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 | d44dddf | 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 | 6a5a14e | 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 | f0007dd | 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 | c6ae4bb | 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 | c6ae4bb | 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 | c6ae4bb | 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 | ff1f1bb | 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 | d44dddf | 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 | d44dddf | 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 | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 484 | /** | 
 | 485 |  * Check if any of the connections' wait queues have events that are too old. | 
 | 486 |  * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
 | 487 |  * Return the time at which we should wake up next. | 
 | 488 |  */ | 
 | 489 | nsecs_t InputDispatcher::processAnrsLocked() { | 
 | 490 |     const nsecs_t currentTime = now(); | 
 | 491 |     nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
 | 492 |     // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
 | 493 |     if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
 | 494 |         if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
 | 495 |             onAnrLocked(mAwaitedFocusedApplication); | 
 | 496 |             mAwaitedFocusedApplication.clear(); | 
 | 497 |             return LONG_LONG_MIN; | 
 | 498 |         } else { | 
 | 499 |             // Keep waiting | 
 | 500 |             const nsecs_t millisRemaining = ns2ms(*mNoFocusedWindowTimeoutTime - currentTime); | 
 | 501 |             ALOGW("Still no focused window. Will drop the event in %" PRId64 "ms", millisRemaining); | 
 | 502 |             nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
 | 503 |         } | 
 | 504 |     } | 
 | 505 |  | 
 | 506 |     // Check if any connection ANRs are due | 
 | 507 |     nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
 | 508 |     if (currentTime < nextAnrCheck) { // most likely scenario | 
 | 509 |         return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
 | 510 |     } | 
 | 511 |  | 
 | 512 |     // If we reached here, we have an unresponsive connection. | 
 | 513 |     sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
 | 514 |     if (connection == nullptr) { | 
 | 515 |         ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
 | 516 |         return nextAnrCheck; | 
 | 517 |     } | 
 | 518 |     connection->responsive = false; | 
 | 519 |     // Stop waking up for this unresponsive connection | 
 | 520 |     mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
 | 521 |     onAnrLocked(connection); | 
 | 522 |     return LONG_LONG_MIN; | 
 | 523 | } | 
 | 524 |  | 
 | 525 | nsecs_t InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
 | 526 |     sp<InputWindowHandle> window = getWindowHandleLocked(token); | 
 | 527 |     if (window != nullptr) { | 
 | 528 |         return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT).count(); | 
 | 529 |     } | 
 | 530 |     return DEFAULT_INPUT_DISPATCHING_TIMEOUT.count(); | 
 | 531 | } | 
 | 532 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
 | 534 |     nsecs_t currentTime = now(); | 
 | 535 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 536 |     // Reset the key repeat timer whenever normal dispatch is suspended while the | 
 | 537 |     // device is in a non-interactive state.  This is to ensure that we abort a key | 
 | 538 |     // repeat if the device is just coming out of sleep. | 
 | 539 |     if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 |         resetKeyRepeatLocked(); | 
 | 541 |     } | 
 | 542 |  | 
 | 543 |     // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
 | 544 |     if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 545 |         if (DEBUG_FOCUS) { | 
 | 546 |             ALOGD("Dispatch frozen.  Waiting some more."); | 
 | 547 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 548 |         return; | 
 | 549 |     } | 
 | 550 |  | 
 | 551 |     // Optimize latency of app switches. | 
 | 552 |     // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
 | 553 |     // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
 | 554 |     bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
 | 555 |     if (mAppSwitchDueTime < *nextWakeupTime) { | 
 | 556 |         *nextWakeupTime = mAppSwitchDueTime; | 
 | 557 |     } | 
 | 558 |  | 
 | 559 |     // Ready to start a new event. | 
 | 560 |     // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 561 |     if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 562 |         if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 |             if (isAppSwitchDue) { | 
 | 564 |                 // The inbound queue is empty so the app switch key we were waiting | 
 | 565 |                 // for will never arrive.  Stop waiting for it. | 
 | 566 |                 resetPendingAppSwitchLocked(false); | 
 | 567 |                 isAppSwitchDue = false; | 
 | 568 |             } | 
 | 569 |  | 
 | 570 |             // Synthesize a key repeat if appropriate. | 
 | 571 |             if (mKeyRepeatState.lastKeyEntry) { | 
 | 572 |                 if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
 | 573 |                     mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
 | 574 |                 } else { | 
 | 575 |                     if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
 | 576 |                         *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
 | 577 |                     } | 
 | 578 |                 } | 
 | 579 |             } | 
 | 580 |  | 
 | 581 |             // Nothing to do if there is no pending event. | 
 | 582 |             if (!mPendingEvent) { | 
 | 583 |                 return; | 
 | 584 |             } | 
 | 585 |         } else { | 
 | 586 |             // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 587 |             mPendingEvent = mInboundQueue.front(); | 
 | 588 |             mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 589 |             traceInboundQueueLengthLocked(); | 
 | 590 |         } | 
 | 591 |  | 
 | 592 |         // Poke user activity for this event. | 
 | 593 |         if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 594 |             pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 |     } | 
 | 597 |  | 
 | 598 |     // Now we have an event to dispatch. | 
 | 599 |     // 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] | 600 |     ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 |     bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 602 |     DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 603 |     if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 604 |         dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 |     } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 606 |         dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 |     } | 
 | 608 |  | 
 | 609 |     if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 610 |         mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 |     } | 
 | 612 |  | 
 | 613 |     switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 614 |         case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 615 |             ConfigurationChangedEntry* typedEntry = | 
 | 616 |                     static_cast<ConfigurationChangedEntry*>(mPendingEvent); | 
 | 617 |             done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 618 |             dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 619 |             break; | 
 | 620 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 622 |         case EventEntry::Type::DEVICE_RESET: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 623 |             DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); | 
 | 624 |             done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 625 |             dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 626 |             break; | 
 | 627 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 628 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 629 |         case EventEntry::Type::FOCUS: { | 
 | 630 |             FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent); | 
 | 631 |             dispatchFocusLocked(currentTime, typedEntry); | 
 | 632 |             done = true; | 
 | 633 |             dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
 | 634 |             break; | 
 | 635 |         } | 
 | 636 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 637 |         case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 638 |             KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); | 
 | 639 |             if (isAppSwitchDue) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 640 |                 if (isAppSwitchKeyEvent(*typedEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 641 |                     resetPendingAppSwitchLocked(true); | 
 | 642 |                     isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 643 |                 } else if (dropReason == DropReason::NOT_DROPPED) { | 
 | 644 |                     dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 645 |                 } | 
 | 646 |             } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 647 |             if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 648 |                 dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 649 |             } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 650 |             if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
 | 651 |                 dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 652 |             } | 
 | 653 |             done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); | 
 | 654 |             break; | 
 | 655 |         } | 
 | 656 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 657 |         case EventEntry::Type::MOTION: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 658 |             MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 659 |             if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
 | 660 |                 dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 661 |             } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 662 |             if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *typedEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 663 |                 dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 664 |             } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 665 |             if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
 | 666 |                 dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 667 |             } | 
 | 668 |             done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); | 
 | 669 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 671 |     } | 
 | 672 |  | 
 | 673 |     if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 674 |         if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 675 |             dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 676 |         } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 677 |         mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 678 |  | 
 | 679 |         releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 680 |         *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 |     } | 
 | 682 | } | 
 | 683 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 684 | /** | 
 | 685 |  * Return true if the events preceding this incoming motion event should be dropped | 
 | 686 |  * Return false otherwise (the default behaviour) | 
 | 687 |  */ | 
 | 688 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 689 |     const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 690 |             (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 691 |  | 
 | 692 |     // Optimize case where the current application is unresponsive and the user | 
 | 693 |     // decides to touch a window in a different application. | 
 | 694 |     // If the application takes too long to catch up then we drop all events preceding | 
 | 695 |     // the touch into the other window. | 
 | 696 |     if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 697 |         int32_t displayId = motionEntry.displayId; | 
 | 698 |         int32_t x = static_cast<int32_t>( | 
 | 699 |                 motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
 | 700 |         int32_t y = static_cast<int32_t>( | 
 | 701 |                 motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
 | 702 |         sp<InputWindowHandle> touchedWindowHandle = | 
 | 703 |                 findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
 | 704 |         if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 705 |             touchedWindowHandle->getApplicationToken() != | 
 | 706 |                     mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 707 |             // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 708 |             ALOGI("Pruning input queue because user touched a different application while waiting " | 
 | 709 |                   "for %s", | 
 | 710 |                   mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 711 |             return true; | 
 | 712 |         } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 713 |  | 
 | 714 |         // Alternatively, maybe there's a gesture monitor that could handle this event | 
 | 715 |         std::vector<TouchedMonitor> gestureMonitors = | 
 | 716 |                 findTouchedGestureMonitorsLocked(displayId, {}); | 
 | 717 |         for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
 | 718 |             sp<Connection> connection = | 
 | 719 |                     getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 720 |             if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 721 |                 // This monitor could take more input. Drop all events preceding this | 
 | 722 |                 // event, so that gesture monitor could get a chance to receive the stream | 
 | 723 |                 ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
 | 724 |                       "responsive gesture monitor that may handle the event", | 
 | 725 |                       mAwaitedFocusedApplication->getName().c_str()); | 
 | 726 |                 return true; | 
 | 727 |             } | 
 | 728 |         } | 
 | 729 |     } | 
 | 730 |  | 
 | 731 |     // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
 | 732 |     // yet been processed by some connections, the dispatcher will wait for these motion | 
 | 733 |     // events to be processed before dispatching the key event. This is because these motion events | 
 | 734 |     // may cause a new window to be launched, which the user might expect to receive focus. | 
 | 735 |     // To prevent waiting forever for such events, just send the key to the currently focused window | 
 | 736 |     if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
 | 737 |         ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
 | 738 |               "just send the pending key event to the focused window."); | 
 | 739 |         mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 740 |     } | 
 | 741 |     return false; | 
 | 742 | } | 
 | 743 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 744 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 745 |     bool needWake = mInboundQueue.empty(); | 
 | 746 |     mInboundQueue.push_back(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 |     traceInboundQueueLengthLocked(); | 
 | 748 |  | 
 | 749 |     switch (entry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 750 |         case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 751 |             // Optimize app switch latency. | 
 | 752 |             // If the application takes too long to catch up then we drop all events preceding | 
 | 753 |             // the app switch key. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 754 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 755 |             if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 756 |                 if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 757 |                     mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 758 |                 } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 759 |                     if (mAppSwitchSawKeyDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 760 | #if DEBUG_APP_SWITCH | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 761 |                         ALOGD("App switch is pending!"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 762 | #endif | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 763 |                         mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 764 |                         mAppSwitchSawKeyDown = false; | 
 | 765 |                         needWake = true; | 
 | 766 |                     } | 
 | 767 |                 } | 
 | 768 |             } | 
 | 769 |             break; | 
 | 770 |         } | 
 | 771 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 772 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 773 |             if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(*entry))) { | 
 | 774 |                 mNextUnblockedEvent = entry; | 
 | 775 |                 needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 |             } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 777 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 778 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 779 |         case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 780 |             LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
 | 781 |             break; | 
 | 782 |         } | 
 | 783 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 784 |         case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 785 |             // nothing to do | 
 | 786 |             break; | 
 | 787 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 |     } | 
 | 789 |  | 
 | 790 |     return needWake; | 
 | 791 | } | 
 | 792 |  | 
 | 793 | void InputDispatcher::addRecentEventLocked(EventEntry* entry) { | 
 | 794 |     entry->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 795 |     mRecentQueue.push_back(entry); | 
 | 796 |     if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
 | 797 |         mRecentQueue.front()->release(); | 
 | 798 |         mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 |     } | 
 | 800 | } | 
 | 801 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 802 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 803 |                                                                  int32_t y, TouchState* touchState, | 
 | 804 |                                                                  bool addOutsideTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 805 |                                                                  bool addPortalWindows) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 806 |     if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { | 
 | 807 |         LOG_ALWAYS_FATAL( | 
 | 808 |                 "Must provide a valid touch state if adding portal windows or outside targets"); | 
 | 809 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 |     // Traverse windows from front to back to find touched window. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 811 |     const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); | 
 | 812 |     for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 |         const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
 | 814 |         if (windowInfo->displayId == displayId) { | 
 | 815 |             int32_t flags = windowInfo->layoutParamsFlags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 816 |  | 
 | 817 |             if (windowInfo->visible) { | 
 | 818 |                 if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 819 |                     bool isTouchModal = (flags & | 
 | 820 |                                          (InputWindowInfo::FLAG_NOT_FOCUSABLE | | 
 | 821 |                                           InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 |                     if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 823 |                         int32_t portalToDisplayId = windowInfo->portalToDisplayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 824 |                         if (portalToDisplayId != ADISPLAY_ID_NONE && | 
 | 825 |                             portalToDisplayId != displayId) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 826 |                             if (addPortalWindows) { | 
 | 827 |                                 // For the monitoring channels of the display. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 828 |                                 touchState->addPortalWindow(windowHandle); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 829 |                             } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 830 |                             return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 831 |                                                              addOutsideTargets, addPortalWindows); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 832 |                         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 833 |                         // Found window. | 
 | 834 |                         return windowHandle; | 
 | 835 |                     } | 
 | 836 |                 } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 837 |  | 
 | 838 |                 if (addOutsideTargets && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 839 |                     touchState->addOrUpdateWindow(windowHandle, | 
 | 840 |                                                   InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
 | 841 |                                                   BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 842 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 843 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 844 |         } | 
 | 845 |     } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 846 |     return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 847 | } | 
 | 848 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 849 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 850 |         int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 851 |     std::vector<TouchedMonitor> touchedMonitors; | 
 | 852 |  | 
 | 853 |     std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
 | 854 |     addGestureMonitors(monitors, touchedMonitors); | 
 | 855 |     for (const sp<InputWindowHandle>& portalWindow : portalWindows) { | 
 | 856 |         const InputWindowInfo* windowInfo = portalWindow->getInfo(); | 
 | 857 |         monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 858 |         addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, | 
 | 859 |                            -windowInfo->frameTop); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 860 |     } | 
 | 861 |     return touchedMonitors; | 
 | 862 | } | 
 | 863 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 864 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 |     const char* reason; | 
 | 866 |     switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 867 |         case DropReason::POLICY: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 868 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 |             ALOGD("Dropped event because policy consumed it."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 871 |             reason = "inbound event was dropped because the policy consumed it"; | 
 | 872 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 873 |         case DropReason::DISABLED: | 
 | 874 |             if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 |                 ALOGI("Dropped event because input dispatch is disabled."); | 
 | 876 |             } | 
 | 877 |             reason = "inbound event was dropped because input dispatch is disabled"; | 
 | 878 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 879 |         case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 880 |             ALOGI("Dropped event because of pending overdue app switch."); | 
 | 881 |             reason = "inbound event was dropped because of pending overdue app switch"; | 
 | 882 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 883 |         case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 884 |             ALOGI("Dropped event because the current application is not responding and the user " | 
 | 885 |                   "has started interacting with a different application."); | 
 | 886 |             reason = "inbound event was dropped because the current application is not responding " | 
 | 887 |                      "and the user has started interacting with a different application"; | 
 | 888 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 889 |         case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 890 |             ALOGI("Dropped event because it is stale."); | 
 | 891 |             reason = "inbound event was dropped because it is stale"; | 
 | 892 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 893 |         case DropReason::NOT_DROPPED: { | 
 | 894 |             LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 895 |             return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 896 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 |     } | 
 | 898 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 899 |     switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 900 |         case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 |             CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
 | 902 |             synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 903 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 905 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 906 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
 | 907 |             if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 |                 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
 | 909 |                 synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 910 |             } else { | 
 | 911 |                 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
 | 912 |                 synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 913 |             } | 
 | 914 |             break; | 
 | 915 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 916 |         case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 917 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 918 |         case EventEntry::Type::DEVICE_RESET: { | 
 | 919 |             LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); | 
 | 920 |             break; | 
 | 921 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 |     } | 
 | 923 | } | 
 | 924 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 925 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 926 |     return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
 | 927 |             keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 928 | } | 
 | 929 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 930 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
 | 931 |     return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
 | 932 |             (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
 | 933 |             (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 934 | } | 
 | 935 |  | 
 | 936 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
 | 937 |     return mAppSwitchDueTime != LONG_LONG_MAX; | 
 | 938 | } | 
 | 939 |  | 
 | 940 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
 | 941 |     mAppSwitchDueTime = LONG_LONG_MAX; | 
 | 942 |  | 
 | 943 | #if DEBUG_APP_SWITCH | 
 | 944 |     if (handled) { | 
 | 945 |         ALOGD("App switch has arrived."); | 
 | 946 |     } else { | 
 | 947 |         ALOGD("App switch was abandoned."); | 
 | 948 |     } | 
 | 949 | #endif | 
 | 950 | } | 
 | 951 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 952 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 953 |     return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 954 | } | 
 | 955 |  | 
 | 956 | bool InputDispatcher::runCommandsLockedInterruptible() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 957 |     if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 958 |         return false; | 
 | 959 |     } | 
 | 960 |  | 
 | 961 |     do { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 962 |         std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 963 |         mCommandQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 |         Command command = commandEntry->command; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 965 |         command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 966 |  | 
 | 967 |         commandEntry->connection.clear(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 968 |     } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 969 |     return true; | 
 | 970 | } | 
 | 971 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 972 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { | 
 | 973 |     mCommandQueue.push_back(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 974 | } | 
 | 975 |  | 
 | 976 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 977 |     while (!mInboundQueue.empty()) { | 
 | 978 |         EventEntry* entry = mInboundQueue.front(); | 
 | 979 |         mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 980 |         releaseInboundEventLocked(entry); | 
 | 981 |     } | 
 | 982 |     traceInboundQueueLengthLocked(); | 
 | 983 | } | 
 | 984 |  | 
 | 985 | void InputDispatcher::releasePendingEventLocked() { | 
 | 986 |     if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 987 |         releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 988 |         mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 989 |     } | 
 | 990 | } | 
 | 991 |  | 
 | 992 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { | 
 | 993 |     InjectionState* injectionState = entry->injectionState; | 
 | 994 |     if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
 | 995 | #if DEBUG_DISPATCH_CYCLE | 
 | 996 |         ALOGD("Injected inbound event was dropped."); | 
 | 997 | #endif | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 998 |         setInjectionResult(entry, INPUT_EVENT_INJECTION_FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 999 |     } | 
 | 1000 |     if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1001 |         mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 |     } | 
 | 1003 |     addRecentEventLocked(entry); | 
 | 1004 |     entry->release(); | 
 | 1005 | } | 
 | 1006 |  | 
 | 1007 | void InputDispatcher::resetKeyRepeatLocked() { | 
 | 1008 |     if (mKeyRepeatState.lastKeyEntry) { | 
 | 1009 |         mKeyRepeatState.lastKeyEntry->release(); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1010 |         mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 |     } | 
 | 1012 | } | 
 | 1013 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1014 | KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 |     KeyEntry* entry = mKeyRepeatState.lastKeyEntry; | 
 | 1016 |  | 
 | 1017 |     // Reuse the repeated key entry if it is otherwise unreferenced. | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1018 |     uint32_t policyFlags = entry->policyFlags & | 
 | 1019 |             (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1020 |     if (entry->refCount == 1) { | 
 | 1021 |         entry->recycle(); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1022 |         entry->id = mIdGenerator.nextId(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1023 |         entry->eventTime = currentTime; | 
 | 1024 |         entry->policyFlags = policyFlags; | 
 | 1025 |         entry->repeatCount += 1; | 
 | 1026 |     } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1027 |         KeyEntry* newEntry = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1028 |                 new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1029 |                              entry->displayId, policyFlags, entry->action, entry->flags, | 
 | 1030 |                              entry->keyCode, entry->scanCode, entry->metaState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1031 |                              entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 |  | 
 | 1033 |         mKeyRepeatState.lastKeyEntry = newEntry; | 
 | 1034 |         entry->release(); | 
 | 1035 |  | 
 | 1036 |         entry = newEntry; | 
 | 1037 |     } | 
 | 1038 |     entry->syntheticRepeat = true; | 
 | 1039 |  | 
 | 1040 |     // Increment reference count since we keep a reference to the event in | 
 | 1041 |     // mKeyRepeatState.lastKeyEntry in addition to the one we return. | 
 | 1042 |     entry->refCount += 1; | 
 | 1043 |  | 
 | 1044 |     mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
 | 1045 |     return entry; | 
 | 1046 | } | 
 | 1047 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1048 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
 | 1049 |                                                          ConfigurationChangedEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1051 |     ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1052 | #endif | 
 | 1053 |  | 
 | 1054 |     // Reset key repeating in case a keyboard device was added or removed or something. | 
 | 1055 |     resetKeyRepeatLocked(); | 
 | 1056 |  | 
 | 1057 |     // 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] | 1058 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 1059 |             &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 |     commandEntry->eventTime = entry->eventTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1061 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 |     return true; | 
 | 1063 | } | 
 | 1064 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1065 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 1067 |     ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry->eventTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1068 |           entry->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1069 | #endif | 
 | 1070 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1071 |     CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1072 |     options.deviceId = entry->deviceId; | 
 | 1073 |     synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 1074 |     return true; | 
 | 1075 | } | 
 | 1076 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1077 | void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1078 |     if (mPendingEvent != nullptr) { | 
 | 1079 |         // Move the pending event to the front of the queue. This will give the chance | 
 | 1080 |         // for the pending event to get dispatched to the newly focused window | 
 | 1081 |         mInboundQueue.push_front(mPendingEvent); | 
 | 1082 |         mPendingEvent = nullptr; | 
 | 1083 |     } | 
 | 1084 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1085 |     FocusEntry* focusEntry = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1086 |             new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1087 |  | 
 | 1088 |     // This event should go to the front of the queue, but behind all other focus events | 
 | 1089 |     // Find the last focus event, and insert right after it | 
 | 1090 |     std::deque<EventEntry*>::reverse_iterator it = | 
 | 1091 |             std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
 | 1092 |                          [](EventEntry* event) { return event->type == EventEntry::Type::FOCUS; }); | 
 | 1093 |  | 
 | 1094 |     // Maintain the order of focus events. Insert the entry after all other focus events. | 
 | 1095 |     mInboundQueue.insert(it.base(), focusEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1096 | } | 
 | 1097 |  | 
 | 1098 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) { | 
 | 1099 |     sp<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
 | 1100 |     if (channel == nullptr) { | 
 | 1101 |         return; // Window has gone away | 
 | 1102 |     } | 
 | 1103 |     InputTarget target; | 
 | 1104 |     target.inputChannel = channel; | 
 | 1105 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 1106 |     entry->dispatchInProgress = true; | 
| Selim Cinek | 3d989c2 | 2020-06-17 21:42:12 +0000 | [diff] [blame] | 1107 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1108 |     dispatchEventLocked(currentTime, entry, {target}); | 
 | 1109 | } | 
 | 1110 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1111 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1112 |                                         DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1113 |     // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 |     if (!entry->dispatchInProgress) { | 
 | 1115 |         if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
 | 1116 |             (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
 | 1117 |             (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
 | 1118 |             if (mKeyRepeatState.lastKeyEntry && | 
 | 1119 |                 mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 |                 // We have seen two identical key downs in a row which indicates that the device | 
 | 1121 |                 // driver is automatically generating key repeats itself.  We take note of the | 
 | 1122 |                 // repeat here, but we disable our own next key repeat timer since it is clear that | 
 | 1123 |                 // we will not need to synthesize key repeats ourselves. | 
 | 1124 |                 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
 | 1125 |                 resetKeyRepeatLocked(); | 
 | 1126 |                 mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
 | 1127 |             } else { | 
 | 1128 |                 // Not a repeat.  Save key down state in case we do see a repeat later. | 
 | 1129 |                 resetKeyRepeatLocked(); | 
 | 1130 |                 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
 | 1131 |             } | 
 | 1132 |             mKeyRepeatState.lastKeyEntry = entry; | 
 | 1133 |             entry->refCount += 1; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1134 |         } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 |             resetKeyRepeatLocked(); | 
 | 1136 |         } | 
 | 1137 |  | 
 | 1138 |         if (entry->repeatCount == 1) { | 
 | 1139 |             entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
 | 1140 |         } else { | 
 | 1141 |             entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
 | 1142 |         } | 
 | 1143 |  | 
 | 1144 |         entry->dispatchInProgress = true; | 
 | 1145 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1146 |         logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 |     } | 
 | 1148 |  | 
 | 1149 |     // Handle case where the policy asked us to try again later last time. | 
 | 1150 |     if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
 | 1151 |         if (currentTime < entry->interceptKeyWakeupTime) { | 
 | 1152 |             if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
 | 1153 |                 *nextWakeupTime = entry->interceptKeyWakeupTime; | 
 | 1154 |             } | 
 | 1155 |             return false; // wait until next wakeup | 
 | 1156 |         } | 
 | 1157 |         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
 | 1158 |         entry->interceptKeyWakeupTime = 0; | 
 | 1159 |     } | 
 | 1160 |  | 
 | 1161 |     // Give the policy a chance to intercept the key. | 
 | 1162 |     if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
 | 1163 |         if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1164 |             std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
| Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1165 |                     &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1166 |             sp<InputWindowHandle> focusedWindowHandle = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1167 |                     getValueByKey(mFocusedWindowHandlesByDisplay, getTargetDisplayId(*entry)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1168 |             if (focusedWindowHandle != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1169 |                 commandEntry->inputChannel = getInputChannelLocked(focusedWindowHandle->getToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1170 |             } | 
 | 1171 |             commandEntry->keyEntry = entry; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1172 |             postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 |             entry->refCount += 1; | 
 | 1174 |             return false; // wait for the command to run | 
 | 1175 |         } else { | 
 | 1176 |             entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
 | 1177 |         } | 
 | 1178 |     } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1179 |         if (*dropReason == DropReason::NOT_DROPPED) { | 
 | 1180 |             *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 |         } | 
 | 1182 |     } | 
 | 1183 |  | 
 | 1184 |     // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1185 |     if (*dropReason != DropReason::NOT_DROPPED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1186 |         setInjectionResult(entry, | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1187 |                            *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1188 |                                                              : INPUT_EVENT_INJECTION_FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1189 |         mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 |         return true; | 
 | 1191 |     } | 
 | 1192 |  | 
 | 1193 |     // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1194 |     std::vector<InputTarget> inputTargets; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1195 |     int32_t injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1196 |             findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1197 |     if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
 | 1198 |         return false; | 
 | 1199 |     } | 
 | 1200 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1201 |     setInjectionResult(entry, injectionResult); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 |     if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { | 
 | 1203 |         return true; | 
 | 1204 |     } | 
 | 1205 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1206 |     // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1207 |     addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 |  | 
 | 1209 |     // Dispatch the key. | 
 | 1210 |     dispatchEventLocked(currentTime, entry, inputTargets); | 
 | 1211 |     return true; | 
 | 1212 | } | 
 | 1213 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1214 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1216 |     ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1217 |           "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
 | 1218 |           "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1219 |           prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
 | 1220 |           entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
 | 1221 |           entry.repeatCount, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | #endif | 
 | 1223 | } | 
 | 1224 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1225 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, | 
 | 1226 |                                            DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1227 |     ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 |     // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1229 |     if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 |         entry->dispatchInProgress = true; | 
 | 1231 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1232 |         logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 |     } | 
 | 1234 |  | 
 | 1235 |     // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1236 |     if (*dropReason != DropReason::NOT_DROPPED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1237 |         setInjectionResult(entry, | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1238 |                            *dropReason == DropReason::POLICY ? INPUT_EVENT_INJECTION_SUCCEEDED | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1239 |                                                              : INPUT_EVENT_INJECTION_FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 |         return true; | 
 | 1241 |     } | 
 | 1242 |  | 
 | 1243 |     bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
 | 1244 |  | 
 | 1245 |     // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1246 |     std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 |  | 
 | 1248 |     bool conflictingPointerActions = false; | 
 | 1249 |     int32_t injectionResult; | 
 | 1250 |     if (isPointerEvent) { | 
 | 1251 |         // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1252 |         injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1253 |                 findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1254 |                                                &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1255 |     } else { | 
 | 1256 |         // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1257 |         injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1258 |                 findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 |     } | 
 | 1260 |     if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { | 
 | 1261 |         return false; | 
 | 1262 |     } | 
 | 1263 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1264 |     setInjectionResult(entry, injectionResult); | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1265 |     if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { | 
 | 1266 |         ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
 | 1267 |         return true; | 
 | 1268 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 |     if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1270 |         CancelationOptions::Mode mode(isPointerEvent | 
 | 1271 |                                               ? CancelationOptions::CANCEL_POINTER_EVENTS | 
 | 1272 |                                               : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
 | 1273 |         CancelationOptions options(mode, "input event injection failed"); | 
 | 1274 |         synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 |         return true; | 
 | 1276 |     } | 
 | 1277 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1278 |     // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1279 |     addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1280 |  | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1281 |     if (isPointerEvent) { | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1282 |         std::unordered_map<int32_t, TouchState>::iterator it = | 
 | 1283 |                 mTouchStatesByDisplay.find(entry->displayId); | 
 | 1284 |         if (it != mTouchStatesByDisplay.end()) { | 
 | 1285 |             const TouchState& state = it->second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1286 |             if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1287 |                 // The event has gone through these portal windows, so we add monitoring targets of | 
 | 1288 |                 // the corresponding displays as well. | 
 | 1289 |                 for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1290 |                     const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1291 |                     addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1292 |                                                      -windowInfo->frameLeft, -windowInfo->frameTop); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1293 |                 } | 
 | 1294 |             } | 
 | 1295 |         } | 
 | 1296 |     } | 
 | 1297 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 |     // Dispatch the motion. | 
 | 1299 |     if (conflictingPointerActions) { | 
 | 1300 |         CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1301 |                                    "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 |         synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 1303 |     } | 
 | 1304 |     dispatchEventLocked(currentTime, entry, inputTargets); | 
 | 1305 |     return true; | 
 | 1306 | } | 
 | 1307 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1308 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1310 |     ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1311 |           ", policyFlags=0x%x, " | 
 | 1312 |           "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
 | 1313 |           "metaState=0x%x, buttonState=0x%x," | 
 | 1314 |           "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1315 |           prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
 | 1316 |           entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, | 
 | 1317 |           entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1318 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1319 |     for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1320 |         ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1321 |               "x=%f, y=%f, pressure=%f, size=%f, " | 
 | 1322 |               "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
 | 1323 |               "orientation=%f", | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1324 |               i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
 | 1325 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
 | 1326 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
 | 1327 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
 | 1328 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
 | 1329 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
 | 1330 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
 | 1331 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
 | 1332 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
 | 1333 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1334 |     } | 
 | 1335 | #endif | 
 | 1336 | } | 
 | 1337 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1338 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, EventEntry* eventEntry, | 
 | 1339 |                                           const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1340 |     ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1341 | #if DEBUG_DISPATCH_CYCLE | 
 | 1342 |     ALOGD("dispatchEventToCurrentInputTargets"); | 
 | 1343 | #endif | 
 | 1344 |  | 
 | 1345 |     ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
 | 1346 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1347 |     pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1349 |     for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1350 |         sp<Connection> connection = | 
 | 1351 |                 getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1352 |         if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1353 |             prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1354 |         } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1355 |             if (DEBUG_FOCUS) { | 
 | 1356 |                 ALOGD("Dropping event delivery to target with channel '%s' because it " | 
 | 1357 |                       "is no longer registered with the input dispatcher.", | 
 | 1358 |                       inputTarget.inputChannel->getName().c_str()); | 
 | 1359 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1360 |         } | 
 | 1361 |     } | 
 | 1362 | } | 
 | 1363 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1364 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
 | 1365 |     // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
 | 1366 |     // If the policy decides to close the app, we will get a channel removal event via | 
 | 1367 |     // unregisterInputChannel, and will clean up the connection that way. We are already not | 
 | 1368 |     // sending new pointers to the connection when it blocked, but focused events will continue to | 
 | 1369 |     // pile up. | 
 | 1370 |     ALOGW("Canceling events for %s because it is unresponsive", | 
 | 1371 |           connection->inputChannel->getName().c_str()); | 
 | 1372 |     if (connection->status == Connection::STATUS_NORMAL) { | 
 | 1373 |         CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
 | 1374 |                                    "application not responding"); | 
 | 1375 |         synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 |     } | 
 | 1377 | } | 
 | 1378 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1379 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1380 |     if (DEBUG_FOCUS) { | 
 | 1381 |         ALOGD("Resetting ANR timeouts."); | 
 | 1382 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1383 |  | 
 | 1384 |     // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1385 |     mNoFocusedWindowTimeoutTime = std::nullopt; | 
 | 1386 |     mAwaitedFocusedApplication.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1387 | } | 
 | 1388 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1389 | /** | 
 | 1390 |  * Get the display id that the given event should go to. If this event specifies a valid display id, | 
 | 1391 |  * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
 | 1392 |  * Focused display is the display that the user most recently interacted with. | 
 | 1393 |  */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1394 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1395 |     int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1396 |     switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1397 |         case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1398 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
 | 1399 |             displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1400 |             break; | 
 | 1401 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1402 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1403 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
 | 1404 |             displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1405 |             break; | 
 | 1406 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1407 |         case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1408 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 1409 |         case EventEntry::Type::DEVICE_RESET: { | 
 | 1410 |             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] | 1411 |             return ADISPLAY_ID_NONE; | 
 | 1412 |         } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1413 |     } | 
 | 1414 |     return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
 | 1415 | } | 
 | 1416 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1417 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
 | 1418 |                                                 const char* focusedWindowName) { | 
 | 1419 |     if (mAnrTracker.empty()) { | 
 | 1420 |         // already processed all events that we waited for | 
 | 1421 |         mKeyIsWaitingForEventsTimeout = std::nullopt; | 
 | 1422 |         return false; | 
 | 1423 |     } | 
 | 1424 |  | 
 | 1425 |     if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
 | 1426 |         // Start the timer | 
 | 1427 |         ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " | 
 | 1428 |               "focus to change", | 
 | 1429 |               focusedWindowName); | 
 | 1430 |         mKeyIsWaitingForEventsTimeout = currentTime + KEY_WAITING_FOR_EVENTS_TIMEOUT.count(); | 
 | 1431 |         return true; | 
 | 1432 |     } | 
 | 1433 |  | 
 | 1434 |     // We still have pending events, and already started the timer | 
 | 1435 |     if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
 | 1436 |         return true; // Still waiting | 
 | 1437 |     } | 
 | 1438 |  | 
 | 1439 |     // Waited too long, and some connection still hasn't processed all motions | 
 | 1440 |     // Just send the key to the focused window | 
 | 1441 |     ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
 | 1442 |           focusedWindowName); | 
 | 1443 |     mKeyIsWaitingForEventsTimeout = std::nullopt; | 
 | 1444 |     return false; | 
 | 1445 | } | 
 | 1446 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1447 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1448 |                                                         const EventEntry& entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1449 |                                                         std::vector<InputTarget>& inputTargets, | 
 | 1450 |                                                         nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1451 |     std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1452 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1453 |     int32_t displayId = getTargetDisplayId(entry); | 
 | 1454 |     sp<InputWindowHandle> focusedWindowHandle = | 
 | 1455 |             getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
 | 1456 |     sp<InputApplicationHandle> focusedApplicationHandle = | 
 | 1457 |             getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
 | 1458 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1459 |     // If there is no currently focused window and no focused application | 
 | 1460 |     // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1461 |     if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
 | 1462 |         ALOGI("Dropping %s event because there is no focused window or focused application in " | 
 | 1463 |               "display %" PRId32 ".", | 
 | 1464 |               EventEntry::typeToString(entry.type), displayId); | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1465 |         return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1466 |     } | 
 | 1467 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1468 |     // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
 | 1469 |     // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
 | 1470 |     // start interacting with another application via touch (app switch). This code can be removed | 
 | 1471 |     // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
 | 1472 |     // an app is expected to have a focused window. | 
 | 1473 |     if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
 | 1474 |         if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
 | 1475 |             // We just discovered that there's no focused window. Start the ANR timer | 
 | 1476 |             const nsecs_t timeout = focusedApplicationHandle->getDispatchingTimeout( | 
 | 1477 |                     DEFAULT_INPUT_DISPATCHING_TIMEOUT.count()); | 
 | 1478 |             mNoFocusedWindowTimeoutTime = currentTime + timeout; | 
 | 1479 |             mAwaitedFocusedApplication = focusedApplicationHandle; | 
 | 1480 |             ALOGW("Waiting because no window has focus but %s may eventually add a " | 
 | 1481 |                   "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
 | 1482 |                   mAwaitedFocusedApplication->getName().c_str(), ns2ms(timeout)); | 
 | 1483 |             *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
 | 1484 |             return INPUT_EVENT_INJECTION_PENDING; | 
 | 1485 |         } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
 | 1486 |             // Already raised ANR. Drop the event | 
 | 1487 |             ALOGE("Dropping %s event because there is no focused window", | 
 | 1488 |                   EventEntry::typeToString(entry.type)); | 
 | 1489 |             return INPUT_EVENT_INJECTION_FAILED; | 
 | 1490 |         } else { | 
 | 1491 |             // Still waiting for the focused window | 
 | 1492 |             return INPUT_EVENT_INJECTION_PENDING; | 
 | 1493 |         } | 
 | 1494 |     } | 
 | 1495 |  | 
 | 1496 |     // we have a valid, non-null focused window | 
 | 1497 |     resetNoFocusedWindowTimeoutLocked(); | 
 | 1498 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1499 |     // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1500 |     if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1501 |         return INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1502 |     } | 
 | 1503 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1504 |     if (focusedWindowHandle->getInfo()->paused) { | 
 | 1505 |         ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
 | 1506 |         return INPUT_EVENT_INJECTION_PENDING; | 
 | 1507 |     } | 
 | 1508 |  | 
 | 1509 |     // If the event is a key event, then we must wait for all previous events to | 
 | 1510 |     // complete before delivering it because previous events may have the | 
 | 1511 |     // side-effect of transferring focus to a different window and we want to | 
 | 1512 |     // ensure that the following keys are sent to the new window. | 
 | 1513 |     // | 
 | 1514 |     // Suppose the user touches a button in a window then immediately presses "A". | 
 | 1515 |     // If the button causes a pop-up window to appear then we want to ensure that | 
 | 1516 |     // the "A" key is delivered to the new pop-up window.  This is because users | 
 | 1517 |     // often anticipate pending UI changes when typing on a keyboard. | 
 | 1518 |     // To obtain this behavior, we must serialize key events with respect to all | 
 | 1519 |     // prior input events. | 
 | 1520 |     if (entry.type == EventEntry::Type::KEY) { | 
 | 1521 |         if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
 | 1522 |             *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
 | 1523 |             return INPUT_EVENT_INJECTION_PENDING; | 
 | 1524 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 |     } | 
 | 1526 |  | 
 | 1527 |     // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1528 |     addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1529 |                           InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
 | 1530 |                           BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 |  | 
 | 1532 |     // Done. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1533 |     return INPUT_EVENT_INJECTION_SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1534 | } | 
 | 1535 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1536 | /** | 
 | 1537 |  * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
 | 1538 |  * that are currently unresponsive. | 
 | 1539 |  */ | 
 | 1540 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
 | 1541 |         const std::vector<TouchedMonitor>& monitors) const { | 
 | 1542 |     std::vector<TouchedMonitor> responsiveMonitors; | 
 | 1543 |     std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
 | 1544 |                  [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
 | 1545 |                      sp<Connection> connection = getConnectionLocked( | 
 | 1546 |                              monitor.monitor.inputChannel->getConnectionToken()); | 
 | 1547 |                      if (connection == nullptr) { | 
 | 1548 |                          ALOGE("Could not find connection for monitor %s", | 
 | 1549 |                                monitor.monitor.inputChannel->getName().c_str()); | 
 | 1550 |                          return false; | 
 | 1551 |                      } | 
 | 1552 |                      if (!connection->responsive) { | 
 | 1553 |                          ALOGW("Unresponsive monitor %s will not get the new gesture", | 
 | 1554 |                                connection->inputChannel->getName().c_str()); | 
 | 1555 |                          return false; | 
 | 1556 |                      } | 
 | 1557 |                      return true; | 
 | 1558 |                  }); | 
 | 1559 |     return responsiveMonitors; | 
 | 1560 | } | 
 | 1561 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1562 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1563 |                                                         const MotionEntry& entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1564 |                                                         std::vector<InputTarget>& inputTargets, | 
 | 1565 |                                                         nsecs_t* nextWakeupTime, | 
 | 1566 |                                                         bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1567 |     ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1568 |     enum InjectionPermission { | 
 | 1569 |         INJECTION_PERMISSION_UNKNOWN, | 
 | 1570 |         INJECTION_PERMISSION_GRANTED, | 
 | 1571 |         INJECTION_PERMISSION_DENIED | 
 | 1572 |     }; | 
 | 1573 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 |     // For security reasons, we defer updating the touch state until we are sure that | 
 | 1575 |     // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1576 |     int32_t displayId = entry.displayId; | 
 | 1577 |     int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 |     int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
 | 1579 |  | 
 | 1580 |     // Update the touch state as needed based on the properties of the touch event. | 
 | 1581 |     int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; | 
 | 1582 |     InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
 | 1583 |     sp<InputWindowHandle> newHoverWindowHandle; | 
 | 1584 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1585 |     // Copy current touch state into tempTouchState. | 
 | 1586 |     // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
 | 1587 |     // 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] | 1588 |     const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1589 |     TouchState tempTouchState; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1590 |     std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
 | 1591 |             mTouchStatesByDisplay.find(displayId); | 
 | 1592 |     if (oldStateIt != mTouchStatesByDisplay.end()) { | 
 | 1593 |         oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1594 |         tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1595 |     } | 
 | 1596 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1597 |     bool isSplit = tempTouchState.split; | 
 | 1598 |     bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
 | 1599 |             (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
 | 1600 |              tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1601 |     bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
 | 1602 |                           maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
 | 1603 |                           maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
 | 1604 |     bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
 | 1605 |                        maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1606 |     const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 |     bool wrongDevice = false; | 
 | 1608 |     if (newGesture) { | 
 | 1609 |         bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1610 |         if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1611 |             ALOGI("Dropping event because a pointer for a different device is already down " | 
 | 1612 |                   "in display %" PRId32, | 
 | 1613 |                   displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1614 |             // TODO: test multiple simultaneous input streams. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 |             injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
 | 1616 |             switchedDevice = false; | 
 | 1617 |             wrongDevice = true; | 
 | 1618 |             goto Failed; | 
 | 1619 |         } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1620 |         tempTouchState.reset(); | 
 | 1621 |         tempTouchState.down = down; | 
 | 1622 |         tempTouchState.deviceId = entry.deviceId; | 
 | 1623 |         tempTouchState.source = entry.source; | 
 | 1624 |         tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1625 |         isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1626 |     } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1627 |         ALOGI("Dropping move event because a pointer for a different device is already active " | 
 | 1628 |               "in display %" PRId32, | 
 | 1629 |               displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1630 |         // TODO: test multiple simultaneous input streams. | 
 | 1631 |         injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
 | 1632 |         switchedDevice = false; | 
 | 1633 |         wrongDevice = true; | 
 | 1634 |         goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1635 |     } | 
 | 1636 |  | 
 | 1637 |     if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
 | 1638 |         /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
 | 1639 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1640 |         int32_t x; | 
 | 1641 |         int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 |         int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1643 |         // Always dispatch mouse events to cursor position. | 
 | 1644 |         if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1645 |             x = int32_t(entry.xCursorPosition); | 
 | 1646 |             y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1647 |         } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1648 |             x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
 | 1649 |             y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1650 |         } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1651 |         bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1652 |         sp<InputWindowHandle> newTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1653 |                 findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
 | 1654 |                                           isDown /*addOutsideTargets*/, true /*addPortalWindows*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1655 |  | 
 | 1656 |         std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1657 |                 ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1658 |                 : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1660 |         // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1661 |         if (newTouchedWindowHandle != nullptr && | 
 | 1662 |             newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1663 |             // New window supports splitting, but we should never split mouse events. | 
 | 1664 |             isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1665 |         } else if (isSplit) { | 
 | 1666 |             // New window does not support splitting but we have already split events. | 
 | 1667 |             // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1668 |             newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1669 |         } | 
 | 1670 |  | 
 | 1671 |         // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1672 |         if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 |             // Try to assign the pointer to the first foreground window we find, if there is one. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1674 |             newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1675 |         } | 
 | 1676 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1677 |         if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
 | 1678 |             ALOGI("Not sending touch event to %s because it is paused", | 
 | 1679 |                   newTouchedWindowHandle->getName().c_str()); | 
 | 1680 |             newTouchedWindowHandle = nullptr; | 
 | 1681 |         } | 
 | 1682 |  | 
 | 1683 |         if (newTouchedWindowHandle != nullptr) { | 
 | 1684 |             sp<Connection> connection = getConnectionLocked(newTouchedWindowHandle->getToken()); | 
 | 1685 |             if (connection == nullptr) { | 
 | 1686 |                 ALOGI("Could not find connection for %s", | 
 | 1687 |                       newTouchedWindowHandle->getName().c_str()); | 
 | 1688 |                 newTouchedWindowHandle = nullptr; | 
 | 1689 |             } else if (!connection->responsive) { | 
 | 1690 |                 // don't send the new touch to an unresponsive window | 
 | 1691 |                 ALOGW("Unresponsive window %s will not get the new gesture at %" PRIu64, | 
 | 1692 |                       newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
 | 1693 |                 newTouchedWindowHandle = nullptr; | 
 | 1694 |             } | 
 | 1695 |         } | 
 | 1696 |  | 
 | 1697 |         // Also don't send the new touch event to unresponsive gesture monitors | 
 | 1698 |         newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
 | 1699 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1700 |         if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
 | 1701 |             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] | 1702 |                   "(%d, %d) in display %" PRId32 ".", | 
 | 1703 |                   x, y, displayId); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1704 |             injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
 | 1705 |             goto Failed; | 
 | 1706 |         } | 
 | 1707 |  | 
 | 1708 |         if (newTouchedWindowHandle != nullptr) { | 
 | 1709 |             // Set target flags. | 
 | 1710 |             int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 1711 |             if (isSplit) { | 
 | 1712 |                 targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 |             } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1714 |             if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
 | 1715 |                 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
 | 1716 |             } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
 | 1717 |                 targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
 | 1718 |             } | 
 | 1719 |  | 
 | 1720 |             // Update hover state. | 
 | 1721 |             if (isHoverAction) { | 
 | 1722 |                 newHoverWindowHandle = newTouchedWindowHandle; | 
 | 1723 |             } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { | 
 | 1724 |                 newHoverWindowHandle = mLastHoverWindowHandle; | 
 | 1725 |             } | 
 | 1726 |  | 
 | 1727 |             // Update the temporary touch state. | 
 | 1728 |             BitSet32 pointerIds; | 
 | 1729 |             if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1730 |                 uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1731 |                 pointerIds.markBit(pointerId); | 
 | 1732 |             } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1733 |             tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1734 |         } | 
 | 1735 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1736 |         tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 |     } else { | 
 | 1738 |         /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
 | 1739 |  | 
 | 1740 |         // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1741 |         if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1742 |             if (DEBUG_FOCUS) { | 
 | 1743 |                 ALOGD("Dropping event because the pointer is not down or we previously " | 
 | 1744 |                       "dropped the pointer down event in display %" PRId32, | 
 | 1745 |                       displayId); | 
 | 1746 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1747 |             injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
 | 1748 |             goto Failed; | 
 | 1749 |         } | 
 | 1750 |  | 
 | 1751 |         // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1752 |         if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1753 |             tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1754 |             int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
 | 1755 |             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] | 1756 |  | 
 | 1757 |             sp<InputWindowHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1758 |                     tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1759 |             sp<InputWindowHandle> newTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1760 |                     findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1761 |             if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
 | 1762 |                 oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1763 |                 if (DEBUG_FOCUS) { | 
 | 1764 |                     ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
 | 1765 |                           oldTouchedWindowHandle->getName().c_str(), | 
 | 1766 |                           newTouchedWindowHandle->getName().c_str(), displayId); | 
 | 1767 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1768 |                 // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1769 |                 tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
 | 1770 |                                                  InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
 | 1771 |                                                  BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1772 |  | 
 | 1773 |                 // Make a slippery entrance into the new window. | 
 | 1774 |                 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
 | 1775 |                     isSplit = true; | 
 | 1776 |                 } | 
 | 1777 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1778 |                 int32_t targetFlags = | 
 | 1779 |                         InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 |                 if (isSplit) { | 
 | 1781 |                     targetFlags |= InputTarget::FLAG_SPLIT; | 
 | 1782 |                 } | 
 | 1783 |                 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
 | 1784 |                     targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
 | 1785 |                 } | 
 | 1786 |  | 
 | 1787 |                 BitSet32 pointerIds; | 
 | 1788 |                 if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1789 |                     pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 |                 } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1791 |                 tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1792 |             } | 
 | 1793 |         } | 
 | 1794 |     } | 
 | 1795 |  | 
 | 1796 |     if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
 | 1797 |         // Let the previous window know that the hover sequence is over. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1798 |         if (mLastHoverWindowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1799 | #if DEBUG_HOVER | 
 | 1800 |             ALOGD("Sending hover exit event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1801 |                   mLastHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1802 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1803 |             tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
 | 1804 |                                              InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1805 |         } | 
 | 1806 |  | 
 | 1807 |         // Let the new window know that the hover sequence is starting. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1808 |         if (newHoverWindowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | #if DEBUG_HOVER | 
 | 1810 |             ALOGD("Sending hover enter event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1811 |                   newHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1812 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1813 |             tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
 | 1814 |                                              InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
 | 1815 |                                              BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 |         } | 
 | 1817 |     } | 
 | 1818 |  | 
 | 1819 |     // Check permission to inject into all touched foreground windows and ensure there | 
 | 1820 |     // is at least one touched foreground window. | 
 | 1821 |     { | 
 | 1822 |         bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1823 |         for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1824 |             if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
 | 1825 |                 haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1826 |                 if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1827 |                     injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; | 
 | 1828 |                     injectionPermission = INJECTION_PERMISSION_DENIED; | 
 | 1829 |                     goto Failed; | 
 | 1830 |                 } | 
 | 1831 |             } | 
 | 1832 |         } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1833 |         bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1834 |         if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1835 |             ALOGI("Dropping event because there is no touched foreground window in display " | 
 | 1836 |                   "%" PRId32 " or gesture monitor to receive it.", | 
 | 1837 |                   displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 |             injectionResult = INPUT_EVENT_INJECTION_FAILED; | 
 | 1839 |             goto Failed; | 
 | 1840 |         } | 
 | 1841 |  | 
 | 1842 |         // Permission granted to injection into all touched foreground windows. | 
 | 1843 |         injectionPermission = INJECTION_PERMISSION_GRANTED; | 
 | 1844 |     } | 
 | 1845 |  | 
 | 1846 |     // Check whether windows listening for outside touches are owned by the same UID. If it is | 
 | 1847 |     // set the policy flag that we will not reveal coordinate information to this window. | 
 | 1848 |     if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
 | 1849 |         sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1850 |                 tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1851 |         if (foregroundWindowHandle) { | 
 | 1852 |             const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1853 |             for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1854 |                 if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
 | 1855 |                     sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; | 
 | 1856 |                     if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1857 |                         tempTouchState.addOrUpdateWindow(inputWindowHandle, | 
 | 1858 |                                                          InputTarget::FLAG_ZERO_COORDS, | 
 | 1859 |                                                          BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1860 |                     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1861 |                 } | 
 | 1862 |             } | 
 | 1863 |         } | 
 | 1864 |     } | 
 | 1865 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1866 |     // If this is the first pointer going down and the touched window has a wallpaper | 
 | 1867 |     // then also add the touched wallpaper windows so they are locked in for the duration | 
 | 1868 |     // of the touch gesture. | 
 | 1869 |     // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
 | 1870 |     // engine only supports touch events.  We would need to add a mechanism similar | 
 | 1871 |     // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
 | 1872 |     if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
 | 1873 |         sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1874 |                 tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1875 |         if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1876 |             const std::vector<sp<InputWindowHandle>> windowHandles = | 
 | 1877 |                     getWindowHandlesLocked(displayId); | 
 | 1878 |             for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1879 |                 const InputWindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1880 |                 if (info->displayId == displayId && | 
 | 1881 |                     windowHandle->getInfo()->layoutParamsType == InputWindowInfo::TYPE_WALLPAPER) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1882 |                     tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1883 |                             .addOrUpdateWindow(windowHandle, | 
 | 1884 |                                                InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
 | 1885 |                                                        InputTarget:: | 
 | 1886 |                                                                FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
 | 1887 |                                                        InputTarget::FLAG_DISPATCH_AS_IS, | 
 | 1888 |                                                BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1889 |                 } | 
 | 1890 |             } | 
 | 1891 |         } | 
 | 1892 |     } | 
 | 1893 |  | 
 | 1894 |     // Success!  Output targets. | 
 | 1895 |     injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; | 
 | 1896 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1897 |     for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1898 |         addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1899 |                               touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1900 |     } | 
 | 1901 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1902 |     for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1903 |         addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1904 |                                   touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1905 |     } | 
 | 1906 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1907 |     // Drop the outside or hover touch windows since we will not care about them | 
 | 1908 |     // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1909 |     tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1910 |  | 
 | 1911 | Failed: | 
 | 1912 |     // Check injection permission once and for all. | 
 | 1913 |     if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1914 |         if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1915 |             injectionPermission = INJECTION_PERMISSION_GRANTED; | 
 | 1916 |         } else { | 
 | 1917 |             injectionPermission = INJECTION_PERMISSION_DENIED; | 
 | 1918 |         } | 
 | 1919 |     } | 
 | 1920 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1921 |     if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
 | 1922 |         return injectionResult; | 
 | 1923 |     } | 
 | 1924 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1925 |     // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1926 |     if (!wrongDevice) { | 
 | 1927 |         if (switchedDevice) { | 
 | 1928 |             if (DEBUG_FOCUS) { | 
 | 1929 |                 ALOGD("Conflicting pointer actions: Switched to a different device."); | 
 | 1930 |             } | 
 | 1931 |             *outConflictingPointerActions = true; | 
 | 1932 |         } | 
 | 1933 |  | 
 | 1934 |         if (isHoverAction) { | 
 | 1935 |             // Started hovering, therefore no longer down. | 
 | 1936 |             if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1937 |                 if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1938 |                     ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
 | 1939 |                           "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1940 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1941 |                 *outConflictingPointerActions = true; | 
 | 1942 |             } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1943 |             tempTouchState.reset(); | 
 | 1944 |             if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
 | 1945 |                 maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
 | 1946 |                 tempTouchState.deviceId = entry.deviceId; | 
 | 1947 |                 tempTouchState.source = entry.source; | 
 | 1948 |                 tempTouchState.displayId = displayId; | 
 | 1949 |             } | 
 | 1950 |         } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
 | 1951 |                    maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
 | 1952 |             // All pointers up or canceled. | 
 | 1953 |             tempTouchState.reset(); | 
 | 1954 |         } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
 | 1955 |             // First pointer went down. | 
 | 1956 |             if (oldState && oldState->down) { | 
 | 1957 |                 if (DEBUG_FOCUS) { | 
 | 1958 |                     ALOGD("Conflicting pointer actions: Down received while already down."); | 
 | 1959 |                 } | 
 | 1960 |                 *outConflictingPointerActions = true; | 
 | 1961 |             } | 
 | 1962 |         } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
 | 1963 |             // One pointer went up. | 
 | 1964 |             if (isSplit) { | 
 | 1965 |                 int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
 | 1966 |                 uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1967 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1968 |                 for (size_t i = 0; i < tempTouchState.windows.size();) { | 
 | 1969 |                     TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
 | 1970 |                     if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
 | 1971 |                         touchedWindow.pointerIds.clearBit(pointerId); | 
 | 1972 |                         if (touchedWindow.pointerIds.isEmpty()) { | 
 | 1973 |                             tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
 | 1974 |                             continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1975 |                         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 |                     } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1977 |                     i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1978 |                 } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1979 |             } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1980 |         } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1981 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1982 |         // Save changes unless the action was scroll in which case the temporary touch | 
 | 1983 |         // state was only valid for this one action. | 
 | 1984 |         if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
 | 1985 |             if (tempTouchState.displayId >= 0) { | 
 | 1986 |                 mTouchStatesByDisplay[displayId] = tempTouchState; | 
 | 1987 |             } else { | 
 | 1988 |                 mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1989 |             } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1990 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1991 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1992 |         // Update hover state. | 
 | 1993 |         mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 |     } | 
 | 1995 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1996 |     return injectionResult; | 
 | 1997 | } | 
 | 1998 |  | 
 | 1999 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2000 |                                             int32_t targetFlags, BitSet32 pointerIds, | 
 | 2001 |                                             std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2002 |     std::vector<InputTarget>::iterator it = | 
 | 2003 |             std::find_if(inputTargets.begin(), inputTargets.end(), | 
 | 2004 |                          [&windowHandle](const InputTarget& inputTarget) { | 
 | 2005 |                              return inputTarget.inputChannel->getConnectionToken() == | 
 | 2006 |                                      windowHandle->getToken(); | 
 | 2007 |                          }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2008 |  | 
| Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2009 |     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2010 |  | 
 | 2011 |     if (it == inputTargets.end()) { | 
 | 2012 |         InputTarget inputTarget; | 
 | 2013 |         sp<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); | 
 | 2014 |         if (inputChannel == nullptr) { | 
 | 2015 |             ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
 | 2016 |             return; | 
 | 2017 |         } | 
 | 2018 |         inputTarget.inputChannel = inputChannel; | 
 | 2019 |         inputTarget.flags = targetFlags; | 
 | 2020 |         inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
 | 2021 |         inputTargets.push_back(inputTarget); | 
 | 2022 |         it = inputTargets.end() - 1; | 
 | 2023 |     } | 
 | 2024 |  | 
 | 2025 |     ALOG_ASSERT(it->flags == targetFlags); | 
 | 2026 |     ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
 | 2027 |  | 
 | 2028 |     it->addPointers(pointerIds, -windowInfo->frameLeft, -windowInfo->frameTop, | 
 | 2029 |                     windowInfo->windowXScale, windowInfo->windowYScale); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2030 | } | 
 | 2031 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2032 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2033 |                                                        int32_t displayId, float xOffset, | 
 | 2034 |                                                        float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2035 |     std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
 | 2036 |             mGlobalMonitorsByDisplay.find(displayId); | 
 | 2037 |  | 
 | 2038 |     if (it != mGlobalMonitorsByDisplay.end()) { | 
 | 2039 |         const std::vector<Monitor>& monitors = it->second; | 
 | 2040 |         for (const Monitor& monitor : monitors) { | 
 | 2041 |             addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2042 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2043 |     } | 
 | 2044 | } | 
 | 2045 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2046 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
 | 2047 |                                                 float yOffset, | 
 | 2048 |                                                 std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2049 |     InputTarget target; | 
 | 2050 |     target.inputChannel = monitor.inputChannel; | 
 | 2051 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2052 |     target.setDefaultPointerInfo(xOffset, yOffset, 1 /* windowXScale */, 1 /* windowYScale */); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2053 |     inputTargets.push_back(target); | 
 | 2054 | } | 
 | 2055 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2056 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2057 |                                                const InjectionState* injectionState) { | 
 | 2058 |     if (injectionState && | 
 | 2059 |         (windowHandle == nullptr || | 
 | 2060 |          windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
 | 2061 |         !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2062 |         if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2063 |             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] | 2064 |                   "owned by uid %d", | 
 | 2065 |                   injectionState->injectorPid, injectionState->injectorUid, | 
 | 2066 |                   windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2067 |         } else { | 
 | 2068 |             ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2069 |                   injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 |         } | 
 | 2071 |         return false; | 
 | 2072 |     } | 
 | 2073 |     return true; | 
 | 2074 | } | 
 | 2075 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2076 | /** | 
 | 2077 |  * Indicate whether one window handle should be considered as obscuring | 
 | 2078 |  * another window handle. We only check a few preconditions. Actually | 
 | 2079 |  * checking the bounds is left to the caller. | 
 | 2080 |  */ | 
 | 2081 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, | 
 | 2082 |                             const sp<InputWindowHandle>& otherHandle) { | 
 | 2083 |     // Compare by token so cloned layers aren't counted | 
 | 2084 |     if (haveSameToken(windowHandle, otherHandle)) { | 
 | 2085 |         return false; | 
 | 2086 |     } | 
 | 2087 |     auto info = windowHandle->getInfo(); | 
 | 2088 |     auto otherInfo = otherHandle->getInfo(); | 
 | 2089 |     if (!otherInfo->visible) { | 
 | 2090 |         return false; | 
| Robert Carr | 98c34a8 | 2020-06-09 15:36:34 -0700 | [diff] [blame] | 2091 |     } else if (info->ownerPid == otherInfo->ownerPid) { | 
 | 2092 |         // If ownerPid is the same we don't generate occlusion events as there | 
 | 2093 |         // is no in-process security boundary. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2094 |         return false; | 
| Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2095 |     } else if (otherInfo->trustedOverlay) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2096 |         return false; | 
 | 2097 |     } else if (otherInfo->displayId != info->displayId) { | 
 | 2098 |         return false; | 
 | 2099 |     } | 
 | 2100 |     return true; | 
 | 2101 | } | 
 | 2102 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2103 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, | 
 | 2104 |                                                     int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 |     int32_t displayId = windowHandle->getInfo()->displayId; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2106 |     const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); | 
 | 2107 |     for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2108 |         if (windowHandle == otherHandle) { | 
 | 2109 |             break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2110 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2111 |         const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2112 |         if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2113 |             otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2114 |             return true; | 
 | 2115 |         } | 
 | 2116 |     } | 
 | 2117 |     return false; | 
 | 2118 | } | 
 | 2119 |  | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2120 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { | 
 | 2121 |     int32_t displayId = windowHandle->getInfo()->displayId; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2122 |     const std::vector<sp<InputWindowHandle>> windowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2123 |     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2124 |     for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2125 |         if (windowHandle == otherHandle) { | 
 | 2126 |             break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2127 |         } | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2128 |         const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2129 |         if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2130 |             otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2131 |             return true; | 
 | 2132 |         } | 
 | 2133 |     } | 
 | 2134 |     return false; | 
 | 2135 | } | 
 | 2136 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2137 | std::string InputDispatcher::getApplicationWindowLabel( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2138 |         const sp<InputApplicationHandle>& applicationHandle, | 
 | 2139 |         const sp<InputWindowHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2140 |     if (applicationHandle != nullptr) { | 
 | 2141 |         if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2142 |             return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2143 |         } else { | 
 | 2144 |             return applicationHandle->getName(); | 
 | 2145 |         } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2146 |     } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2147 |         return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2148 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2149 |         return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 |     } | 
 | 2151 | } | 
 | 2152 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2153 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2154 |     if (eventEntry.type == EventEntry::Type::FOCUS) { | 
 | 2155 |         // Focus events are passed to apps, but do not represent user activity. | 
 | 2156 |         return; | 
 | 2157 |     } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2158 |     int32_t displayId = getTargetDisplayId(eventEntry); | 
 | 2159 |     sp<InputWindowHandle> focusedWindowHandle = | 
 | 2160 |             getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
 | 2161 |     if (focusedWindowHandle != nullptr) { | 
 | 2162 |         const InputWindowInfo* info = focusedWindowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2163 |         if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { | 
 | 2164 | #if DEBUG_DISPATCH_CYCLE | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2165 |             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] | 2166 | #endif | 
 | 2167 |             return; | 
 | 2168 |         } | 
 | 2169 |     } | 
 | 2170 |  | 
 | 2171 |     int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2172 |     switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2173 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2174 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
 | 2175 |             if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2176 |                 return; | 
 | 2177 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2178 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2179 |             if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2180 |                 eventType = USER_ACTIVITY_EVENT_TOUCH; | 
 | 2181 |             } | 
 | 2182 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2184 |         case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2185 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
 | 2186 |             if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2187 |                 return; | 
 | 2188 |             } | 
 | 2189 |             eventType = USER_ACTIVITY_EVENT_BUTTON; | 
 | 2190 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2191 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2192 |         case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2193 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 2194 |         case EventEntry::Type::DEVICE_RESET: { | 
 | 2195 |             LOG_ALWAYS_FATAL("%s events are not user activity", | 
 | 2196 |                              EventEntry::typeToString(eventEntry.type)); | 
 | 2197 |             break; | 
 | 2198 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 |     } | 
 | 2200 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2201 |     std::unique_ptr<CommandEntry> commandEntry = | 
 | 2202 |             std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2203 |     commandEntry->eventTime = eventEntry.eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2204 |     commandEntry->userActivityEventType = eventType; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2205 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2206 | } | 
 | 2207 |  | 
 | 2208 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2209 |                                                  const sp<Connection>& connection, | 
 | 2210 |                                                  EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2211 |                                                  const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2212 |     if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2213 |         std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2214 |                 StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2215 |                              connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2216 |         ATRACE_NAME(message.c_str()); | 
 | 2217 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2218 | #if DEBUG_DISPATCH_CYCLE | 
 | 2219 |     ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2220 |           "globalScaleFactor=%f, pointerIds=0x%x %s", | 
 | 2221 |           connection->getInputChannelName().c_str(), inputTarget.flags, | 
 | 2222 |           inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
 | 2223 |           inputTarget.getPointerInfoString().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2224 | #endif | 
 | 2225 |  | 
 | 2226 |     // Skip this event if the connection status is not normal. | 
 | 2227 |     // We don't want to enqueue additional outbound events if the connection is broken. | 
 | 2228 |     if (connection->status != Connection::STATUS_NORMAL) { | 
 | 2229 | #if DEBUG_DISPATCH_CYCLE | 
 | 2230 |         ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2231 |               connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | #endif | 
 | 2233 |         return; | 
 | 2234 |     } | 
 | 2235 |  | 
 | 2236 |     // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2237 |     if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
 | 2238 |         LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
 | 2239 |                             "Entry type %s should not have FLAG_SPLIT", | 
 | 2240 |                             EventEntry::typeToString(eventEntry->type)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2241 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2242 |         const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2243 |         if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2244 |             MotionEntry* splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2245 |                     splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2246 |             if (!splitMotionEntry) { | 
 | 2247 |                 return; // split event was dropped | 
 | 2248 |             } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2249 |             if (DEBUG_FOCUS) { | 
 | 2250 |                 ALOGD("channel '%s' ~ Split motion event.", | 
 | 2251 |                       connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2252 |                 logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2253 |             } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2254 |             enqueueDispatchEntriesLocked(currentTime, connection, splitMotionEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2255 |             splitMotionEntry->release(); | 
 | 2256 |             return; | 
 | 2257 |         } | 
 | 2258 |     } | 
 | 2259 |  | 
 | 2260 |     // Not splitting.  Enqueue dispatch entries for the event as is. | 
 | 2261 |     enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
 | 2262 | } | 
 | 2263 |  | 
 | 2264 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2265 |                                                    const sp<Connection>& connection, | 
 | 2266 |                                                    EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2267 |                                                    const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2268 |     if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2269 |         std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2270 |                 StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2271 |                              connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2272 |         ATRACE_NAME(message.c_str()); | 
 | 2273 |     } | 
 | 2274 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2275 |     bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 |  | 
 | 2277 |     // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2278 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2279 |                                InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2280 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2281 |                                InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2282 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2283 |                                InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2284 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2285 |                                InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2286 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2287 |                                InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2288 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2289 |                                InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 |  | 
 | 2291 |     // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2292 |     if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2293 |         startDispatchCycleLocked(currentTime, connection); | 
 | 2294 |     } | 
 | 2295 | } | 
 | 2296 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2297 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
 | 2298 |                                                  EventEntry* eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2299 |                                                  const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2300 |                                                  int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2301 |     if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2302 |         std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
 | 2303 |                                            connection->getInputChannelName().c_str(), | 
 | 2304 |                                            dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2305 |         ATRACE_NAME(message.c_str()); | 
 | 2306 |     } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2307 |     int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 |     if (!(inputTargetFlags & dispatchMode)) { | 
 | 2309 |         return; | 
 | 2310 |     } | 
 | 2311 |     inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
 | 2312 |  | 
 | 2313 |     // This is a new event. | 
 | 2314 |     // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2315 |     std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2316 |             createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2317 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2318 |     // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
 | 2319 |     // different EventEntry than what was passed in. | 
 | 2320 |     EventEntry* newEntry = dispatchEntry->eventEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2321 |     // Apply target flags and update the connection's input state. | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2322 |     switch (newEntry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2323 |         case EventEntry::Type::KEY: { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2324 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2325 |             dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2326 |             dispatchEntry->resolvedAction = keyEntry.action; | 
 | 2327 |             dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2328 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2329 |             if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
 | 2330 |                                                  dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2331 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2332 |                 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", | 
 | 2333 |                       connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2334 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2335 |                 return; // skip the inconsistent event | 
 | 2336 |             } | 
 | 2337 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2339 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2340 |         case EventEntry::Type::MOTION: { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2341 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2342 |             // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
 | 2343 |             // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
 | 2344 |             constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
 | 2345 |                     static_cast<int32_t>(IdGenerator::Source::OTHER); | 
 | 2346 |             dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2347 |             if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
 | 2348 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
 | 2349 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
 | 2350 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
 | 2351 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
 | 2352 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
 | 2353 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
 | 2354 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
 | 2355 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
 | 2356 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
 | 2357 |             } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2358 |                 dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2359 |                 dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2360 |             } | 
 | 2361 |             if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2362 |                 !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
 | 2363 |                                                    motionEntry.displayId)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2365 |                 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " | 
 | 2366 |                       "event", | 
 | 2367 |                       connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2368 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2369 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
 | 2370 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2371 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2372 |             dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2373 |             if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
 | 2374 |                 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
 | 2375 |             } | 
 | 2376 |             if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
 | 2377 |                 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
 | 2378 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2379 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2380 |             if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
 | 2381 |                                                     dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2382 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2383 |                 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
 | 2384 |                       "event", | 
 | 2385 |                       connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2386 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2387 |                 return; // skip the inconsistent event | 
 | 2388 |             } | 
 | 2389 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2390 |             dispatchEntry->resolvedEventId = | 
 | 2391 |                     dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
 | 2392 |                     ? mIdGenerator.nextId() | 
 | 2393 |                     : motionEntry.id; | 
 | 2394 |             if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
 | 2395 |                 std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
 | 2396 |                                                    ") to MotionEvent(id=0x%" PRIx32 ").", | 
 | 2397 |                                                    motionEntry.id, dispatchEntry->resolvedEventId); | 
 | 2398 |                 ATRACE_NAME(message.c_str()); | 
 | 2399 |             } | 
 | 2400 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2401 |             dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2402 |                                             inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2403 |  | 
 | 2404 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2405 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2406 |         case EventEntry::Type::FOCUS: { | 
 | 2407 |             break; | 
 | 2408 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2409 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 2410 |         case EventEntry::Type::DEVICE_RESET: { | 
 | 2411 |             LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2412 |                              EventEntry::typeToString(newEntry->type)); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2413 |             break; | 
 | 2414 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2415 |     } | 
 | 2416 |  | 
 | 2417 |     // Remember that we are waiting for this dispatch to complete. | 
 | 2418 |     if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2419 |         incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2420 |     } | 
 | 2421 |  | 
 | 2422 |     // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2423 |     connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2424 |     traceOutboundQueueLength(connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2425 | } | 
 | 2426 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2427 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2428 |                                                       const sp<IBinder>& newToken) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2429 |     int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2430 |     uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
 | 2431 |     if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2432 |         return; | 
 | 2433 |     } | 
 | 2434 |  | 
 | 2435 |     sp<InputWindowHandle> inputWindowHandle = getWindowHandleLocked(newToken); | 
 | 2436 |     if (inputWindowHandle == nullptr) { | 
 | 2437 |         return; | 
 | 2438 |     } | 
 | 2439 |  | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2440 |     sp<InputWindowHandle> focusedWindowHandle = | 
| Tiger Huang | 0683fe7 | 2019-06-03 21:50:55 +0800 | [diff] [blame] | 2441 |             getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2442 |  | 
 | 2443 |     bool hasFocusChanged = !focusedWindowHandle || focusedWindowHandle->getToken() != newToken; | 
 | 2444 |  | 
 | 2445 |     if (!hasFocusChanged) { | 
 | 2446 |         return; | 
 | 2447 |     } | 
 | 2448 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2449 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 2450 |             &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2451 |     commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2452 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2453 | } | 
 | 2454 |  | 
 | 2455 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2456 |                                                const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2457 |     if (ATRACE_ENABLED()) { | 
 | 2458 |         std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2459 |                                            connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2460 |         ATRACE_NAME(message.c_str()); | 
 | 2461 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2462 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2463 |     ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2464 | #endif | 
 | 2465 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2466 |     while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
 | 2467 |         DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2468 |         dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2469 |         const nsecs_t timeout = | 
 | 2470 |                 getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
 | 2471 |         dispatchEntry->timeoutTime = currentTime + timeout; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2472 |  | 
 | 2473 |         // Publish the event. | 
 | 2474 |         status_t status; | 
 | 2475 |         EventEntry* eventEntry = dispatchEntry->eventEntry; | 
 | 2476 |         switch (eventEntry->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2477 |             case EventEntry::Type::KEY: { | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2478 |                 const KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); | 
 | 2479 |                 std::array<uint8_t, 32> hmac = getSignature(*keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2480 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2481 |                 // Publish the key event. | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2482 |                 status = | 
 | 2483 |                         connection->inputPublisher | 
 | 2484 |                                 .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId, | 
 | 2485 |                                                  keyEntry->deviceId, keyEntry->source, | 
 | 2486 |                                                  keyEntry->displayId, std::move(hmac), | 
 | 2487 |                                                  dispatchEntry->resolvedAction, | 
 | 2488 |                                                  dispatchEntry->resolvedFlags, keyEntry->keyCode, | 
 | 2489 |                                                  keyEntry->scanCode, keyEntry->metaState, | 
 | 2490 |                                                  keyEntry->repeatCount, keyEntry->downTime, | 
 | 2491 |                                                  keyEntry->eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2492 |                 break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2493 |             } | 
 | 2494 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2495 |             case EventEntry::Type::MOTION: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2496 |                 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2497 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2498 |                 PointerCoords scaledCoords[MAX_POINTERS]; | 
 | 2499 |                 const PointerCoords* usingCoords = motionEntry->pointerCoords; | 
 | 2500 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2501 |                 // Set the X and Y offset and X and Y scale depending on the input source. | 
 | 2502 |                 float xOffset = 0.0f, yOffset = 0.0f; | 
 | 2503 |                 float xScale = 1.0f, yScale = 1.0f; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2504 |                 if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) && | 
 | 2505 |                     !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
 | 2506 |                     float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2507 |                     xScale = dispatchEntry->windowXScale; | 
 | 2508 |                     yScale = dispatchEntry->windowYScale; | 
 | 2509 |                     xOffset = dispatchEntry->xOffset * xScale; | 
 | 2510 |                     yOffset = dispatchEntry->yOffset * yScale; | 
 | 2511 |                     if (globalScaleFactor != 1.0f) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2512 |                         for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { | 
 | 2513 |                             scaledCoords[i] = motionEntry->pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2514 |                             // Don't apply window scale here since we don't want scale to affect raw | 
 | 2515 |                             // coordinates. The scale will be sent back to the client and applied | 
 | 2516 |                             // later when requesting relative coordinates. | 
 | 2517 |                             scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
 | 2518 |                                                   1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2519 |                         } | 
 | 2520 |                         usingCoords = scaledCoords; | 
 | 2521 |                     } | 
 | 2522 |                 } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2523 |                     // We don't want the dispatch target to know. | 
 | 2524 |                     if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
 | 2525 |                         for (uint32_t i = 0; i < motionEntry->pointerCount; i++) { | 
 | 2526 |                             scaledCoords[i].clear(); | 
 | 2527 |                         } | 
 | 2528 |                         usingCoords = scaledCoords; | 
 | 2529 |                     } | 
 | 2530 |                 } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2531 |  | 
 | 2532 |                 std::array<uint8_t, 32> hmac = getSignature(*motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2533 |  | 
 | 2534 |                 // Publish the motion event. | 
 | 2535 |                 status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2536 |                                  .publishMotionEvent(dispatchEntry->seq, | 
 | 2537 |                                                      dispatchEntry->resolvedEventId, | 
 | 2538 |                                                      motionEntry->deviceId, motionEntry->source, | 
 | 2539 |                                                      motionEntry->displayId, std::move(hmac), | 
 | 2540 |                                                      dispatchEntry->resolvedAction, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2541 |                                                      motionEntry->actionButton, | 
 | 2542 |                                                      dispatchEntry->resolvedFlags, | 
 | 2543 |                                                      motionEntry->edgeFlags, motionEntry->metaState, | 
 | 2544 |                                                      motionEntry->buttonState, | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2545 |                                                      motionEntry->classification, xScale, yScale, | 
 | 2546 |                                                      xOffset, yOffset, motionEntry->xPrecision, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2547 |                                                      motionEntry->yPrecision, | 
 | 2548 |                                                      motionEntry->xCursorPosition, | 
 | 2549 |                                                      motionEntry->yCursorPosition, | 
 | 2550 |                                                      motionEntry->downTime, motionEntry->eventTime, | 
 | 2551 |                                                      motionEntry->pointerCount, | 
 | 2552 |                                                      motionEntry->pointerProperties, usingCoords); | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 2553 |                 reportTouchEventForStatistics(*motionEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2554 |                 break; | 
 | 2555 |             } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2556 |             case EventEntry::Type::FOCUS: { | 
 | 2557 |                 FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry); | 
 | 2558 |                 status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2559 |                                                                       focusEntry->id, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2560 |                                                                       focusEntry->hasFocus, | 
 | 2561 |                                                                       mInTouchMode); | 
 | 2562 |                 break; | 
 | 2563 |             } | 
 | 2564 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2565 |             case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 2566 |             case EventEntry::Type::DEVICE_RESET: { | 
 | 2567 |                 LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
 | 2568 |                                  EventEntry::typeToString(eventEntry->type)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2569 |                 return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2570 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2571 |         } | 
 | 2572 |  | 
 | 2573 |         // Check the result. | 
 | 2574 |         if (status) { | 
 | 2575 |             if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2576 |                 if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2577 |                     ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2578 |                           "This is unexpected because the wait queue is empty, so the pipe " | 
 | 2579 |                           "should be empty and we shouldn't have any problems writing an " | 
 | 2580 |                           "event to it, status=%d", | 
 | 2581 |                           connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2582 |                     abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
 | 2583 |                 } else { | 
 | 2584 |                     // Pipe is full and we are waiting for the app to finish process some events | 
 | 2585 |                     // before sending more events to it. | 
 | 2586 | #if DEBUG_DISPATCH_CYCLE | 
 | 2587 |                     ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2588 |                           "waiting for the application to catch up", | 
 | 2589 |                           connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2590 | #endif | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2591 |                 } | 
 | 2592 |             } else { | 
 | 2593 |                 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2594 |                       "status=%d", | 
 | 2595 |                       connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2596 |                 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
 | 2597 |             } | 
 | 2598 |             return; | 
 | 2599 |         } | 
 | 2600 |  | 
 | 2601 |         // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2602 |         connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
 | 2603 |                                                     connection->outboundQueue.end(), | 
 | 2604 |                                                     dispatchEntry)); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2605 |         traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2606 |         connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2607 |         if (connection->responsive) { | 
 | 2608 |             mAnrTracker.insert(dispatchEntry->timeoutTime, | 
 | 2609 |                                connection->inputChannel->getConnectionToken()); | 
 | 2610 |         } | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2611 |         traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2612 |     } | 
 | 2613 | } | 
 | 2614 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2615 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
 | 2616 |         const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
 | 2617 |     int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
 | 2618 |     if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
 | 2619 |         // Only sign events up and down events as the purely move events | 
 | 2620 |         // are tied to their up/down counterparts so signing would be redundant. | 
 | 2621 |         VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
 | 2622 |         verifiedEvent.actionMasked = actionMasked; | 
 | 2623 |         verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
 | 2624 |         return mHmacKeyManager.sign(verifiedEvent); | 
 | 2625 |     } | 
 | 2626 |     return INVALID_HMAC; | 
 | 2627 | } | 
 | 2628 |  | 
 | 2629 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
 | 2630 |         const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
 | 2631 |     VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
 | 2632 |     verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
 | 2633 |     verifiedEvent.action = dispatchEntry.resolvedAction; | 
 | 2634 |     return mHmacKeyManager.sign(verifiedEvent); | 
 | 2635 | } | 
 | 2636 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2637 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2638 |                                                 const sp<Connection>& connection, uint32_t seq, | 
 | 2639 |                                                 bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2640 | #if DEBUG_DISPATCH_CYCLE | 
 | 2641 |     ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2642 |           connection->getInputChannelName().c_str(), seq, toString(handled)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2643 | #endif | 
 | 2644 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2645 |     if (connection->status == Connection::STATUS_BROKEN || | 
 | 2646 |         connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2647 |         return; | 
 | 2648 |     } | 
 | 2649 |  | 
 | 2650 |     // Notify other system components and prepare to start the next dispatch cycle. | 
 | 2651 |     onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); | 
 | 2652 | } | 
 | 2653 |  | 
 | 2654 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2655 |                                                      const sp<Connection>& connection, | 
 | 2656 |                                                      bool notify) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2657 | #if DEBUG_DISPATCH_CYCLE | 
 | 2658 |     ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2659 |           connection->getInputChannelName().c_str(), toString(notify)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2660 | #endif | 
 | 2661 |  | 
 | 2662 |     // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2663 |     drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2664 |     traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2665 |     drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2666 |     traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2667 |  | 
 | 2668 |     // The connection appears to be unrecoverably broken. | 
 | 2669 |     // Ignore already broken or zombie connections. | 
 | 2670 |     if (connection->status == Connection::STATUS_NORMAL) { | 
 | 2671 |         connection->status = Connection::STATUS_BROKEN; | 
 | 2672 |  | 
 | 2673 |         if (notify) { | 
 | 2674 |             // Notify other system components. | 
 | 2675 |             onDispatchCycleBrokenLocked(currentTime, connection); | 
 | 2676 |         } | 
 | 2677 |     } | 
 | 2678 | } | 
 | 2679 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2680 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
 | 2681 |     while (!queue.empty()) { | 
 | 2682 |         DispatchEntry* dispatchEntry = queue.front(); | 
 | 2683 |         queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2684 |         releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2685 |     } | 
 | 2686 | } | 
 | 2687 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2688 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2689 |     if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2690 |         decrementPendingForegroundDispatches(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2691 |     } | 
 | 2692 |     delete dispatchEntry; | 
 | 2693 | } | 
 | 2694 |  | 
 | 2695 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { | 
 | 2696 |     InputDispatcher* d = static_cast<InputDispatcher*>(data); | 
 | 2697 |  | 
 | 2698 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2699 |         std::scoped_lock _l(d->mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2700 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2701 |         if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2702 |             ALOGE("Received spurious receive callback for unknown input channel.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2703 |                   "fd=%d, events=0x%x", | 
 | 2704 |                   fd, events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2705 |             return 0; // remove the callback | 
 | 2706 |         } | 
 | 2707 |  | 
 | 2708 |         bool notify; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2709 |         sp<Connection> connection = d->mConnectionsByFd[fd]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 |         if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
 | 2711 |             if (!(events & ALOOPER_EVENT_INPUT)) { | 
 | 2712 |                 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2713 |                       "events=0x%x", | 
 | 2714 |                       connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2715 |                 return 1; | 
 | 2716 |             } | 
 | 2717 |  | 
 | 2718 |             nsecs_t currentTime = now(); | 
 | 2719 |             bool gotOne = false; | 
 | 2720 |             status_t status; | 
 | 2721 |             for (;;) { | 
 | 2722 |                 uint32_t seq; | 
 | 2723 |                 bool handled; | 
 | 2724 |                 status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); | 
 | 2725 |                 if (status) { | 
 | 2726 |                     break; | 
 | 2727 |                 } | 
 | 2728 |                 d->finishDispatchCycleLocked(currentTime, connection, seq, handled); | 
 | 2729 |                 gotOne = true; | 
 | 2730 |             } | 
 | 2731 |             if (gotOne) { | 
 | 2732 |                 d->runCommandsLockedInterruptible(); | 
 | 2733 |                 if (status == WOULD_BLOCK) { | 
 | 2734 |                     return 1; | 
 | 2735 |                 } | 
 | 2736 |             } | 
 | 2737 |  | 
 | 2738 |             notify = status != DEAD_OBJECT || !connection->monitor; | 
 | 2739 |             if (notify) { | 
 | 2740 |                 ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2741 |                       connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2742 |             } | 
 | 2743 |         } else { | 
 | 2744 |             // Monitor channels are never explicitly unregistered. | 
 | 2745 |             // We do it automatically when the remote endpoint is closed so don't warn | 
 | 2746 |             // about them. | 
| arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 2747 |             const bool stillHaveWindowHandle = | 
 | 2748 |                     d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != | 
 | 2749 |                     nullptr; | 
 | 2750 |             notify = !connection->monitor && stillHaveWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2751 |             if (notify) { | 
 | 2752 |                 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2753 |                       "events=0x%x", | 
 | 2754 |                       connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2755 |             } | 
 | 2756 |         } | 
 | 2757 |  | 
 | 2758 |         // Unregister the channel. | 
 | 2759 |         d->unregisterInputChannelLocked(connection->inputChannel, notify); | 
 | 2760 |         return 0; // remove the callback | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2761 |     }             // release lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | } | 
 | 2763 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2764 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2765 |         const CancelationOptions& options) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2766 |     for (const auto& pair : mConnectionsByFd) { | 
 | 2767 |         synthesizeCancelationEventsForConnectionLocked(pair.second, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2768 |     } | 
 | 2769 | } | 
 | 2770 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2771 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2772 |         const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2773 |     synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
 | 2774 |     synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
 | 2775 | } | 
 | 2776 |  | 
 | 2777 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
 | 2778 |         const CancelationOptions& options, | 
 | 2779 |         std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
 | 2780 |     for (const auto& it : monitorsByDisplay) { | 
 | 2781 |         const std::vector<Monitor>& monitors = it.second; | 
 | 2782 |         for (const Monitor& monitor : monitors) { | 
 | 2783 |             synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2784 |         } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 2785 |     } | 
 | 2786 | } | 
 | 2787 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2788 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
 | 2789 |         const sp<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2790 |     sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2791 |     if (connection == nullptr) { | 
 | 2792 |         return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2793 |     } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2794 |  | 
 | 2795 |     synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2796 | } | 
 | 2797 |  | 
 | 2798 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
 | 2799 |         const sp<Connection>& connection, const CancelationOptions& options) { | 
 | 2800 |     if (connection->status == Connection::STATUS_BROKEN) { | 
 | 2801 |         return; | 
 | 2802 |     } | 
 | 2803 |  | 
 | 2804 |     nsecs_t currentTime = now(); | 
 | 2805 |  | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 2806 |     std::vector<EventEntry*> cancelationEvents = | 
 | 2807 |             connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2808 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2809 |     if (cancelationEvents.empty()) { | 
 | 2810 |         return; | 
 | 2811 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2812 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2813 |     ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
 | 2814 |           "with reality: %s, mode=%d.", | 
 | 2815 |           connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
 | 2816 |           options.mode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2817 | #endif | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2818 |  | 
 | 2819 |     InputTarget target; | 
 | 2820 |     sp<InputWindowHandle> windowHandle = | 
 | 2821 |             getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
 | 2822 |     if (windowHandle != nullptr) { | 
 | 2823 |         const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
 | 2824 |         target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, | 
 | 2825 |                                      windowInfo->windowXScale, windowInfo->windowYScale); | 
 | 2826 |         target.globalScaleFactor = windowInfo->globalScaleFactor; | 
 | 2827 |     } | 
 | 2828 |     target.inputChannel = connection->inputChannel; | 
 | 2829 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 2830 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2831 |     for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
 | 2832 |         EventEntry* cancelationEventEntry = cancelationEvents[i]; | 
 | 2833 |         switch (cancelationEventEntry->type) { | 
 | 2834 |             case EventEntry::Type::KEY: { | 
 | 2835 |                 logOutboundKeyDetails("cancel - ", | 
 | 2836 |                                       static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
 | 2837 |                 break; | 
| 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 |             case EventEntry::Type::MOTION: { | 
 | 2840 |                 logOutboundMotionDetails("cancel - ", | 
 | 2841 |                                          static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
 | 2842 |                 break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2843 |             } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2844 |             case EventEntry::Type::FOCUS: { | 
 | 2845 |                 LOG_ALWAYS_FATAL("Canceling focus events is not supported"); | 
 | 2846 |                 break; | 
 | 2847 |             } | 
 | 2848 |             case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 2849 |             case EventEntry::Type::DEVICE_RESET: { | 
 | 2850 |                 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
 | 2851 |                                  EventEntry::typeToString(cancelationEventEntry->type)); | 
 | 2852 |                 break; | 
 | 2853 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 |         } | 
 | 2855 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2856 |         enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref | 
 | 2857 |                                    target, InputTarget::FLAG_DISPATCH_AS_IS); | 
 | 2858 |  | 
 | 2859 |         cancelationEventEntry->release(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2860 |     } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 2861 |  | 
 | 2862 |     startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2863 | } | 
 | 2864 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2865 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
 | 2866 |         const sp<Connection>& connection) { | 
 | 2867 |     if (connection->status == Connection::STATUS_BROKEN) { | 
 | 2868 |         return; | 
 | 2869 |     } | 
 | 2870 |  | 
 | 2871 |     nsecs_t currentTime = now(); | 
 | 2872 |  | 
 | 2873 |     std::vector<EventEntry*> downEvents = | 
 | 2874 |             connection->inputState.synthesizePointerDownEvents(currentTime); | 
 | 2875 |  | 
 | 2876 |     if (downEvents.empty()) { | 
 | 2877 |         return; | 
 | 2878 |     } | 
 | 2879 |  | 
 | 2880 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 2881 |         ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
 | 2882 |               connection->getInputChannelName().c_str(), downEvents.size()); | 
 | 2883 | #endif | 
 | 2884 |  | 
 | 2885 |     InputTarget target; | 
 | 2886 |     sp<InputWindowHandle> windowHandle = | 
 | 2887 |             getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
 | 2888 |     if (windowHandle != nullptr) { | 
 | 2889 |         const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
 | 2890 |         target.setDefaultPointerInfo(-windowInfo->frameLeft, -windowInfo->frameTop, | 
 | 2891 |                                      windowInfo->windowXScale, windowInfo->windowYScale); | 
 | 2892 |         target.globalScaleFactor = windowInfo->globalScaleFactor; | 
 | 2893 |     } | 
 | 2894 |     target.inputChannel = connection->inputChannel; | 
 | 2895 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 2896 |  | 
 | 2897 |     for (EventEntry* downEventEntry : downEvents) { | 
 | 2898 |         switch (downEventEntry->type) { | 
 | 2899 |             case EventEntry::Type::MOTION: { | 
 | 2900 |                 logOutboundMotionDetails("down - ", | 
 | 2901 |                         static_cast<const MotionEntry&>(*downEventEntry)); | 
 | 2902 |                 break; | 
 | 2903 |             } | 
 | 2904 |  | 
 | 2905 |             case EventEntry::Type::KEY: | 
 | 2906 |             case EventEntry::Type::FOCUS: | 
 | 2907 |             case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 2908 |             case EventEntry::Type::DEVICE_RESET: { | 
 | 2909 |                 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
 | 2910 |                                      EventEntry::typeToString(downEventEntry->type)); | 
 | 2911 |                 break; | 
 | 2912 |             } | 
 | 2913 |         } | 
 | 2914 |  | 
 | 2915 |         enqueueDispatchEntryLocked(connection, downEventEntry, // increments ref | 
 | 2916 |                                    target, InputTarget::FLAG_DISPATCH_AS_IS); | 
 | 2917 |  | 
 | 2918 |         downEventEntry->release(); | 
 | 2919 |     } | 
 | 2920 |  | 
 | 2921 |     startDispatchCycleLocked(currentTime, connection); | 
 | 2922 | } | 
 | 2923 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2924 | MotionEntry* InputDispatcher::splitMotionEvent(const MotionEntry& originalMotionEntry, | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2925 |                                                BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2926 |     ALOG_ASSERT(pointerIds.value != 0); | 
 | 2927 |  | 
 | 2928 |     uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
 | 2929 |     PointerProperties splitPointerProperties[MAX_POINTERS]; | 
 | 2930 |     PointerCoords splitPointerCoords[MAX_POINTERS]; | 
 | 2931 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2932 |     uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2933 |     uint32_t splitPointerCount = 0; | 
 | 2934 |  | 
 | 2935 |     for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2936 |          originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2937 |         const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2938 |                 originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 |         uint32_t pointerId = uint32_t(pointerProperties.id); | 
 | 2940 |         if (pointerIds.hasBit(pointerId)) { | 
 | 2941 |             splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
 | 2942 |             splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
 | 2943 |             splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2944 |                     originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 |             splitPointerCount += 1; | 
 | 2946 |         } | 
 | 2947 |     } | 
 | 2948 |  | 
 | 2949 |     if (splitPointerCount != pointerIds.count()) { | 
 | 2950 |         // This is bad.  We are missing some of the pointers that we expected to deliver. | 
 | 2951 |         // Most likely this indicates that we received an ACTION_MOVE events that has | 
 | 2952 |         // different pointer ids than we expected based on the previous ACTION_DOWN | 
 | 2953 |         // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
 | 2954 |         // in this way. | 
 | 2955 |         ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2956 |               "we expected there to be %d pointers.  This probably means we received " | 
 | 2957 |               "a broken sequence of pointer ids from the input device.", | 
 | 2958 |               splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2959 |         return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2960 |     } | 
 | 2961 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2962 |     int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2963 |     int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2964 |     if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
 | 2965 |         maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2966 |         int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
 | 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 |             if (pointerIds.count() == 1) { | 
 | 2972 |                 // The first/last pointer went down/up. | 
 | 2973 |                 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2974 |                         ? AMOTION_EVENT_ACTION_DOWN | 
 | 2975 |                         : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2976 |             } else { | 
 | 2977 |                 // A secondary pointer went down/up. | 
 | 2978 |                 uint32_t splitPointerIndex = 0; | 
 | 2979 |                 while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
 | 2980 |                     splitPointerIndex += 1; | 
 | 2981 |                 } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2982 |                 action = maskedAction | | 
 | 2983 |                         (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2984 |             } | 
 | 2985 |         } else { | 
 | 2986 |             // An unrelated pointer changed. | 
 | 2987 |             action = AMOTION_EVENT_ACTION_MOVE; | 
 | 2988 |         } | 
 | 2989 |     } | 
 | 2990 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2991 |     int32_t newId = mIdGenerator.nextId(); | 
 | 2992 |     if (ATRACE_ENABLED()) { | 
 | 2993 |         std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
 | 2994 |                                            ") to MotionEvent(id=0x%" PRIx32 ").", | 
 | 2995 |                                            originalMotionEntry.id, newId); | 
 | 2996 |         ATRACE_NAME(message.c_str()); | 
 | 2997 |     } | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2998 |     MotionEntry* splitMotionEntry = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2999 |             new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId, | 
 | 3000 |                             originalMotionEntry.source, originalMotionEntry.displayId, | 
 | 3001 |                             originalMotionEntry.policyFlags, action, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3002 |                             originalMotionEntry.actionButton, originalMotionEntry.flags, | 
 | 3003 |                             originalMotionEntry.metaState, originalMotionEntry.buttonState, | 
 | 3004 |                             originalMotionEntry.classification, originalMotionEntry.edgeFlags, | 
 | 3005 |                             originalMotionEntry.xPrecision, originalMotionEntry.yPrecision, | 
 | 3006 |                             originalMotionEntry.xCursorPosition, | 
 | 3007 |                             originalMotionEntry.yCursorPosition, originalMotionEntry.downTime, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3008 |                             splitPointerCount, splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3009 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3010 |     if (originalMotionEntry.injectionState) { | 
 | 3011 |         splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3012 |         splitMotionEntry->injectionState->refCount += 1; | 
 | 3013 |     } | 
 | 3014 |  | 
 | 3015 |     return splitMotionEntry; | 
 | 3016 | } | 
 | 3017 |  | 
 | 3018 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
 | 3019 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3020 |     ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3021 | #endif | 
 | 3022 |  | 
 | 3023 |     bool needWake; | 
 | 3024 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3025 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3026 |  | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3027 |         ConfigurationChangedEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3028 |                 new ConfigurationChangedEntry(args->id, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3029 |         needWake = enqueueInboundEventLocked(newEntry); | 
 | 3030 |     } // release lock | 
 | 3031 |  | 
 | 3032 |     if (needWake) { | 
 | 3033 |         mLooper->wake(); | 
 | 3034 |     } | 
 | 3035 | } | 
 | 3036 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3037 | /** | 
 | 3038 |  * If one of the meta shortcuts is detected, process them here: | 
 | 3039 |  *     Meta + Backspace -> generate BACK | 
 | 3040 |  *     Meta + Enter -> generate HOME | 
 | 3041 |  * This will potentially overwrite keyCode and metaState. | 
 | 3042 |  */ | 
 | 3043 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3044 |                                               int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3045 |     if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
 | 3046 |         int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
 | 3047 |         if (keyCode == AKEYCODE_DEL) { | 
 | 3048 |             newKeyCode = AKEYCODE_BACK; | 
 | 3049 |         } else if (keyCode == AKEYCODE_ENTER) { | 
 | 3050 |             newKeyCode = AKEYCODE_HOME; | 
 | 3051 |         } | 
 | 3052 |         if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3053 |             std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3054 |             struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3055 |             mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3056 |             keyCode = newKeyCode; | 
 | 3057 |             metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
 | 3058 |         } | 
 | 3059 |     } else if (action == AKEY_EVENT_ACTION_UP) { | 
 | 3060 |         // In order to maintain a consistent stream of up and down events, check to see if the key | 
 | 3061 |         // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
 | 3062 |         // 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] | 3063 |         std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3064 |         struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3065 |         auto replacementIt = mReplacedKeys.find(replacement); | 
 | 3066 |         if (replacementIt != mReplacedKeys.end()) { | 
 | 3067 |             keyCode = replacementIt->second; | 
 | 3068 |             mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3069 |             metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
 | 3070 |         } | 
 | 3071 |     } | 
 | 3072 | } | 
 | 3073 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3074 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
 | 3075 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3076 |     ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
 | 3077 |           "policyFlags=0x%x, action=0x%x, " | 
 | 3078 |           "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
 | 3079 |           args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
 | 3080 |           args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
 | 3081 |           args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3082 | #endif | 
 | 3083 |     if (!validateKeyEvent(args->action)) { | 
 | 3084 |         return; | 
 | 3085 |     } | 
 | 3086 |  | 
 | 3087 |     uint32_t policyFlags = args->policyFlags; | 
 | 3088 |     int32_t flags = args->flags; | 
 | 3089 |     int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3090 |     // InputDispatcher tracks and generates key repeats on behalf of | 
 | 3091 |     // whatever notifies it, so repeatCount should always be set to 0 | 
 | 3092 |     constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3093 |     if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
 | 3094 |         policyFlags |= POLICY_FLAG_VIRTUAL; | 
 | 3095 |         flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
 | 3096 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3097 |     if (policyFlags & POLICY_FLAG_FUNCTION) { | 
 | 3098 |         metaState |= AMETA_FUNCTION_ON; | 
 | 3099 |     } | 
 | 3100 |  | 
 | 3101 |     policyFlags |= POLICY_FLAG_TRUSTED; | 
 | 3102 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3103 |     int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3104 |     accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3105 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3106 |     KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3107 |     event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3108 |                      args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
 | 3109 |                      args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3110 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3111 |     android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3112 |     mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3113 |     if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3114 |         ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3115 |               std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3116 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3117 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3118 |     bool needWake; | 
 | 3119 |     { // acquire lock | 
 | 3120 |         mLock.lock(); | 
 | 3121 |  | 
 | 3122 |         if (shouldSendKeyToInputFilterLocked(args)) { | 
 | 3123 |             mLock.unlock(); | 
 | 3124 |  | 
 | 3125 |             policyFlags |= POLICY_FLAG_FILTERED; | 
 | 3126 |             if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
 | 3127 |                 return; // event was consumed by the filter | 
 | 3128 |             } | 
 | 3129 |  | 
 | 3130 |             mLock.lock(); | 
 | 3131 |         } | 
 | 3132 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3133 |         KeyEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3134 |                 new KeyEntry(args->id, args->eventTime, args->deviceId, args->source, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3135 |                              args->displayId, policyFlags, args->action, flags, keyCode, | 
 | 3136 |                              args->scanCode, metaState, repeatCount, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3137 |  | 
 | 3138 |         needWake = enqueueInboundEventLocked(newEntry); | 
 | 3139 |         mLock.unlock(); | 
 | 3140 |     } // release lock | 
 | 3141 |  | 
 | 3142 |     if (needWake) { | 
 | 3143 |         mLooper->wake(); | 
 | 3144 |     } | 
 | 3145 | } | 
 | 3146 |  | 
 | 3147 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
 | 3148 |     return mInputFilterEnabled; | 
 | 3149 | } | 
 | 3150 |  | 
 | 3151 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
 | 3152 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3153 |     ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
 | 3154 |           "displayId=%" PRId32 ", policyFlags=0x%x, " | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3155 |           "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
 | 3156 |           "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
| Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3157 |           "yCursorPosition=%f, downTime=%" PRId64, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3158 |           args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
 | 3159 |           args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
 | 3160 |           args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
 | 3161 |           args->xCursorPosition, args->yCursorPosition, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3162 |     for (uint32_t i = 0; i < args->pointerCount; i++) { | 
 | 3163 |         ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3164 |               "x=%f, y=%f, pressure=%f, size=%f, " | 
 | 3165 |               "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
 | 3166 |               "orientation=%f", | 
 | 3167 |               i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
 | 3168 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
 | 3169 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
 | 3170 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
 | 3171 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
 | 3172 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
 | 3173 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
 | 3174 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
 | 3175 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
 | 3176 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3177 |     } | 
 | 3178 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3179 |     if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
 | 3180 |                              args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3181 |         return; | 
 | 3182 |     } | 
 | 3183 |  | 
 | 3184 |     uint32_t policyFlags = args->policyFlags; | 
 | 3185 |     policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3186 |  | 
 | 3187 |     android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3188 |     mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3189 |     if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3190 |         ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3191 |               std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3192 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3193 |  | 
 | 3194 |     bool needWake; | 
 | 3195 |     { // acquire lock | 
 | 3196 |         mLock.lock(); | 
 | 3197 |  | 
 | 3198 |         if (shouldSendMotionToInputFilterLocked(args)) { | 
 | 3199 |             mLock.unlock(); | 
 | 3200 |  | 
 | 3201 |             MotionEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3202 |             event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
 | 3203 |                              args->action, args->actionButton, args->flags, args->edgeFlags, | 
 | 3204 |                              args->metaState, args->buttonState, args->classification, 1 /*xScale*/, | 
 | 3205 |                              1 /*yScale*/, 0 /* xOffset */, 0 /* yOffset */, args->xPrecision, | 
 | 3206 |                              args->yPrecision, args->xCursorPosition, args->yCursorPosition, | 
 | 3207 |                              args->downTime, args->eventTime, args->pointerCount, | 
 | 3208 |                              args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3209 |  | 
 | 3210 |             policyFlags |= POLICY_FLAG_FILTERED; | 
 | 3211 |             if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
 | 3212 |                 return; // event was consumed by the filter | 
 | 3213 |             } | 
 | 3214 |  | 
 | 3215 |             mLock.lock(); | 
 | 3216 |         } | 
 | 3217 |  | 
 | 3218 |         // Just enqueue a new motion event. | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3219 |         MotionEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3220 |                 new MotionEntry(args->id, args->eventTime, args->deviceId, args->source, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3221 |                                 args->displayId, policyFlags, args->action, args->actionButton, | 
 | 3222 |                                 args->flags, args->metaState, args->buttonState, | 
 | 3223 |                                 args->classification, args->edgeFlags, args->xPrecision, | 
 | 3224 |                                 args->yPrecision, args->xCursorPosition, args->yCursorPosition, | 
 | 3225 |                                 args->downTime, args->pointerCount, args->pointerProperties, | 
 | 3226 |                                 args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 |  | 
 | 3228 |         needWake = enqueueInboundEventLocked(newEntry); | 
 | 3229 |         mLock.unlock(); | 
 | 3230 |     } // release lock | 
 | 3231 |  | 
 | 3232 |     if (needWake) { | 
 | 3233 |         mLooper->wake(); | 
 | 3234 |     } | 
 | 3235 | } | 
 | 3236 |  | 
 | 3237 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3238 |     return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3239 | } | 
 | 3240 |  | 
 | 3241 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
 | 3242 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3243 |     ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3244 |           "switchMask=0x%08x", | 
 | 3245 |           args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3246 | #endif | 
 | 3247 |  | 
 | 3248 |     uint32_t policyFlags = args->policyFlags; | 
 | 3249 |     policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3250 |     mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3251 | } | 
 | 3252 |  | 
 | 3253 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
 | 3254 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3255 |     ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
 | 3256 |           args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 | #endif | 
 | 3258 |  | 
 | 3259 |     bool needWake; | 
 | 3260 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3261 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3262 |  | 
| Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 3263 |         DeviceResetEntry* newEntry = | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3264 |                 new DeviceResetEntry(args->id, args->eventTime, args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3265 |         needWake = enqueueInboundEventLocked(newEntry); | 
 | 3266 |     } // release lock | 
 | 3267 |  | 
 | 3268 |     if (needWake) { | 
 | 3269 |         mLooper->wake(); | 
 | 3270 |     } | 
 | 3271 | } | 
 | 3272 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3273 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid, | 
 | 3274 |                                           int32_t injectorUid, int32_t syncMode, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3275 |                                           std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3276 | #if DEBUG_INBOUND_EVENT_DETAILS | 
 | 3277 |     ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3278 |           "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
 | 3279 |           event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3280 | #endif | 
 | 3281 |  | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3282 |     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] | 3283 |  | 
 | 3284 |     policyFlags |= POLICY_FLAG_INJECTED; | 
 | 3285 |     if (hasInjectionPermission(injectorPid, injectorUid)) { | 
 | 3286 |         policyFlags |= POLICY_FLAG_TRUSTED; | 
 | 3287 |     } | 
 | 3288 |  | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3289 |     std::queue<EventEntry*> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 |     switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3291 |         case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3292 |             const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
 | 3293 |             int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3294 |             if (!validateKeyEvent(action)) { | 
 | 3295 |                 return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3296 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3297 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3298 |             int32_t flags = incomingKey.getFlags(); | 
 | 3299 |             int32_t keyCode = incomingKey.getKeyCode(); | 
 | 3300 |             int32_t metaState = incomingKey.getMetaState(); | 
 | 3301 |             accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3302 |                                     /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3303 |             KeyEvent keyEvent; | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3304 |             keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3305 |                                 incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
 | 3306 |                                 incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
 | 3307 |                                 incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3309 |             if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
 | 3310 |                 policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3311 |             } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3312 |  | 
 | 3313 |             if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
 | 3314 |                 android::base::Timer t; | 
 | 3315 |                 mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
 | 3316 |                 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3317 |                     ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
 | 3318 |                           std::to_string(t.duration().count()).c_str()); | 
 | 3319 |                 } | 
 | 3320 |             } | 
 | 3321 |  | 
 | 3322 |             mLock.lock(); | 
 | 3323 |             KeyEntry* injectedEntry = | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3324 |                     new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(), | 
 | 3325 |                                  VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| arthurhung | b1462ec | 2020-04-20 17:18:37 +0800 | [diff] [blame] | 3326 |                                  incomingKey.getDisplayId(), policyFlags, action, flags, keyCode, | 
 | 3327 |                                  incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3328 |                                  incomingKey.getDownTime()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3329 |             injectedEntries.push(injectedEntry); | 
 | 3330 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3331 |         } | 
 | 3332 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3333 |         case AINPUT_EVENT_TYPE_MOTION: { | 
 | 3334 |             const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); | 
 | 3335 |             int32_t action = motionEvent->getAction(); | 
 | 3336 |             size_t pointerCount = motionEvent->getPointerCount(); | 
 | 3337 |             const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); | 
 | 3338 |             int32_t actionButton = motionEvent->getActionButton(); | 
 | 3339 |             int32_t displayId = motionEvent->getDisplayId(); | 
 | 3340 |             if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
 | 3341 |                 return INPUT_EVENT_INJECTION_FAILED; | 
 | 3342 |             } | 
 | 3343 |  | 
 | 3344 |             if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
 | 3345 |                 nsecs_t eventTime = motionEvent->getEventTime(); | 
 | 3346 |                 android::base::Timer t; | 
 | 3347 |                 mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
 | 3348 |                 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3349 |                     ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
 | 3350 |                           std::to_string(t.duration().count()).c_str()); | 
 | 3351 |                 } | 
 | 3352 |             } | 
 | 3353 |  | 
 | 3354 |             mLock.lock(); | 
 | 3355 |             const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); | 
 | 3356 |             const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); | 
 | 3357 |             MotionEntry* injectedEntry = | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3358 |                     new MotionEntry(motionEvent->getId(), *sampleEventTimes, VIRTUAL_KEYBOARD_ID, | 
 | 3359 |                                     motionEvent->getSource(), motionEvent->getDisplayId(), | 
 | 3360 |                                     policyFlags, action, actionButton, motionEvent->getFlags(), | 
 | 3361 |                                     motionEvent->getMetaState(), motionEvent->getButtonState(), | 
 | 3362 |                                     motionEvent->getClassification(), motionEvent->getEdgeFlags(), | 
 | 3363 |                                     motionEvent->getXPrecision(), motionEvent->getYPrecision(), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3364 |                                     motionEvent->getRawXCursorPosition(), | 
 | 3365 |                                     motionEvent->getRawYCursorPosition(), | 
 | 3366 |                                     motionEvent->getDownTime(), uint32_t(pointerCount), | 
 | 3367 |                                     pointerProperties, samplePointerCoords, | 
 | 3368 |                                     motionEvent->getXOffset(), motionEvent->getYOffset()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3369 |             injectedEntries.push(injectedEntry); | 
 | 3370 |             for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { | 
 | 3371 |                 sampleEventTimes += 1; | 
 | 3372 |                 samplePointerCoords += pointerCount; | 
 | 3373 |                 MotionEntry* nextInjectedEntry = | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3374 |                         new MotionEntry(motionEvent->getId(), *sampleEventTimes, | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3375 |                                         VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3376 |                                         motionEvent->getDisplayId(), policyFlags, action, | 
 | 3377 |                                         actionButton, motionEvent->getFlags(), | 
 | 3378 |                                         motionEvent->getMetaState(), motionEvent->getButtonState(), | 
 | 3379 |                                         motionEvent->getClassification(), | 
 | 3380 |                                         motionEvent->getEdgeFlags(), motionEvent->getXPrecision(), | 
 | 3381 |                                         motionEvent->getYPrecision(), | 
 | 3382 |                                         motionEvent->getRawXCursorPosition(), | 
 | 3383 |                                         motionEvent->getRawYCursorPosition(), | 
 | 3384 |                                         motionEvent->getDownTime(), uint32_t(pointerCount), | 
 | 3385 |                                         pointerProperties, samplePointerCoords, | 
 | 3386 |                                         motionEvent->getXOffset(), motionEvent->getYOffset()); | 
 | 3387 |                 injectedEntries.push(nextInjectedEntry); | 
 | 3388 |             } | 
 | 3389 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3391 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3392 |         default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3393 |             ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3394 |             return INPUT_EVENT_INJECTION_FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3395 |     } | 
 | 3396 |  | 
 | 3397 |     InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
 | 3398 |     if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { | 
 | 3399 |         injectionState->injectionIsAsync = true; | 
 | 3400 |     } | 
 | 3401 |  | 
 | 3402 |     injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3403 |     injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3404 |  | 
 | 3405 |     bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3406 |     while (!injectedEntries.empty()) { | 
 | 3407 |         needWake |= enqueueInboundEventLocked(injectedEntries.front()); | 
 | 3408 |         injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3409 |     } | 
 | 3410 |  | 
 | 3411 |     mLock.unlock(); | 
 | 3412 |  | 
 | 3413 |     if (needWake) { | 
 | 3414 |         mLooper->wake(); | 
 | 3415 |     } | 
 | 3416 |  | 
 | 3417 |     int32_t injectionResult; | 
 | 3418 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3419 |         std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3420 |  | 
 | 3421 |         if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { | 
 | 3422 |             injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; | 
 | 3423 |         } else { | 
 | 3424 |             for (;;) { | 
 | 3425 |                 injectionResult = injectionState->injectionResult; | 
 | 3426 |                 if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { | 
 | 3427 |                     break; | 
 | 3428 |                 } | 
 | 3429 |  | 
 | 3430 |                 nsecs_t remainingTimeout = endTime - now(); | 
 | 3431 |                 if (remainingTimeout <= 0) { | 
 | 3432 | #if DEBUG_INJECTION | 
 | 3433 |                     ALOGD("injectInputEvent - Timed out waiting for injection result " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3434 |                           "to become available."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3435 | #endif | 
 | 3436 |                     injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; | 
 | 3437 |                     break; | 
 | 3438 |                 } | 
 | 3439 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3440 |                 mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3441 |             } | 
 | 3442 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3443 |             if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED && | 
 | 3444 |                 syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3445 |                 while (injectionState->pendingForegroundDispatches != 0) { | 
 | 3446 | #if DEBUG_INJECTION | 
 | 3447 |                     ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3448 |                           injectionState->pendingForegroundDispatches); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3449 | #endif | 
 | 3450 |                     nsecs_t remainingTimeout = endTime - now(); | 
 | 3451 |                     if (remainingTimeout <= 0) { | 
 | 3452 | #if DEBUG_INJECTION | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3453 |                         ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
 | 3454 |                               "dispatches to finish."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3455 | #endif | 
 | 3456 |                         injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; | 
 | 3457 |                         break; | 
 | 3458 |                     } | 
 | 3459 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3460 |                     mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 |                 } | 
 | 3462 |             } | 
 | 3463 |         } | 
 | 3464 |  | 
 | 3465 |         injectionState->release(); | 
 | 3466 |     } // release lock | 
 | 3467 |  | 
 | 3468 | #if DEBUG_INJECTION | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3469 |     ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3470 |           injectionResult, injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3471 | #endif | 
 | 3472 |  | 
 | 3473 |     return injectionResult; | 
 | 3474 | } | 
 | 3475 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3476 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3477 |     std::array<uint8_t, 32> calculatedHmac; | 
 | 3478 |     std::unique_ptr<VerifiedInputEvent> result; | 
 | 3479 |     switch (event.getType()) { | 
 | 3480 |         case AINPUT_EVENT_TYPE_KEY: { | 
 | 3481 |             const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
 | 3482 |             VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
 | 3483 |             result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
 | 3484 |             calculatedHmac = mHmacKeyManager.sign(verifiedKeyEvent); | 
 | 3485 |             break; | 
 | 3486 |         } | 
 | 3487 |         case AINPUT_EVENT_TYPE_MOTION: { | 
 | 3488 |             const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
 | 3489 |             VerifiedMotionEvent verifiedMotionEvent = | 
 | 3490 |                     verifiedMotionEventFromMotionEvent(motionEvent); | 
 | 3491 |             result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
 | 3492 |             calculatedHmac = mHmacKeyManager.sign(verifiedMotionEvent); | 
 | 3493 |             break; | 
 | 3494 |         } | 
 | 3495 |         default: { | 
 | 3496 |             ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
 | 3497 |             return nullptr; | 
 | 3498 |         } | 
 | 3499 |     } | 
 | 3500 |     if (calculatedHmac == INVALID_HMAC) { | 
 | 3501 |         return nullptr; | 
 | 3502 |     } | 
 | 3503 |     if (calculatedHmac != event.getHmac()) { | 
 | 3504 |         return nullptr; | 
 | 3505 |     } | 
 | 3506 |     return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3507 | } | 
 | 3508 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3510 |     return injectorUid == 0 || | 
 | 3511 |             mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | } | 
 | 3513 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3514 | void InputDispatcher::setInjectionResult(EventEntry* entry, int32_t injectionResult) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3515 |     InjectionState* injectionState = entry->injectionState; | 
 | 3516 |     if (injectionState) { | 
 | 3517 | #if DEBUG_INJECTION | 
 | 3518 |         ALOGD("Setting input event injection result to %d.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3519 |               "injectorPid=%d, injectorUid=%d", | 
 | 3520 |               injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3521 | #endif | 
 | 3522 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3523 |         if (injectionState->injectionIsAsync && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3524 |             // Log the outcome since the injector did not wait for the injection result. | 
 | 3525 |             switch (injectionResult) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3526 |                 case INPUT_EVENT_INJECTION_SUCCEEDED: | 
 | 3527 |                     ALOGV("Asynchronous input event injection succeeded."); | 
 | 3528 |                     break; | 
 | 3529 |                 case INPUT_EVENT_INJECTION_FAILED: | 
 | 3530 |                     ALOGW("Asynchronous input event injection failed."); | 
 | 3531 |                     break; | 
 | 3532 |                 case INPUT_EVENT_INJECTION_PERMISSION_DENIED: | 
 | 3533 |                     ALOGW("Asynchronous input event injection permission denied."); | 
 | 3534 |                     break; | 
 | 3535 |                 case INPUT_EVENT_INJECTION_TIMED_OUT: | 
 | 3536 |                     ALOGW("Asynchronous input event injection timed out."); | 
 | 3537 |                     break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3538 |             } | 
 | 3539 |         } | 
 | 3540 |  | 
 | 3541 |         injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3542 |         mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3543 |     } | 
 | 3544 | } | 
 | 3545 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3546 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3547 |     InjectionState* injectionState = entry->injectionState; | 
 | 3548 |     if (injectionState) { | 
 | 3549 |         injectionState->pendingForegroundDispatches += 1; | 
 | 3550 |     } | 
 | 3551 | } | 
 | 3552 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3553 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry* entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 |     InjectionState* injectionState = entry->injectionState; | 
 | 3555 |     if (injectionState) { | 
 | 3556 |         injectionState->pendingForegroundDispatches -= 1; | 
 | 3557 |  | 
 | 3558 |         if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3559 |             mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3560 |         } | 
 | 3561 |     } | 
 | 3562 | } | 
 | 3563 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3564 | std::vector<sp<InputWindowHandle>> InputDispatcher::getWindowHandlesLocked( | 
 | 3565 |         int32_t displayId) const { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3566 |     return getValueByKey(mWindowHandlesByDisplay, displayId); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3567 | } | 
 | 3568 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3569 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3570 |         const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3571 |     if (windowHandleToken == nullptr) { | 
 | 3572 |         return nullptr; | 
 | 3573 |     } | 
 | 3574 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3575 |     for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3576 |         const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
 | 3577 |         for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3578 |             if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3579 |                 return windowHandle; | 
 | 3580 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3581 |         } | 
 | 3582 |     } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3583 |     return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3584 | } | 
 | 3585 |  | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3586 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { | 
 | 3587 |     for (auto& it : mWindowHandlesByDisplay) { | 
 | 3588 |         const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
 | 3589 |         for (const sp<InputWindowHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3590 |             if (handle->getId() == windowHandle->getId() && | 
 | 3591 |                 handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3592 |                 if (windowHandle->getInfo()->displayId != it.first) { | 
 | 3593 |                     ALOGE("Found window %s in display %" PRId32 | 
 | 3594 |                           ", but it should belong to display %" PRId32, | 
 | 3595 |                           windowHandle->getName().c_str(), it.first, | 
 | 3596 |                           windowHandle->getInfo()->displayId); | 
 | 3597 |                 } | 
 | 3598 |                 return true; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3599 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3600 |         } | 
 | 3601 |     } | 
 | 3602 |     return false; | 
 | 3603 | } | 
 | 3604 |  | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3605 | sp<InputChannel> InputDispatcher::getInputChannelLocked(const sp<IBinder>& token) const { | 
 | 3606 |     size_t count = mInputChannelsByToken.count(token); | 
 | 3607 |     if (count == 0) { | 
 | 3608 |         return nullptr; | 
 | 3609 |     } | 
 | 3610 |     return mInputChannelsByToken.at(token); | 
 | 3611 | } | 
 | 3612 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3613 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
 | 3614 |         const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
 | 3615 |     if (inputWindowHandles.empty()) { | 
 | 3616 |         // Remove all handles on a display if there are no windows left. | 
 | 3617 |         mWindowHandlesByDisplay.erase(displayId); | 
 | 3618 |         return; | 
 | 3619 |     } | 
 | 3620 |  | 
 | 3621 |     // Since we compare the pointer of input window handles across window updates, we need | 
 | 3622 |     // to make sure the handle object for the same window stays unchanged across updates. | 
 | 3623 |     const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3624 |     std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3625 |     for (const sp<InputWindowHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3626 |         oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3627 |     } | 
 | 3628 |  | 
 | 3629 |     std::vector<sp<InputWindowHandle>> newHandles; | 
 | 3630 |     for (const sp<InputWindowHandle>& handle : inputWindowHandles) { | 
 | 3631 |         if (!handle->updateInfo()) { | 
 | 3632 |             // handle no longer valid | 
 | 3633 |             continue; | 
 | 3634 |         } | 
 | 3635 |  | 
 | 3636 |         const InputWindowInfo* info = handle->getInfo(); | 
 | 3637 |         if ((getInputChannelLocked(handle->getToken()) == nullptr && | 
 | 3638 |              info->portalToDisplayId == ADISPLAY_ID_NONE)) { | 
 | 3639 |             const bool noInputChannel = | 
 | 3640 |                     info->inputFeatures & InputWindowInfo::INPUT_FEATURE_NO_INPUT_CHANNEL; | 
 | 3641 |             const bool canReceiveInput = | 
 | 3642 |                     !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_TOUCHABLE) || | 
 | 3643 |                     !(info->layoutParamsFlags & InputWindowInfo::FLAG_NOT_FOCUSABLE); | 
 | 3644 |             if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3645 |                 ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3646 |                       handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3647 |                 continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3648 |             } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3649 |         } | 
 | 3650 |  | 
 | 3651 |         if (info->displayId != displayId) { | 
 | 3652 |             ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
 | 3653 |                   handle->getName().c_str(), displayId, info->displayId); | 
 | 3654 |             continue; | 
 | 3655 |         } | 
 | 3656 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3657 |         if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
 | 3658 |                 (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3659 |             const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3660 |             oldHandle->updateFrom(handle); | 
 | 3661 |             newHandles.push_back(oldHandle); | 
 | 3662 |         } else { | 
 | 3663 |             newHandles.push_back(handle); | 
 | 3664 |         } | 
 | 3665 |     } | 
 | 3666 |  | 
 | 3667 |     // Insert or replace | 
 | 3668 |     mWindowHandlesByDisplay[displayId] = newHandles; | 
 | 3669 | } | 
 | 3670 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3671 | void InputDispatcher::setInputWindows( | 
 | 3672 |         const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { | 
 | 3673 |     { // acquire lock | 
 | 3674 |         std::scoped_lock _l(mLock); | 
 | 3675 |         for (auto const& i : handlesPerDisplay) { | 
 | 3676 |             setInputWindowsLocked(i.second, i.first); | 
 | 3677 |         } | 
 | 3678 |     } | 
 | 3679 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 3680 |     mLooper->wake(); | 
 | 3681 | } | 
 | 3682 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3683 | /** | 
 | 3684 |  * Called from InputManagerService, update window handle list by displayId that can receive input. | 
 | 3685 |  * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
 | 3686 |  * If set an empty list, remove all handles from the specific display. | 
 | 3687 |  * For focused handle, check if need to change and send a cancel event to previous one. | 
 | 3688 |  * For removed handle, check if need to send a cancel event if already in touch. | 
 | 3689 |  */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3690 | void InputDispatcher::setInputWindowsLocked( | 
 | 3691 |         const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3692 |     if (DEBUG_FOCUS) { | 
 | 3693 |         std::string windowList; | 
 | 3694 |         for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { | 
 | 3695 |             windowList += iwh->getName() + " "; | 
 | 3696 |         } | 
 | 3697 |         ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
 | 3698 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3700 |     // Copy old handles for release if they are no longer present. | 
 | 3701 |     const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3702 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3703 |     updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3704 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3705 |     sp<InputWindowHandle> newFocusedWindowHandle = nullptr; | 
 | 3706 |     bool foundHoveredWindow = false; | 
 | 3707 |     for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
 | 3708 |         // Set newFocusedWindowHandle to the top most focused window instead of the last one | 
 | 3709 |         if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus && | 
 | 3710 |             windowHandle->getInfo()->visible) { | 
 | 3711 |             newFocusedWindowHandle = windowHandle; | 
 | 3712 |         } | 
 | 3713 |         if (windowHandle == mLastHoverWindowHandle) { | 
 | 3714 |             foundHoveredWindow = true; | 
 | 3715 |         } | 
 | 3716 |     } | 
 | 3717 |  | 
 | 3718 |     if (!foundHoveredWindow) { | 
 | 3719 |         mLastHoverWindowHandle = nullptr; | 
 | 3720 |     } | 
 | 3721 |  | 
 | 3722 |     sp<InputWindowHandle> oldFocusedWindowHandle = | 
 | 3723 |             getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
 | 3724 |  | 
 | 3725 |     if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) { | 
 | 3726 |         if (oldFocusedWindowHandle != nullptr) { | 
 | 3727 |             if (DEBUG_FOCUS) { | 
 | 3728 |                 ALOGD("Focus left window: %s in display %" PRId32, | 
 | 3729 |                       oldFocusedWindowHandle->getName().c_str(), displayId); | 
| Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3730 |             } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3731 |             sp<InputChannel> focusedInputChannel = | 
 | 3732 |                     getInputChannelLocked(oldFocusedWindowHandle->getToken()); | 
 | 3733 |             if (focusedInputChannel != nullptr) { | 
 | 3734 |                 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
 | 3735 |                                            "focus left window"); | 
 | 3736 |                 synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
 | 3737 |                 enqueueFocusEventLocked(*oldFocusedWindowHandle, false /*hasFocus*/); | 
| Garfield Tan | bd0fbcd | 2018-11-30 12:45:03 -0800 | [diff] [blame] | 3738 |             } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3739 |             mFocusedWindowHandlesByDisplay.erase(displayId); | 
 | 3740 |         } | 
 | 3741 |         if (newFocusedWindowHandle != nullptr) { | 
 | 3742 |             if (DEBUG_FOCUS) { | 
 | 3743 |                 ALOGD("Focus entered window: %s in display %" PRId32, | 
 | 3744 |                       newFocusedWindowHandle->getName().c_str(), displayId); | 
 | 3745 |             } | 
 | 3746 |             mFocusedWindowHandlesByDisplay[displayId] = newFocusedWindowHandle; | 
 | 3747 |             enqueueFocusEventLocked(*newFocusedWindowHandle, true /*hasFocus*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3748 |         } | 
 | 3749 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3750 |         if (mFocusedDisplayId == displayId) { | 
 | 3751 |             onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3752 |         } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3753 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3754 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3755 |     std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
 | 3756 |             mTouchStatesByDisplay.find(displayId); | 
 | 3757 |     if (stateIt != mTouchStatesByDisplay.end()) { | 
 | 3758 |         TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3759 |         for (size_t i = 0; i < state.windows.size();) { | 
 | 3760 |             TouchedWindow& touchedWindow = state.windows[i]; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3761 |             if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3762 |                 if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3763 |                     ALOGD("Touched window was removed: %s in display %" PRId32, | 
 | 3764 |                           touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3765 |                 } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3766 |                 sp<InputChannel> touchedInputChannel = | 
 | 3767 |                         getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
 | 3768 |                 if (touchedInputChannel != nullptr) { | 
 | 3769 |                     CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
 | 3770 |                                                "touched window was removed"); | 
 | 3771 |                     synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3772 |                 } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3773 |                 state.windows.erase(state.windows.begin() + i); | 
 | 3774 |             } else { | 
 | 3775 |                 ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 |             } | 
 | 3777 |         } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3778 |     } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3779 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3780 |     // Release information for windows that are no longer present. | 
 | 3781 |     // This ensures that unused input channels are released promptly. | 
 | 3782 |     // Otherwise, they might stick around until the window handle is destroyed | 
 | 3783 |     // which might not happen until the next GC. | 
 | 3784 |     for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3785 |         if (!hasWindowHandleLocked(oldWindowHandle)) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3786 |             if (DEBUG_FOCUS) { | 
 | 3787 |                 ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3788 |             } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3789 |             oldWindowHandle->releaseChannel(); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 3790 |         } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 3791 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 | } | 
 | 3793 |  | 
 | 3794 | void InputDispatcher::setFocusedApplication( | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3795 |         int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3796 |     if (DEBUG_FOCUS) { | 
 | 3797 |         ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
 | 3798 |               inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
 | 3799 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3800 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3801 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3802 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3803 |         sp<InputApplicationHandle> oldFocusedApplicationHandle = | 
 | 3804 |                 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3805 |  | 
 | 3806 |         if (oldFocusedApplicationHandle == mAwaitedFocusedApplication && | 
 | 3807 |             inputApplicationHandle != oldFocusedApplicationHandle) { | 
 | 3808 |             resetNoFocusedWindowTimeoutLocked(); | 
 | 3809 |         } | 
 | 3810 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3811 |         if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3812 |             if (oldFocusedApplicationHandle != inputApplicationHandle) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3813 |                 mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3814 |             } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3815 |         } else if (oldFocusedApplicationHandle != nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3816 |             oldFocusedApplicationHandle.clear(); | 
 | 3817 |             mFocusedApplicationHandlesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3818 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3819 |     } // release lock | 
 | 3820 |  | 
 | 3821 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 3822 |     mLooper->wake(); | 
 | 3823 | } | 
 | 3824 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3825 | /** | 
 | 3826 |  * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
 | 3827 |  * the display not specified. | 
 | 3828 |  * | 
 | 3829 |  * We track any unreleased events for each window. If a window loses the ability to receive the | 
 | 3830 |  * released event, we will send a cancel event to it. So when the focused display is changed, we | 
 | 3831 |  * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
 | 3832 |  * display. The display-specified events won't be affected. | 
 | 3833 |  */ | 
 | 3834 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3835 |     if (DEBUG_FOCUS) { | 
 | 3836 |         ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
 | 3837 |     } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3838 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3839 |         std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3840 |  | 
 | 3841 |         if (mFocusedDisplayId != displayId) { | 
 | 3842 |             sp<InputWindowHandle> oldFocusedWindowHandle = | 
 | 3843 |                     getValueByKey(mFocusedWindowHandlesByDisplay, mFocusedDisplayId); | 
 | 3844 |             if (oldFocusedWindowHandle != nullptr) { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3845 |                 sp<InputChannel> inputChannel = | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3846 |                         getInputChannelLocked(oldFocusedWindowHandle->getToken()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3847 |                 if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3848 |                     CancelationOptions | 
 | 3849 |                             options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
 | 3850 |                                     "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3851 |                     options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3852 |                     synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
 | 3853 |                 } | 
 | 3854 |             } | 
 | 3855 |             mFocusedDisplayId = displayId; | 
 | 3856 |  | 
 | 3857 |             // Sanity check | 
 | 3858 |             sp<InputWindowHandle> newFocusedWindowHandle = | 
 | 3859 |                     getValueByKey(mFocusedWindowHandlesByDisplay, displayId); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 3860 |             onFocusChangedLocked(oldFocusedWindowHandle, newFocusedWindowHandle); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 3861 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3862 |             if (newFocusedWindowHandle == nullptr) { | 
 | 3863 |                 ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
 | 3864 |                 if (!mFocusedWindowHandlesByDisplay.empty()) { | 
 | 3865 |                     ALOGE("But another display has a focused window:"); | 
 | 3866 |                     for (auto& it : mFocusedWindowHandlesByDisplay) { | 
 | 3867 |                         const int32_t displayId = it.first; | 
 | 3868 |                         const sp<InputWindowHandle>& windowHandle = it.second; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3869 |                         ALOGE("Display #%" PRId32 " has focused window: '%s'\n", displayId, | 
 | 3870 |                               windowHandle->getName().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3871 |                     } | 
 | 3872 |                 } | 
 | 3873 |             } | 
 | 3874 |         } | 
 | 3875 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3876 |         if (DEBUG_FOCUS) { | 
 | 3877 |             logDispatchStateLocked(); | 
 | 3878 |         } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3879 |     } // release lock | 
 | 3880 |  | 
 | 3881 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 3882 |     mLooper->wake(); | 
 | 3883 | } | 
 | 3884 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3886 |     if (DEBUG_FOCUS) { | 
 | 3887 |         ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
 | 3888 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3889 |  | 
 | 3890 |     bool changed; | 
 | 3891 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3892 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 |  | 
 | 3894 |         if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
 | 3895 |             if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3896 |                 resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3897 |             } | 
 | 3898 |  | 
 | 3899 |             if (mDispatchEnabled && !enabled) { | 
 | 3900 |                 resetAndDropEverythingLocked("dispatcher is being disabled"); | 
 | 3901 |             } | 
 | 3902 |  | 
 | 3903 |             mDispatchEnabled = enabled; | 
 | 3904 |             mDispatchFrozen = frozen; | 
 | 3905 |             changed = true; | 
 | 3906 |         } else { | 
 | 3907 |             changed = false; | 
 | 3908 |         } | 
 | 3909 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3910 |         if (DEBUG_FOCUS) { | 
 | 3911 |             logDispatchStateLocked(); | 
 | 3912 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3913 |     } // release lock | 
 | 3914 |  | 
 | 3915 |     if (changed) { | 
 | 3916 |         // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 3917 |         mLooper->wake(); | 
 | 3918 |     } | 
 | 3919 | } | 
 | 3920 |  | 
 | 3921 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3922 |     if (DEBUG_FOCUS) { | 
 | 3923 |         ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
 | 3924 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3925 |  | 
 | 3926 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3927 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3928 |  | 
 | 3929 |         if (mInputFilterEnabled == enabled) { | 
 | 3930 |             return; | 
 | 3931 |         } | 
 | 3932 |  | 
 | 3933 |         mInputFilterEnabled = enabled; | 
 | 3934 |         resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
 | 3935 |     } // release lock | 
 | 3936 |  | 
 | 3937 |     // Wake up poll loop since there might be work to do to drop everything. | 
 | 3938 |     mLooper->wake(); | 
 | 3939 | } | 
 | 3940 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 3941 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
 | 3942 |     std::scoped_lock lock(mLock); | 
 | 3943 |     mInTouchMode = inTouchMode; | 
 | 3944 | } | 
 | 3945 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3946 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { | 
 | 3947 |     if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3948 |         if (DEBUG_FOCUS) { | 
 | 3949 |             ALOGD("Trivial transfer to same window."); | 
 | 3950 |         } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3951 |         return true; | 
 | 3952 |     } | 
 | 3953 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3955 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3956 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3957 |         sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
 | 3958 |         sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3959 |         if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3960 |             ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 |             return false; | 
 | 3962 |         } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3963 |         if (DEBUG_FOCUS) { | 
 | 3964 |             ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
 | 3965 |                   fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
 | 3966 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3967 |         if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3968 |             if (DEBUG_FOCUS) { | 
 | 3969 |                 ALOGD("Cannot transfer focus because windows are on different displays."); | 
 | 3970 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3971 |             return false; | 
 | 3972 |         } | 
 | 3973 |  | 
 | 3974 |         bool found = false; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3975 |         for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
 | 3976 |             TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3977 |             for (size_t i = 0; i < state.windows.size(); i++) { | 
 | 3978 |                 const TouchedWindow& touchedWindow = state.windows[i]; | 
 | 3979 |                 if (touchedWindow.windowHandle == fromWindowHandle) { | 
 | 3980 |                     int32_t oldTargetFlags = touchedWindow.targetFlags; | 
 | 3981 |                     BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3982 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3983 |                     state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3985 |                     int32_t newTargetFlags = oldTargetFlags & | 
 | 3986 |                             (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
 | 3987 |                              InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3988 |                     state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3989 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 3990 |                     found = true; | 
 | 3991 |                     goto Found; | 
 | 3992 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 |             } | 
 | 3994 |         } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3995 |     Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3996 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3997 |         if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3998 |             if (DEBUG_FOCUS) { | 
 | 3999 |                 ALOGD("Focus transfer failed because from window did not have focus."); | 
 | 4000 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 |             return false; | 
 | 4002 |         } | 
 | 4003 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4004 |         sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
 | 4005 |         sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4006 |         if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4007 |             fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4008 |             CancelationOptions | 
 | 4009 |                     options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
 | 4010 |                             "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4011 |             synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4012 |             synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4013 |         } | 
 | 4014 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4015 |         if (DEBUG_FOCUS) { | 
 | 4016 |             logDispatchStateLocked(); | 
 | 4017 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4018 |     } // release lock | 
 | 4019 |  | 
 | 4020 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 4021 |     mLooper->wake(); | 
 | 4022 |     return true; | 
 | 4023 | } | 
 | 4024 |  | 
 | 4025 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4026 |     if (DEBUG_FOCUS) { | 
 | 4027 |         ALOGD("Resetting and dropping all events (%s).", reason); | 
 | 4028 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4029 |  | 
 | 4030 |     CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
 | 4031 |     synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 4032 |  | 
 | 4033 |     resetKeyRepeatLocked(); | 
 | 4034 |     releasePendingEventLocked(); | 
 | 4035 |     drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4036 |     resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4037 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4038 |     mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4039 |     mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4040 |     mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4041 |     mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4042 | } | 
 | 4043 |  | 
 | 4044 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4045 |     std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4046 |     dumpDispatchStateLocked(dump); | 
 | 4047 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4048 |     std::istringstream stream(dump); | 
 | 4049 |     std::string line; | 
 | 4050 |  | 
 | 4051 |     while (std::getline(stream, line, '\n')) { | 
 | 4052 |         ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4053 |     } | 
 | 4054 | } | 
 | 4055 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4056 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4057 |     dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
 | 4058 |     dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
 | 4059 |     dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4060 |     dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4062 |     if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
 | 4063 |         dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
 | 4064 |         for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
 | 4065 |             const int32_t displayId = it.first; | 
 | 4066 |             const sp<InputApplicationHandle>& applicationHandle = it.second; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4067 |             dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4068 |                                          ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4069 |                                  displayId, applicationHandle->getName().c_str(), | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4070 |                                  ns2ms(applicationHandle | 
 | 4071 |                                                ->getDispatchingTimeout( | 
 | 4072 |                                                        DEFAULT_INPUT_DISPATCHING_TIMEOUT) | 
 | 4073 |                                                .count())); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4074 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4075 |     } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4076 |         dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4077 |     } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4078 |  | 
 | 4079 |     if (!mFocusedWindowHandlesByDisplay.empty()) { | 
 | 4080 |         dump += StringPrintf(INDENT "FocusedWindows:\n"); | 
 | 4081 |         for (auto& it : mFocusedWindowHandlesByDisplay) { | 
 | 4082 |             const int32_t displayId = it.first; | 
 | 4083 |             const sp<InputWindowHandle>& windowHandle = it.second; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4084 |             dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, | 
 | 4085 |                                  windowHandle->getName().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4086 |         } | 
 | 4087 |     } else { | 
 | 4088 |         dump += StringPrintf(INDENT "FocusedWindows: <none>\n"); | 
 | 4089 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4090 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4091 |     if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4092 |         dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4093 |         for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
 | 4094 |             const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4095 |             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] | 4096 |                                  state.displayId, toString(state.down), toString(state.split), | 
 | 4097 |                                  state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4098 |             if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4099 |                 dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4100 |                 for (size_t i = 0; i < state.windows.size(); i++) { | 
 | 4101 |                     const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4102 |                     dump += StringPrintf(INDENT4 | 
 | 4103 |                                          "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
 | 4104 |                                          i, touchedWindow.windowHandle->getName().c_str(), | 
 | 4105 |                                          touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4106 |                 } | 
 | 4107 |             } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4108 |                 dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4109 |             } | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4110 |             if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4111 |                 dump += INDENT3 "Portal windows:\n"; | 
 | 4112 |                 for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4113 |                     const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4114 |                     dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, | 
 | 4115 |                                          portalWindowHandle->getName().c_str()); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4116 |                 } | 
 | 4117 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4118 |         } | 
 | 4119 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4120 |         dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 |     } | 
 | 4122 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4123 |     if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4124 |         for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4125 |             const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4126 |             dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4127 |             if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4128 |                 dump += INDENT2 "Windows:\n"; | 
 | 4129 |                 for (size_t i = 0; i < windowHandles.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4130 |                     const sp<InputWindowHandle>& windowHandle = windowHandles[i]; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4131 |                     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4133 |                     dump += StringPrintf(INDENT3 "%zu: name='%s', displayId=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4134 |                                                  "portalToDisplayId=%d, paused=%s, hasFocus=%s, " | 
| chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4135 |                                                  "hasWallpaper=%s, visible=%s, canReceiveKeys=%s, " | 
 | 4136 |                                                  "flags=0x%08x, type=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4137 |                                                  "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4138 |                                                  "windowScale=(%f,%f), touchableRegion=", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4139 |                                          i, windowInfo->name.c_str(), windowInfo->displayId, | 
 | 4140 |                                          windowInfo->portalToDisplayId, | 
 | 4141 |                                          toString(windowInfo->paused), | 
 | 4142 |                                          toString(windowInfo->hasFocus), | 
 | 4143 |                                          toString(windowInfo->hasWallpaper), | 
 | 4144 |                                          toString(windowInfo->visible), | 
 | 4145 |                                          toString(windowInfo->canReceiveKeys), | 
 | 4146 |                                          windowInfo->layoutParamsFlags, | 
| chaviw | cb92321 | 2019-12-30 14:05:11 -0800 | [diff] [blame] | 4147 |                                          windowInfo->layoutParamsType, windowInfo->frameLeft, | 
 | 4148 |                                          windowInfo->frameTop, windowInfo->frameRight, | 
 | 4149 |                                          windowInfo->frameBottom, windowInfo->globalScaleFactor, | 
 | 4150 |                                          windowInfo->windowXScale, windowInfo->windowYScale); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4151 |                     dumpRegion(dump, windowInfo->touchableRegion); | 
 | 4152 |                     dump += StringPrintf(", inputFeatures=0x%08x", windowInfo->inputFeatures); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4153 |                     dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
 | 4154 |                                          "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4155 |                                          windowInfo->ownerPid, windowInfo->ownerUid, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4156 |                                          ns2ms(windowInfo->dispatchingTimeout)); | 
| Siarhei Vishniakou | 67d4450 | 2020-04-09 11:09:29 -0700 | [diff] [blame] | 4157 |                     dump += StringPrintf(INDENT4 "    flags: %s\n", | 
 | 4158 |                                          inputWindowFlagsToString(windowInfo->layoutParamsFlags) | 
 | 4159 |                                                  .c_str()); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4160 |                 } | 
 | 4161 |             } else { | 
 | 4162 |                 dump += INDENT2 "Windows: <none>\n"; | 
 | 4163 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4164 |         } | 
 | 4165 |     } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4166 |         dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 |     } | 
 | 4168 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4169 |     if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4170 |         for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4171 |             const std::vector<Monitor>& monitors = it.second; | 
 | 4172 |             dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
 | 4173 |             dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4174 |         } | 
 | 4175 |         for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4176 |             const std::vector<Monitor>& monitors = it.second; | 
 | 4177 |             dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
 | 4178 |             dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4179 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 |     } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4181 |         dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4182 |     } | 
 | 4183 |  | 
 | 4184 |     nsecs_t currentTime = now(); | 
 | 4185 |  | 
 | 4186 |     // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4187 |     if (!mRecentQueue.empty()) { | 
 | 4188 |         dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
 | 4189 |         for (EventEntry* entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4190 |             dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 |             entry->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4192 |             dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4193 |         } | 
 | 4194 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4195 |         dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4196 |     } | 
 | 4197 |  | 
 | 4198 |     // Dump event currently being dispatched. | 
 | 4199 |     if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4200 |         dump += INDENT "PendingEvent:\n"; | 
 | 4201 |         dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 |         mPendingEvent->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4203 |         dump += StringPrintf(", age=%" PRId64 "ms\n", | 
 | 4204 |                              ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4205 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4206 |         dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4207 |     } | 
 | 4208 |  | 
 | 4209 |     // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4210 |     if (!mInboundQueue.empty()) { | 
 | 4211 |         dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
 | 4212 |         for (EventEntry* entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4213 |             dump += INDENT2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4214 |             entry->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4215 |             dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 |         } | 
 | 4217 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4218 |         dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4219 |     } | 
 | 4220 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4221 |     if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4222 |         dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4223 |         for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
 | 4224 |             const KeyReplacement& replacement = pair.first; | 
 | 4225 |             int32_t newKeyCode = pair.second; | 
 | 4226 |             dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4227 |                                  replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4228 |         } | 
 | 4229 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4230 |         dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4231 |     } | 
 | 4232 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4233 |     if (!mConnectionsByFd.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4234 |         dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4235 |         for (const auto& pair : mConnectionsByFd) { | 
 | 4236 |             const sp<Connection>& connection = pair.second; | 
 | 4237 |             dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4238 |                                          "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4239 |                                  pair.first, connection->getInputChannelName().c_str(), | 
 | 4240 |                                  connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4241 |                                  toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4243 |             if (!connection->outboundQueue.empty()) { | 
 | 4244 |                 dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
 | 4245 |                                      connection->outboundQueue.size()); | 
 | 4246 |                 for (DispatchEntry* entry : connection->outboundQueue) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4247 |                     dump.append(INDENT4); | 
 | 4248 |                     entry->eventEntry->appendDescription(dump); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4249 |                     dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 | 
 | 4250 |                                          "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4251 |                                          entry->targetFlags, entry->resolvedAction, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4252 |                                          ns2ms(currentTime - entry->eventEntry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4253 |                 } | 
 | 4254 |             } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4255 |                 dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 |             } | 
 | 4257 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4258 |             if (!connection->waitQueue.empty()) { | 
 | 4259 |                 dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
 | 4260 |                                      connection->waitQueue.size()); | 
 | 4261 |                 for (DispatchEntry* entry : connection->waitQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4262 |                     dump += INDENT4; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4263 |                     entry->eventEntry->appendDescription(dump); | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4264 |                     dump += StringPrintf(", targetFlags=0x%08x, resolvedAction=%d, " | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4265 |                                          "age=%" PRId64 "ms, wait=%" PRId64 "ms\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4266 |                                          entry->targetFlags, entry->resolvedAction, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4267 |                                          ns2ms(currentTime - entry->eventEntry->eventTime), | 
 | 4268 |                                          ns2ms(currentTime - entry->deliveryTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 |                 } | 
 | 4270 |             } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4271 |                 dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4272 |             } | 
 | 4273 |         } | 
 | 4274 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4275 |         dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4276 |     } | 
 | 4277 |  | 
 | 4278 |     if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4279 |         dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
 | 4280 |                              ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4281 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4282 |         dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 |     } | 
 | 4284 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4285 |     dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4286 |     dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
 | 4287 |     dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
 | 4288 |                          ns2ms(mConfig.keyRepeatTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4289 | } | 
 | 4290 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4291 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
 | 4292 |     const size_t numMonitors = monitors.size(); | 
 | 4293 |     for (size_t i = 0; i < numMonitors; i++) { | 
 | 4294 |         const Monitor& monitor = monitors[i]; | 
 | 4295 |         const sp<InputChannel>& channel = monitor.inputChannel; | 
 | 4296 |         dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
 | 4297 |         dump += "\n"; | 
 | 4298 |     } | 
 | 4299 | } | 
 | 4300 |  | 
| Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4301 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | #if DEBUG_REGISTRATION | 
| Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 4303 |     ALOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4304 | #endif | 
 | 4305 |  | 
 | 4306 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4307 |         std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4308 |         sp<Connection> existingConnection = getConnectionLocked(inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4309 |         if (existingConnection != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 |             ALOGW("Attempted to register already registered input channel '%s'", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4311 |                   inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 |             return BAD_VALUE; | 
 | 4313 |         } | 
 | 4314 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4315 |         sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 |  | 
 | 4317 |         int fd = inputChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4318 |         mConnectionsByFd[fd] = connection; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4319 |         mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 |         mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
 | 4322 |     } // release lock | 
 | 4323 |  | 
 | 4324 |     // Wake the looper because some connections have changed. | 
 | 4325 |     mLooper->wake(); | 
 | 4326 |     return OK; | 
 | 4327 | } | 
 | 4328 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4329 | status_t InputDispatcher::registerInputMonitor(const sp<InputChannel>& inputChannel, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4330 |                                                int32_t displayId, bool isGestureMonitor) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4331 |     { // acquire lock | 
 | 4332 |         std::scoped_lock _l(mLock); | 
 | 4333 |  | 
 | 4334 |         if (displayId < 0) { | 
 | 4335 |             ALOGW("Attempted to register input monitor without a specified display."); | 
 | 4336 |             return BAD_VALUE; | 
 | 4337 |         } | 
 | 4338 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4339 |         if (inputChannel->getConnectionToken() == nullptr) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4340 |             ALOGW("Attempted to register input monitor without an identifying token."); | 
 | 4341 |             return BAD_VALUE; | 
 | 4342 |         } | 
 | 4343 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4344 |         sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4345 |  | 
 | 4346 |         const int fd = inputChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4347 |         mConnectionsByFd[fd] = connection; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4348 |         mInputChannelsByToken[inputChannel->getConnectionToken()] = inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4349 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4350 |         auto& monitorsByDisplay = | 
 | 4351 |                 isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4352 |         monitorsByDisplay[displayId].emplace_back(inputChannel); | 
 | 4353 |  | 
 | 4354 |         mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4355 |     } | 
 | 4356 |     // Wake the looper because some connections have changed. | 
 | 4357 |     mLooper->wake(); | 
 | 4358 |     return OK; | 
 | 4359 | } | 
 | 4360 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4361 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { | 
 | 4362 | #if DEBUG_REGISTRATION | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4363 |     ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | #endif | 
 | 4365 |  | 
 | 4366 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4367 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 |  | 
 | 4369 |         status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); | 
 | 4370 |         if (status) { | 
 | 4371 |             return status; | 
 | 4372 |         } | 
 | 4373 |     } // release lock | 
 | 4374 |  | 
 | 4375 |     // Wake the poll loop because removing the connection may have changed the current | 
 | 4376 |     // synchronization state. | 
 | 4377 |     mLooper->wake(); | 
 | 4378 |     return OK; | 
 | 4379 | } | 
 | 4380 |  | 
 | 4381 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4382 |                                                        bool notify) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4383 |     sp<Connection> connection = getConnectionLocked(inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4384 |     if (connection == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 |         ALOGW("Attempted to unregister already unregistered input channel '%s'", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4386 |               inputChannel->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 |         return BAD_VALUE; | 
 | 4388 |     } | 
 | 4389 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4390 |     removeConnectionLocked(connection); | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4391 |     mInputChannelsByToken.erase(inputChannel->getConnectionToken()); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4392 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4393 |     if (connection->monitor) { | 
 | 4394 |         removeMonitorChannelLocked(inputChannel); | 
 | 4395 |     } | 
 | 4396 |  | 
 | 4397 |     mLooper->removeFd(inputChannel->getFd()); | 
 | 4398 |  | 
 | 4399 |     nsecs_t currentTime = now(); | 
 | 4400 |     abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
 | 4401 |  | 
 | 4402 |     connection->status = Connection::STATUS_ZOMBIE; | 
 | 4403 |     return OK; | 
 | 4404 | } | 
 | 4405 |  | 
 | 4406 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4407 |     removeMonitorChannelLocked(inputChannel, mGlobalMonitorsByDisplay); | 
 | 4408 |     removeMonitorChannelLocked(inputChannel, mGestureMonitorsByDisplay); | 
 | 4409 | } | 
 | 4410 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4411 | void InputDispatcher::removeMonitorChannelLocked( | 
 | 4412 |         const sp<InputChannel>& inputChannel, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4413 |         std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4414 |     for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4415 |         std::vector<Monitor>& monitors = it->second; | 
 | 4416 |         const size_t numMonitors = monitors.size(); | 
 | 4417 |         for (size_t i = 0; i < numMonitors; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4418 |             if (monitors[i].inputChannel == inputChannel) { | 
 | 4419 |                 monitors.erase(monitors.begin() + i); | 
 | 4420 |                 break; | 
 | 4421 |             } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4422 |         } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4423 |         if (monitors.empty()) { | 
 | 4424 |             it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4425 |         } else { | 
 | 4426 |             ++it; | 
 | 4427 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4428 |     } | 
 | 4429 | } | 
 | 4430 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4431 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
 | 4432 |     { // acquire lock | 
 | 4433 |         std::scoped_lock _l(mLock); | 
 | 4434 |         std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
 | 4435 |  | 
 | 4436 |         if (!foundDisplayId) { | 
 | 4437 |             ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
 | 4438 |             return BAD_VALUE; | 
 | 4439 |         } | 
 | 4440 |         int32_t displayId = foundDisplayId.value(); | 
 | 4441 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4442 |         std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
 | 4443 |                 mTouchStatesByDisplay.find(displayId); | 
 | 4444 |         if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4445 |             ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
 | 4446 |             return BAD_VALUE; | 
 | 4447 |         } | 
 | 4448 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4449 |         TouchState& state = stateIt->second; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4450 |         std::optional<int32_t> foundDeviceId; | 
 | 4451 |         for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4452 |             if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4453 |                 foundDeviceId = state.deviceId; | 
 | 4454 |             } | 
 | 4455 |         } | 
 | 4456 |         if (!foundDeviceId || !state.down) { | 
 | 4457 |             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] | 4458 |                   " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4459 |             return BAD_VALUE; | 
 | 4460 |         } | 
 | 4461 |         int32_t deviceId = foundDeviceId.value(); | 
 | 4462 |  | 
 | 4463 |         // Send cancel events to all the input channels we're stealing from. | 
 | 4464 |         CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4465 |                                    "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4466 |         options.deviceId = deviceId; | 
 | 4467 |         options.displayId = displayId; | 
 | 4468 |         for (const TouchedWindow& window : state.windows) { | 
 | 4469 |             sp<InputChannel> channel = getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4470 |             if (channel != nullptr) { | 
 | 4471 |                 synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
 | 4472 |             } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4473 |         } | 
 | 4474 |         // Then clear the current touch state so we stop dispatching to them as well. | 
 | 4475 |         state.filterNonMonitors(); | 
 | 4476 |     } | 
 | 4477 |     return OK; | 
 | 4478 | } | 
 | 4479 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4480 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
 | 4481 |         const sp<IBinder>& token) { | 
 | 4482 |     for (const auto& it : mGestureMonitorsByDisplay) { | 
 | 4483 |         const std::vector<Monitor>& monitors = it.second; | 
 | 4484 |         for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4485 |             if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4486 |                 return it.first; | 
 | 4487 |             } | 
 | 4488 |         } | 
 | 4489 |     } | 
 | 4490 |     return std::nullopt; | 
 | 4491 | } | 
 | 4492 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4493 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4494 |     if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4495 |         return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4496 |     } | 
 | 4497 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4498 |     for (const auto& pair : mConnectionsByFd) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4499 |         const sp<Connection>& connection = pair.second; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4500 |         if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4501 |             return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4502 |         } | 
 | 4503 |     } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4504 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4505 |     return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4506 | } | 
 | 4507 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4508 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4509 |     mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4510 |     removeByValue(mConnectionsByFd, connection); | 
 | 4511 | } | 
 | 4512 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4513 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, | 
 | 4514 |                                                     const sp<Connection>& connection, uint32_t seq, | 
 | 4515 |                                                     bool handled) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4516 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 4517 |             &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4518 |     commandEntry->connection = connection; | 
 | 4519 |     commandEntry->eventTime = currentTime; | 
 | 4520 |     commandEntry->seq = seq; | 
 | 4521 |     commandEntry->handled = handled; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4522 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4523 | } | 
 | 4524 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4525 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, | 
 | 4526 |                                                   const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 |     ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4528 |           connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4530 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 4531 |             &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4532 |     commandEntry->connection = connection; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4533 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | } | 
 | 4535 |  | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4536 | void InputDispatcher::onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4537 |                                            const sp<InputWindowHandle>& newFocus) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4538 |     sp<IBinder> oldToken = oldFocus != nullptr ? oldFocus->getToken() : nullptr; | 
 | 4539 |     sp<IBinder> newToken = newFocus != nullptr ? newFocus->getToken() : nullptr; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4540 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 4541 |             &InputDispatcher::doNotifyFocusChangedLockedInterruptible); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4542 |     commandEntry->oldToken = oldToken; | 
 | 4543 |     commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4544 |     postCommandLocked(std::move(commandEntry)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4545 | } | 
 | 4546 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4547 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { | 
 | 4548 |     // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
 | 4549 |     // is already healthy again. Don't raise ANR in this situation | 
 | 4550 |     if (connection->waitQueue.empty()) { | 
 | 4551 |         ALOGI("Not raising ANR because the connection %s has recovered", | 
 | 4552 |               connection->inputChannel->getName().c_str()); | 
 | 4553 |         return; | 
 | 4554 |     } | 
 | 4555 |     /** | 
 | 4556 |      * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
 | 4557 |      * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
 | 4558 |      * has changed. This could cause newer entries to time out before the already dispatched | 
 | 4559 |      * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
 | 4560 |      * processes the events linearly. So providing information about the oldest entry seems to be | 
 | 4561 |      * most useful. | 
 | 4562 |      */ | 
 | 4563 |     DispatchEntry* oldestEntry = *connection->waitQueue.begin(); | 
 | 4564 |     const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
 | 4565 |     std::string reason = | 
 | 4566 |             android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
 | 4567 |                                         connection->inputChannel->getName().c_str(), | 
 | 4568 |                                         ns2ms(currentWait), | 
 | 4569 |                                         oldestEntry->eventEntry->getDescription().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4570 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4571 |     updateLastAnrStateLocked(getWindowHandleLocked(connection->inputChannel->getConnectionToken()), | 
 | 4572 |                              reason); | 
 | 4573 |  | 
 | 4574 |     std::unique_ptr<CommandEntry> commandEntry = | 
 | 4575 |             std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
 | 4576 |     commandEntry->inputApplicationHandle = nullptr; | 
 | 4577 |     commandEntry->inputChannel = connection->inputChannel; | 
 | 4578 |     commandEntry->reason = std::move(reason); | 
 | 4579 |     postCommandLocked(std::move(commandEntry)); | 
 | 4580 | } | 
 | 4581 |  | 
 | 4582 | void InputDispatcher::onAnrLocked(const sp<InputApplicationHandle>& application) { | 
 | 4583 |     std::string reason = android::base::StringPrintf("%s does not have a focused window", | 
 | 4584 |                                                      application->getName().c_str()); | 
 | 4585 |  | 
 | 4586 |     updateLastAnrStateLocked(application, reason); | 
 | 4587 |  | 
 | 4588 |     std::unique_ptr<CommandEntry> commandEntry = | 
 | 4589 |             std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
 | 4590 |     commandEntry->inputApplicationHandle = application; | 
 | 4591 |     commandEntry->inputChannel = nullptr; | 
 | 4592 |     commandEntry->reason = std::move(reason); | 
 | 4593 |     postCommandLocked(std::move(commandEntry)); | 
 | 4594 | } | 
 | 4595 |  | 
 | 4596 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, | 
 | 4597 |                                                const std::string& reason) { | 
 | 4598 |     const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
 | 4599 |     updateLastAnrStateLocked(windowLabel, reason); | 
 | 4600 | } | 
 | 4601 |  | 
 | 4602 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputApplicationHandle>& application, | 
 | 4603 |                                                const std::string& reason) { | 
 | 4604 |     const std::string windowLabel = getApplicationWindowLabel(application, nullptr); | 
 | 4605 |     updateLastAnrStateLocked(windowLabel, reason); | 
 | 4606 | } | 
 | 4607 |  | 
 | 4608 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
 | 4609 |                                                const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4610 |     // 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] | 4611 |     time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4612 |     struct tm tm; | 
 | 4613 |     localtime_r(&t, &tm); | 
 | 4614 |     char timestr[64]; | 
 | 4615 |     strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4616 |     mLastAnrState.clear(); | 
 | 4617 |     mLastAnrState += INDENT "ANR:\n"; | 
 | 4618 |     mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4619 |     mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
 | 4620 |     mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4621 |     dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4622 | } | 
 | 4623 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4624 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4625 |     mLock.unlock(); | 
 | 4626 |  | 
 | 4627 |     mPolicy->notifyConfigurationChanged(commandEntry->eventTime); | 
 | 4628 |  | 
 | 4629 |     mLock.lock(); | 
 | 4630 | } | 
 | 4631 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4632 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4633 |     sp<Connection> connection = commandEntry->connection; | 
 | 4634 |  | 
 | 4635 |     if (connection->status != Connection::STATUS_ZOMBIE) { | 
 | 4636 |         mLock.unlock(); | 
 | 4637 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4638 |         mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4639 |  | 
 | 4640 |         mLock.lock(); | 
 | 4641 |     } | 
 | 4642 | } | 
 | 4643 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4644 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4645 |     sp<IBinder> oldToken = commandEntry->oldToken; | 
 | 4646 |     sp<IBinder> newToken = commandEntry->newToken; | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4647 |     mLock.unlock(); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4648 |     mPolicy->notifyFocusChanged(oldToken, newToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4649 |     mLock.lock(); | 
 | 4650 | } | 
 | 4651 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4652 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4653 |     sp<IBinder> token = | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4654 |             commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4655 |     mLock.unlock(); | 
 | 4656 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4657 |     const nsecs_t timeoutExtension = | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4658 |             mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4659 |  | 
 | 4660 |     mLock.lock(); | 
 | 4661 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4662 |     if (timeoutExtension > 0) { | 
 | 4663 |         extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); | 
 | 4664 |     } else { | 
 | 4665 |         // stop waking up for events in this connection, it is already not responding | 
 | 4666 |         sp<Connection> connection = getConnectionLocked(token); | 
 | 4667 |         if (connection == nullptr) { | 
 | 4668 |             return; | 
 | 4669 |         } | 
 | 4670 |         cancelEventsForAnrLocked(connection); | 
 | 4671 |     } | 
 | 4672 | } | 
 | 4673 |  | 
 | 4674 | void InputDispatcher::extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application, | 
 | 4675 |                                               const sp<IBinder>& connectionToken, | 
 | 4676 |                                               nsecs_t timeoutExtension) { | 
 | 4677 |     sp<Connection> connection = getConnectionLocked(connectionToken); | 
 | 4678 |     if (connection == nullptr) { | 
 | 4679 |         if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) { | 
 | 4680 |             // Maybe ANR happened because there's no focused window? | 
 | 4681 |             mNoFocusedWindowTimeoutTime = now() + timeoutExtension; | 
 | 4682 |             mAwaitedFocusedApplication = application; | 
 | 4683 |         } else { | 
 | 4684 |             // It's also possible that the connection already disappeared. No action necessary. | 
 | 4685 |         } | 
 | 4686 |         return; | 
 | 4687 |     } | 
 | 4688 |  | 
 | 4689 |     ALOGI("Raised ANR, but the policy wants to keep waiting on %s for %" PRId64 "ms longer", | 
 | 4690 |           connection->inputChannel->getName().c_str(), ns2ms(timeoutExtension)); | 
 | 4691 |  | 
 | 4692 |     connection->responsive = true; | 
 | 4693 |     const nsecs_t newTimeout = now() + timeoutExtension; | 
 | 4694 |     for (DispatchEntry* entry : connection->waitQueue) { | 
 | 4695 |         if (newTimeout >= entry->timeoutTime) { | 
 | 4696 |             // Already removed old entries when connection was marked unresponsive | 
 | 4697 |             entry->timeoutTime = newTimeout; | 
 | 4698 |             mAnrTracker.insert(entry->timeoutTime, connectionToken); | 
 | 4699 |         } | 
 | 4700 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | } | 
 | 4702 |  | 
 | 4703 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( | 
 | 4704 |         CommandEntry* commandEntry) { | 
 | 4705 |     KeyEntry* entry = commandEntry->keyEntry; | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4706 |     KeyEvent event = createKeyEvent(*entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4707 |  | 
 | 4708 |     mLock.unlock(); | 
 | 4709 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4710 |     android::base::Timer t; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4711 |     sp<IBinder> token = commandEntry->inputChannel != nullptr | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4712 |             ? commandEntry->inputChannel->getConnectionToken() | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4713 |             : nullptr; | 
 | 4714 |     nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry->policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4715 |     if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 4716 |         ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4717 |               std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4718 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4719 |  | 
 | 4720 |     mLock.lock(); | 
 | 4721 |  | 
 | 4722 |     if (delay < 0) { | 
 | 4723 |         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
 | 4724 |     } else if (!delay) { | 
 | 4725 |         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
 | 4726 |     } else { | 
 | 4727 |         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
 | 4728 |         entry->interceptKeyWakeupTime = now() + delay; | 
 | 4729 |     } | 
 | 4730 |     entry->release(); | 
 | 4731 | } | 
 | 4732 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4733 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { | 
 | 4734 |     mLock.unlock(); | 
 | 4735 |     mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); | 
 | 4736 |     mLock.lock(); | 
 | 4737 | } | 
 | 4738 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4739 | /** | 
 | 4740 |  * Connection is responsive if it has no events in the waitQueue that are older than the | 
 | 4741 |  * current time. | 
 | 4742 |  */ | 
 | 4743 | static bool isConnectionResponsive(const Connection& connection) { | 
 | 4744 |     const nsecs_t currentTime = now(); | 
 | 4745 |     for (const DispatchEntry* entry : connection.waitQueue) { | 
 | 4746 |         if (entry->timeoutTime < currentTime) { | 
 | 4747 |             return false; | 
 | 4748 |         } | 
 | 4749 |     } | 
 | 4750 |     return true; | 
 | 4751 | } | 
 | 4752 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4753 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4754 |     sp<Connection> connection = commandEntry->connection; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4755 |     const nsecs_t finishTime = commandEntry->eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4756 |     uint32_t seq = commandEntry->seq; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4757 |     const bool handled = commandEntry->handled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4758 |  | 
 | 4759 |     // Handle post-event policy actions. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4760 |     std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4761 |     if (dispatchEntryIt == connection->waitQueue.end()) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4762 |         return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4763 |     } | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4764 |     DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4765 |     const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4766 |     if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4767 |         ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
 | 4768 |               ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4769 |     } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 4770 |     reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4771 |  | 
 | 4772 |     bool restartEvent; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4773 |     if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4774 |         KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); | 
 | 4775 |         restartEvent = | 
 | 4776 |                 afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 4777 |     } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4778 |         MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); | 
 | 4779 |         restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, | 
 | 4780 |                                                            handled); | 
 | 4781 |     } else { | 
 | 4782 |         restartEvent = false; | 
 | 4783 |     } | 
 | 4784 |  | 
 | 4785 |     // Dequeue the event and start the next cycle. | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 4786 |     // Because the lock might have been released, it is possible that the | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4787 |     // contents of the wait queue to have been drained, so we need to double-check | 
 | 4788 |     // a few things. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4789 |     dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
 | 4790 |     if (dispatchEntryIt != connection->waitQueue.end()) { | 
 | 4791 |         dispatchEntry = *dispatchEntryIt; | 
 | 4792 |         connection->waitQueue.erase(dispatchEntryIt); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4793 |         mAnrTracker.erase(dispatchEntry->timeoutTime, | 
 | 4794 |                           connection->inputChannel->getConnectionToken()); | 
 | 4795 |         if (!connection->responsive) { | 
 | 4796 |             connection->responsive = isConnectionResponsive(*connection); | 
 | 4797 |         } | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4798 |         traceWaitQueueLength(connection); | 
 | 4799 |         if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4800 |             connection->outboundQueue.push_front(dispatchEntry); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 4801 |             traceOutboundQueueLength(connection); | 
 | 4802 |         } else { | 
 | 4803 |             releaseDispatchEntry(dispatchEntry); | 
 | 4804 |         } | 
 | 4805 |     } | 
 | 4806 |  | 
 | 4807 |     // Start the next dispatch cycle for this connection. | 
 | 4808 |     startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4809 | } | 
 | 4810 |  | 
 | 4811 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4812 |                                                        DispatchEntry* dispatchEntry, | 
 | 4813 |                                                        KeyEntry* keyEntry, bool handled) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4814 |     if (keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4815 |         if (!handled) { | 
 | 4816 |             // Report the key as unhandled, since the fallback was not handled. | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4817 |             mReporter->reportUnhandledKey(keyEntry->id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4818 |         } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4819 |         return false; | 
 | 4820 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4821 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4822 |     // Get the fallback key state. | 
 | 4823 |     // Clear it out after dispatching the UP. | 
 | 4824 |     int32_t originalKeyCode = keyEntry->keyCode; | 
 | 4825 |     int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
 | 4826 |     if (keyEntry->action == AKEY_EVENT_ACTION_UP) { | 
 | 4827 |         connection->inputState.removeFallbackKey(originalKeyCode); | 
 | 4828 |     } | 
 | 4829 |  | 
 | 4830 |     if (handled || !dispatchEntry->hasForegroundTarget()) { | 
 | 4831 |         // If the application handles the original key for which we previously | 
 | 4832 |         // generated a fallback or if the window is not a foreground window, | 
 | 4833 |         // then cancel the associated fallback key, if any. | 
 | 4834 |         if (fallbackKeyCode != -1) { | 
 | 4835 |             // Dispatch the unhandled key to the policy with the cancel flag. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4836 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4837 |             ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4838 |                   "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
 | 4839 |                   keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, | 
 | 4840 |                   keyEntry->policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4841 | #endif | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4842 |             KeyEvent event = createKeyEvent(*keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4843 |             event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4844 |  | 
 | 4845 |             mLock.unlock(); | 
 | 4846 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4847 |             mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4848 |                                           keyEntry->policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4849 |  | 
 | 4850 |             mLock.lock(); | 
 | 4851 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4852 |             // Cancel the fallback key. | 
 | 4853 |             if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 |                 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4855 |                                            "application handled the original non-fallback key " | 
 | 4856 |                                            "or is no longer a foreground target, " | 
 | 4857 |                                            "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 |                 options.keyCode = fallbackKeyCode; | 
 | 4859 |                 synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 |             } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4861 |             connection->inputState.removeFallbackKey(originalKeyCode); | 
 | 4862 |         } | 
 | 4863 |     } else { | 
 | 4864 |         // If the application did not handle a non-fallback key, first check | 
 | 4865 |         // that we are in a good state to perform unhandled key event processing | 
 | 4866 |         // Then ask the policy what to do with it. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4867 |         bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN && keyEntry->repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4868 |         if (fallbackKeyCode == -1 && !initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4869 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4870 |             ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4871 |                   "since this is not an initial down.  " | 
 | 4872 |                   "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
 | 4873 |                   originalKeyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4874 | #endif | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4875 |             return false; | 
 | 4876 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4877 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4878 |         // Dispatch the unhandled key to the policy. | 
 | 4879 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 4880 |         ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4881 |               "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
 | 4882 |               keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, keyEntry->policyFlags); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4883 | #endif | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 4884 |         KeyEvent event = createKeyEvent(*keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4885 |  | 
 | 4886 |         mLock.unlock(); | 
 | 4887 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4888 |         bool fallback = | 
 | 4889 |                 mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
 | 4890 |                                               &event, keyEntry->policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4891 |  | 
 | 4892 |         mLock.lock(); | 
 | 4893 |  | 
 | 4894 |         if (connection->status != Connection::STATUS_NORMAL) { | 
 | 4895 |             connection->inputState.removeFallbackKey(originalKeyCode); | 
 | 4896 |             return false; | 
 | 4897 |         } | 
 | 4898 |  | 
 | 4899 |         // Latch the fallback keycode for this key on an initial down. | 
 | 4900 |         // The fallback keycode cannot change at any other point in the lifecycle. | 
 | 4901 |         if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4902 |             if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4903 |                 fallbackKeyCode = event.getKeyCode(); | 
 | 4904 |             } else { | 
 | 4905 |                 fallbackKeyCode = AKEYCODE_UNKNOWN; | 
 | 4906 |             } | 
 | 4907 |             connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
 | 4908 |         } | 
 | 4909 |  | 
 | 4910 |         ALOG_ASSERT(fallbackKeyCode != -1); | 
 | 4911 |  | 
 | 4912 |         // Cancel the fallback key if the policy decides not to send it anymore. | 
 | 4913 |         // We will continue to dispatch the key to the policy but we will no | 
 | 4914 |         // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4915 |         if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
 | 4916 |             (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4917 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 4918 |             if (fallback) { | 
 | 4919 |                 ALOGD("Unhandled key event: Policy requested to send key %d" | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4920 |                       "as a fallback for %d, but on the DOWN it had requested " | 
 | 4921 |                       "to send %d instead.  Fallback canceled.", | 
 | 4922 |                       event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4923 |             } else { | 
 | 4924 |                 ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4925 |                       "but on the DOWN it had requested to send %d.  " | 
 | 4926 |                       "Fallback canceled.", | 
 | 4927 |                       originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4928 |             } | 
 | 4929 | #endif | 
 | 4930 |  | 
 | 4931 |             CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
 | 4932 |                                        "canceling fallback, policy no longer desires it"); | 
 | 4933 |             options.keyCode = fallbackKeyCode; | 
 | 4934 |             synthesizeCancelationEventsForConnectionLocked(connection, options); | 
 | 4935 |  | 
 | 4936 |             fallback = false; | 
 | 4937 |             fallbackKeyCode = AKEYCODE_UNKNOWN; | 
 | 4938 |             if (keyEntry->action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4939 |                 connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4940 |             } | 
 | 4941 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4942 |  | 
 | 4943 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4944 |         { | 
 | 4945 |             std::string msg; | 
 | 4946 |             const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
 | 4947 |                     connection->inputState.getFallbackKeys(); | 
 | 4948 |             for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4949 |                 msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4950 |             } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4951 |             ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4952 |                   fallbackKeys.size(), msg.c_str()); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4953 |         } | 
 | 4954 | #endif | 
 | 4955 |  | 
 | 4956 |         if (fallback) { | 
 | 4957 |             // Restart the dispatch cycle using the fallback key. | 
 | 4958 |             keyEntry->eventTime = event.getEventTime(); | 
 | 4959 |             keyEntry->deviceId = event.getDeviceId(); | 
 | 4960 |             keyEntry->source = event.getSource(); | 
 | 4961 |             keyEntry->displayId = event.getDisplayId(); | 
 | 4962 |             keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
 | 4963 |             keyEntry->keyCode = fallbackKeyCode; | 
 | 4964 |             keyEntry->scanCode = event.getScanCode(); | 
 | 4965 |             keyEntry->metaState = event.getMetaState(); | 
 | 4966 |             keyEntry->repeatCount = event.getRepeatCount(); | 
 | 4967 |             keyEntry->downTime = event.getDownTime(); | 
 | 4968 |             keyEntry->syntheticRepeat = false; | 
 | 4969 |  | 
 | 4970 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 4971 |             ALOGD("Unhandled key event: Dispatching fallback key.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4972 |                   "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
 | 4973 |                   originalKeyCode, fallbackKeyCode, keyEntry->metaState); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 4974 | #endif | 
 | 4975 |             return true; // restart the event | 
 | 4976 |         } else { | 
 | 4977 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 4978 |             ALOGD("Unhandled key event: No fallback key."); | 
 | 4979 | #endif | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 4980 |  | 
 | 4981 |             // Report the key as unhandled, since there is no fallback key. | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4982 |             mReporter->reportUnhandledKey(keyEntry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4983 |         } | 
 | 4984 |     } | 
 | 4985 |     return false; | 
 | 4986 | } | 
 | 4987 |  | 
 | 4988 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4989 |                                                           DispatchEntry* dispatchEntry, | 
 | 4990 |                                                           MotionEntry* motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4991 |     return false; | 
 | 4992 | } | 
 | 4993 |  | 
 | 4994 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { | 
 | 4995 |     mLock.unlock(); | 
 | 4996 |  | 
 | 4997 |     mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); | 
 | 4998 |  | 
 | 4999 |     mLock.lock(); | 
 | 5000 | } | 
 | 5001 |  | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5002 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { | 
 | 5003 |     KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5004 |     event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5005 |                      entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
 | 5006 |                      entry.repeatCount, entry.downTime, entry.eventTime); | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5007 |     return event; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | } | 
 | 5009 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5010 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, | 
 | 5011 |                                                const Connection& connection, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5012 |     // TODO Write some statistics about how long we spend waiting. | 
 | 5013 | } | 
 | 5014 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5015 | /** | 
 | 5016 |  * Report the touch event latency to the statsd server. | 
 | 5017 |  * Input events are reported for statistics if: | 
 | 5018 |  * - This is a touchscreen event | 
 | 5019 |  * - InputFilter is not enabled | 
 | 5020 |  * - Event is not injected or synthesized | 
 | 5021 |  * | 
 | 5022 |  * Statistics should be reported before calling addValue, to prevent a fresh new sample | 
 | 5023 |  * from getting aggregated with the "old" data. | 
 | 5024 |  */ | 
 | 5025 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) | 
 | 5026 |         REQUIRES(mLock) { | 
 | 5027 |     const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && | 
 | 5028 |             !(motionEntry.isSynthesized()) && !mInputFilterEnabled; | 
 | 5029 |     if (!reportForStatistics) { | 
 | 5030 |         return; | 
 | 5031 |     } | 
 | 5032 |  | 
 | 5033 |     if (mTouchStatistics.shouldReport()) { | 
 | 5034 |         android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), | 
 | 5035 |                                    mTouchStatistics.getMax(), mTouchStatistics.getMean(), | 
 | 5036 |                                    mTouchStatistics.getStDev(), mTouchStatistics.getCount()); | 
 | 5037 |         mTouchStatistics.reset(); | 
 | 5038 |     } | 
 | 5039 |     const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); | 
 | 5040 |     mTouchStatistics.addValue(latencyMicros); | 
 | 5041 | } | 
 | 5042 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5043 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
 | 5044 |     if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5045 |         ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5046 |     } | 
 | 5047 | } | 
 | 5048 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5049 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5050 |     if (ATRACE_ENABLED()) { | 
 | 5051 |         char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5052 |         snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5053 |         ATRACE_INT(counterName, connection->outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5054 |     } | 
 | 5055 | } | 
 | 5056 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5057 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5058 |     if (ATRACE_ENABLED()) { | 
 | 5059 |         char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5060 |         snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5061 |         ATRACE_INT(counterName, connection->waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5062 |     } | 
 | 5063 | } | 
 | 5064 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5065 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5066 |     std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5067 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5068 |     dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 |     dumpDispatchStateLocked(dump); | 
 | 5070 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5071 |     if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5072 |         dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5073 |         dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5074 |     } | 
 | 5075 | } | 
 | 5076 |  | 
 | 5077 | void InputDispatcher::monitor() { | 
 | 5078 |     // 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] | 5079 |     std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5080 |     mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5081 |     mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5082 | } | 
 | 5083 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5084 | /** | 
 | 5085 |  * Wake up the dispatcher and wait until it processes all events and commands. | 
 | 5086 |  * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
 | 5087 |  * this method can be safely called from any thread, as long as you've ensured that | 
 | 5088 |  * the work you are interested in completing has already been queued. | 
 | 5089 |  */ | 
 | 5090 | bool InputDispatcher::waitForIdle() { | 
 | 5091 |     /** | 
 | 5092 |      * Timeout should represent the longest possible time that a device might spend processing | 
 | 5093 |      * events and commands. | 
 | 5094 |      */ | 
 | 5095 |     constexpr std::chrono::duration TIMEOUT = 100ms; | 
 | 5096 |     std::unique_lock lock(mLock); | 
 | 5097 |     mLooper->wake(); | 
 | 5098 |     std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
 | 5099 |     return result == std::cv_status::no_timeout; | 
 | 5100 | } | 
 | 5101 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5102 | } // namespace android::inputdispatcher |