| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #define LOG_TAG "InputDispatcher" | 
|  | 18 | #define ATRACE_TAG ATRACE_TAG_INPUT | 
|  | 19 |  | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 |  | 
|  | 22 | // Log detailed debug messages about each inbound event notification to the dispatcher. | 
|  | 23 | #define DEBUG_INBOUND_EVENT_DETAILS 0 | 
|  | 24 |  | 
|  | 25 | // Log detailed debug messages about each outbound event processed by the dispatcher. | 
|  | 26 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 | 
|  | 27 |  | 
|  | 28 | // Log debug messages about the dispatch cycle. | 
|  | 29 | #define DEBUG_DISPATCH_CYCLE 0 | 
|  | 30 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 31 | // Log debug messages about channel creation | 
|  | 32 | #define DEBUG_CHANNEL_CREATION 0 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | // Log debug messages about input event injection. | 
|  | 35 | #define DEBUG_INJECTION 0 | 
|  | 36 |  | 
|  | 37 | // Log debug messages about input focus tracking. | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 38 | static constexpr bool DEBUG_FOCUS = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 |  | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 40 | // Log debug messages about touch occlusion | 
|  | 41 | // STOPSHIP(b/169067926): Set to false | 
|  | 42 | static constexpr bool DEBUG_TOUCH_OCCLUSION = true; | 
|  | 43 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | // Log debug messages about the app switch latency optimization. | 
|  | 45 | #define DEBUG_APP_SWITCH 0 | 
|  | 46 |  | 
|  | 47 | // Log debug messages about hover events. | 
|  | 48 | #define DEBUG_HOVER 0 | 
|  | 49 |  | 
|  | 50 | #include "InputDispatcher.h" | 
|  | 51 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 52 | #include <android-base/chrono_utils.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 53 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 54 | #include <android/os/IInputConstants.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 55 | #include <binder/Binder.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 56 | #include <input/InputDevice.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 57 | #include <input/InputWindow.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 58 | #include <log/log.h> | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 59 | #include <log/log_event_list.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 60 | #include <powermanager/PowerManager.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 61 | #include <statslog.h> | 
|  | 62 | #include <unistd.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 63 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 65 | #include <cerrno> | 
|  | 66 | #include <cinttypes> | 
|  | 67 | #include <climits> | 
|  | 68 | #include <cstddef> | 
|  | 69 | #include <ctime> | 
|  | 70 | #include <queue> | 
|  | 71 | #include <sstream> | 
|  | 72 |  | 
|  | 73 | #include "Connection.h" | 
|  | 74 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 75 | #define INDENT "  " | 
|  | 76 | #define INDENT2 "    " | 
|  | 77 | #define INDENT3 "      " | 
|  | 78 | #define INDENT4 "        " | 
|  | 79 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 80 | using android::base::StringPrintf; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 81 | using android::os::BlockUntrustedTouchesMode; | 
|  | 82 | using android::os::InputEventInjectionResult; | 
|  | 83 | using android::os::InputEventInjectionSync; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 84 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 85 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 |  | 
|  | 87 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 88 | // from which to determine an appropriate dispatching timeout. | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 89 | constexpr std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = | 
|  | 90 | std::chrono::milliseconds(android::os::IInputConstants::DEFAULT_DISPATCHING_TIMEOUT_MILLIS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 |  | 
|  | 92 | // Amount of time to allow for all pending events to be processed when an app switch | 
|  | 93 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
|  | 94 | // 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] | 95 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 |  | 
|  | 97 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) | 
|  | 98 | // before considering it stale and dropping it. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 99 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 100 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | // 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] | 102 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
|  | 103 |  | 
|  | 104 | // Log a warning when an interception call takes longer than this to process. | 
|  | 105 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 106 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 107 | // Additional key latency in case a connection is still processing some motion events. | 
|  | 108 | // This will help with the case when a user touched a button that opens a new window, | 
|  | 109 | // and gives us the chance to dispatch the key to this new window. | 
|  | 110 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
|  | 111 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 113 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
|  | 114 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 115 | // Event log tags. See EventLogTags.logtags for reference | 
|  | 116 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; | 
|  | 117 | constexpr int LOGTAG_INPUT_FOCUS = 62001; | 
|  | 118 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | static inline nsecs_t now() { | 
|  | 120 | return systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | static inline const char* toString(bool value) { | 
|  | 124 | return value ? "true" : "false"; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 128 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
|  | 129 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | static bool isValidKeyAction(int32_t action) { | 
|  | 133 | switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 134 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 135 | case AKEY_EVENT_ACTION_UP: | 
|  | 136 | return true; | 
|  | 137 | default: | 
|  | 138 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | static bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 143 | if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | ALOGE("Key event has invalid action code 0x%x", action); | 
|  | 145 | return false; | 
|  | 146 | } | 
|  | 147 | return true; | 
|  | 148 | } | 
|  | 149 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 150 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 151 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 152 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 153 | case AMOTION_EVENT_ACTION_UP: | 
|  | 154 | case AMOTION_EVENT_ACTION_CANCEL: | 
|  | 155 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 156 | case AMOTION_EVENT_ACTION_OUTSIDE: | 
|  | 157 | case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
|  | 158 | case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
|  | 159 | case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
|  | 160 | case AMOTION_EVENT_ACTION_SCROLL: | 
|  | 161 | return true; | 
|  | 162 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 163 | case AMOTION_EVENT_ACTION_POINTER_UP: { | 
|  | 164 | int32_t index = getMotionEventActionPointerIndex(action); | 
|  | 165 | return index >= 0 && index < pointerCount; | 
|  | 166 | } | 
|  | 167 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
|  | 168 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
|  | 169 | return actionButton != 0; | 
|  | 170 | default: | 
|  | 171 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | } | 
|  | 173 | } | 
|  | 174 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 175 | static int64_t millis(std::chrono::nanoseconds t) { | 
|  | 176 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | 
|  | 177 | } | 
|  | 178 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 179 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 180 | const PointerProperties* pointerProperties) { | 
|  | 181 | if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 182 | ALOGE("Motion event has invalid action code 0x%x", action); | 
|  | 183 | return false; | 
|  | 184 | } | 
|  | 185 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 186 | 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] | 187 | pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 188 | return false; | 
|  | 189 | } | 
|  | 190 | BitSet32 pointerIdBits; | 
|  | 191 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 192 | int32_t id = pointerProperties[i].id; | 
|  | 193 | if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 194 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
|  | 195 | MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 | return false; | 
|  | 197 | } | 
|  | 198 | if (pointerIdBits.hasBit(id)) { | 
|  | 199 | ALOGE("Motion event has duplicate pointer id %d", id); | 
|  | 200 | return false; | 
|  | 201 | } | 
|  | 202 | pointerIdBits.markBit(id); | 
|  | 203 | } | 
|  | 204 | return true; | 
|  | 205 | } | 
|  | 206 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 207 | static std::string dumpRegion(const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | if (region.isEmpty()) { | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 209 | return "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 212 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 213 | bool first = true; | 
|  | 214 | Region::const_iterator cur = region.begin(); | 
|  | 215 | Region::const_iterator const tail = region.end(); | 
|  | 216 | while (cur != tail) { | 
|  | 217 | if (first) { | 
|  | 218 | first = false; | 
|  | 219 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 220 | dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 222 | 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] | 223 | cur++; | 
|  | 224 | } | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 225 | return dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 228 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { | 
|  | 229 | constexpr size_t maxEntries = 50; // max events to print | 
|  | 230 | constexpr size_t skipBegin = maxEntries / 2; | 
|  | 231 | const size_t skipEnd = queue.size() - maxEntries / 2; | 
|  | 232 | // skip from maxEntries / 2 ... size() - maxEntries/2 | 
|  | 233 | // only print from 0 .. skipBegin and then from skipEnd .. size() | 
|  | 234 |  | 
|  | 235 | std::string dump; | 
|  | 236 | for (size_t i = 0; i < queue.size(); i++) { | 
|  | 237 | const DispatchEntry& entry = *queue[i]; | 
|  | 238 | if (i >= skipBegin && i < skipEnd) { | 
|  | 239 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); | 
|  | 240 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' | 
|  | 241 | continue; | 
|  | 242 | } | 
|  | 243 | dump.append(INDENT4); | 
|  | 244 | dump += entry.eventEntry->getDescription(); | 
|  | 245 | dump += StringPrintf(", seq=%" PRIu32 | 
|  | 246 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", | 
|  | 247 | entry.seq, entry.targetFlags, entry.resolvedAction, | 
|  | 248 | ns2ms(currentTime - entry.eventEntry->eventTime)); | 
|  | 249 | if (entry.deliveryTime != 0) { | 
|  | 250 | // This entry was delivered, so add information on how long we've been waiting | 
|  | 251 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); | 
|  | 252 | } | 
|  | 253 | dump.append("\n"); | 
|  | 254 | } | 
|  | 255 | return dump; | 
|  | 256 | } | 
|  | 257 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 258 | /** | 
|  | 259 | * Find the entry in std::unordered_map by key, and return it. | 
|  | 260 | * If the entry is not found, return a default constructed entry. | 
|  | 261 | * | 
|  | 262 | * Useful when the entries are vectors, since an empty vector will be returned | 
|  | 263 | * if the entry is not found. | 
|  | 264 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
|  | 265 | */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 266 | template <typename K, typename V> | 
|  | 267 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 268 | auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 269 | return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 272 | /** | 
|  | 273 | * Find the entry in std::unordered_map by value, and remove it. | 
|  | 274 | * If more than one entry has the same value, then all matching | 
|  | 275 | * key-value pairs will be removed. | 
|  | 276 | * | 
|  | 277 | * Return true if at least one value has been removed. | 
|  | 278 | */ | 
|  | 279 | template <typename K, typename V> | 
|  | 280 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { | 
|  | 281 | bool removed = false; | 
|  | 282 | for (auto it = map.begin(); it != map.end();) { | 
|  | 283 | if (it->second == value) { | 
|  | 284 | it = map.erase(it); | 
|  | 285 | removed = true; | 
|  | 286 | } else { | 
|  | 287 | it++; | 
|  | 288 | } | 
|  | 289 | } | 
|  | 290 | return removed; | 
|  | 291 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 292 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 293 | /** | 
|  | 294 | * Find the entry in std::unordered_map by key and return the value as an optional. | 
|  | 295 | */ | 
|  | 296 | template <typename K, typename V> | 
|  | 297 | static std::optional<V> getOptionalValueByKey(const std::unordered_map<K, V>& map, K key) { | 
|  | 298 | auto it = map.find(key); | 
|  | 299 | return it != map.end() ? std::optional<V>{it->second} : std::nullopt; | 
|  | 300 | } | 
|  | 301 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 302 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { | 
|  | 303 | if (first == second) { | 
|  | 304 | return true; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | if (first == nullptr || second == nullptr) { | 
|  | 308 | return false; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | return first->getToken() == second->getToken(); | 
|  | 312 | } | 
|  | 313 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 314 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
|  | 315 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; | 
|  | 316 | } | 
|  | 317 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 318 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 319 | std::shared_ptr<EventEntry> eventEntry, | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 320 | int32_t inputTargetFlags) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 321 | if (inputTarget.useDefaultPointerTransform()) { | 
|  | 322 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 323 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 324 | inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
|  | 327 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
|  | 328 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
|  | 329 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 330 | std::vector<PointerCoords> pointerCoords; | 
|  | 331 | pointerCoords.resize(motionEntry.pointerCount); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 332 |  | 
|  | 333 | // Use the first pointer information to normalize all other pointers. This could be any pointer | 
|  | 334 | // as long as all other pointers are normalized to the same value and the final DispatchEntry | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 335 | // uses the transform for the normalized pointer. | 
|  | 336 | const ui::Transform& firstPointerTransform = | 
|  | 337 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; | 
|  | 338 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 339 |  | 
|  | 340 | // Iterate through all pointers in the event to normalize against the first. | 
|  | 341 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
|  | 342 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
|  | 343 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 344 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 345 |  | 
|  | 346 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 347 | // First, apply the current pointer's transform to update the coordinates into | 
|  | 348 | // window space. | 
|  | 349 | pointerCoords[pointerIndex].transform(currTransform); | 
|  | 350 | // Next, apply the inverse transform of the normalized coordinates so the | 
|  | 351 | // current coordinates are transformed into the normalized coordinate space. | 
|  | 352 | pointerCoords[pointerIndex].transform(inverseFirstTransform); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 355 | std::unique_ptr<MotionEntry> combinedMotionEntry = | 
|  | 356 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, | 
|  | 357 | motionEntry.deviceId, motionEntry.source, | 
|  | 358 | motionEntry.displayId, motionEntry.policyFlags, | 
|  | 359 | motionEntry.action, motionEntry.actionButton, | 
|  | 360 | motionEntry.flags, motionEntry.metaState, | 
|  | 361 | motionEntry.buttonState, motionEntry.classification, | 
|  | 362 | motionEntry.edgeFlags, motionEntry.xPrecision, | 
|  | 363 | motionEntry.yPrecision, motionEntry.xCursorPosition, | 
|  | 364 | motionEntry.yCursorPosition, motionEntry.downTime, | 
|  | 365 | motionEntry.pointerCount, motionEntry.pointerProperties, | 
|  | 366 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 367 |  | 
|  | 368 | if (motionEntry.injectionState) { | 
|  | 369 | combinedMotionEntry->injectionState = motionEntry.injectionState; | 
|  | 370 | combinedMotionEntry->injectionState->refCount += 1; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 374 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, | 
|  | 375 | firstPointerTransform, inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 376 | return dispatchEntry; | 
|  | 377 | } | 
|  | 378 |  | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 379 | static void addGestureMonitors(const std::vector<Monitor>& monitors, | 
|  | 380 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, | 
|  | 381 | float yOffset = 0) { | 
|  | 382 | if (monitors.empty()) { | 
|  | 383 | return; | 
|  | 384 | } | 
|  | 385 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); | 
|  | 386 | for (const Monitor& monitor : monitors) { | 
|  | 387 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); | 
|  | 388 | } | 
|  | 389 | } | 
|  | 390 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 391 | static status_t openInputChannelPair(const std::string& name, | 
|  | 392 | std::shared_ptr<InputChannel>& serverChannel, | 
|  | 393 | std::unique_ptr<InputChannel>& clientChannel) { | 
|  | 394 | std::unique_ptr<InputChannel> uniqueServerChannel; | 
|  | 395 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); | 
|  | 396 |  | 
|  | 397 | serverChannel = std::move(uniqueServerChannel); | 
|  | 398 | return result; | 
|  | 399 | } | 
|  | 400 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 401 | const char* InputDispatcher::typeToString(InputDispatcher::FocusResult result) { | 
|  | 402 | switch (result) { | 
|  | 403 | case InputDispatcher::FocusResult::OK: | 
|  | 404 | return "Ok"; | 
|  | 405 | case InputDispatcher::FocusResult::NO_WINDOW: | 
|  | 406 | return "Window not found"; | 
|  | 407 | case InputDispatcher::FocusResult::NOT_FOCUSABLE: | 
|  | 408 | return "Window not focusable"; | 
|  | 409 | case InputDispatcher::FocusResult::NOT_VISIBLE: | 
|  | 410 | return "Window not visible"; | 
|  | 411 | } | 
|  | 412 | } | 
|  | 413 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 414 | template <typename T> | 
|  | 415 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { | 
|  | 416 | if (lhs == nullptr && rhs == nullptr) { | 
|  | 417 | return true; | 
|  | 418 | } | 
|  | 419 | if (lhs == nullptr || rhs == nullptr) { | 
|  | 420 | return false; | 
|  | 421 | } | 
|  | 422 | return *lhs == *rhs; | 
|  | 423 | } | 
|  | 424 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 425 | // --- InputDispatcher --- | 
|  | 426 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 427 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
|  | 428 | : mPolicy(policy), | 
|  | 429 | mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 430 | mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 431 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 432 | mAppSwitchSawKeyDown(false), | 
|  | 433 | mAppSwitchDueTime(LONG_LONG_MAX), | 
|  | 434 | mNextUnblockedEvent(nullptr), | 
|  | 435 | mDispatchEnabled(false), | 
|  | 436 | mDispatchFrozen(false), | 
|  | 437 | mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 438 | // mInTouchMode will be initialized by the WindowManager to the default device config. | 
|  | 439 | // To avoid leaking stack in case that call never comes, and for tests, | 
|  | 440 | // initialize it here anyways. | 
|  | 441 | mInTouchMode(true), | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 442 | mMaximumObscuringOpacityForTouch(1.0f), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 443 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 444 | mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 445 | mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 446 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 447 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 448 |  | 
|  | 449 | policy->getDispatcherConfiguration(&mConfig); | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | InputDispatcher::~InputDispatcher() { | 
|  | 453 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 454 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 455 |  | 
|  | 456 | resetKeyRepeatLocked(); | 
|  | 457 | releasePendingEventLocked(); | 
|  | 458 | drainInboundQueueLocked(); | 
|  | 459 | } | 
|  | 460 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 461 | while (!mConnectionsByFd.empty()) { | 
|  | 462 | sp<Connection> connection = mConnectionsByFd.begin()->second; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 463 | removeInputChannel(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 464 | } | 
|  | 465 | } | 
|  | 466 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 467 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 468 | if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 469 | return ALREADY_EXISTS; | 
|  | 470 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 471 | mThread = std::make_unique<InputThread>( | 
|  | 472 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
|  | 473 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 474 | } | 
|  | 475 |  | 
|  | 476 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 477 | if (mThread && mThread->isCallingThread()) { | 
|  | 478 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 479 | return INVALID_OPERATION; | 
|  | 480 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 481 | mThread.reset(); | 
|  | 482 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 483 | } | 
|  | 484 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 485 | void InputDispatcher::dispatchOnce() { | 
|  | 486 | nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
|  | 487 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 488 | std::scoped_lock _l(mLock); | 
|  | 489 | mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 |  | 
|  | 491 | // Run a dispatch loop if there are no pending commands. | 
|  | 492 | // The dispatch loop might enqueue commands to run afterwards. | 
|  | 493 | if (!haveCommandsLocked()) { | 
|  | 494 | dispatchOnceInnerLocked(&nextWakeupTime); | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | // Run all pending commands if there are any. | 
|  | 498 | // If any commands were run then force the next poll to wake up immediately. | 
|  | 499 | if (runCommandsLockedInterruptible()) { | 
|  | 500 | nextWakeupTime = LONG_LONG_MIN; | 
|  | 501 | } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 502 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 503 | // If we are still waiting for ack on some events, | 
|  | 504 | // we might have to wake up earlier to check if an app is anr'ing. | 
|  | 505 | const nsecs_t nextAnrCheck = processAnrsLocked(); | 
|  | 506 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
|  | 507 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 508 | // We are about to enter an infinitely long sleep, because we have no commands or | 
|  | 509 | // pending or queued events | 
|  | 510 | if (nextWakeupTime == LONG_LONG_MAX) { | 
|  | 511 | mDispatcherEnteredIdle.notify_all(); | 
|  | 512 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 513 | } // release lock | 
|  | 514 |  | 
|  | 515 | // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
|  | 516 | nsecs_t currentTime = now(); | 
|  | 517 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
|  | 518 | mLooper->pollOnce(timeoutMillis); | 
|  | 519 | } | 
|  | 520 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 521 | /** | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 522 | * Raise ANR if there is no focused window. | 
|  | 523 | * Before the ANR is raised, do a final state check: | 
|  | 524 | * 1. The currently focused application must be the same one we are waiting for. | 
|  | 525 | * 2. Ensure we still don't have a focused window. | 
|  | 526 | */ | 
|  | 527 | void InputDispatcher::processNoFocusedWindowAnrLocked() { | 
|  | 528 | // Check if the application that we are waiting for is still focused. | 
|  | 529 | std::shared_ptr<InputApplicationHandle> focusedApplication = | 
|  | 530 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); | 
|  | 531 | if (focusedApplication == nullptr || | 
|  | 532 | focusedApplication->getApplicationToken() != | 
|  | 533 | mAwaitedFocusedApplication->getApplicationToken()) { | 
|  | 534 | // Unexpected because we should have reset the ANR timer when focused application changed | 
|  | 535 | ALOGE("Waited for a focused window, but focused application has already changed to %s", | 
|  | 536 | focusedApplication->getName().c_str()); | 
|  | 537 | return; // The focused application has changed. | 
|  | 538 | } | 
|  | 539 |  | 
|  | 540 | const sp<InputWindowHandle>& focusedWindowHandle = | 
|  | 541 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); | 
|  | 542 | if (focusedWindowHandle != nullptr) { | 
|  | 543 | return; // We now have a focused window. No need for ANR. | 
|  | 544 | } | 
|  | 545 | onAnrLocked(mAwaitedFocusedApplication); | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | /** | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 549 | * Check if any of the connections' wait queues have events that are too old. | 
|  | 550 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
|  | 551 | * Return the time at which we should wake up next. | 
|  | 552 | */ | 
|  | 553 | nsecs_t InputDispatcher::processAnrsLocked() { | 
|  | 554 | const nsecs_t currentTime = now(); | 
|  | 555 | nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
|  | 556 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
|  | 557 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
|  | 558 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 559 | processNoFocusedWindowAnrLocked(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 560 | mAwaitedFocusedApplication.reset(); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 561 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 562 | return LONG_LONG_MIN; | 
|  | 563 | } else { | 
| Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 564 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 565 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
|  | 566 | } | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | // Check if any connection ANRs are due | 
|  | 570 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
|  | 571 | if (currentTime < nextAnrCheck) { // most likely scenario | 
|  | 572 | return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | // If we reached here, we have an unresponsive connection. | 
|  | 576 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
|  | 577 | if (connection == nullptr) { | 
|  | 578 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
|  | 579 | return nextAnrCheck; | 
|  | 580 | } | 
|  | 581 | connection->responsive = false; | 
|  | 582 | // Stop waking up for this unresponsive connection | 
|  | 583 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 584 | onAnrLocked(*connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 585 | return LONG_LONG_MIN; | 
|  | 586 | } | 
|  | 587 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 588 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 589 | sp<InputWindowHandle> window = getWindowHandleLocked(token); | 
|  | 590 | if (window != nullptr) { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 591 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 592 | } | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 593 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 594 | } | 
|  | 595 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
|  | 597 | nsecs_t currentTime = now(); | 
|  | 598 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 599 | // Reset the key repeat timer whenever normal dispatch is suspended while the | 
|  | 600 | // device is in a non-interactive state.  This is to ensure that we abort a key | 
|  | 601 | // repeat if the device is just coming out of sleep. | 
|  | 602 | if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 603 | resetKeyRepeatLocked(); | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
|  | 607 | if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 608 | if (DEBUG_FOCUS) { | 
|  | 609 | ALOGD("Dispatch frozen.  Waiting some more."); | 
|  | 610 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 | return; | 
|  | 612 | } | 
|  | 613 |  | 
|  | 614 | // Optimize latency of app switches. | 
|  | 615 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
|  | 616 | // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
|  | 617 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
|  | 618 | if (mAppSwitchDueTime < *nextWakeupTime) { | 
|  | 619 | *nextWakeupTime = mAppSwitchDueTime; | 
|  | 620 | } | 
|  | 621 |  | 
|  | 622 | // Ready to start a new event. | 
|  | 623 | // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 624 | if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 625 | if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | if (isAppSwitchDue) { | 
|  | 627 | // The inbound queue is empty so the app switch key we were waiting | 
|  | 628 | // for will never arrive.  Stop waiting for it. | 
|  | 629 | resetPendingAppSwitchLocked(false); | 
|  | 630 | isAppSwitchDue = false; | 
|  | 631 | } | 
|  | 632 |  | 
|  | 633 | // Synthesize a key repeat if appropriate. | 
|  | 634 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 635 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
|  | 636 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
|  | 637 | } else { | 
|  | 638 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
|  | 639 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
|  | 640 | } | 
|  | 641 | } | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | // Nothing to do if there is no pending event. | 
|  | 645 | if (!mPendingEvent) { | 
|  | 646 | return; | 
|  | 647 | } | 
|  | 648 | } else { | 
|  | 649 | // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 650 | mPendingEvent = mInboundQueue.front(); | 
|  | 651 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 652 | traceInboundQueueLengthLocked(); | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | // Poke user activity for this event. | 
|  | 656 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 657 | pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 658 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | } | 
|  | 660 |  | 
|  | 661 | // Now we have an event to dispatch. | 
|  | 662 | // 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] | 663 | ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 664 | bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 665 | DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 667 | dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 669 | dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | } | 
|  | 671 |  | 
|  | 672 | if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 673 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 | } | 
|  | 675 |  | 
|  | 676 | switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 677 | case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 678 | const ConfigurationChangedEntry& typedEntry = | 
|  | 679 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 680 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 681 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 682 | break; | 
|  | 683 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 684 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 685 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 686 | const DeviceResetEntry& typedEntry = | 
|  | 687 | static_cast<const DeviceResetEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 688 | done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 689 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 690 | break; | 
|  | 691 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 693 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 694 | std::shared_ptr<FocusEntry> typedEntry = | 
|  | 695 | std::static_pointer_cast<FocusEntry>(mPendingEvent); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 696 | dispatchFocusLocked(currentTime, typedEntry); | 
|  | 697 | done = true; | 
|  | 698 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
|  | 699 | break; | 
|  | 700 | } | 
|  | 701 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 702 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 703 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 704 | if (isAppSwitchDue) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 705 | if (isAppSwitchKeyEvent(*keyEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 706 | resetPendingAppSwitchLocked(true); | 
|  | 707 | isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 708 | } else if (dropReason == DropReason::NOT_DROPPED) { | 
|  | 709 | dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 710 | } | 
|  | 711 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 712 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 713 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 714 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 715 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 716 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 717 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 718 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 719 | break; | 
|  | 720 | } | 
|  | 721 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 722 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 723 | std::shared_ptr<MotionEntry> motionEntry = | 
|  | 724 | std::static_pointer_cast<MotionEntry>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 725 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 726 | dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 728 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 729 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 730 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 731 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 732 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 733 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 734 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 735 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 736 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 737 | } | 
|  | 738 |  | 
|  | 739 | if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 740 | if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 741 | dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 743 | mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 744 |  | 
|  | 745 | releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 746 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | } | 
|  | 748 | } | 
|  | 749 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 750 | /** | 
|  | 751 | * Return true if the events preceding this incoming motion event should be dropped | 
|  | 752 | * Return false otherwise (the default behaviour) | 
|  | 753 | */ | 
|  | 754 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 755 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 756 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 757 |  | 
|  | 758 | // Optimize case where the current application is unresponsive and the user | 
|  | 759 | // decides to touch a window in a different application. | 
|  | 760 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 761 | // the touch into the other window. | 
|  | 762 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 763 | int32_t displayId = motionEntry.displayId; | 
|  | 764 | int32_t x = static_cast<int32_t>( | 
|  | 765 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 766 | int32_t y = static_cast<int32_t>( | 
|  | 767 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
|  | 768 | sp<InputWindowHandle> touchedWindowHandle = | 
|  | 769 | findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
|  | 770 | if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 771 | touchedWindowHandle->getApplicationToken() != | 
|  | 772 | mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 773 | // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 774 | ALOGI("Pruning input queue because user touched a different application while waiting " | 
|  | 775 | "for %s", | 
|  | 776 | mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 777 | return true; | 
|  | 778 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 779 |  | 
|  | 780 | // Alternatively, maybe there's a gesture monitor that could handle this event | 
|  | 781 | std::vector<TouchedMonitor> gestureMonitors = | 
|  | 782 | findTouchedGestureMonitorsLocked(displayId, {}); | 
|  | 783 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
|  | 784 | sp<Connection> connection = | 
|  | 785 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 786 | if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 787 | // This monitor could take more input. Drop all events preceding this | 
|  | 788 | // event, so that gesture monitor could get a chance to receive the stream | 
|  | 789 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
|  | 790 | "responsive gesture monitor that may handle the event", | 
|  | 791 | mAwaitedFocusedApplication->getName().c_str()); | 
|  | 792 | return true; | 
|  | 793 | } | 
|  | 794 | } | 
|  | 795 | } | 
|  | 796 |  | 
|  | 797 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
|  | 798 | // yet been processed by some connections, the dispatcher will wait for these motion | 
|  | 799 | // events to be processed before dispatching the key event. This is because these motion events | 
|  | 800 | // may cause a new window to be launched, which the user might expect to receive focus. | 
|  | 801 | // To prevent waiting forever for such events, just send the key to the currently focused window | 
|  | 802 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
|  | 803 | ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
|  | 804 | "just send the pending key event to the focused window."); | 
|  | 805 | mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 806 | } | 
|  | 807 | return false; | 
|  | 808 | } | 
|  | 809 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 810 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 811 | bool needWake = mInboundQueue.empty(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 812 | mInboundQueue.push_back(std::move(newEntry)); | 
|  | 813 | EventEntry& entry = *(mInboundQueue.back()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 | traceInboundQueueLengthLocked(); | 
|  | 815 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 816 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 817 | case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 818 | // Optimize app switch latency. | 
|  | 819 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 820 | // the app switch key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 821 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 822 | if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 823 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 824 | mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 825 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 826 | if (mAppSwitchSawKeyDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 827 | #if DEBUG_APP_SWITCH | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 828 | ALOGD("App switch is pending!"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 829 | #endif | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 830 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 831 | mAppSwitchSawKeyDown = false; | 
|  | 832 | needWake = true; | 
|  | 833 | } | 
|  | 834 | } | 
|  | 835 | } | 
|  | 836 | break; | 
|  | 837 | } | 
|  | 838 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 839 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 840 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { | 
|  | 841 | mNextUnblockedEvent = mInboundQueue.back(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 842 | needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 843 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 844 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 845 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 846 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 847 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
|  | 848 | break; | 
|  | 849 | } | 
|  | 850 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 851 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 852 | // nothing to do | 
|  | 853 | break; | 
|  | 854 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 855 | } | 
|  | 856 |  | 
|  | 857 | return needWake; | 
|  | 858 | } | 
|  | 859 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 860 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 861 | mRecentQueue.push_back(entry); | 
|  | 862 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 863 | mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 864 | } | 
|  | 865 | } | 
|  | 866 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 867 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 868 | int32_t y, TouchState* touchState, | 
|  | 869 | bool addOutsideTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 870 | bool addPortalWindows) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 871 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { | 
|  | 872 | LOG_ALWAYS_FATAL( | 
|  | 873 | "Must provide a valid touch state if adding portal windows or outside targets"); | 
|  | 874 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | // Traverse windows from front to back to find touched window. | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 876 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 877 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 878 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 879 | if (windowInfo->displayId == displayId) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 880 | auto flags = windowInfo->flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 881 |  | 
|  | 882 | if (windowInfo->visible) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 883 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
|  | 884 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && | 
|  | 885 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 886 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 887 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | if (portalToDisplayId != ADISPLAY_ID_NONE && | 
|  | 889 | portalToDisplayId != displayId) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 890 | if (addPortalWindows) { | 
|  | 891 | // For the monitoring channels of the display. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 892 | touchState->addPortalWindow(windowHandle); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 893 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 894 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 895 | addOutsideTargets, addPortalWindows); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 896 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | // Found window. | 
|  | 898 | return windowHandle; | 
|  | 899 | } | 
|  | 900 | } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 901 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 902 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 903 | touchState->addOrUpdateWindow(windowHandle, | 
|  | 904 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
|  | 905 | BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 906 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | } | 
|  | 909 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 910 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | } | 
|  | 912 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 913 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 914 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 915 | std::vector<TouchedMonitor> touchedMonitors; | 
|  | 916 |  | 
|  | 917 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
|  | 918 | addGestureMonitors(monitors, touchedMonitors); | 
|  | 919 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { | 
|  | 920 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); | 
|  | 921 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 922 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, | 
|  | 923 | -windowInfo->frameTop); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 924 | } | 
|  | 925 | return touchedMonitors; | 
|  | 926 | } | 
|  | 927 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 928 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 929 | const char* reason; | 
|  | 930 | switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 931 | case DropReason::POLICY: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 933 | ALOGD("Dropped event because policy consumed it."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 934 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 935 | reason = "inbound event was dropped because the policy consumed it"; | 
|  | 936 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 937 | case DropReason::DISABLED: | 
|  | 938 | if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 939 | ALOGI("Dropped event because input dispatch is disabled."); | 
|  | 940 | } | 
|  | 941 | reason = "inbound event was dropped because input dispatch is disabled"; | 
|  | 942 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 943 | case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 944 | ALOGI("Dropped event because of pending overdue app switch."); | 
|  | 945 | reason = "inbound event was dropped because of pending overdue app switch"; | 
|  | 946 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 947 | case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 948 | ALOGI("Dropped event because the current application is not responding and the user " | 
|  | 949 | "has started interacting with a different application."); | 
|  | 950 | reason = "inbound event was dropped because the current application is not responding " | 
|  | 951 | "and the user has started interacting with a different application"; | 
|  | 952 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 953 | case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 954 | ALOGI("Dropped event because it is stale."); | 
|  | 955 | reason = "inbound event was dropped because it is stale"; | 
|  | 956 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 957 | case DropReason::NOT_DROPPED: { | 
|  | 958 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 959 | return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 960 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 961 | } | 
|  | 962 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 963 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 964 | case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 965 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 966 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 967 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 968 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 969 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 970 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 971 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 972 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
|  | 973 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 974 | } else { | 
|  | 975 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 976 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 977 | } | 
|  | 978 | break; | 
|  | 979 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 980 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 981 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 982 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 983 | LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); | 
|  | 984 | break; | 
|  | 985 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 | } | 
|  | 987 | } | 
|  | 988 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 989 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 990 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
|  | 991 | keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 992 | } | 
|  | 993 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 994 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
|  | 995 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
|  | 996 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 997 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 998 | } | 
|  | 999 |  | 
|  | 1000 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
|  | 1001 | return mAppSwitchDueTime != LONG_LONG_MAX; | 
|  | 1002 | } | 
|  | 1003 |  | 
|  | 1004 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
|  | 1005 | mAppSwitchDueTime = LONG_LONG_MAX; | 
|  | 1006 |  | 
|  | 1007 | #if DEBUG_APP_SWITCH | 
|  | 1008 | if (handled) { | 
|  | 1009 | ALOGD("App switch has arrived."); | 
|  | 1010 | } else { | 
|  | 1011 | ALOGD("App switch was abandoned."); | 
|  | 1012 | } | 
|  | 1013 | #endif | 
|  | 1014 | } | 
|  | 1015 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1016 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1017 | return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | bool InputDispatcher::runCommandsLockedInterruptible() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1021 | if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1022 | return false; | 
|  | 1023 | } | 
|  | 1024 |  | 
|  | 1025 | do { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1026 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1027 | mCommandQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | Command command = commandEntry->command; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1029 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 |  | 
|  | 1031 | commandEntry->connection.clear(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1032 | } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1033 | return true; | 
|  | 1034 | } | 
|  | 1035 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1036 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { | 
|  | 1037 | mCommandQueue.push_back(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | } | 
|  | 1039 |  | 
|  | 1040 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1041 | while (!mInboundQueue.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1042 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1043 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1044 | releaseInboundEventLocked(entry); | 
|  | 1045 | } | 
|  | 1046 | traceInboundQueueLengthLocked(); | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | void InputDispatcher::releasePendingEventLocked() { | 
|  | 1050 | if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1052 | mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1053 | } | 
|  | 1054 | } | 
|  | 1055 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1056 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | InjectionState* injectionState = entry->injectionState; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1058 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1059 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1060 | ALOGD("Injected inbound event was dropped."); | 
|  | 1061 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1062 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1063 | } | 
|  | 1064 | if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1065 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | } | 
|  | 1067 | addRecentEventLocked(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } | 
|  | 1069 |  | 
|  | 1070 | void InputDispatcher::resetKeyRepeatLocked() { | 
|  | 1071 | if (mKeyRepeatState.lastKeyEntry) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1072 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1073 | } | 
|  | 1074 | } | 
|  | 1075 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1076 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
|  | 1077 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1078 |  | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1079 | uint32_t policyFlags = entry->policyFlags & | 
|  | 1080 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1081 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1082 | std::shared_ptr<KeyEntry> newEntry = | 
|  | 1083 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, | 
|  | 1084 | entry->source, entry->displayId, policyFlags, entry->action, | 
|  | 1085 | entry->flags, entry->keyCode, entry->scanCode, | 
|  | 1086 | entry->metaState, entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1087 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1088 | newEntry->syntheticRepeat = true; | 
|  | 1089 | mKeyRepeatState.lastKeyEntry = newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1090 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1091 | return newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | } | 
|  | 1093 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1094 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1095 | const ConfigurationChangedEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1096 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1097 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1098 | #endif | 
|  | 1099 |  | 
|  | 1100 | // Reset key repeating in case a keyboard device was added or removed or something. | 
|  | 1101 | resetKeyRepeatLocked(); | 
|  | 1102 |  | 
|  | 1103 | // 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] | 1104 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 1105 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1106 | commandEntry->eventTime = entry.eventTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1107 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | return true; | 
|  | 1109 | } | 
|  | 1110 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1111 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, | 
|  | 1112 | const DeviceResetEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1113 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1114 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, | 
|  | 1115 | entry.deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1116 | #endif | 
|  | 1117 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1118 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1119 | options.deviceId = entry.deviceId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1121 | return true; | 
|  | 1122 | } | 
|  | 1123 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1124 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1125 | std::string_view reason) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1126 | if (mPendingEvent != nullptr) { | 
|  | 1127 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 1128 | // for the pending event to get dispatched to the newly focused window | 
|  | 1129 | mInboundQueue.push_front(mPendingEvent); | 
|  | 1130 | mPendingEvent = nullptr; | 
|  | 1131 | } | 
|  | 1132 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1133 | std::unique_ptr<FocusEntry> focusEntry = | 
|  | 1134 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, | 
|  | 1135 | reason); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1136 |  | 
|  | 1137 | // This event should go to the front of the queue, but behind all other focus events | 
|  | 1138 | // Find the last focus event, and insert right after it | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1139 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1140 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1141 | [](const std::shared_ptr<EventEntry>& event) { | 
|  | 1142 | return event->type == EventEntry::Type::FOCUS; | 
|  | 1143 | }); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1144 |  | 
|  | 1145 | // Maintain the order of focus events. Insert the entry after all other focus events. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1146 | mInboundQueue.insert(it.base(), std::move(focusEntry)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1147 | } | 
|  | 1148 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1149 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1150 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1151 | if (channel == nullptr) { | 
|  | 1152 | return; // Window has gone away | 
|  | 1153 | } | 
|  | 1154 | InputTarget target; | 
|  | 1155 | target.inputChannel = channel; | 
|  | 1156 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1157 | entry->dispatchInProgress = true; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1158 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + | 
|  | 1159 | channel->getName(); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1160 | std::string reason = std::string("reason=").append(entry->reason); | 
|  | 1161 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1162 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1163 | } | 
|  | 1164 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1165 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1166 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1168 | if (!entry->dispatchInProgress) { | 
|  | 1169 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
|  | 1170 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1171 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
|  | 1172 | if (mKeyRepeatState.lastKeyEntry && | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1173 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | // We have seen two identical key downs in a row which indicates that the device | 
|  | 1175 | // driver is automatically generating key repeats itself.  We take note of the | 
|  | 1176 | // repeat here, but we disable our own next key repeat timer since it is clear that | 
|  | 1177 | // we will not need to synthesize key repeats ourselves. | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1178 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { | 
|  | 1179 | // Make sure we don't get key down from a different device. If a different | 
|  | 1180 | // device Id has same key pressed down, the new device Id will replace the | 
|  | 1181 | // current one to hold the key repeat with repeat count reset. | 
|  | 1182 | // In the future when got a KEY_UP on the device id, drop it and do not | 
|  | 1183 | // stop the key repeat on current device. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
|  | 1185 | resetKeyRepeatLocked(); | 
|  | 1186 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
|  | 1187 | } else { | 
|  | 1188 | // Not a repeat.  Save key down state in case we do see a repeat later. | 
|  | 1189 | resetKeyRepeatLocked(); | 
|  | 1190 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
|  | 1191 | } | 
|  | 1192 | mKeyRepeatState.lastKeyEntry = entry; | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1193 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && | 
|  | 1194 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1195 | // The key on device 'deviceId' is still down, do not stop key repeat | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1196 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 1197 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); | 
|  | 1198 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1199 | } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | resetKeyRepeatLocked(); | 
|  | 1201 | } | 
|  | 1202 |  | 
|  | 1203 | if (entry->repeatCount == 1) { | 
|  | 1204 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1205 | } else { | 
|  | 1206 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | entry->dispatchInProgress = true; | 
|  | 1210 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1211 | logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } | 
|  | 1213 |  | 
|  | 1214 | // Handle case where the policy asked us to try again later last time. | 
|  | 1215 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1216 | if (currentTime < entry->interceptKeyWakeupTime) { | 
|  | 1217 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
|  | 1218 | *nextWakeupTime = entry->interceptKeyWakeupTime; | 
|  | 1219 | } | 
|  | 1220 | return false; // wait until next wakeup | 
|  | 1221 | } | 
|  | 1222 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1223 | entry->interceptKeyWakeupTime = 0; | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | // Give the policy a chance to intercept the key. | 
|  | 1227 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
|  | 1228 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1229 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
| Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1230 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1231 | sp<IBinder> focusedWindowToken = | 
|  | 1232 | getValueByKey(mFocusedWindowTokenByDisplay, getTargetDisplayId(*entry)); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1233 | commandEntry->connectionToken = focusedWindowToken; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1234 | commandEntry->keyEntry = entry; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1235 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | return false; // wait for the command to run | 
|  | 1237 | } else { | 
|  | 1238 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 1239 | } | 
|  | 1240 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1241 | if (*dropReason == DropReason::NOT_DROPPED) { | 
|  | 1242 | *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | } | 
|  | 1244 | } | 
|  | 1245 |  | 
|  | 1246 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1247 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1249 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1250 | : InputEventInjectionResult::FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1251 | mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | return true; | 
|  | 1253 | } | 
|  | 1254 |  | 
|  | 1255 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1256 | std::vector<InputTarget> inputTargets; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1257 | InputEventInjectionResult injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1258 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1259 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1260 | return false; | 
|  | 1261 | } | 
|  | 1262 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1263 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1264 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | return true; | 
|  | 1266 | } | 
|  | 1267 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1268 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1269 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1270 |  | 
|  | 1271 | // Dispatch the key. | 
|  | 1272 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1273 | return true; | 
|  | 1274 | } | 
|  | 1275 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1276 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1278 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1279 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
|  | 1280 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1281 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1282 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 1283 | entry.repeatCount, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | #endif | 
|  | 1285 | } | 
|  | 1286 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1287 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1288 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1289 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1290 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1291 | if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | entry->dispatchInProgress = true; | 
|  | 1293 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1294 | logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1295 | } | 
|  | 1296 |  | 
|  | 1297 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1298 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1299 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1300 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1301 | : InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | return true; | 
|  | 1303 | } | 
|  | 1304 |  | 
|  | 1305 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
|  | 1306 |  | 
|  | 1307 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1308 | std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 |  | 
|  | 1310 | bool conflictingPointerActions = false; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1311 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1312 | if (isPointerEvent) { | 
|  | 1313 | // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1314 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1315 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1316 | &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | } else { | 
|  | 1318 | // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1319 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1320 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1321 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1322 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1323 | return false; | 
|  | 1324 | } | 
|  | 1325 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1326 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1327 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1328 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
|  | 1329 | return true; | 
|  | 1330 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1331 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1332 | CancelationOptions::Mode mode(isPointerEvent | 
|  | 1333 | ? CancelationOptions::CANCEL_POINTER_EVENTS | 
|  | 1334 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
|  | 1335 | CancelationOptions options(mode, "input event injection failed"); | 
|  | 1336 | synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | return true; | 
|  | 1338 | } | 
|  | 1339 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1340 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1341 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 |  | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1343 | if (isPointerEvent) { | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1344 | std::unordered_map<int32_t, TouchState>::iterator it = | 
|  | 1345 | mTouchStatesByDisplay.find(entry->displayId); | 
|  | 1346 | if (it != mTouchStatesByDisplay.end()) { | 
|  | 1347 | const TouchState& state = it->second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1348 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1349 | // The event has gone through these portal windows, so we add monitoring targets of | 
|  | 1350 | // the corresponding displays as well. | 
|  | 1351 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1352 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1353 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1354 | -windowInfo->frameLeft, -windowInfo->frameTop); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1355 | } | 
|  | 1356 | } | 
|  | 1357 | } | 
|  | 1358 | } | 
|  | 1359 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1360 | // Dispatch the motion. | 
|  | 1361 | if (conflictingPointerActions) { | 
|  | 1362 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1363 | "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1365 | } | 
|  | 1366 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1367 | return true; | 
|  | 1368 | } | 
|  | 1369 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1370 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1371 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1372 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1373 | ", policyFlags=0x%x, " | 
|  | 1374 | "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
|  | 1375 | "metaState=0x%x, buttonState=0x%x," | 
|  | 1376 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1377 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
|  | 1378 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, | 
|  | 1379 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1380 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1381 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1382 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1383 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 1384 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 1385 | "orientation=%f", | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1386 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
|  | 1387 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 1388 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 1389 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 1390 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 1391 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 1392 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 1393 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 1394 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 1395 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1396 | } | 
|  | 1397 | #endif | 
|  | 1398 | } | 
|  | 1399 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1400 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, | 
|  | 1401 | std::shared_ptr<EventEntry> eventEntry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1402 | const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1403 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1404 | #if DEBUG_DISPATCH_CYCLE | 
|  | 1405 | ALOGD("dispatchEventToCurrentInputTargets"); | 
|  | 1406 | #endif | 
|  | 1407 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1408 | updateInteractionTokensLocked(*eventEntry, inputTargets); | 
|  | 1409 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1410 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
|  | 1411 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1412 | pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1413 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1414 | for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1415 | sp<Connection> connection = | 
|  | 1416 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1417 | if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1418 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1419 | } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1420 | if (DEBUG_FOCUS) { | 
|  | 1421 | ALOGD("Dropping event delivery to target with channel '%s' because it " | 
|  | 1422 | "is no longer registered with the input dispatcher.", | 
|  | 1423 | inputTarget.inputChannel->getName().c_str()); | 
|  | 1424 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1425 | } | 
|  | 1426 | } | 
|  | 1427 | } | 
|  | 1428 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1429 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
|  | 1430 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
|  | 1431 | // If the policy decides to close the app, we will get a channel removal event via | 
|  | 1432 | // unregisterInputChannel, and will clean up the connection that way. We are already not | 
|  | 1433 | // sending new pointers to the connection when it blocked, but focused events will continue to | 
|  | 1434 | // pile up. | 
|  | 1435 | ALOGW("Canceling events for %s because it is unresponsive", | 
|  | 1436 | connection->inputChannel->getName().c_str()); | 
|  | 1437 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 1438 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
|  | 1439 | "application not responding"); | 
|  | 1440 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1441 | } | 
|  | 1442 | } | 
|  | 1443 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1444 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1445 | if (DEBUG_FOCUS) { | 
|  | 1446 | ALOGD("Resetting ANR timeouts."); | 
|  | 1447 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1448 |  | 
|  | 1449 | // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1450 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1451 | mAwaitedFocusedApplication.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1452 | } | 
|  | 1453 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1454 | /** | 
|  | 1455 | * Get the display id that the given event should go to. If this event specifies a valid display id, | 
|  | 1456 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
|  | 1457 | * Focused display is the display that the user most recently interacted with. | 
|  | 1458 | */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1459 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1460 | int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1461 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1462 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1463 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 1464 | displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1465 | break; | 
|  | 1466 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1467 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1468 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1469 | displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1470 | break; | 
|  | 1471 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1472 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1473 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 1474 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 1475 | 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] | 1476 | return ADISPLAY_ID_NONE; | 
|  | 1477 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1478 | } | 
|  | 1479 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
|  | 1480 | } | 
|  | 1481 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1482 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
|  | 1483 | const char* focusedWindowName) { | 
|  | 1484 | if (mAnrTracker.empty()) { | 
|  | 1485 | // already processed all events that we waited for | 
|  | 1486 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1487 | return false; | 
|  | 1488 | } | 
|  | 1489 |  | 
|  | 1490 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
|  | 1491 | // Start the timer | 
|  | 1492 | ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " | 
|  | 1493 | "focus to change", | 
|  | 1494 | focusedWindowName); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1495 | mKeyIsWaitingForEventsTimeout = currentTime + | 
|  | 1496 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) | 
|  | 1497 | .count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1498 | return true; | 
|  | 1499 | } | 
|  | 1500 |  | 
|  | 1501 | // We still have pending events, and already started the timer | 
|  | 1502 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
|  | 1503 | return true; // Still waiting | 
|  | 1504 | } | 
|  | 1505 |  | 
|  | 1506 | // Waited too long, and some connection still hasn't processed all motions | 
|  | 1507 | // Just send the key to the focused window | 
|  | 1508 | ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
|  | 1509 | focusedWindowName); | 
|  | 1510 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1511 | return false; | 
|  | 1512 | } | 
|  | 1513 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1514 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( | 
|  | 1515 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1516 | nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1517 | std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1519 | int32_t displayId = getTargetDisplayId(entry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1520 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1521 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1522 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 1523 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1524 | // If there is no currently focused window and no focused application | 
|  | 1525 | // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1526 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
|  | 1527 | ALOGI("Dropping %s event because there is no focused window or focused application in " | 
|  | 1528 | "display %" PRId32 ".", | 
|  | 1529 | EventEntry::typeToString(entry.type), displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1530 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | } | 
|  | 1532 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1533 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
|  | 1534 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
|  | 1535 | // start interacting with another application via touch (app switch). This code can be removed | 
|  | 1536 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
|  | 1537 | // an app is expected to have a focused window. | 
|  | 1538 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
|  | 1539 | if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
|  | 1540 | // We just discovered that there's no focused window. Start the ANR timer | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1541 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( | 
|  | 1542 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
|  | 1543 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1544 | mAwaitedFocusedApplication = focusedApplicationHandle; | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1545 | mAwaitedApplicationDisplayId = displayId; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1546 | ALOGW("Waiting because no window has focus but %s may eventually add a " | 
|  | 1547 | "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1548 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1549 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1550 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1551 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
|  | 1552 | // Already raised ANR. Drop the event | 
|  | 1553 | ALOGE("Dropping %s event because there is no focused window", | 
|  | 1554 | EventEntry::typeToString(entry.type)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1555 | return InputEventInjectionResult::FAILED; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1556 | } else { | 
|  | 1557 | // Still waiting for the focused window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1558 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1559 | } | 
|  | 1560 | } | 
|  | 1561 |  | 
|  | 1562 | // we have a valid, non-null focused window | 
|  | 1563 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 1564 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1565 | // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1566 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1567 | return InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1568 | } | 
|  | 1569 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1570 | if (focusedWindowHandle->getInfo()->paused) { | 
|  | 1571 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1572 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1573 | } | 
|  | 1574 |  | 
|  | 1575 | // If the event is a key event, then we must wait for all previous events to | 
|  | 1576 | // complete before delivering it because previous events may have the | 
|  | 1577 | // side-effect of transferring focus to a different window and we want to | 
|  | 1578 | // ensure that the following keys are sent to the new window. | 
|  | 1579 | // | 
|  | 1580 | // Suppose the user touches a button in a window then immediately presses "A". | 
|  | 1581 | // If the button causes a pop-up window to appear then we want to ensure that | 
|  | 1582 | // the "A" key is delivered to the new pop-up window.  This is because users | 
|  | 1583 | // often anticipate pending UI changes when typing on a keyboard. | 
|  | 1584 | // To obtain this behavior, we must serialize key events with respect to all | 
|  | 1585 | // prior input events. | 
|  | 1586 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 1587 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
|  | 1588 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1589 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1590 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | } | 
|  | 1592 |  | 
|  | 1593 | // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1594 | addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1595 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1596 | BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 |  | 
|  | 1598 | // Done. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1599 | return InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1600 | } | 
|  | 1601 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1602 | /** | 
|  | 1603 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
|  | 1604 | * that are currently unresponsive. | 
|  | 1605 | */ | 
|  | 1606 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
|  | 1607 | const std::vector<TouchedMonitor>& monitors) const { | 
|  | 1608 | std::vector<TouchedMonitor> responsiveMonitors; | 
|  | 1609 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
|  | 1610 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
|  | 1611 | sp<Connection> connection = getConnectionLocked( | 
|  | 1612 | monitor.monitor.inputChannel->getConnectionToken()); | 
|  | 1613 | if (connection == nullptr) { | 
|  | 1614 | ALOGE("Could not find connection for monitor %s", | 
|  | 1615 | monitor.monitor.inputChannel->getName().c_str()); | 
|  | 1616 | return false; | 
|  | 1617 | } | 
|  | 1618 | if (!connection->responsive) { | 
|  | 1619 | ALOGW("Unresponsive monitor %s will not get the new gesture", | 
|  | 1620 | connection->inputChannel->getName().c_str()); | 
|  | 1621 | return false; | 
|  | 1622 | } | 
|  | 1623 | return true; | 
|  | 1624 | }); | 
|  | 1625 | return responsiveMonitors; | 
|  | 1626 | } | 
|  | 1627 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1628 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( | 
|  | 1629 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1630 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1631 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1632 | enum InjectionPermission { | 
|  | 1633 | INJECTION_PERMISSION_UNKNOWN, | 
|  | 1634 | INJECTION_PERMISSION_GRANTED, | 
|  | 1635 | INJECTION_PERMISSION_DENIED | 
|  | 1636 | }; | 
|  | 1637 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | // For security reasons, we defer updating the touch state until we are sure that | 
|  | 1639 | // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1640 | int32_t displayId = entry.displayId; | 
|  | 1641 | int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
|  | 1643 |  | 
|  | 1644 | // Update the touch state as needed based on the properties of the touch event. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1645 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1646 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1647 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); | 
|  | 1648 | sp<InputWindowHandle> newTouchedWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1650 | // Copy current touch state into tempTouchState. | 
|  | 1651 | // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
|  | 1652 | // 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] | 1653 | const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1654 | TouchState tempTouchState; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1655 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
|  | 1656 | mTouchStatesByDisplay.find(displayId); | 
|  | 1657 | if (oldStateIt != mTouchStatesByDisplay.end()) { | 
|  | 1658 | oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1659 | tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1660 | } | 
|  | 1661 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1662 | bool isSplit = tempTouchState.split; | 
|  | 1663 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
|  | 1664 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
|  | 1665 | tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1666 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
|  | 1667 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1668 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
|  | 1669 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
|  | 1670 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1671 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1672 | bool wrongDevice = false; | 
|  | 1673 | if (newGesture) { | 
|  | 1674 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1675 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1676 | ALOGI("Dropping event because a pointer for a different device is already down " | 
|  | 1677 | "in display %" PRId32, | 
|  | 1678 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1679 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1680 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1681 | switchedDevice = false; | 
|  | 1682 | wrongDevice = true; | 
|  | 1683 | goto Failed; | 
|  | 1684 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1685 | tempTouchState.reset(); | 
|  | 1686 | tempTouchState.down = down; | 
|  | 1687 | tempTouchState.deviceId = entry.deviceId; | 
|  | 1688 | tempTouchState.source = entry.source; | 
|  | 1689 | tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1690 | isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1691 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1692 | ALOGI("Dropping move event because a pointer for a different device is already active " | 
|  | 1693 | "in display %" PRId32, | 
|  | 1694 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1695 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1696 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1697 | switchedDevice = false; | 
|  | 1698 | wrongDevice = true; | 
|  | 1699 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1700 | } | 
|  | 1701 |  | 
|  | 1702 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
|  | 1703 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
|  | 1704 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1705 | int32_t x; | 
|  | 1706 | int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1707 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1708 | // Always dispatch mouse events to cursor position. | 
|  | 1709 | if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1710 | x = int32_t(entry.xCursorPosition); | 
|  | 1711 | y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1712 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1713 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1714 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1715 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1716 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1717 | newTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1718 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
|  | 1719 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1720 |  | 
|  | 1721 | std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1722 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1723 | : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1724 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1725 | // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1726 | if (newTouchedWindowHandle != nullptr && | 
|  | 1727 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1728 | // New window supports splitting, but we should never split mouse events. | 
|  | 1729 | isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | } else if (isSplit) { | 
|  | 1731 | // New window does not support splitting but we have already split events. | 
|  | 1732 | // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1733 | newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1734 | } | 
|  | 1735 |  | 
|  | 1736 | // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1737 | if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | // 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] | 1739 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1740 | } | 
|  | 1741 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1742 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
|  | 1743 | ALOGI("Not sending touch event to %s because it is paused", | 
|  | 1744 | newTouchedWindowHandle->getName().c_str()); | 
|  | 1745 | newTouchedWindowHandle = nullptr; | 
|  | 1746 | } | 
|  | 1747 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1748 | // Ensure the window has a connection and the connection is responsive | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1749 | if (newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1750 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); | 
|  | 1751 | if (!isResponsive) { | 
|  | 1752 | ALOGW("%s will not receive the new gesture at %" PRIu64, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1753 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
|  | 1754 | newTouchedWindowHandle = nullptr; | 
|  | 1755 | } | 
|  | 1756 | } | 
|  | 1757 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1758 | // Drop events that can't be trusted due to occlusion | 
|  | 1759 | if (newTouchedWindowHandle != nullptr && | 
|  | 1760 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { | 
|  | 1761 | TouchOcclusionInfo occlusionInfo = | 
|  | 1762 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1763 | if (!isTouchTrustedLocked(occlusionInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 1764 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 1765 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); | 
|  | 1766 | for (const auto& log : occlusionInfo.debugInfo) { | 
|  | 1767 | ALOGD("%s", log.c_str()); | 
|  | 1768 | } | 
|  | 1769 | } | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1770 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); | 
|  | 1771 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { | 
|  | 1772 | ALOGW("Dropping untrusted touch event due to %s/%d", | 
|  | 1773 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); | 
|  | 1774 | newTouchedWindowHandle = nullptr; | 
|  | 1775 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1776 | } | 
|  | 1777 | } | 
|  | 1778 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1779 | // Also don't send the new touch event to unresponsive gesture monitors | 
|  | 1780 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
|  | 1781 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1782 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
|  | 1783 | 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] | 1784 | "(%d, %d) in display %" PRId32 ".", | 
|  | 1785 | x, y, displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1786 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1787 | goto Failed; | 
|  | 1788 | } | 
|  | 1789 |  | 
|  | 1790 | if (newTouchedWindowHandle != nullptr) { | 
|  | 1791 | // Set target flags. | 
|  | 1792 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1793 | if (isSplit) { | 
|  | 1794 | targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1795 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1796 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 1797 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 1798 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 1799 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1800 | } | 
|  | 1801 |  | 
|  | 1802 | // Update hover state. | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1803 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 1804 | newHoverWindowHandle = nullptr; | 
|  | 1805 | } else if (isHoverAction) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1806 | newHoverWindowHandle = newTouchedWindowHandle; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1807 | } | 
|  | 1808 |  | 
|  | 1809 | // Update the temporary touch state. | 
|  | 1810 | BitSet32 pointerIds; | 
|  | 1811 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1812 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1813 | pointerIds.markBit(pointerId); | 
|  | 1814 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1815 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | } | 
|  | 1817 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1818 | tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | } else { | 
|  | 1820 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
|  | 1821 |  | 
|  | 1822 | // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1823 | if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1824 | if (DEBUG_FOCUS) { | 
|  | 1825 | ALOGD("Dropping event because the pointer is not down or we previously " | 
|  | 1826 | "dropped the pointer down event in display %" PRId32, | 
|  | 1827 | displayId); | 
|  | 1828 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1829 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1830 | goto Failed; | 
|  | 1831 | } | 
|  | 1832 |  | 
|  | 1833 | // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1834 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1835 | tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1836 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 1837 | 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] | 1838 |  | 
|  | 1839 | sp<InputWindowHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1840 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1841 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1842 | if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
|  | 1843 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1844 | if (DEBUG_FOCUS) { | 
|  | 1845 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
|  | 1846 | oldTouchedWindowHandle->getName().c_str(), | 
|  | 1847 | newTouchedWindowHandle->getName().c_str(), displayId); | 
|  | 1848 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1849 | // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1850 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
|  | 1851 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
|  | 1852 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 |  | 
|  | 1854 | // Make a slippery entrance into the new window. | 
|  | 1855 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
|  | 1856 | isSplit = true; | 
|  | 1857 | } | 
|  | 1858 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1859 | int32_t targetFlags = | 
|  | 1860 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1861 | if (isSplit) { | 
|  | 1862 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 1863 | } | 
|  | 1864 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 1865 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 1866 | } | 
|  | 1867 |  | 
|  | 1868 | BitSet32 pointerIds; | 
|  | 1869 | if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1870 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1871 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1872 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1873 | } | 
|  | 1874 | } | 
|  | 1875 | } | 
|  | 1876 |  | 
|  | 1877 | if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1878 | // Let the previous window know that the hover sequence is over, unless we already did it | 
|  | 1879 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 1880 | if (mLastHoverWindowHandle != nullptr && | 
|  | 1881 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || | 
|  | 1882 | mLastHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1883 | #if DEBUG_HOVER | 
|  | 1884 | ALOGD("Sending hover exit event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1885 | mLastHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1886 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1887 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
|  | 1888 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1889 | } | 
|  | 1890 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1891 | // Let the new window know that the hover sequence is starting, unless we already did it | 
|  | 1892 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 1893 | if (newHoverWindowHandle != nullptr && | 
|  | 1894 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 1895 | newHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1896 | #if DEBUG_HOVER | 
|  | 1897 | ALOGD("Sending hover enter event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1898 | newHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1899 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1900 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
|  | 1901 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
|  | 1902 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | } | 
|  | 1904 | } | 
|  | 1905 |  | 
|  | 1906 | // Check permission to inject into all touched foreground windows and ensure there | 
|  | 1907 | // is at least one touched foreground window. | 
|  | 1908 | { | 
|  | 1909 | bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1910 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1911 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
|  | 1912 | haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1913 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1914 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1915 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 1916 | goto Failed; | 
|  | 1917 | } | 
|  | 1918 | } | 
|  | 1919 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1920 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1921 | if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1922 | ALOGI("Dropping event because there is no touched foreground window in display " | 
|  | 1923 | "%" PRId32 " or gesture monitor to receive it.", | 
|  | 1924 | displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1925 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | goto Failed; | 
|  | 1927 | } | 
|  | 1928 |  | 
|  | 1929 | // Permission granted to injection into all touched foreground windows. | 
|  | 1930 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 1931 | } | 
|  | 1932 |  | 
|  | 1933 | // Check whether windows listening for outside touches are owned by the same UID. If it is | 
|  | 1934 | // set the policy flag that we will not reveal coordinate information to this window. | 
|  | 1935 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1936 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1937 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1938 | if (foregroundWindowHandle) { | 
|  | 1939 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1940 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1941 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 1942 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; | 
|  | 1943 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1944 | tempTouchState.addOrUpdateWindow(inputWindowHandle, | 
|  | 1945 | InputTarget::FLAG_ZERO_COORDS, | 
|  | 1946 | BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1947 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | } | 
|  | 1949 | } | 
|  | 1950 | } | 
|  | 1951 | } | 
|  | 1952 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1953 | // If this is the first pointer going down and the touched window has a wallpaper | 
|  | 1954 | // then also add the touched wallpaper windows so they are locked in for the duration | 
|  | 1955 | // of the touch gesture. | 
|  | 1956 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
|  | 1957 | // engine only supports touch events.  We would need to add a mechanism similar | 
|  | 1958 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
|  | 1959 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 1960 | sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1961 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1962 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1963 | const std::vector<sp<InputWindowHandle>>& windowHandles = | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1964 | getWindowHandlesLocked(displayId); | 
|  | 1965 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1966 | const InputWindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1967 | if (info->displayId == displayId && | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1968 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1969 | tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1970 | .addOrUpdateWindow(windowHandle, | 
|  | 1971 | InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
|  | 1972 | InputTarget:: | 
|  | 1973 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
|  | 1974 | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1975 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | } | 
|  | 1977 | } | 
|  | 1978 | } | 
|  | 1979 | } | 
|  | 1980 |  | 
|  | 1981 | // Success!  Output targets. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1982 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1983 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1984 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1986 | touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | } | 
|  | 1988 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1989 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1990 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1991 | touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1992 | } | 
|  | 1993 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | // Drop the outside or hover touch windows since we will not care about them | 
|  | 1995 | // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1996 | tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1997 |  | 
|  | 1998 | Failed: | 
|  | 1999 | // Check injection permission once and for all. | 
|  | 2000 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2001 | if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2002 | injectionPermission = INJECTION_PERMISSION_GRANTED; | 
|  | 2003 | } else { | 
|  | 2004 | injectionPermission = INJECTION_PERMISSION_DENIED; | 
|  | 2005 | } | 
|  | 2006 | } | 
|  | 2007 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2008 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
|  | 2009 | return injectionResult; | 
|  | 2010 | } | 
|  | 2011 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2012 | // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2013 | if (!wrongDevice) { | 
|  | 2014 | if (switchedDevice) { | 
|  | 2015 | if (DEBUG_FOCUS) { | 
|  | 2016 | ALOGD("Conflicting pointer actions: Switched to a different device."); | 
|  | 2017 | } | 
|  | 2018 | *outConflictingPointerActions = true; | 
|  | 2019 | } | 
|  | 2020 |  | 
|  | 2021 | if (isHoverAction) { | 
|  | 2022 | // Started hovering, therefore no longer down. | 
|  | 2023 | if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2024 | if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2025 | ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
|  | 2026 | "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2027 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2028 | *outConflictingPointerActions = true; | 
|  | 2029 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2030 | tempTouchState.reset(); | 
|  | 2031 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2032 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
|  | 2033 | tempTouchState.deviceId = entry.deviceId; | 
|  | 2034 | tempTouchState.source = entry.source; | 
|  | 2035 | tempTouchState.displayId = displayId; | 
|  | 2036 | } | 
|  | 2037 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
|  | 2038 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2039 | // All pointers up or canceled. | 
|  | 2040 | tempTouchState.reset(); | 
|  | 2041 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 2042 | // First pointer went down. | 
|  | 2043 | if (oldState && oldState->down) { | 
|  | 2044 | if (DEBUG_FOCUS) { | 
|  | 2045 | ALOGD("Conflicting pointer actions: Down received while already down."); | 
|  | 2046 | } | 
|  | 2047 | *outConflictingPointerActions = true; | 
|  | 2048 | } | 
|  | 2049 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 2050 | // One pointer went up. | 
|  | 2051 | if (isSplit) { | 
|  | 2052 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 2053 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2054 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2055 | for (size_t i = 0; i < tempTouchState.windows.size();) { | 
|  | 2056 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
|  | 2057 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
|  | 2058 | touchedWindow.pointerIds.clearBit(pointerId); | 
|  | 2059 | if (touchedWindow.pointerIds.isEmpty()) { | 
|  | 2060 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
|  | 2061 | continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2062 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2063 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2064 | i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2065 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2066 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2067 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2068 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2069 | // Save changes unless the action was scroll in which case the temporary touch | 
|  | 2070 | // state was only valid for this one action. | 
|  | 2071 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
|  | 2072 | if (tempTouchState.displayId >= 0) { | 
|  | 2073 | mTouchStatesByDisplay[displayId] = tempTouchState; | 
|  | 2074 | } else { | 
|  | 2075 | mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2076 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2077 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2078 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2079 | // Update hover state. | 
|  | 2080 | mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2081 | } | 
|  | 2082 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | return injectionResult; | 
|  | 2084 | } | 
|  | 2085 |  | 
|  | 2086 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2087 | int32_t targetFlags, BitSet32 pointerIds, | 
|  | 2088 | std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2089 | std::vector<InputTarget>::iterator it = | 
|  | 2090 | std::find_if(inputTargets.begin(), inputTargets.end(), | 
|  | 2091 | [&windowHandle](const InputTarget& inputTarget) { | 
|  | 2092 | return inputTarget.inputChannel->getConnectionToken() == | 
|  | 2093 | windowHandle->getToken(); | 
|  | 2094 | }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2095 |  | 
| Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2096 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2097 |  | 
|  | 2098 | if (it == inputTargets.end()) { | 
|  | 2099 | InputTarget inputTarget; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2100 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 2101 | getInputChannelLocked(windowHandle->getToken()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2102 | if (inputChannel == nullptr) { | 
|  | 2103 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
|  | 2104 | return; | 
|  | 2105 | } | 
|  | 2106 | inputTarget.inputChannel = inputChannel; | 
|  | 2107 | inputTarget.flags = targetFlags; | 
|  | 2108 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 2109 | inputTargets.push_back(inputTarget); | 
|  | 2110 | it = inputTargets.end() - 1; | 
|  | 2111 | } | 
|  | 2112 |  | 
|  | 2113 | ALOG_ASSERT(it->flags == targetFlags); | 
|  | 2114 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
|  | 2115 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2116 | it->addPointers(pointerIds, windowInfo->transform); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2117 | } | 
|  | 2118 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2119 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2120 | int32_t displayId, float xOffset, | 
|  | 2121 | float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2122 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
|  | 2123 | mGlobalMonitorsByDisplay.find(displayId); | 
|  | 2124 |  | 
|  | 2125 | if (it != mGlobalMonitorsByDisplay.end()) { | 
|  | 2126 | const std::vector<Monitor>& monitors = it->second; | 
|  | 2127 | for (const Monitor& monitor : monitors) { | 
|  | 2128 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2129 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2130 | } | 
|  | 2131 | } | 
|  | 2132 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2133 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
|  | 2134 | float yOffset, | 
|  | 2135 | std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2136 | InputTarget target; | 
|  | 2137 | target.inputChannel = monitor.inputChannel; | 
|  | 2138 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2139 | ui::Transform t; | 
|  | 2140 | t.set(xOffset, yOffset); | 
|  | 2141 | target.setDefaultPointerTransform(t); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2142 | inputTargets.push_back(target); | 
|  | 2143 | } | 
|  | 2144 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2145 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2146 | const InjectionState* injectionState) { | 
|  | 2147 | if (injectionState && | 
|  | 2148 | (windowHandle == nullptr || | 
|  | 2149 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
|  | 2150 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2151 | if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | 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] | 2153 | "owned by uid %d", | 
|  | 2154 | injectionState->injectorPid, injectionState->injectorUid, | 
|  | 2155 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2156 | } else { | 
|  | 2157 | ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2158 | injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2159 | } | 
|  | 2160 | return false; | 
|  | 2161 | } | 
|  | 2162 | return true; | 
|  | 2163 | } | 
|  | 2164 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2165 | /** | 
|  | 2166 | * Indicate whether one window handle should be considered as obscuring | 
|  | 2167 | * another window handle. We only check a few preconditions. Actually | 
|  | 2168 | * checking the bounds is left to the caller. | 
|  | 2169 | */ | 
|  | 2170 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, | 
|  | 2171 | const sp<InputWindowHandle>& otherHandle) { | 
|  | 2172 | // Compare by token so cloned layers aren't counted | 
|  | 2173 | if (haveSameToken(windowHandle, otherHandle)) { | 
|  | 2174 | return false; | 
|  | 2175 | } | 
|  | 2176 | auto info = windowHandle->getInfo(); | 
|  | 2177 | auto otherInfo = otherHandle->getInfo(); | 
|  | 2178 | if (!otherInfo->visible) { | 
|  | 2179 | return false; | 
| Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2180 | } else if (otherInfo->alpha == 0 && | 
|  | 2181 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
|  | 2182 | // Those act as if they were invisible, so we don't need to flag them. | 
|  | 2183 | // We do want to potentially flag touchable windows even if they have 0 | 
|  | 2184 | // opacity, since they can consume touches and alter the effects of the | 
|  | 2185 | // user interaction (eg. apps that rely on | 
|  | 2186 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those | 
|  | 2187 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. | 
|  | 2188 | return false; | 
| Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2189 | } else if (info->ownerUid == otherInfo->ownerUid) { | 
|  | 2190 | // If ownerUid is the same we don't generate occlusion events as there | 
|  | 2191 | // is no security boundary within an uid. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2192 | return false; | 
| Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2193 | } else if (otherInfo->trustedOverlay) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2194 | return false; | 
|  | 2195 | } else if (otherInfo->displayId != info->displayId) { | 
|  | 2196 | return false; | 
|  | 2197 | } | 
|  | 2198 | return true; | 
|  | 2199 | } | 
|  | 2200 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2201 | /** | 
|  | 2202 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is | 
|  | 2203 | * untrusted, one should check: | 
|  | 2204 | * | 
|  | 2205 | * 1. If result.hasBlockingOcclusion is true. | 
|  | 2206 | *    If it's, it means the touch should be blocked due to a window with occlusion mode of | 
|  | 2207 | *    BLOCK_UNTRUSTED. | 
|  | 2208 | * | 
|  | 2209 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. | 
|  | 2210 | *    If it is (and 1 is false), then the touch should be blocked because a stack of windows | 
|  | 2211 | *    (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed | 
|  | 2212 | *    obscuring opacity above the threshold. Note that if there was no window of occlusion mode | 
|  | 2213 | *    USE_OPACITY, result.obscuringOpacity would've been 0 and since | 
|  | 2214 | *    mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. | 
|  | 2215 | * | 
|  | 2216 | * If neither of those is true, then it means the touch can be allowed. | 
|  | 2217 | */ | 
|  | 2218 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( | 
|  | 2219 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2220 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 2221 | int32_t displayId = windowInfo->displayId; | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2222 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2223 | TouchOcclusionInfo info; | 
|  | 2224 | info.hasBlockingOcclusion = false; | 
|  | 2225 | info.obscuringOpacity = 0; | 
|  | 2226 | info.obscuringUid = -1; | 
|  | 2227 | std::map<int32_t, float> opacityByUid; | 
|  | 2228 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
|  | 2229 | if (windowHandle == otherHandle) { | 
|  | 2230 | break; // All future windows are below us. Exit early. | 
|  | 2231 | } | 
|  | 2232 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
|  | 2233 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2234 | windowInfo->ownerUid != otherInfo->ownerUid && otherInfo->frameContainsPoint(x, y)) { | 
|  | 2235 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2236 | info.debugInfo.push_back( | 
|  | 2237 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); | 
|  | 2238 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2239 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so | 
|  | 2240 | // we perform the checks below to see if the touch can be propagated or not based on the | 
|  | 2241 | // window's touch occlusion mode | 
|  | 2242 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { | 
|  | 2243 | info.hasBlockingOcclusion = true; | 
|  | 2244 | info.obscuringUid = otherInfo->ownerUid; | 
|  | 2245 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2246 | break; | 
|  | 2247 | } | 
|  | 2248 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { | 
|  | 2249 | uint32_t uid = otherInfo->ownerUid; | 
|  | 2250 | float opacity = | 
|  | 2251 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; | 
|  | 2252 | // Given windows A and B: | 
|  | 2253 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] | 
|  | 2254 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); | 
|  | 2255 | opacityByUid[uid] = opacity; | 
|  | 2256 | if (opacity > info.obscuringOpacity) { | 
|  | 2257 | info.obscuringOpacity = opacity; | 
|  | 2258 | info.obscuringUid = uid; | 
|  | 2259 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2260 | } | 
|  | 2261 | } | 
|  | 2262 | } | 
|  | 2263 | } | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2264 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2265 | info.debugInfo.push_back( | 
|  | 2266 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); | 
|  | 2267 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2268 | return info; | 
|  | 2269 | } | 
|  | 2270 |  | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2271 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, | 
|  | 2272 | bool isTouchedWindow) const { | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2273 | return StringPrintf(INDENT2 "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 | 
|  | 2274 | ", mode=%s, alpha=%.2f, " | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 2275 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 | 
|  | 2276 | "], touchableRegion=%s, window={%s}, applicationInfo=%s, " | 
|  | 2277 | "flags={%s}, inputFeatures={%s}, hasToken=%s\n", | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2278 | (isTouchedWindow) ? "[TOUCHED] " : "", | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2279 | NamedEnum::string(info->type, "%" PRId32).c_str(), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2280 | info->packageName.c_str(), info->ownerUid, info->id, | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2281 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, | 
|  | 2282 | info->frameTop, info->frameRight, info->frameBottom, | 
|  | 2283 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 2284 | info->applicationInfo.name.c_str(), info->flags.string().c_str(), | 
|  | 2285 | info->inputFeatures.string().c_str(), toString(info->token != nullptr)); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2286 | } | 
|  | 2287 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2288 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { | 
|  | 2289 | if (occlusionInfo.hasBlockingOcclusion) { | 
|  | 2290 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), | 
|  | 2291 | occlusionInfo.obscuringUid); | 
|  | 2292 | return false; | 
|  | 2293 | } | 
|  | 2294 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { | 
|  | 2295 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " | 
|  | 2296 | "%.2f, maximum allowed = %.2f)", | 
|  | 2297 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, | 
|  | 2298 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); | 
|  | 2299 | return false; | 
|  | 2300 | } | 
|  | 2301 | return true; | 
|  | 2302 | } | 
|  | 2303 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2304 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, | 
|  | 2305 | int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2306 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2307 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2308 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2309 | if (windowHandle == otherHandle) { | 
|  | 2310 | break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2311 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2312 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2313 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2314 | otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2315 | return true; | 
|  | 2316 | } | 
|  | 2317 | } | 
|  | 2318 | return false; | 
|  | 2319 | } | 
|  | 2320 |  | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2321 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 2322 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2323 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2324 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2325 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2326 | if (windowHandle == otherHandle) { | 
|  | 2327 | break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2328 | } | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2329 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2330 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2331 | otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2332 | return true; | 
|  | 2333 | } | 
|  | 2334 | } | 
|  | 2335 | return false; | 
|  | 2336 | } | 
|  | 2337 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2338 | std::string InputDispatcher::getApplicationWindowLabel( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2339 | const std::shared_ptr<InputApplicationHandle>& applicationHandle, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2340 | const sp<InputWindowHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2341 | if (applicationHandle != nullptr) { | 
|  | 2342 | if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2343 | return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | } else { | 
|  | 2345 | return applicationHandle->getName(); | 
|  | 2346 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2347 | } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2348 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2349 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2350 | return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2351 | } | 
|  | 2352 | } | 
|  | 2353 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2354 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2355 | if (eventEntry.type == EventEntry::Type::FOCUS) { | 
|  | 2356 | // Focus events are passed to apps, but do not represent user activity. | 
|  | 2357 | return; | 
|  | 2358 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2359 | int32_t displayId = getTargetDisplayId(eventEntry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2360 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2361 | if (focusedWindowHandle != nullptr) { | 
|  | 2362 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2363 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2364 | #if DEBUG_DISPATCH_CYCLE | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2365 | 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] | 2366 | #endif | 
|  | 2367 | return; | 
|  | 2368 | } | 
|  | 2369 | } | 
|  | 2370 |  | 
|  | 2371 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2372 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2373 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2374 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 2375 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2376 | return; | 
|  | 2377 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2378 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2379 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2380 | eventType = USER_ACTIVITY_EVENT_TOUCH; | 
|  | 2381 | } | 
|  | 2382 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2383 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2384 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2385 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2386 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2387 | return; | 
|  | 2388 | } | 
|  | 2389 | eventType = USER_ACTIVITY_EVENT_BUTTON; | 
|  | 2390 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2391 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2392 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2393 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2394 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2395 | LOG_ALWAYS_FATAL("%s events are not user activity", | 
|  | 2396 | EventEntry::typeToString(eventEntry.type)); | 
|  | 2397 | break; | 
|  | 2398 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2399 | } | 
|  | 2400 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2401 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 2402 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2403 | commandEntry->eventTime = eventEntry.eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2404 | commandEntry->userActivityEventType = eventType; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2405 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2406 | } | 
|  | 2407 |  | 
|  | 2408 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2409 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2410 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2411 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2412 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2413 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2414 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2415 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2416 | ATRACE_NAME(message.c_str()); | 
|  | 2417 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2418 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2419 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2420 | "globalScaleFactor=%f, pointerIds=0x%x %s", | 
|  | 2421 | connection->getInputChannelName().c_str(), inputTarget.flags, | 
|  | 2422 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
|  | 2423 | inputTarget.getPointerInfoString().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2424 | #endif | 
|  | 2425 |  | 
|  | 2426 | // Skip this event if the connection status is not normal. | 
|  | 2427 | // We don't want to enqueue additional outbound events if the connection is broken. | 
|  | 2428 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 2429 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2430 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2431 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2432 | #endif | 
|  | 2433 | return; | 
|  | 2434 | } | 
|  | 2435 |  | 
|  | 2436 | // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2437 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
|  | 2438 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
|  | 2439 | "Entry type %s should not have FLAG_SPLIT", | 
|  | 2440 | EventEntry::typeToString(eventEntry->type)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2441 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2442 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2443 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2444 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2445 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2446 | if (!splitMotionEntry) { | 
|  | 2447 | return; // split event was dropped | 
|  | 2448 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2449 | if (DEBUG_FOCUS) { | 
|  | 2450 | ALOGD("channel '%s' ~ Split motion event.", | 
|  | 2451 | connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2452 | logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2453 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2454 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), | 
|  | 2455 | inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2456 | return; | 
|  | 2457 | } | 
|  | 2458 | } | 
|  | 2459 |  | 
|  | 2460 | // Not splitting.  Enqueue dispatch entries for the event as is. | 
|  | 2461 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
|  | 2462 | } | 
|  | 2463 |  | 
|  | 2464 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2465 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2466 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2467 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2468 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2469 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2470 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2471 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2472 | ATRACE_NAME(message.c_str()); | 
|  | 2473 | } | 
|  | 2474 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2475 | bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2476 |  | 
|  | 2477 | // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2478 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2479 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2480 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2481 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2482 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2483 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2484 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2485 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2486 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2487 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2488 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2489 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2490 |  | 
|  | 2491 | // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2492 | if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2493 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2494 | } | 
|  | 2495 | } | 
|  | 2496 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2497 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2498 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2499 | const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2500 | int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2501 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2502 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
|  | 2503 | connection->getInputChannelName().c_str(), | 
|  | 2504 | dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2505 | ATRACE_NAME(message.c_str()); | 
|  | 2506 | } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2507 | int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2508 | if (!(inputTargetFlags & dispatchMode)) { | 
|  | 2509 | return; | 
|  | 2510 | } | 
|  | 2511 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
|  | 2512 |  | 
|  | 2513 | // This is a new event. | 
|  | 2514 | // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2515 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2516 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2517 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2518 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
|  | 2519 | // different EventEntry than what was passed in. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2520 | EventEntry& newEntry = *(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2521 | // Apply target flags and update the connection's input state. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2522 | switch (newEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2523 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2524 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2525 | dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2526 | dispatchEntry->resolvedAction = keyEntry.action; | 
|  | 2527 | dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2528 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2529 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
|  | 2530 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2531 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2532 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", | 
|  | 2533 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2534 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2535 | return; // skip the inconsistent event | 
|  | 2536 | } | 
|  | 2537 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2538 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2539 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2540 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2541 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2542 | // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
|  | 2543 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
|  | 2544 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
|  | 2545 | static_cast<int32_t>(IdGenerator::Source::OTHER); | 
|  | 2546 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2547 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2548 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
|  | 2549 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
|  | 2550 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
|  | 2551 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
|  | 2552 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2553 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
|  | 2554 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
|  | 2555 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
|  | 2556 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
|  | 2557 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2558 | dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2559 | dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2560 | } | 
|  | 2561 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2562 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
|  | 2563 | motionEntry.displayId)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2564 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2565 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " | 
|  | 2566 | "event", | 
|  | 2567 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2568 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2569 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 2570 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2571 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2572 | dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2573 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
|  | 2574 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 2575 | } | 
|  | 2576 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
|  | 2577 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2578 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2579 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2580 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
|  | 2581 | dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2582 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2583 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
|  | 2584 | "event", | 
|  | 2585 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2586 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2587 | return; // skip the inconsistent event | 
|  | 2588 | } | 
|  | 2589 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2590 | dispatchEntry->resolvedEventId = | 
|  | 2591 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
|  | 2592 | ? mIdGenerator.nextId() | 
|  | 2593 | : motionEntry.id; | 
|  | 2594 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
|  | 2595 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
|  | 2596 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 2597 | motionEntry.id, dispatchEntry->resolvedEventId); | 
|  | 2598 | ATRACE_NAME(message.c_str()); | 
|  | 2599 | } | 
|  | 2600 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2601 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2602 | inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2603 |  | 
|  | 2604 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2605 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2606 | case EventEntry::Type::FOCUS: { | 
|  | 2607 | break; | 
|  | 2608 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2609 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2610 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2611 | LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2612 | EventEntry::typeToString(newEntry.type)); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2613 | break; | 
|  | 2614 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2615 | } | 
|  | 2616 |  | 
|  | 2617 | // Remember that we are waiting for this dispatch to complete. | 
|  | 2618 | if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2619 | incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | } | 
|  | 2621 |  | 
|  | 2622 | // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2623 | connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2624 | traceOutboundQueueLength(connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2625 | } | 
|  | 2626 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2627 | /** | 
|  | 2628 | * This function is purely for debugging. It helps us understand where the user interaction | 
|  | 2629 | * was taking place. For example, if user is touching launcher, we will see a log that user | 
|  | 2630 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. | 
|  | 2631 | * We will see both launcher and wallpaper in that list. | 
|  | 2632 | * Once the interaction with a particular set of connections starts, no new logs will be printed | 
|  | 2633 | * until the set of interacted connections changes. | 
|  | 2634 | * | 
|  | 2635 | * The following items are skipped, to reduce the logspam: | 
|  | 2636 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged | 
|  | 2637 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). | 
|  | 2638 | * This includes situations like the soft BACK button key. When the user releases (lifts up the | 
|  | 2639 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. | 
|  | 2640 | * Both of those ACTION_UP events would not be logged | 
|  | 2641 | * Monitors (both gesture and global): any gesture monitors or global monitors receiving events | 
|  | 2642 | * will not be logged. This is omitted to reduce the amount of data printed. | 
|  | 2643 | * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore | 
|  | 2644 | * gesture monitor is the only connection receiving the remainder of the gesture. | 
|  | 2645 | */ | 
|  | 2646 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, | 
|  | 2647 | const std::vector<InputTarget>& targets) { | 
|  | 2648 | // Skip ACTION_UP events, and all events other than keys and motions | 
|  | 2649 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 2650 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 2651 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
|  | 2652 | return; | 
|  | 2653 | } | 
|  | 2654 | } else if (entry.type == EventEntry::Type::MOTION) { | 
|  | 2655 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 2656 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || | 
|  | 2657 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2658 | return; | 
|  | 2659 | } | 
|  | 2660 | } else { | 
|  | 2661 | return; // Not a key or a motion | 
|  | 2662 | } | 
|  | 2663 |  | 
|  | 2664 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; | 
|  | 2665 | std::vector<sp<Connection>> newConnections; | 
|  | 2666 | for (const InputTarget& target : targets) { | 
|  | 2667 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == | 
|  | 2668 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2669 | continue; // Skip windows that receive ACTION_OUTSIDE | 
|  | 2670 | } | 
|  | 2671 |  | 
|  | 2672 | sp<IBinder> token = target.inputChannel->getConnectionToken(); | 
|  | 2673 | sp<Connection> connection = getConnectionLocked(token); | 
|  | 2674 | if (connection == nullptr || connection->monitor) { | 
|  | 2675 | continue; // We only need to keep track of the non-monitor connections. | 
|  | 2676 | } | 
|  | 2677 | newConnectionTokens.insert(std::move(token)); | 
|  | 2678 | newConnections.emplace_back(connection); | 
|  | 2679 | } | 
|  | 2680 | if (newConnectionTokens == mInteractionConnectionTokens) { | 
|  | 2681 | return; // no change | 
|  | 2682 | } | 
|  | 2683 | mInteractionConnectionTokens = newConnectionTokens; | 
|  | 2684 |  | 
|  | 2685 | std::string windowList; | 
|  | 2686 | for (const sp<Connection>& connection : newConnections) { | 
|  | 2687 | windowList += connection->getWindowName() + ", "; | 
|  | 2688 | } | 
|  | 2689 | std::string message = "Interaction with windows: " + windowList; | 
|  | 2690 | if (windowList.empty()) { | 
|  | 2691 | message += "<none>"; | 
|  | 2692 | } | 
|  | 2693 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; | 
|  | 2694 | } | 
|  | 2695 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2696 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2697 | const sp<IBinder>& token) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2698 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2699 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
|  | 2700 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2701 | return; | 
|  | 2702 | } | 
|  | 2703 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2704 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
|  | 2705 | if (focusedToken == token) { | 
|  | 2706 | // ignore since token is focused | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2707 | return; | 
|  | 2708 | } | 
|  | 2709 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2710 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 2711 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2712 | commandEntry->newToken = token; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2713 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2714 | } | 
|  | 2715 |  | 
|  | 2716 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2717 | const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2718 | if (ATRACE_ENABLED()) { | 
|  | 2719 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2720 | connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2721 | ATRACE_NAME(message.c_str()); | 
|  | 2722 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2723 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2724 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2725 | #endif | 
|  | 2726 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2727 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
|  | 2728 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2729 | dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2730 | const std::chrono::nanoseconds timeout = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2731 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2732 | dispatchEntry->timeoutTime = currentTime + timeout.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2733 |  | 
|  | 2734 | // Publish the event. | 
|  | 2735 | status_t status; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2736 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); | 
|  | 2737 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2738 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2739 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2740 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2741 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2742 | // Publish the key event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2743 | status = connection->inputPublisher | 
|  | 2744 | .publishKeyEvent(dispatchEntry->seq, | 
|  | 2745 | dispatchEntry->resolvedEventId, keyEntry.deviceId, | 
|  | 2746 | keyEntry.source, keyEntry.displayId, | 
|  | 2747 | std::move(hmac), dispatchEntry->resolvedAction, | 
|  | 2748 | dispatchEntry->resolvedFlags, keyEntry.keyCode, | 
|  | 2749 | keyEntry.scanCode, keyEntry.metaState, | 
|  | 2750 | keyEntry.repeatCount, keyEntry.downTime, | 
|  | 2751 | keyEntry.eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2752 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2753 | } | 
|  | 2754 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2755 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2756 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2757 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2758 | PointerCoords scaledCoords[MAX_POINTERS]; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2759 | const PointerCoords* usingCoords = motionEntry.pointerCoords; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2760 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2761 | // Set the X and Y offset and X and Y scale depending on the input source. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2762 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2763 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
|  | 2764 | float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2765 | if (globalScaleFactor != 1.0f) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2766 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
|  | 2767 | scaledCoords[i] = motionEntry.pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2768 | // Don't apply window scale here since we don't want scale to affect raw | 
|  | 2769 | // coordinates. The scale will be sent back to the client and applied | 
|  | 2770 | // later when requesting relative coordinates. | 
|  | 2771 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
|  | 2772 | 1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2773 | } | 
|  | 2774 | usingCoords = scaledCoords; | 
|  | 2775 | } | 
|  | 2776 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2777 | // We don't want the dispatch target to know. | 
|  | 2778 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2779 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2780 | scaledCoords[i].clear(); | 
|  | 2781 | } | 
|  | 2782 | usingCoords = scaledCoords; | 
|  | 2783 | } | 
|  | 2784 | } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2785 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2786 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2787 |  | 
|  | 2788 | // Publish the motion event. | 
|  | 2789 | status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2790 | .publishMotionEvent(dispatchEntry->seq, | 
|  | 2791 | dispatchEntry->resolvedEventId, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2792 | motionEntry.deviceId, motionEntry.source, | 
|  | 2793 | motionEntry.displayId, std::move(hmac), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2794 | dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2795 | motionEntry.actionButton, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2796 | dispatchEntry->resolvedFlags, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2797 | motionEntry.edgeFlags, motionEntry.metaState, | 
|  | 2798 | motionEntry.buttonState, | 
|  | 2799 | motionEntry.classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2800 | dispatchEntry->transform, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2801 | motionEntry.xPrecision, motionEntry.yPrecision, | 
|  | 2802 | motionEntry.xCursorPosition, | 
|  | 2803 | motionEntry.yCursorPosition, | 
|  | 2804 | motionEntry.downTime, motionEntry.eventTime, | 
|  | 2805 | motionEntry.pointerCount, | 
|  | 2806 | motionEntry.pointerProperties, usingCoords); | 
|  | 2807 | reportTouchEventForStatistics(motionEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2808 | break; | 
|  | 2809 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2810 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2811 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2812 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2813 | focusEntry.id, | 
|  | 2814 | focusEntry.hasFocus, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2815 | mInTouchMode); | 
|  | 2816 | break; | 
|  | 2817 | } | 
|  | 2818 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2819 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 2820 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 2821 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2822 | EventEntry::typeToString(eventEntry.type)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 2824 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2825 | } | 
|  | 2826 |  | 
|  | 2827 | // Check the result. | 
|  | 2828 | if (status) { | 
|  | 2829 | if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2830 | if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2832 | "This is unexpected because the wait queue is empty, so the pipe " | 
|  | 2833 | "should be empty and we shouldn't have any problems writing an " | 
|  | 2834 | "event to it, status=%d", | 
|  | 2835 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2836 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 2837 | } else { | 
|  | 2838 | // Pipe is full and we are waiting for the app to finish process some events | 
|  | 2839 | // before sending more events to it. | 
|  | 2840 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2841 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2842 | "waiting for the application to catch up", | 
|  | 2843 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2844 | #endif | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2845 | } | 
|  | 2846 | } else { | 
|  | 2847 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2848 | "status=%d", | 
|  | 2849 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2850 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 2851 | } | 
|  | 2852 | return; | 
|  | 2853 | } | 
|  | 2854 |  | 
|  | 2855 | // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2856 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
|  | 2857 | connection->outboundQueue.end(), | 
|  | 2858 | dispatchEntry)); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2859 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2860 | connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2861 | if (connection->responsive) { | 
|  | 2862 | mAnrTracker.insert(dispatchEntry->timeoutTime, | 
|  | 2863 | connection->inputChannel->getConnectionToken()); | 
|  | 2864 | } | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2865 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2866 | } | 
|  | 2867 | } | 
|  | 2868 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2869 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { | 
|  | 2870 | size_t size; | 
|  | 2871 | switch (event.type) { | 
|  | 2872 | case VerifiedInputEvent::Type::KEY: { | 
|  | 2873 | size = sizeof(VerifiedKeyEvent); | 
|  | 2874 | break; | 
|  | 2875 | } | 
|  | 2876 | case VerifiedInputEvent::Type::MOTION: { | 
|  | 2877 | size = sizeof(VerifiedMotionEvent); | 
|  | 2878 | break; | 
|  | 2879 | } | 
|  | 2880 | } | 
|  | 2881 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
|  | 2882 | return mHmacKeyManager.sign(start, size); | 
|  | 2883 | } | 
|  | 2884 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2885 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 2886 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 2887 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
|  | 2888 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
|  | 2889 | // Only sign events up and down events as the purely move events | 
|  | 2890 | // are tied to their up/down counterparts so signing would be redundant. | 
|  | 2891 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
|  | 2892 | verifiedEvent.actionMasked = actionMasked; | 
|  | 2893 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2894 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2895 | } | 
|  | 2896 | return INVALID_HMAC; | 
|  | 2897 | } | 
|  | 2898 |  | 
|  | 2899 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 2900 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 2901 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
|  | 2902 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
|  | 2903 | verifiedEvent.action = dispatchEntry.resolvedAction; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2904 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2905 | } | 
|  | 2906 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2908 | const sp<Connection>& connection, uint32_t seq, | 
|  | 2909 | bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2910 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2911 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2912 | connection->getInputChannelName().c_str(), seq, toString(handled)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2913 | #endif | 
|  | 2914 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2915 | if (connection->status == Connection::STATUS_BROKEN || | 
|  | 2916 | connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2917 | return; | 
|  | 2918 | } | 
|  | 2919 |  | 
|  | 2920 | // Notify other system components and prepare to start the next dispatch cycle. | 
|  | 2921 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); | 
|  | 2922 | } | 
|  | 2923 |  | 
|  | 2924 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2925 | const sp<Connection>& connection, | 
|  | 2926 | bool notify) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | #if DEBUG_DISPATCH_CYCLE | 
|  | 2928 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2929 | connection->getInputChannelName().c_str(), toString(notify)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2930 | #endif | 
|  | 2931 |  | 
|  | 2932 | // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2933 | drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2934 | traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2935 | drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2936 | traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2937 |  | 
|  | 2938 | // The connection appears to be unrecoverably broken. | 
|  | 2939 | // Ignore already broken or zombie connections. | 
|  | 2940 | if (connection->status == Connection::STATUS_NORMAL) { | 
|  | 2941 | connection->status = Connection::STATUS_BROKEN; | 
|  | 2942 |  | 
|  | 2943 | if (notify) { | 
|  | 2944 | // Notify other system components. | 
|  | 2945 | onDispatchCycleBrokenLocked(currentTime, connection); | 
|  | 2946 | } | 
|  | 2947 | } | 
|  | 2948 | } | 
|  | 2949 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2950 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
|  | 2951 | while (!queue.empty()) { | 
|  | 2952 | DispatchEntry* dispatchEntry = queue.front(); | 
|  | 2953 | queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2954 | releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2955 | } | 
|  | 2956 | } | 
|  | 2957 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 2958 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2959 | if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2960 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2961 | } | 
|  | 2962 | delete dispatchEntry; | 
|  | 2963 | } | 
|  | 2964 |  | 
|  | 2965 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { | 
|  | 2966 | InputDispatcher* d = static_cast<InputDispatcher*>(data); | 
|  | 2967 |  | 
|  | 2968 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 2969 | std::scoped_lock _l(d->mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2970 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2971 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2972 | ALOGE("Received spurious receive callback for unknown input channel.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2973 | "fd=%d, events=0x%x", | 
|  | 2974 | fd, events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2975 | return 0; // remove the callback | 
|  | 2976 | } | 
|  | 2977 |  | 
|  | 2978 | bool notify; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2979 | sp<Connection> connection = d->mConnectionsByFd[fd]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2980 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
|  | 2981 | if (!(events & ALOOPER_EVENT_INPUT)) { | 
|  | 2982 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2983 | "events=0x%x", | 
|  | 2984 | connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2985 | return 1; | 
|  | 2986 | } | 
|  | 2987 |  | 
|  | 2988 | nsecs_t currentTime = now(); | 
|  | 2989 | bool gotOne = false; | 
|  | 2990 | status_t status; | 
|  | 2991 | for (;;) { | 
|  | 2992 | uint32_t seq; | 
|  | 2993 | bool handled; | 
|  | 2994 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); | 
|  | 2995 | if (status) { | 
|  | 2996 | break; | 
|  | 2997 | } | 
|  | 2998 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); | 
|  | 2999 | gotOne = true; | 
|  | 3000 | } | 
|  | 3001 | if (gotOne) { | 
|  | 3002 | d->runCommandsLockedInterruptible(); | 
|  | 3003 | if (status == WOULD_BLOCK) { | 
|  | 3004 | return 1; | 
|  | 3005 | } | 
|  | 3006 | } | 
|  | 3007 |  | 
|  | 3008 | notify = status != DEAD_OBJECT || !connection->monitor; | 
|  | 3009 | if (notify) { | 
|  | 3010 | ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3011 | connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3012 | } | 
|  | 3013 | } else { | 
|  | 3014 | // Monitor channels are never explicitly unregistered. | 
|  | 3015 | // We do it automatically when the remote endpoint is closed so don't warn | 
|  | 3016 | // about them. | 
| arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 3017 | const bool stillHaveWindowHandle = | 
|  | 3018 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != | 
|  | 3019 | nullptr; | 
|  | 3020 | notify = !connection->monitor && stillHaveWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3021 | if (notify) { | 
|  | 3022 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3023 | "events=0x%x", | 
|  | 3024 | connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3025 | } | 
|  | 3026 | } | 
|  | 3027 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3028 | // Remove the channel. | 
|  | 3029 | d->removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3030 | return 0; // remove the callback | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3031 | }             // release lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3032 | } | 
|  | 3033 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3034 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | const CancelationOptions& options) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3036 | for (const auto& pair : mConnectionsByFd) { | 
|  | 3037 | synthesizeCancelationEventsForConnectionLocked(pair.second, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3038 | } | 
|  | 3039 | } | 
|  | 3040 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3041 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3042 | const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3043 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
|  | 3044 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
|  | 3045 | } | 
|  | 3046 |  | 
|  | 3047 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
|  | 3048 | const CancelationOptions& options, | 
|  | 3049 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
|  | 3050 | for (const auto& it : monitorsByDisplay) { | 
|  | 3051 | const std::vector<Monitor>& monitors = it.second; | 
|  | 3052 | for (const Monitor& monitor : monitors) { | 
|  | 3053 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3054 | } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3055 | } | 
|  | 3056 | } | 
|  | 3057 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3058 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3059 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3060 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3061 | if (connection == nullptr) { | 
|  | 3062 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3063 | } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3064 |  | 
|  | 3065 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | } | 
|  | 3067 |  | 
|  | 3068 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
|  | 3069 | const sp<Connection>& connection, const CancelationOptions& options) { | 
|  | 3070 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 3071 | return; | 
|  | 3072 | } | 
|  | 3073 |  | 
|  | 3074 | nsecs_t currentTime = now(); | 
|  | 3075 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3076 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3077 | connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3079 | if (cancelationEvents.empty()) { | 
|  | 3080 | return; | 
|  | 3081 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3082 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3083 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
|  | 3084 | "with reality: %s, mode=%d.", | 
|  | 3085 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
|  | 3086 | options.mode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3087 | #endif | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3088 |  | 
|  | 3089 | InputTarget target; | 
|  | 3090 | sp<InputWindowHandle> windowHandle = | 
|  | 3091 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3092 | if (windowHandle != nullptr) { | 
|  | 3093 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3094 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3095 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3096 | } | 
|  | 3097 | target.inputChannel = connection->inputChannel; | 
|  | 3098 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3099 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3100 | for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3101 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3102 | switch (cancelationEventEntry->type) { | 
|  | 3103 | case EventEntry::Type::KEY: { | 
|  | 3104 | logOutboundKeyDetails("cancel - ", | 
|  | 3105 | static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
|  | 3106 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3108 | case EventEntry::Type::MOTION: { | 
|  | 3109 | logOutboundMotionDetails("cancel - ", | 
|  | 3110 | static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
|  | 3111 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3112 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3113 | case EventEntry::Type::FOCUS: { | 
|  | 3114 | LOG_ALWAYS_FATAL("Canceling focus events is not supported"); | 
|  | 3115 | break; | 
|  | 3116 | } | 
|  | 3117 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 3118 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 3119 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
|  | 3120 | EventEntry::typeToString(cancelationEventEntry->type)); | 
|  | 3121 | break; | 
|  | 3122 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3123 | } | 
|  | 3124 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3125 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, | 
|  | 3126 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3127 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3128 |  | 
|  | 3129 | startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3130 | } | 
|  | 3131 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3132 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
|  | 3133 | const sp<Connection>& connection) { | 
|  | 3134 | if (connection->status == Connection::STATUS_BROKEN) { | 
|  | 3135 | return; | 
|  | 3136 | } | 
|  | 3137 |  | 
|  | 3138 | nsecs_t currentTime = now(); | 
|  | 3139 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3140 | std::vector<std::unique_ptr<EventEntry>> downEvents = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3141 | connection->inputState.synthesizePointerDownEvents(currentTime); | 
|  | 3142 |  | 
|  | 3143 | if (downEvents.empty()) { | 
|  | 3144 | return; | 
|  | 3145 | } | 
|  | 3146 |  | 
|  | 3147 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 3148 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
|  | 3149 | connection->getInputChannelName().c_str(), downEvents.size()); | 
|  | 3150 | #endif | 
|  | 3151 |  | 
|  | 3152 | InputTarget target; | 
|  | 3153 | sp<InputWindowHandle> windowHandle = | 
|  | 3154 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3155 | if (windowHandle != nullptr) { | 
|  | 3156 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3157 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3158 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3159 | } | 
|  | 3160 | target.inputChannel = connection->inputChannel; | 
|  | 3161 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3162 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3163 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3164 | switch (downEventEntry->type) { | 
|  | 3165 | case EventEntry::Type::MOTION: { | 
|  | 3166 | logOutboundMotionDetails("down - ", | 
|  | 3167 | static_cast<const MotionEntry&>(*downEventEntry)); | 
|  | 3168 | break; | 
|  | 3169 | } | 
|  | 3170 |  | 
|  | 3171 | case EventEntry::Type::KEY: | 
|  | 3172 | case EventEntry::Type::FOCUS: | 
|  | 3173 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 3174 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 3175 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
|  | 3176 | EventEntry::typeToString(downEventEntry->type)); | 
|  | 3177 | break; | 
|  | 3178 | } | 
|  | 3179 | } | 
|  | 3180 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3181 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, | 
|  | 3182 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3183 | } | 
|  | 3184 |  | 
|  | 3185 | startDispatchCycleLocked(currentTime, connection); | 
|  | 3186 | } | 
|  | 3187 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3188 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( | 
|  | 3189 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3190 | ALOG_ASSERT(pointerIds.value != 0); | 
|  | 3191 |  | 
|  | 3192 | uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
|  | 3193 | PointerProperties splitPointerProperties[MAX_POINTERS]; | 
|  | 3194 | PointerCoords splitPointerCoords[MAX_POINTERS]; | 
|  | 3195 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3196 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3197 | uint32_t splitPointerCount = 0; | 
|  | 3198 |  | 
|  | 3199 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3200 | originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3201 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3202 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3203 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3204 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3205 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
|  | 3206 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
|  | 3207 | splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3208 | originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3209 | splitPointerCount += 1; | 
|  | 3210 | } | 
|  | 3211 | } | 
|  | 3212 |  | 
|  | 3213 | if (splitPointerCount != pointerIds.count()) { | 
|  | 3214 | // This is bad.  We are missing some of the pointers that we expected to deliver. | 
|  | 3215 | // Most likely this indicates that we received an ACTION_MOVE events that has | 
|  | 3216 | // different pointer ids than we expected based on the previous ACTION_DOWN | 
|  | 3217 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
|  | 3218 | // in this way. | 
|  | 3219 | ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3220 | "we expected there to be %d pointers.  This probably means we received " | 
|  | 3221 | "a broken sequence of pointer ids from the input device.", | 
|  | 3222 | splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3223 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3224 | } | 
|  | 3225 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3226 | int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3228 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
|  | 3229 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3230 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 3231 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3232 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3233 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3234 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3235 | if (pointerIds.count() == 1) { | 
|  | 3236 | // The first/last pointer went down/up. | 
|  | 3237 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3238 | ? AMOTION_EVENT_ACTION_DOWN | 
|  | 3239 | : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | } else { | 
|  | 3241 | // A secondary pointer went down/up. | 
|  | 3242 | uint32_t splitPointerIndex = 0; | 
|  | 3243 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
|  | 3244 | splitPointerIndex += 1; | 
|  | 3245 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3246 | action = maskedAction | | 
|  | 3247 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3248 | } | 
|  | 3249 | } else { | 
|  | 3250 | // An unrelated pointer changed. | 
|  | 3251 | action = AMOTION_EVENT_ACTION_MOVE; | 
|  | 3252 | } | 
|  | 3253 | } | 
|  | 3254 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3255 | int32_t newId = mIdGenerator.nextId(); | 
|  | 3256 | if (ATRACE_ENABLED()) { | 
|  | 3257 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
|  | 3258 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3259 | originalMotionEntry.id, newId); | 
|  | 3260 | ATRACE_NAME(message.c_str()); | 
|  | 3261 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3262 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
|  | 3263 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, | 
|  | 3264 | originalMotionEntry.deviceId, originalMotionEntry.source, | 
|  | 3265 | originalMotionEntry.displayId, | 
|  | 3266 | originalMotionEntry.policyFlags, action, | 
|  | 3267 | originalMotionEntry.actionButton, | 
|  | 3268 | originalMotionEntry.flags, originalMotionEntry.metaState, | 
|  | 3269 | originalMotionEntry.buttonState, | 
|  | 3270 | originalMotionEntry.classification, | 
|  | 3271 | originalMotionEntry.edgeFlags, | 
|  | 3272 | originalMotionEntry.xPrecision, | 
|  | 3273 | originalMotionEntry.yPrecision, | 
|  | 3274 | originalMotionEntry.xCursorPosition, | 
|  | 3275 | originalMotionEntry.yCursorPosition, | 
|  | 3276 | originalMotionEntry.downTime, splitPointerCount, | 
|  | 3277 | splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3278 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3279 | if (originalMotionEntry.injectionState) { | 
|  | 3280 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | splitMotionEntry->injectionState->refCount += 1; | 
|  | 3282 | } | 
|  | 3283 |  | 
|  | 3284 | return splitMotionEntry; | 
|  | 3285 | } | 
|  | 3286 |  | 
|  | 3287 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
|  | 3288 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3289 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | #endif | 
|  | 3291 |  | 
|  | 3292 | bool needWake; | 
|  | 3293 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3294 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3295 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3296 | std::unique_ptr<ConfigurationChangedEntry> newEntry = | 
|  | 3297 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); | 
|  | 3298 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3299 | } // release lock | 
|  | 3300 |  | 
|  | 3301 | if (needWake) { | 
|  | 3302 | mLooper->wake(); | 
|  | 3303 | } | 
|  | 3304 | } | 
|  | 3305 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3306 | /** | 
|  | 3307 | * If one of the meta shortcuts is detected, process them here: | 
|  | 3308 | *     Meta + Backspace -> generate BACK | 
|  | 3309 | *     Meta + Enter -> generate HOME | 
|  | 3310 | * This will potentially overwrite keyCode and metaState. | 
|  | 3311 | */ | 
|  | 3312 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3313 | int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3314 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
|  | 3315 | int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
|  | 3316 | if (keyCode == AKEYCODE_DEL) { | 
|  | 3317 | newKeyCode = AKEYCODE_BACK; | 
|  | 3318 | } else if (keyCode == AKEYCODE_ENTER) { | 
|  | 3319 | newKeyCode = AKEYCODE_HOME; | 
|  | 3320 | } | 
|  | 3321 | if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3322 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3323 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3324 | mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3325 | keyCode = newKeyCode; | 
|  | 3326 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3327 | } | 
|  | 3328 | } else if (action == AKEY_EVENT_ACTION_UP) { | 
|  | 3329 | // In order to maintain a consistent stream of up and down events, check to see if the key | 
|  | 3330 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
|  | 3331 | // 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] | 3332 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3333 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3334 | auto replacementIt = mReplacedKeys.find(replacement); | 
|  | 3335 | if (replacementIt != mReplacedKeys.end()) { | 
|  | 3336 | keyCode = replacementIt->second; | 
|  | 3337 | mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3338 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3339 | } | 
|  | 3340 | } | 
|  | 3341 | } | 
|  | 3342 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
|  | 3344 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3345 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 3346 | "policyFlags=0x%x, action=0x%x, " | 
|  | 3347 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
|  | 3348 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
|  | 3349 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
|  | 3350 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3351 | #endif | 
|  | 3352 | if (!validateKeyEvent(args->action)) { | 
|  | 3353 | return; | 
|  | 3354 | } | 
|  | 3355 |  | 
|  | 3356 | uint32_t policyFlags = args->policyFlags; | 
|  | 3357 | int32_t flags = args->flags; | 
|  | 3358 | int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3359 | // InputDispatcher tracks and generates key repeats on behalf of | 
|  | 3360 | // whatever notifies it, so repeatCount should always be set to 0 | 
|  | 3361 | constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
|  | 3363 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 3364 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
|  | 3365 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3366 | if (policyFlags & POLICY_FLAG_FUNCTION) { | 
|  | 3367 | metaState |= AMETA_FUNCTION_ON; | 
|  | 3368 | } | 
|  | 3369 |  | 
|  | 3370 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3371 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3372 | int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3373 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3374 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3375 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3376 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3377 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3378 | args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3379 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3380 | android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3381 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3382 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3383 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3384 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3385 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3387 | bool needWake; | 
|  | 3388 | { // acquire lock | 
|  | 3389 | mLock.lock(); | 
|  | 3390 |  | 
|  | 3391 | if (shouldSendKeyToInputFilterLocked(args)) { | 
|  | 3392 | mLock.unlock(); | 
|  | 3393 |  | 
|  | 3394 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3395 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3396 | return; // event was consumed by the filter | 
|  | 3397 | } | 
|  | 3398 |  | 
|  | 3399 | mLock.lock(); | 
|  | 3400 | } | 
|  | 3401 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3402 | std::unique_ptr<KeyEntry> newEntry = | 
|  | 3403 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, | 
|  | 3404 | args->displayId, policyFlags, args->action, flags, | 
|  | 3405 | keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3406 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3407 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3408 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3409 | mLock.unlock(); | 
|  | 3410 | } // release lock | 
|  | 3411 |  | 
|  | 3412 | if (needWake) { | 
|  | 3413 | mLooper->wake(); | 
|  | 3414 | } | 
|  | 3415 | } | 
|  | 3416 |  | 
|  | 3417 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
|  | 3418 | return mInputFilterEnabled; | 
|  | 3419 | } | 
|  | 3420 |  | 
|  | 3421 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
|  | 3422 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3423 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3424 | "displayId=%" PRId32 ", policyFlags=0x%x, " | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3425 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
|  | 3426 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
| Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3427 | "yCursorPosition=%f, downTime=%" PRId64, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3428 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
|  | 3429 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
|  | 3430 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
|  | 3431 | args->xCursorPosition, args->yCursorPosition, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3432 | for (uint32_t i = 0; i < args->pointerCount; i++) { | 
|  | 3433 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3434 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 3435 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 3436 | "orientation=%f", | 
|  | 3437 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
|  | 3438 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 3439 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 3440 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 3441 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 3442 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 3443 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 3444 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 3445 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 3446 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3447 | } | 
|  | 3448 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3449 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
|  | 3450 | args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 | return; | 
|  | 3452 | } | 
|  | 3453 |  | 
|  | 3454 | uint32_t policyFlags = args->policyFlags; | 
|  | 3455 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3456 |  | 
|  | 3457 | android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3458 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3459 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3460 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3461 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3462 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3463 |  | 
|  | 3464 | bool needWake; | 
|  | 3465 | { // acquire lock | 
|  | 3466 | mLock.lock(); | 
|  | 3467 |  | 
|  | 3468 | if (shouldSendMotionToInputFilterLocked(args)) { | 
|  | 3469 | mLock.unlock(); | 
|  | 3470 |  | 
|  | 3471 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3472 | ui::Transform transform; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3473 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
|  | 3474 | args->action, args->actionButton, args->flags, args->edgeFlags, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3475 | args->metaState, args->buttonState, args->classification, transform, | 
|  | 3476 | args->xPrecision, args->yPrecision, args->xCursorPosition, | 
|  | 3477 | args->yCursorPosition, args->downTime, args->eventTime, | 
|  | 3478 | args->pointerCount, args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 |  | 
|  | 3480 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 3481 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3482 | return; // event was consumed by the filter | 
|  | 3483 | } | 
|  | 3484 |  | 
|  | 3485 | mLock.lock(); | 
|  | 3486 | } | 
|  | 3487 |  | 
|  | 3488 | // Just enqueue a new motion event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3489 | std::unique_ptr<MotionEntry> newEntry = | 
|  | 3490 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 3491 | args->source, args->displayId, policyFlags, | 
|  | 3492 | args->action, args->actionButton, args->flags, | 
|  | 3493 | args->metaState, args->buttonState, | 
|  | 3494 | args->classification, args->edgeFlags, | 
|  | 3495 | args->xPrecision, args->yPrecision, | 
|  | 3496 | args->xCursorPosition, args->yCursorPosition, | 
|  | 3497 | args->downTime, args->pointerCount, | 
|  | 3498 | args->pointerProperties, args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3499 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3500 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3501 | mLock.unlock(); | 
|  | 3502 | } // release lock | 
|  | 3503 |  | 
|  | 3504 | if (needWake) { | 
|  | 3505 | mLooper->wake(); | 
|  | 3506 | } | 
|  | 3507 | } | 
|  | 3508 |  | 
|  | 3509 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3510 | return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3511 | } | 
|  | 3512 |  | 
|  | 3513 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
|  | 3514 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3515 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3516 | "switchMask=0x%08x", | 
|  | 3517 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3518 | #endif | 
|  | 3519 |  | 
|  | 3520 | uint32_t policyFlags = args->policyFlags; | 
|  | 3521 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3522 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3523 | } | 
|  | 3524 |  | 
|  | 3525 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
|  | 3526 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3527 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
|  | 3528 | args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3529 | #endif | 
|  | 3530 |  | 
|  | 3531 | bool needWake; | 
|  | 3532 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3533 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3535 | std::unique_ptr<DeviceResetEntry> newEntry = | 
|  | 3536 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); | 
|  | 3537 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3538 | } // release lock | 
|  | 3539 |  | 
|  | 3540 | if (needWake) { | 
|  | 3541 | mLooper->wake(); | 
|  | 3542 | } | 
|  | 3543 | } | 
|  | 3544 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3545 | InputEventInjectionResult InputDispatcher::injectInputEvent( | 
|  | 3546 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, | 
|  | 3547 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | #if DEBUG_INBOUND_EVENT_DETAILS | 
|  | 3549 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3550 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
|  | 3551 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3552 | #endif | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3553 | 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] | 3554 |  | 
|  | 3555 | policyFlags |= POLICY_FLAG_INJECTED; | 
|  | 3556 | if (hasInjectionPermission(injectorPid, injectorUid)) { | 
|  | 3557 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3558 | } | 
|  | 3559 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3560 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3561 | switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3562 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3563 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
|  | 3564 | int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3565 | if (!validateKeyEvent(action)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3566 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3567 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3568 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3569 | int32_t flags = incomingKey.getFlags(); | 
|  | 3570 | int32_t keyCode = incomingKey.getKeyCode(); | 
|  | 3571 | int32_t metaState = incomingKey.getMetaState(); | 
|  | 3572 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3573 | /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3574 | KeyEvent keyEvent; | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3575 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3576 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
|  | 3577 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
|  | 3578 | incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3579 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3580 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
|  | 3581 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3582 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3583 |  | 
|  | 3584 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3585 | android::base::Timer t; | 
|  | 3586 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
|  | 3587 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3588 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
|  | 3589 | std::to_string(t.duration().count()).c_str()); | 
|  | 3590 | } | 
|  | 3591 | } | 
|  | 3592 |  | 
|  | 3593 | mLock.lock(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3594 | std::unique_ptr<KeyEntry> injectedEntry = | 
|  | 3595 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), | 
|  | 3596 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
|  | 3597 | incomingKey.getDisplayId(), policyFlags, action, | 
|  | 3598 | flags, keyCode, incomingKey.getScanCode(), metaState, | 
|  | 3599 | incomingKey.getRepeatCount(), | 
|  | 3600 | incomingKey.getDownTime()); | 
|  | 3601 | injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3602 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3603 | } | 
|  | 3604 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3605 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3606 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); | 
|  | 3607 | int32_t action = motionEvent->getAction(); | 
|  | 3608 | size_t pointerCount = motionEvent->getPointerCount(); | 
|  | 3609 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); | 
|  | 3610 | int32_t actionButton = motionEvent->getActionButton(); | 
|  | 3611 | int32_t displayId = motionEvent->getDisplayId(); | 
|  | 3612 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3613 | return InputEventInjectionResult::FAILED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3614 | } | 
|  | 3615 |  | 
|  | 3616 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 3617 | nsecs_t eventTime = motionEvent->getEventTime(); | 
|  | 3618 | android::base::Timer t; | 
|  | 3619 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
|  | 3620 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3621 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
|  | 3622 | std::to_string(t.duration().count()).c_str()); | 
|  | 3623 | } | 
|  | 3624 | } | 
|  | 3625 |  | 
|  | 3626 | mLock.lock(); | 
|  | 3627 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); | 
|  | 3628 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3629 | std::unique_ptr<MotionEntry> injectedEntry = | 
|  | 3630 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, | 
|  | 3631 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
|  | 3632 | motionEvent->getDisplayId(), policyFlags, action, | 
|  | 3633 | actionButton, motionEvent->getFlags(), | 
|  | 3634 | motionEvent->getMetaState(), | 
|  | 3635 | motionEvent->getButtonState(), | 
|  | 3636 | motionEvent->getClassification(), | 
|  | 3637 | motionEvent->getEdgeFlags(), | 
|  | 3638 | motionEvent->getXPrecision(), | 
|  | 3639 | motionEvent->getYPrecision(), | 
|  | 3640 | motionEvent->getRawXCursorPosition(), | 
|  | 3641 | motionEvent->getRawYCursorPosition(), | 
|  | 3642 | motionEvent->getDownTime(), | 
|  | 3643 | uint32_t(pointerCount), pointerProperties, | 
|  | 3644 | samplePointerCoords, motionEvent->getXOffset(), | 
|  | 3645 | motionEvent->getYOffset()); | 
|  | 3646 | injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3647 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { | 
|  | 3648 | sampleEventTimes += 1; | 
|  | 3649 | samplePointerCoords += pointerCount; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3650 | std::unique_ptr<MotionEntry> nextInjectedEntry = | 
|  | 3651 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, | 
|  | 3652 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
|  | 3653 | motionEvent->getDisplayId(), policyFlags, | 
|  | 3654 | action, actionButton, motionEvent->getFlags(), | 
|  | 3655 | motionEvent->getMetaState(), | 
|  | 3656 | motionEvent->getButtonState(), | 
|  | 3657 | motionEvent->getClassification(), | 
|  | 3658 | motionEvent->getEdgeFlags(), | 
|  | 3659 | motionEvent->getXPrecision(), | 
|  | 3660 | motionEvent->getYPrecision(), | 
|  | 3661 | motionEvent->getRawXCursorPosition(), | 
|  | 3662 | motionEvent->getRawYCursorPosition(), | 
|  | 3663 | motionEvent->getDownTime(), | 
|  | 3664 | uint32_t(pointerCount), pointerProperties, | 
|  | 3665 | samplePointerCoords, | 
|  | 3666 | motionEvent->getXOffset(), | 
|  | 3667 | motionEvent->getYOffset()); | 
|  | 3668 | injectedEntries.push(std::move(nextInjectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3669 | } | 
|  | 3670 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3671 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3672 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3673 | default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3674 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3675 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3676 | } | 
|  | 3677 |  | 
|  | 3678 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3679 | if (syncMode == InputEventInjectionSync::NONE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3680 | injectionState->injectionIsAsync = true; | 
|  | 3681 | } | 
|  | 3682 |  | 
|  | 3683 | injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3684 | injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3685 |  | 
|  | 3686 | bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3687 | while (!injectedEntries.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3688 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3689 | injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3690 | } | 
|  | 3691 |  | 
|  | 3692 | mLock.unlock(); | 
|  | 3693 |  | 
|  | 3694 | if (needWake) { | 
|  | 3695 | mLooper->wake(); | 
|  | 3696 | } | 
|  | 3697 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3698 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3700 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3701 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3702 | if (syncMode == InputEventInjectionSync::NONE) { | 
|  | 3703 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | } else { | 
|  | 3705 | for (;;) { | 
|  | 3706 | injectionResult = injectionState->injectionResult; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3707 | if (injectionResult != InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | break; | 
|  | 3709 | } | 
|  | 3710 |  | 
|  | 3711 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 3712 | if (remainingTimeout <= 0) { | 
|  | 3713 | #if DEBUG_INJECTION | 
|  | 3714 | ALOGD("injectInputEvent - Timed out waiting for injection result " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3715 | "to become available."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3716 | #endif | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3717 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3718 | break; | 
|  | 3719 | } | 
|  | 3720 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3721 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3722 | } | 
|  | 3723 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3724 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && | 
|  | 3725 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3726 | while (injectionState->pendingForegroundDispatches != 0) { | 
|  | 3727 | #if DEBUG_INJECTION | 
|  | 3728 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3729 | injectionState->pendingForegroundDispatches); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | #endif | 
|  | 3731 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 3732 | if (remainingTimeout <= 0) { | 
|  | 3733 | #if DEBUG_INJECTION | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3734 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
|  | 3735 | "dispatches to finish."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 | #endif | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3737 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3738 | break; | 
|  | 3739 | } | 
|  | 3740 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3741 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3742 | } | 
|  | 3743 | } | 
|  | 3744 | } | 
|  | 3745 |  | 
|  | 3746 | injectionState->release(); | 
|  | 3747 | } // release lock | 
|  | 3748 |  | 
|  | 3749 | #if DEBUG_INJECTION | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3750 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3751 | injectionResult, injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3752 | #endif | 
|  | 3753 |  | 
|  | 3754 | return injectionResult; | 
|  | 3755 | } | 
|  | 3756 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3757 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3758 | std::array<uint8_t, 32> calculatedHmac; | 
|  | 3759 | std::unique_ptr<VerifiedInputEvent> result; | 
|  | 3760 | switch (event.getType()) { | 
|  | 3761 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 3762 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 3763 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
|  | 3764 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3765 | calculatedHmac = sign(verifiedKeyEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3766 | break; | 
|  | 3767 | } | 
|  | 3768 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 3769 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 3770 | VerifiedMotionEvent verifiedMotionEvent = | 
|  | 3771 | verifiedMotionEventFromMotionEvent(motionEvent); | 
|  | 3772 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3773 | calculatedHmac = sign(verifiedMotionEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3774 | break; | 
|  | 3775 | } | 
|  | 3776 | default: { | 
|  | 3777 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
|  | 3778 | return nullptr; | 
|  | 3779 | } | 
|  | 3780 | } | 
|  | 3781 | if (calculatedHmac == INVALID_HMAC) { | 
|  | 3782 | return nullptr; | 
|  | 3783 | } | 
|  | 3784 | if (calculatedHmac != event.getHmac()) { | 
|  | 3785 | return nullptr; | 
|  | 3786 | } | 
|  | 3787 | return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 3788 | } | 
|  | 3789 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3790 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3791 | return injectorUid == 0 || | 
|  | 3792 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | } | 
|  | 3794 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3795 | void InputDispatcher::setInjectionResult(EventEntry& entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3796 | InputEventInjectionResult injectionResult) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3797 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | if (injectionState) { | 
|  | 3799 | #if DEBUG_INJECTION | 
|  | 3800 | ALOGD("Setting input event injection result to %d.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3801 | "injectorPid=%d, injectorUid=%d", | 
|  | 3802 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3803 | #endif | 
|  | 3804 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3805 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3806 | // Log the outcome since the injector did not wait for the injection result. | 
|  | 3807 | switch (injectionResult) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3808 | case InputEventInjectionResult::SUCCEEDED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3809 | ALOGV("Asynchronous input event injection succeeded."); | 
|  | 3810 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3811 | case InputEventInjectionResult::FAILED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3812 | ALOGW("Asynchronous input event injection failed."); | 
|  | 3813 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3814 | case InputEventInjectionResult::PERMISSION_DENIED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3815 | ALOGW("Asynchronous input event injection permission denied."); | 
|  | 3816 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3817 | case InputEventInjectionResult::TIMED_OUT: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3818 | ALOGW("Asynchronous input event injection timed out."); | 
|  | 3819 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3820 | case InputEventInjectionResult::PENDING: | 
|  | 3821 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); | 
|  | 3822 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3823 | } | 
|  | 3824 | } | 
|  | 3825 |  | 
|  | 3826 | injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3827 | mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 | } | 
|  | 3829 | } | 
|  | 3830 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3831 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 3832 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3833 | if (injectionState) { | 
|  | 3834 | injectionState->pendingForegroundDispatches += 1; | 
|  | 3835 | } | 
|  | 3836 | } | 
|  | 3837 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3838 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 3839 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3840 | if (injectionState) { | 
|  | 3841 | injectionState->pendingForegroundDispatches -= 1; | 
|  | 3842 |  | 
|  | 3843 | if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3844 | mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3845 | } | 
|  | 3846 | } | 
|  | 3847 | } | 
|  | 3848 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3849 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3850 | int32_t displayId) const { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3851 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; | 
|  | 3852 | auto it = mWindowHandlesByDisplay.find(displayId); | 
|  | 3853 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3854 | } | 
|  | 3855 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3856 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3857 | const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3858 | if (windowHandleToken == nullptr) { | 
|  | 3859 | return nullptr; | 
|  | 3860 | } | 
|  | 3861 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3862 | for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3863 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 3864 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 3865 | if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3866 | return windowHandle; | 
|  | 3867 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3868 | } | 
|  | 3869 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3870 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3871 | } | 
|  | 3872 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3873 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, | 
|  | 3874 | int displayId) const { | 
|  | 3875 | if (windowHandleToken == nullptr) { | 
|  | 3876 | return nullptr; | 
|  | 3877 | } | 
|  | 3878 |  | 
|  | 3879 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
|  | 3880 | if (windowHandle->getToken() == windowHandleToken) { | 
|  | 3881 | return windowHandle; | 
|  | 3882 | } | 
|  | 3883 | } | 
|  | 3884 | return nullptr; | 
|  | 3885 | } | 
|  | 3886 |  | 
|  | 3887 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
|  | 3888 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 3889 | return getWindowHandleLocked(focusedToken, displayId); | 
|  | 3890 | } | 
|  | 3891 |  | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3892 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { | 
|  | 3893 | for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3894 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3895 | for (const sp<InputWindowHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 3896 | if (handle->getId() == windowHandle->getId() && | 
|  | 3897 | handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 3898 | if (windowHandle->getInfo()->displayId != it.first) { | 
|  | 3899 | ALOGE("Found window %s in display %" PRId32 | 
|  | 3900 | ", but it should belong to display %" PRId32, | 
|  | 3901 | windowHandle->getName().c_str(), it.first, | 
|  | 3902 | windowHandle->getInfo()->displayId); | 
|  | 3903 | } | 
|  | 3904 | return true; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3905 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3906 | } | 
|  | 3907 | } | 
|  | 3908 | return false; | 
|  | 3909 | } | 
|  | 3910 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3911 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { | 
|  | 3912 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); | 
|  | 3913 | const bool noInputChannel = | 
|  | 3914 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 3915 | if (connection != nullptr && noInputChannel) { | 
|  | 3916 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", | 
|  | 3917 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); | 
|  | 3918 | return false; | 
|  | 3919 | } | 
|  | 3920 |  | 
|  | 3921 | if (connection == nullptr) { | 
|  | 3922 | if (!noInputChannel) { | 
|  | 3923 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); | 
|  | 3924 | } | 
|  | 3925 | return false; | 
|  | 3926 | } | 
|  | 3927 | if (!connection->responsive) { | 
|  | 3928 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); | 
|  | 3929 | return false; | 
|  | 3930 | } | 
|  | 3931 | return true; | 
|  | 3932 | } | 
|  | 3933 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3934 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( | 
|  | 3935 | const sp<IBinder>& token) const { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 3936 | size_t count = mInputChannelsByToken.count(token); | 
|  | 3937 | if (count == 0) { | 
|  | 3938 | return nullptr; | 
|  | 3939 | } | 
|  | 3940 | return mInputChannelsByToken.at(token); | 
|  | 3941 | } | 
|  | 3942 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3943 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
|  | 3944 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
|  | 3945 | if (inputWindowHandles.empty()) { | 
|  | 3946 | // Remove all handles on a display if there are no windows left. | 
|  | 3947 | mWindowHandlesByDisplay.erase(displayId); | 
|  | 3948 | return; | 
|  | 3949 | } | 
|  | 3950 |  | 
|  | 3951 | // Since we compare the pointer of input window handles across window updates, we need | 
|  | 3952 | // to make sure the handle object for the same window stays unchanged across updates. | 
|  | 3953 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3954 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3955 | for (const sp<InputWindowHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3956 | oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3957 | } | 
|  | 3958 |  | 
|  | 3959 | std::vector<sp<InputWindowHandle>> newHandles; | 
|  | 3960 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { | 
|  | 3961 | if (!handle->updateInfo()) { | 
|  | 3962 | // handle no longer valid | 
|  | 3963 | continue; | 
|  | 3964 | } | 
|  | 3965 |  | 
|  | 3966 | const InputWindowInfo* info = handle->getInfo(); | 
|  | 3967 | if ((getInputChannelLocked(handle->getToken()) == nullptr && | 
|  | 3968 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { | 
|  | 3969 | const bool noInputChannel = | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3970 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 3971 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || | 
|  | 3972 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3973 | if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 3974 | ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3975 | handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 3976 | continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3977 | } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3978 | } | 
|  | 3979 |  | 
|  | 3980 | if (info->displayId != displayId) { | 
|  | 3981 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
|  | 3982 | handle->getName().c_str(), displayId, info->displayId); | 
|  | 3983 | continue; | 
|  | 3984 | } | 
|  | 3985 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 3986 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
|  | 3987 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3988 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 3989 | oldHandle->updateFrom(handle); | 
|  | 3990 | newHandles.push_back(oldHandle); | 
|  | 3991 | } else { | 
|  | 3992 | newHandles.push_back(handle); | 
|  | 3993 | } | 
|  | 3994 | } | 
|  | 3995 |  | 
|  | 3996 | // Insert or replace | 
|  | 3997 | mWindowHandlesByDisplay[displayId] = newHandles; | 
|  | 3998 | } | 
|  | 3999 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4000 | void InputDispatcher::setInputWindows( | 
|  | 4001 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { | 
|  | 4002 | { // acquire lock | 
|  | 4003 | std::scoped_lock _l(mLock); | 
|  | 4004 | for (auto const& i : handlesPerDisplay) { | 
|  | 4005 | setInputWindowsLocked(i.second, i.first); | 
|  | 4006 | } | 
|  | 4007 | } | 
|  | 4008 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4009 | mLooper->wake(); | 
|  | 4010 | } | 
|  | 4011 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4012 | /** | 
|  | 4013 | * Called from InputManagerService, update window handle list by displayId that can receive input. | 
|  | 4014 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
|  | 4015 | * If set an empty list, remove all handles from the specific display. | 
|  | 4016 | * For focused handle, check if need to change and send a cancel event to previous one. | 
|  | 4017 | * For removed handle, check if need to send a cancel event if already in touch. | 
|  | 4018 | */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4019 | void InputDispatcher::setInputWindowsLocked( | 
|  | 4020 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4021 | if (DEBUG_FOCUS) { | 
|  | 4022 | std::string windowList; | 
|  | 4023 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { | 
|  | 4024 | windowList += iwh->getName() + " "; | 
|  | 4025 | } | 
|  | 4026 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
|  | 4027 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4028 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4029 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL | 
|  | 4030 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { | 
|  | 4031 | const bool noInputWindow = | 
|  | 4032 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4033 | if (noInputWindow && window->getToken() != nullptr) { | 
|  | 4034 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", | 
|  | 4035 | window->getName().c_str()); | 
|  | 4036 | window->releaseChannel(); | 
|  | 4037 | } | 
|  | 4038 | } | 
|  | 4039 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4040 | // Copy old handles for release if they are no longer present. | 
|  | 4041 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4042 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4043 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4044 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4045 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 4046 | if (mLastHoverWindowHandle && | 
|  | 4047 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == | 
|  | 4048 | windowHandles.end()) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4049 | mLastHoverWindowHandle = nullptr; | 
|  | 4050 | } | 
|  | 4051 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4052 | sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 4053 | if (focusedToken) { | 
|  | 4054 | FocusResult result = checkTokenFocusableLocked(focusedToken, displayId); | 
|  | 4055 | if (result != FocusResult::OK) { | 
|  | 4056 | onFocusChangedLocked(focusedToken, nullptr, displayId, typeToString(result)); | 
|  | 4057 | } | 
|  | 4058 | } | 
|  | 4059 |  | 
|  | 4060 | std::optional<FocusRequest> focusRequest = | 
|  | 4061 | getOptionalValueByKey(mPendingFocusRequests, displayId); | 
|  | 4062 | if (focusRequest) { | 
|  | 4063 | // If the window from the pending request is now visible, provide it focus. | 
|  | 4064 | FocusResult result = handleFocusRequestLocked(*focusRequest); | 
|  | 4065 | if (result != FocusResult::NOT_VISIBLE) { | 
|  | 4066 | // Drop the request if we were able to change the focus or we cannot change | 
|  | 4067 | // it for another reason. | 
|  | 4068 | mPendingFocusRequests.erase(displayId); | 
|  | 4069 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4070 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4072 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4073 | mTouchStatesByDisplay.find(displayId); | 
|  | 4074 | if (stateIt != mTouchStatesByDisplay.end()) { | 
|  | 4075 | TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4076 | for (size_t i = 0; i < state.windows.size();) { | 
|  | 4077 | TouchedWindow& touchedWindow = state.windows[i]; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4078 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4079 | if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4080 | ALOGD("Touched window was removed: %s in display %" PRId32, | 
|  | 4081 | touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4082 | } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4083 | std::shared_ptr<InputChannel> touchedInputChannel = | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4084 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
|  | 4085 | if (touchedInputChannel != nullptr) { | 
|  | 4086 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4087 | "touched window was removed"); | 
|  | 4088 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4090 | state.windows.erase(state.windows.begin() + i); | 
|  | 4091 | } else { | 
|  | 4092 | ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | } | 
|  | 4094 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4095 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4096 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4097 | // Release information for windows that are no longer present. | 
|  | 4098 | // This ensures that unused input channels are released promptly. | 
|  | 4099 | // Otherwise, they might stick around until the window handle is destroyed | 
|  | 4100 | // which might not happen until the next GC. | 
|  | 4101 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4102 | if (!hasWindowHandleLocked(oldWindowHandle)) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4103 | if (DEBUG_FOCUS) { | 
|  | 4104 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4105 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4106 | oldWindowHandle->releaseChannel(); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4107 | } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4108 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4109 | } | 
|  | 4110 |  | 
|  | 4111 | void InputDispatcher::setFocusedApplication( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4112 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4113 | if (DEBUG_FOCUS) { | 
|  | 4114 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
|  | 4115 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
|  | 4116 | } | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4117 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4118 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4120 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4121 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4122 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4123 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { | 
|  | 4124 | return; // This application is already focused. No need to wake up or change anything. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4125 | } | 
|  | 4126 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4127 | // Set the new application handle. | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4128 | if (inputApplicationHandle != nullptr) { | 
|  | 4129 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
|  | 4130 | } else { | 
|  | 4131 | mFocusedApplicationHandlesByDisplay.erase(displayId); | 
|  | 4132 | } | 
|  | 4133 |  | 
|  | 4134 | // No matter what the old focused application was, stop waiting on it because it is | 
|  | 4135 | // no longer focused. | 
|  | 4136 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4137 | } // release lock | 
|  | 4138 |  | 
|  | 4139 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4140 | mLooper->wake(); | 
|  | 4141 | } | 
|  | 4142 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4143 | /** | 
|  | 4144 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
|  | 4145 | * the display not specified. | 
|  | 4146 | * | 
|  | 4147 | * We track any unreleased events for each window. If a window loses the ability to receive the | 
|  | 4148 | * released event, we will send a cancel event to it. So when the focused display is changed, we | 
|  | 4149 | * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
|  | 4150 | * display. The display-specified events won't be affected. | 
|  | 4151 | */ | 
|  | 4152 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4153 | if (DEBUG_FOCUS) { | 
|  | 4154 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
|  | 4155 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4156 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4157 | std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4158 |  | 
|  | 4159 | if (mFocusedDisplayId != displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4160 | sp<IBinder> oldFocusedWindowToken = | 
|  | 4161 | getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
|  | 4162 | if (oldFocusedWindowToken != nullptr) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4163 | std::shared_ptr<InputChannel> inputChannel = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4164 | getInputChannelLocked(oldFocusedWindowToken); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4165 | if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4166 | CancelationOptions | 
|  | 4167 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 4168 | "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4169 | options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4170 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 4171 | } | 
|  | 4172 | } | 
|  | 4173 | mFocusedDisplayId = displayId; | 
|  | 4174 |  | 
| Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4175 | // Find new focused window and validate | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4176 | sp<IBinder> newFocusedWindowToken = | 
|  | 4177 | getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 4178 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4179 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4180 | if (newFocusedWindowToken == nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4181 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4182 | if (!mFocusedWindowTokenByDisplay.empty()) { | 
|  | 4183 | ALOGE("But another display has a focused window\n%s", | 
|  | 4184 | dumpFocusedWindowsLocked().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4185 | } | 
|  | 4186 | } | 
|  | 4187 | } | 
|  | 4188 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4189 | if (DEBUG_FOCUS) { | 
|  | 4190 | logDispatchStateLocked(); | 
|  | 4191 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4192 | } // release lock | 
|  | 4193 |  | 
|  | 4194 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4195 | mLooper->wake(); | 
|  | 4196 | } | 
|  | 4197 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4198 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4199 | if (DEBUG_FOCUS) { | 
|  | 4200 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
|  | 4201 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 |  | 
|  | 4203 | bool changed; | 
|  | 4204 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4205 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4206 |  | 
|  | 4207 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
|  | 4208 | if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4209 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4210 | } | 
|  | 4211 |  | 
|  | 4212 | if (mDispatchEnabled && !enabled) { | 
|  | 4213 | resetAndDropEverythingLocked("dispatcher is being disabled"); | 
|  | 4214 | } | 
|  | 4215 |  | 
|  | 4216 | mDispatchEnabled = enabled; | 
|  | 4217 | mDispatchFrozen = frozen; | 
|  | 4218 | changed = true; | 
|  | 4219 | } else { | 
|  | 4220 | changed = false; | 
|  | 4221 | } | 
|  | 4222 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4223 | if (DEBUG_FOCUS) { | 
|  | 4224 | logDispatchStateLocked(); | 
|  | 4225 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4226 | } // release lock | 
|  | 4227 |  | 
|  | 4228 | if (changed) { | 
|  | 4229 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4230 | mLooper->wake(); | 
|  | 4231 | } | 
|  | 4232 | } | 
|  | 4233 |  | 
|  | 4234 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4235 | if (DEBUG_FOCUS) { | 
|  | 4236 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
|  | 4237 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 |  | 
|  | 4239 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4240 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4241 |  | 
|  | 4242 | if (mInputFilterEnabled == enabled) { | 
|  | 4243 | return; | 
|  | 4244 | } | 
|  | 4245 |  | 
|  | 4246 | mInputFilterEnabled = enabled; | 
|  | 4247 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
|  | 4248 | } // release lock | 
|  | 4249 |  | 
|  | 4250 | // Wake up poll loop since there might be work to do to drop everything. | 
|  | 4251 | mLooper->wake(); | 
|  | 4252 | } | 
|  | 4253 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4254 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
|  | 4255 | std::scoped_lock lock(mLock); | 
|  | 4256 | mInTouchMode = inTouchMode; | 
|  | 4257 | } | 
|  | 4258 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4259 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { | 
|  | 4260 | if (opacity < 0 || opacity > 1) { | 
|  | 4261 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); | 
|  | 4262 | return; | 
|  | 4263 | } | 
|  | 4264 |  | 
|  | 4265 | std::scoped_lock lock(mLock); | 
|  | 4266 | mMaximumObscuringOpacityForTouch = opacity; | 
|  | 4267 | } | 
|  | 4268 |  | 
|  | 4269 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { | 
|  | 4270 | std::scoped_lock lock(mLock); | 
|  | 4271 | mBlockUntrustedTouchesMode = mode; | 
|  | 4272 | } | 
|  | 4273 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4274 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { | 
|  | 4275 | if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4276 | if (DEBUG_FOCUS) { | 
|  | 4277 | ALOGD("Trivial transfer to same window."); | 
|  | 4278 | } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4279 | return true; | 
|  | 4280 | } | 
|  | 4281 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4283 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4284 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4285 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
|  | 4286 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4287 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4288 | ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4289 | return false; | 
|  | 4290 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4291 | if (DEBUG_FOCUS) { | 
|  | 4292 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
|  | 4293 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
|  | 4294 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4296 | if (DEBUG_FOCUS) { | 
|  | 4297 | ALOGD("Cannot transfer focus because windows are on different displays."); | 
|  | 4298 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4299 | return false; | 
|  | 4300 | } | 
|  | 4301 |  | 
|  | 4302 | bool found = false; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4303 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4304 | TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4305 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4306 | const TouchedWindow& touchedWindow = state.windows[i]; | 
|  | 4307 | if (touchedWindow.windowHandle == fromWindowHandle) { | 
|  | 4308 | int32_t oldTargetFlags = touchedWindow.targetFlags; | 
|  | 4309 | BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4311 | state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4313 | int32_t newTargetFlags = oldTargetFlags & | 
|  | 4314 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
|  | 4315 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4316 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4318 | found = true; | 
|  | 4319 | goto Found; | 
|  | 4320 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | } | 
|  | 4322 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4323 | Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4324 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4325 | if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4326 | if (DEBUG_FOCUS) { | 
|  | 4327 | ALOGD("Focus transfer failed because from window did not have focus."); | 
|  | 4328 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | return false; | 
|  | 4330 | } | 
|  | 4331 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4332 | sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
|  | 4333 | sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4334 | if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4335 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4336 | CancelationOptions | 
|  | 4337 | options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4338 | "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4339 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4340 | synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4341 | } | 
|  | 4342 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4343 | if (DEBUG_FOCUS) { | 
|  | 4344 | logDispatchStateLocked(); | 
|  | 4345 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | } // release lock | 
|  | 4347 |  | 
|  | 4348 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4349 | mLooper->wake(); | 
|  | 4350 | return true; | 
|  | 4351 | } | 
|  | 4352 |  | 
|  | 4353 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4354 | if (DEBUG_FOCUS) { | 
|  | 4355 | ALOGD("Resetting and dropping all events (%s).", reason); | 
|  | 4356 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 |  | 
|  | 4358 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
|  | 4359 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 4360 |  | 
|  | 4361 | resetKeyRepeatLocked(); | 
|  | 4362 | releasePendingEventLocked(); | 
|  | 4363 | drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4364 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4366 | mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4367 | mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4369 | mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4370 | } | 
|  | 4371 |  | 
|  | 4372 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4373 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | dumpDispatchStateLocked(dump); | 
|  | 4375 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4376 | std::istringstream stream(dump); | 
|  | 4377 | std::string line; | 
|  | 4378 |  | 
|  | 4379 | while (std::getline(stream, line, '\n')) { | 
|  | 4380 | ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | } | 
|  | 4382 | } | 
|  | 4383 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4384 | std::string InputDispatcher::dumpFocusedWindowsLocked() { | 
|  | 4385 | if (mFocusedWindowTokenByDisplay.empty()) { | 
|  | 4386 | return INDENT "FocusedWindows: <none>\n"; | 
|  | 4387 | } | 
|  | 4388 |  | 
|  | 4389 | std::string dump; | 
|  | 4390 | dump += INDENT "FocusedWindows:\n"; | 
|  | 4391 | for (auto& it : mFocusedWindowTokenByDisplay) { | 
|  | 4392 | const int32_t displayId = it.first; | 
|  | 4393 | const sp<InputWindowHandle> windowHandle = getFocusedWindowHandleLocked(displayId); | 
|  | 4394 | if (windowHandle) { | 
|  | 4395 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, | 
|  | 4396 | windowHandle->getName().c_str()); | 
|  | 4397 | } else { | 
|  | 4398 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
|  | 4399 | " has focused token without a window'\n", | 
|  | 4400 | displayId); | 
|  | 4401 | } | 
|  | 4402 | } | 
|  | 4403 | return dump; | 
|  | 4404 | } | 
|  | 4405 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 4406 | std::string InputDispatcher::dumpPendingFocusRequestsLocked() { | 
|  | 4407 | if (mPendingFocusRequests.empty()) { | 
|  | 4408 | return INDENT "mPendingFocusRequests: <none>\n"; | 
|  | 4409 | } | 
|  | 4410 |  | 
|  | 4411 | std::string dump; | 
|  | 4412 | dump += INDENT "mPendingFocusRequests:\n"; | 
|  | 4413 | for (const auto& [displayId, focusRequest] : mPendingFocusRequests) { | 
|  | 4414 | // Rather than printing raw values for focusRequest.token and focusRequest.focusedToken, | 
|  | 4415 | // try to resolve them to actual windows. | 
|  | 4416 | std::string windowName = getConnectionNameLocked(focusRequest.token); | 
|  | 4417 | std::string focusedWindowName = getConnectionNameLocked(focusRequest.focusedToken); | 
|  | 4418 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", token->%s, focusedToken->%s\n", | 
|  | 4419 | displayId, windowName.c_str(), focusedWindowName.c_str()); | 
|  | 4420 | } | 
|  | 4421 | return dump; | 
|  | 4422 | } | 
|  | 4423 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4424 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4425 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
|  | 4426 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
|  | 4427 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4428 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4429 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4430 | if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
|  | 4431 | dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
|  | 4432 | for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
|  | 4433 | const int32_t displayId = it.first; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4434 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4435 | const std::chrono::duration timeout = | 
|  | 4436 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4437 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4438 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4439 | displayId, applicationHandle->getName().c_str(), millis(timeout)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4440 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4441 | } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4442 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4443 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4444 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4445 | dump += dumpFocusedWindowsLocked(); | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 4446 | dump += dumpPendingFocusRequestsLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4447 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4448 | if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4449 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4450 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 4451 | const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4452 | 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] | 4453 | state.displayId, toString(state.down), toString(state.split), | 
|  | 4454 | state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4455 | if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4456 | dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4457 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 4458 | const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4459 | dump += StringPrintf(INDENT4 | 
|  | 4460 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
|  | 4461 | i, touchedWindow.windowHandle->getName().c_str(), | 
|  | 4462 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4463 | } | 
|  | 4464 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4465 | dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4466 | } | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4467 | if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4468 | dump += INDENT3 "Portal windows:\n"; | 
|  | 4469 | for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4470 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4471 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, | 
|  | 4472 | portalWindowHandle->getName().c_str()); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4473 | } | 
|  | 4474 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4475 | } | 
|  | 4476 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4477 | dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | } | 
|  | 4479 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4480 | if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4481 | for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4482 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4483 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4484 | if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4485 | dump += INDENT2 "Windows:\n"; | 
|  | 4486 | for (size_t i = 0; i < windowHandles.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4487 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4488 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4489 |  | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4490 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4491 | "portalToDisplayId=%d, paused=%s, focusable=%s, " | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4492 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4493 | "flags=%s, type=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4494 | "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4495 | "applicationInfo=%s, " | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4496 | "touchableRegion=", | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4497 | i, windowInfo->name.c_str(), windowInfo->id, | 
|  | 4498 | windowInfo->displayId, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4499 | toString(windowInfo->paused), | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4500 | toString(windowInfo->focusable), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4501 | toString(windowInfo->hasWallpaper), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4502 | toString(windowInfo->visible), windowInfo->alpha, | 
| Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4503 | windowInfo->flags.string().c_str(), | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4504 | static_cast<int32_t>(windowInfo->type), | 
|  | 4505 | windowInfo->frameLeft, windowInfo->frameTop, | 
|  | 4506 | windowInfo->frameRight, windowInfo->frameBottom, | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4507 | windowInfo->globalScaleFactor, | 
|  | 4508 | windowInfo->applicationInfo.name.c_str()); | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4509 | dump += dumpRegion(windowInfo->touchableRegion); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4510 | dump += StringPrintf(", inputFeatures=%s", | 
|  | 4511 | windowInfo->inputFeatures.string().c_str()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4512 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4513 | "ms, trustedOverlay=%s, hasToken=%s\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4514 | windowInfo->ownerPid, windowInfo->ownerUid, | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4515 | millis(windowInfo->dispatchingTimeout), | 
|  | 4516 | toString(windowInfo->trustedOverlay), | 
|  | 4517 | toString(windowInfo->token != nullptr)); | 
| chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4518 | windowInfo->transform.dump(dump, "transform", INDENT4); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4519 | } | 
|  | 4520 | } else { | 
|  | 4521 | dump += INDENT2 "Windows: <none>\n"; | 
|  | 4522 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4523 | } | 
|  | 4524 | } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4525 | dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4526 | } | 
|  | 4527 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4528 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4529 | for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4530 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4531 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
|  | 4532 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4533 | } | 
|  | 4534 | for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4535 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4536 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
|  | 4537 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4538 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4539 | } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4540 | dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4541 | } | 
|  | 4542 |  | 
|  | 4543 | nsecs_t currentTime = now(); | 
|  | 4544 |  | 
|  | 4545 | // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4546 | if (!mRecentQueue.empty()) { | 
|  | 4547 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4548 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4549 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4550 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4551 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4552 | } | 
|  | 4553 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4554 | dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4555 | } | 
|  | 4556 |  | 
|  | 4557 | // Dump event currently being dispatched. | 
|  | 4558 | if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4559 | dump += INDENT "PendingEvent:\n"; | 
|  | 4560 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4561 | dump += mPendingEvent->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4562 | dump += StringPrintf(", age=%" PRId64 "ms\n", | 
|  | 4563 | ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4565 | dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4566 | } | 
|  | 4567 |  | 
|  | 4568 | // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4569 | if (!mInboundQueue.empty()) { | 
|  | 4570 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4571 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4572 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4573 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4574 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4575 | } | 
|  | 4576 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4577 | dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4578 | } | 
|  | 4579 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4580 | if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4581 | dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4582 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
|  | 4583 | const KeyReplacement& replacement = pair.first; | 
|  | 4584 | int32_t newKeyCode = pair.second; | 
|  | 4585 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4586 | replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4587 | } | 
|  | 4588 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4589 | dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4590 | } | 
|  | 4591 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4592 | if (!mConnectionsByFd.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4593 | dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4594 | for (const auto& pair : mConnectionsByFd) { | 
|  | 4595 | const sp<Connection>& connection = pair.second; | 
|  | 4596 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4597 | "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4598 | pair.first, connection->getInputChannelName().c_str(), | 
|  | 4599 | connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4600 | toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4602 | if (!connection->outboundQueue.empty()) { | 
|  | 4603 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
|  | 4604 | connection->outboundQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4605 | dump += dumpQueue(connection->outboundQueue, currentTime); | 
|  | 4606 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4607 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4608 | dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4609 | } | 
|  | 4610 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4611 | if (!connection->waitQueue.empty()) { | 
|  | 4612 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
|  | 4613 | connection->waitQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4614 | dump += dumpQueue(connection->waitQueue, currentTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4615 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4616 | dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | } | 
|  | 4618 | } | 
|  | 4619 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4620 | dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4621 | } | 
|  | 4622 |  | 
|  | 4623 | if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4624 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
|  | 4625 | ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4626 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4627 | dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4628 | } | 
|  | 4629 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4630 | dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4631 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
|  | 4632 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
|  | 4633 | ns2ms(mConfig.keyRepeatTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4634 | } | 
|  | 4635 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4636 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
|  | 4637 | const size_t numMonitors = monitors.size(); | 
|  | 4638 | for (size_t i = 0; i < numMonitors; i++) { | 
|  | 4639 | const Monitor& monitor = monitors[i]; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4640 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4641 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
|  | 4642 | dump += "\n"; | 
|  | 4643 | } | 
|  | 4644 | } | 
|  | 4645 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4646 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel( | 
|  | 4647 | const std::string& name) { | 
|  | 4648 | #if DEBUG_CHANNEL_CREATION | 
|  | 4649 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4650 | #endif | 
|  | 4651 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4652 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 4653 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 4654 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 4655 |  | 
|  | 4656 | if (result) { | 
|  | 4657 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 4658 | } | 
|  | 4659 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4660 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4661 | std::scoped_lock _l(mLock); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4662 | sp<Connection> connection = new Connection(serverChannel, false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4663 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4664 | int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4665 | mConnectionsByFd[fd] = connection; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4666 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4667 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4668 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
|  | 4669 | } // release lock | 
|  | 4670 |  | 
|  | 4671 | // Wake the looper because some connections have changed. | 
|  | 4672 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4673 | return clientChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4674 | } | 
|  | 4675 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4676 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor( | 
|  | 4677 | int32_t displayId, bool isGestureMonitor, const std::string& name) { | 
|  | 4678 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 4679 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 4680 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 4681 | if (result) { | 
|  | 4682 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 4683 | } | 
|  | 4684 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4685 | { // acquire lock | 
|  | 4686 | std::scoped_lock _l(mLock); | 
|  | 4687 |  | 
|  | 4688 | if (displayId < 0) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4689 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name | 
|  | 4690 | << " without a specified display."; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4691 | } | 
|  | 4692 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4693 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4694 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4695 | const int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4696 | mConnectionsByFd[fd] = connection; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4697 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4698 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4699 | auto& monitorsByDisplay = | 
|  | 4700 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4701 | monitorsByDisplay[displayId].emplace_back(serverChannel); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4702 |  | 
|  | 4703 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4704 | } | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4705 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4706 | // Wake the looper because some connections have changed. | 
|  | 4707 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4708 | return clientChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4709 | } | 
|  | 4710 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4711 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4712 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4713 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4714 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4715 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4716 | if (status) { | 
|  | 4717 | return status; | 
|  | 4718 | } | 
|  | 4719 | } // release lock | 
|  | 4720 |  | 
|  | 4721 | // Wake the poll loop because removing the connection may have changed the current | 
|  | 4722 | // synchronization state. | 
|  | 4723 | mLooper->wake(); | 
|  | 4724 | return OK; | 
|  | 4725 | } | 
|  | 4726 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4727 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, | 
|  | 4728 | bool notify) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4729 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4730 | if (connection == nullptr) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4731 | ALOGW("Attempted to unregister already unregistered input channel"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4732 | return BAD_VALUE; | 
|  | 4733 | } | 
|  | 4734 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4735 | removeConnectionLocked(connection); | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4736 | mInputChannelsByToken.erase(connectionToken); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4737 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | if (connection->monitor) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4739 | removeMonitorChannelLocked(connectionToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4740 | } | 
|  | 4741 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4742 | mLooper->removeFd(connection->inputChannel->getFd()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4743 |  | 
|  | 4744 | nsecs_t currentTime = now(); | 
|  | 4745 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
|  | 4746 |  | 
|  | 4747 | connection->status = Connection::STATUS_ZOMBIE; | 
|  | 4748 | return OK; | 
|  | 4749 | } | 
|  | 4750 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4751 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { | 
|  | 4752 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); | 
|  | 4753 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4754 | } | 
|  | 4755 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4756 | void InputDispatcher::removeMonitorChannelLocked( | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4757 | const sp<IBinder>& connectionToken, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4758 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4759 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4760 | std::vector<Monitor>& monitors = it->second; | 
|  | 4761 | const size_t numMonitors = monitors.size(); | 
|  | 4762 | for (size_t i = 0; i < numMonitors; i++) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 4763 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4764 | monitors.erase(monitors.begin() + i); | 
|  | 4765 | break; | 
|  | 4766 | } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4767 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4768 | if (monitors.empty()) { | 
|  | 4769 | it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4770 | } else { | 
|  | 4771 | ++it; | 
|  | 4772 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | } | 
|  | 4774 | } | 
|  | 4775 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4776 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
|  | 4777 | { // acquire lock | 
|  | 4778 | std::scoped_lock _l(mLock); | 
|  | 4779 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
|  | 4780 |  | 
|  | 4781 | if (!foundDisplayId) { | 
|  | 4782 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
|  | 4783 | return BAD_VALUE; | 
|  | 4784 | } | 
|  | 4785 | int32_t displayId = foundDisplayId.value(); | 
|  | 4786 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4787 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4788 | mTouchStatesByDisplay.find(displayId); | 
|  | 4789 | if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4790 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
|  | 4791 | return BAD_VALUE; | 
|  | 4792 | } | 
|  | 4793 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4794 | TouchState& state = stateIt->second; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4795 | std::optional<int32_t> foundDeviceId; | 
|  | 4796 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4797 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4798 | foundDeviceId = state.deviceId; | 
|  | 4799 | } | 
|  | 4800 | } | 
|  | 4801 | if (!foundDeviceId || !state.down) { | 
|  | 4802 | 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] | 4803 | " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4804 | return BAD_VALUE; | 
|  | 4805 | } | 
|  | 4806 | int32_t deviceId = foundDeviceId.value(); | 
|  | 4807 |  | 
|  | 4808 | // Send cancel events to all the input channels we're stealing from. | 
|  | 4809 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4810 | "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4811 | options.deviceId = deviceId; | 
|  | 4812 | options.displayId = displayId; | 
|  | 4813 | for (const TouchedWindow& window : state.windows) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4814 | std::shared_ptr<InputChannel> channel = | 
|  | 4815 | getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4816 | if (channel != nullptr) { | 
|  | 4817 | synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
|  | 4818 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4819 | } | 
|  | 4820 | // Then clear the current touch state so we stop dispatching to them as well. | 
|  | 4821 | state.filterNonMonitors(); | 
|  | 4822 | } | 
|  | 4823 | return OK; | 
|  | 4824 | } | 
|  | 4825 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4826 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
|  | 4827 | const sp<IBinder>& token) { | 
|  | 4828 | for (const auto& it : mGestureMonitorsByDisplay) { | 
|  | 4829 | const std::vector<Monitor>& monitors = it.second; | 
|  | 4830 | for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4831 | if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4832 | return it.first; | 
|  | 4833 | } | 
|  | 4834 | } | 
|  | 4835 | } | 
|  | 4836 | return std::nullopt; | 
|  | 4837 | } | 
|  | 4838 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4839 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4840 | if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4841 | return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4842 | } | 
|  | 4843 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4844 | for (const auto& pair : mConnectionsByFd) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4845 | const sp<Connection>& connection = pair.second; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4846 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4847 | return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 | } | 
|  | 4849 | } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 4850 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4851 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4852 | } | 
|  | 4853 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 4854 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { | 
|  | 4855 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 4856 | if (connection == nullptr) { | 
|  | 4857 | return "<nullptr>"; | 
|  | 4858 | } | 
|  | 4859 | return connection->getInputChannelName(); | 
|  | 4860 | } | 
|  | 4861 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4862 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4863 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4864 | removeByValue(mConnectionsByFd, connection); | 
|  | 4865 | } | 
|  | 4866 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4867 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, | 
|  | 4868 | const sp<Connection>& connection, uint32_t seq, | 
|  | 4869 | bool handled) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4870 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4871 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4872 | commandEntry->connection = connection; | 
|  | 4873 | commandEntry->eventTime = currentTime; | 
|  | 4874 | commandEntry->seq = seq; | 
|  | 4875 | commandEntry->handled = handled; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4876 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4877 | } | 
|  | 4878 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4879 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, | 
|  | 4880 | const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4881 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4882 | connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4883 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4884 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4885 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4886 | commandEntry->connection = connection; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4887 | postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4888 | } | 
|  | 4889 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4890 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, | 
|  | 4891 | const sp<IBinder>& newToken) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4892 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4893 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 4894 | commandEntry->oldToken = oldToken; | 
|  | 4895 | commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 4896 | postCommandLocked(std::move(commandEntry)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4897 | } | 
|  | 4898 |  | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4899 | void InputDispatcher::onAnrLocked(const Connection& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4900 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
|  | 4901 | // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4902 | if (connection.waitQueue.empty()) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4903 | ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4904 | connection.inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4905 | return; | 
|  | 4906 | } | 
|  | 4907 | /** | 
|  | 4908 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
|  | 4909 | * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
|  | 4910 | * has changed. This could cause newer entries to time out before the already dispatched | 
|  | 4911 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
|  | 4912 | * processes the events linearly. So providing information about the oldest entry seems to be | 
|  | 4913 | * most useful. | 
|  | 4914 | */ | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4915 | DispatchEntry* oldestEntry = *connection.waitQueue.begin(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4916 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
|  | 4917 | std::string reason = | 
|  | 4918 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 4919 | connection.inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4920 | ns2ms(currentWait), | 
|  | 4921 | oldestEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 4922 | sp<IBinder> connectionToken = connection.inputChannel->getConnectionToken(); | 
|  | 4923 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4924 |  | 
|  | 4925 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 4926 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
|  | 4927 | commandEntry->inputApplicationHandle = nullptr; | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 4928 | commandEntry->connectionToken = connectionToken; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4929 | commandEntry->reason = std::move(reason); | 
|  | 4930 | postCommandLocked(std::move(commandEntry)); | 
|  | 4931 | } | 
|  | 4932 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4933 | void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4934 | std::string reason = android::base::StringPrintf("%s does not have a focused window", | 
|  | 4935 | application->getName().c_str()); | 
|  | 4936 |  | 
|  | 4937 | updateLastAnrStateLocked(application, reason); | 
|  | 4938 |  | 
|  | 4939 | std::unique_ptr<CommandEntry> commandEntry = | 
|  | 4940 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyAnrLockedInterruptible); | 
|  | 4941 | commandEntry->inputApplicationHandle = application; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4942 | commandEntry->reason = std::move(reason); | 
|  | 4943 | postCommandLocked(std::move(commandEntry)); | 
|  | 4944 | } | 
|  | 4945 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 4946 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { | 
|  | 4947 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
|  | 4948 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); | 
|  | 4949 | commandEntry->obscuringPackage = obscuringPackage; | 
|  | 4950 | postCommandLocked(std::move(commandEntry)); | 
|  | 4951 | } | 
|  | 4952 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4953 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, | 
|  | 4954 | const std::string& reason) { | 
|  | 4955 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
|  | 4956 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 4957 | } | 
|  | 4958 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4959 | void InputDispatcher::updateLastAnrStateLocked( | 
|  | 4960 | const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4961 | const std::string windowLabel = getApplicationWindowLabel(application, nullptr); | 
|  | 4962 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 4963 | } | 
|  | 4964 |  | 
|  | 4965 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
|  | 4966 | const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4967 | // 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] | 4968 | time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4969 | struct tm tm; | 
|  | 4970 | localtime_r(&t, &tm); | 
|  | 4971 | char timestr[64]; | 
|  | 4972 | strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4973 | mLastAnrState.clear(); | 
|  | 4974 | mLastAnrState += INDENT "ANR:\n"; | 
|  | 4975 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4976 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
|  | 4977 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 4978 | dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4979 | } | 
|  | 4980 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4981 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4982 | mLock.unlock(); | 
|  | 4983 |  | 
|  | 4984 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); | 
|  | 4985 |  | 
|  | 4986 | mLock.lock(); | 
|  | 4987 | } | 
|  | 4988 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4989 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4990 | sp<Connection> connection = commandEntry->connection; | 
|  | 4991 |  | 
|  | 4992 | if (connection->status != Connection::STATUS_ZOMBIE) { | 
|  | 4993 | mLock.unlock(); | 
|  | 4994 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 4995 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4996 |  | 
|  | 4997 | mLock.lock(); | 
|  | 4998 | } | 
|  | 4999 | } | 
|  | 5000 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5001 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5002 | sp<IBinder> oldToken = commandEntry->oldToken; | 
|  | 5003 | sp<IBinder> newToken = commandEntry->newToken; | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5004 | mLock.unlock(); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5005 | mPolicy->notifyFocusChanged(oldToken, newToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5006 | mLock.lock(); | 
|  | 5007 | } | 
|  | 5008 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5009 | void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5010 | mLock.unlock(); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5011 | const sp<IBinder>& token = commandEntry->connectionToken; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 5012 | const std::chrono::nanoseconds timeoutExtension = | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5013 | mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5014 |  | 
|  | 5015 | mLock.lock(); | 
|  | 5016 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 5017 | if (timeoutExtension > 0s) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5018 | extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension); | 
|  | 5019 | } else { | 
|  | 5020 | // stop waking up for events in this connection, it is already not responding | 
|  | 5021 | sp<Connection> connection = getConnectionLocked(token); | 
|  | 5022 | if (connection == nullptr) { | 
|  | 5023 | return; | 
|  | 5024 | } | 
|  | 5025 | cancelEventsForAnrLocked(connection); | 
|  | 5026 | } | 
|  | 5027 | } | 
|  | 5028 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5029 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5030 | mLock.unlock(); | 
|  | 5031 |  | 
|  | 5032 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); | 
|  | 5033 |  | 
|  | 5034 | mLock.lock(); | 
|  | 5035 | } | 
|  | 5036 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5037 | void InputDispatcher::extendAnrTimeoutsLocked( | 
|  | 5038 | const std::shared_ptr<InputApplicationHandle>& application, | 
|  | 5039 | const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) { | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 5040 | if (connectionToken == nullptr && application != nullptr) { | 
|  | 5041 | // The ANR happened because there's no focused window | 
|  | 5042 | mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count(); | 
|  | 5043 | mAwaitedFocusedApplication = application; | 
|  | 5044 | } | 
|  | 5045 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5046 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 5047 | if (connection == nullptr) { | 
| Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 5048 | // It's possible that the connection already disappeared. No action necessary. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5049 | return; | 
|  | 5050 | } | 
|  | 5051 |  | 
|  | 5052 | ALOGI("Raised ANR, but the policy wants to keep waiting on %s for %" PRId64 "ms longer", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 5053 | connection->inputChannel->getName().c_str(), millis(timeoutExtension)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5054 |  | 
|  | 5055 | connection->responsive = true; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 5056 | const nsecs_t newTimeout = now() + timeoutExtension.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5057 | for (DispatchEntry* entry : connection->waitQueue) { | 
|  | 5058 | if (newTimeout >= entry->timeoutTime) { | 
|  | 5059 | // Already removed old entries when connection was marked unresponsive | 
|  | 5060 | entry->timeoutTime = newTimeout; | 
|  | 5061 | mAnrTracker.insert(entry->timeoutTime, connectionToken); | 
|  | 5062 | } | 
|  | 5063 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | } | 
|  | 5065 |  | 
|  | 5066 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( | 
|  | 5067 | CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5068 | KeyEntry& entry = *(commandEntry->keyEntry); | 
|  | 5069 | KeyEvent event = createKeyEvent(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5070 |  | 
|  | 5071 | mLock.unlock(); | 
|  | 5072 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5073 | android::base::Timer t; | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5074 | const sp<IBinder>& token = commandEntry->connectionToken; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5075 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5076 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 5077 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5078 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5079 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5080 |  | 
|  | 5081 | mLock.lock(); | 
|  | 5082 |  | 
|  | 5083 | if (delay < 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5084 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5085 | } else if (!delay) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5086 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5087 | } else { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5088 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
|  | 5089 | entry.interceptKeyWakeupTime = now() + delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5091 | } | 
|  | 5092 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5093 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5094 | mLock.unlock(); | 
|  | 5095 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); | 
|  | 5096 | mLock.lock(); | 
|  | 5097 | } | 
|  | 5098 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5099 | /** | 
|  | 5100 | * Connection is responsive if it has no events in the waitQueue that are older than the | 
|  | 5101 | * current time. | 
|  | 5102 | */ | 
|  | 5103 | static bool isConnectionResponsive(const Connection& connection) { | 
|  | 5104 | const nsecs_t currentTime = now(); | 
|  | 5105 | for (const DispatchEntry* entry : connection.waitQueue) { | 
|  | 5106 | if (entry->timeoutTime < currentTime) { | 
|  | 5107 | return false; | 
|  | 5108 | } | 
|  | 5109 | } | 
|  | 5110 | return true; | 
|  | 5111 | } | 
|  | 5112 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5113 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5114 | sp<Connection> connection = commandEntry->connection; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5115 | const nsecs_t finishTime = commandEntry->eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5116 | uint32_t seq = commandEntry->seq; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5117 | const bool handled = commandEntry->handled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5118 |  | 
|  | 5119 | // Handle post-event policy actions. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5120 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5121 | if (dispatchEntryIt == connection->waitQueue.end()) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5122 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5123 | } | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5124 | DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5125 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5126 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5127 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
|  | 5128 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5129 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5130 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5131 |  | 
|  | 5132 | bool restartEvent; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5133 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5134 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5135 | restartEvent = | 
|  | 5136 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5137 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5138 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5139 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, | 
|  | 5140 | handled); | 
|  | 5141 | } else { | 
|  | 5142 | restartEvent = false; | 
|  | 5143 | } | 
|  | 5144 |  | 
|  | 5145 | // Dequeue the event and start the next cycle. | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5146 | // Because the lock might have been released, it is possible that the | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5147 | // contents of the wait queue to have been drained, so we need to double-check | 
|  | 5148 | // a few things. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5149 | dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 5150 | if (dispatchEntryIt != connection->waitQueue.end()) { | 
|  | 5151 | dispatchEntry = *dispatchEntryIt; | 
|  | 5152 | connection->waitQueue.erase(dispatchEntryIt); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5153 | mAnrTracker.erase(dispatchEntry->timeoutTime, | 
|  | 5154 | connection->inputChannel->getConnectionToken()); | 
|  | 5155 | if (!connection->responsive) { | 
|  | 5156 | connection->responsive = isConnectionResponsive(*connection); | 
|  | 5157 | } | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5158 | traceWaitQueueLength(connection); | 
|  | 5159 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5160 | connection->outboundQueue.push_front(dispatchEntry); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5161 | traceOutboundQueueLength(connection); | 
|  | 5162 | } else { | 
|  | 5163 | releaseDispatchEntry(dispatchEntry); | 
|  | 5164 | } | 
|  | 5165 | } | 
|  | 5166 |  | 
|  | 5167 | // Start the next dispatch cycle for this connection. | 
|  | 5168 | startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | } | 
|  | 5170 |  | 
|  | 5171 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5172 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5173 | KeyEntry& keyEntry, bool handled) { | 
|  | 5174 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5175 | if (!handled) { | 
|  | 5176 | // Report the key as unhandled, since the fallback was not handled. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5177 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5178 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5179 | return false; | 
|  | 5180 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5181 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5182 | // Get the fallback key state. | 
|  | 5183 | // Clear it out after dispatching the UP. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5184 | int32_t originalKeyCode = keyEntry.keyCode; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5185 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5186 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5187 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5188 | } | 
|  | 5189 |  | 
|  | 5190 | if (handled || !dispatchEntry->hasForegroundTarget()) { | 
|  | 5191 | // If the application handles the original key for which we previously | 
|  | 5192 | // generated a fallback or if the window is not a foreground window, | 
|  | 5193 | // then cancel the associated fallback key, if any. | 
|  | 5194 | if (fallbackKeyCode != -1) { | 
|  | 5195 | // Dispatch the unhandled key to the policy with the cancel flag. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5196 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5197 | ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5198 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5199 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5201 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5202 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5203 |  | 
|  | 5204 | mLock.unlock(); | 
|  | 5205 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5206 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5207 | keyEntry.policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5208 |  | 
|  | 5209 | mLock.lock(); | 
|  | 5210 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5211 | // Cancel the fallback key. | 
|  | 5212 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5213 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5214 | "application handled the original non-fallback key " | 
|  | 5215 | "or is no longer a foreground target, " | 
|  | 5216 | "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 | options.keyCode = fallbackKeyCode; | 
|  | 5218 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5219 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5220 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5221 | } | 
|  | 5222 | } else { | 
|  | 5223 | // If the application did not handle a non-fallback key, first check | 
|  | 5224 | // that we are in a good state to perform unhandled key event processing | 
|  | 5225 | // Then ask the policy what to do with it. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5226 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5227 | if (fallbackKeyCode == -1 && !initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5228 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5229 | ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5230 | "since this is not an initial down.  " | 
|  | 5231 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5232 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5233 | #endif | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5234 | return false; | 
|  | 5235 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5236 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5237 | // Dispatch the unhandled key to the policy. | 
|  | 5238 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5239 | ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5240 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5241 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5242 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5243 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5244 |  | 
|  | 5245 | mLock.unlock(); | 
|  | 5246 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5247 | bool fallback = | 
|  | 5248 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5249 | &event, keyEntry.policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5250 |  | 
|  | 5251 | mLock.lock(); | 
|  | 5252 |  | 
|  | 5253 | if (connection->status != Connection::STATUS_NORMAL) { | 
|  | 5254 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5255 | return false; | 
|  | 5256 | } | 
|  | 5257 |  | 
|  | 5258 | // Latch the fallback keycode for this key on an initial down. | 
|  | 5259 | // The fallback keycode cannot change at any other point in the lifecycle. | 
|  | 5260 | if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5261 | if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5262 | fallbackKeyCode = event.getKeyCode(); | 
|  | 5263 | } else { | 
|  | 5264 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 5265 | } | 
|  | 5266 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
|  | 5267 | } | 
|  | 5268 |  | 
|  | 5269 | ALOG_ASSERT(fallbackKeyCode != -1); | 
|  | 5270 |  | 
|  | 5271 | // Cancel the fallback key if the policy decides not to send it anymore. | 
|  | 5272 | // We will continue to dispatch the key to the policy but we will no | 
|  | 5273 | // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5274 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
|  | 5275 | (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5276 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5277 | if (fallback) { | 
|  | 5278 | ALOGD("Unhandled key event: Policy requested to send key %d" | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5279 | "as a fallback for %d, but on the DOWN it had requested " | 
|  | 5280 | "to send %d instead.  Fallback canceled.", | 
|  | 5281 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5282 | } else { | 
|  | 5283 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5284 | "but on the DOWN it had requested to send %d.  " | 
|  | 5285 | "Fallback canceled.", | 
|  | 5286 | originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5287 | } | 
|  | 5288 | #endif | 
|  | 5289 |  | 
|  | 5290 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
|  | 5291 | "canceling fallback, policy no longer desires it"); | 
|  | 5292 | options.keyCode = fallbackKeyCode; | 
|  | 5293 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
|  | 5294 |  | 
|  | 5295 | fallback = false; | 
|  | 5296 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5297 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5298 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5299 | } | 
|  | 5300 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 |  | 
|  | 5302 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5303 | { | 
|  | 5304 | std::string msg; | 
|  | 5305 | const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
|  | 5306 | connection->inputState.getFallbackKeys(); | 
|  | 5307 | for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5308 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5309 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5310 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5311 | fallbackKeys.size(), msg.c_str()); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5312 | } | 
|  | 5313 | #endif | 
|  | 5314 |  | 
|  | 5315 | if (fallback) { | 
|  | 5316 | // Restart the dispatch cycle using the fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5317 | keyEntry.eventTime = event.getEventTime(); | 
|  | 5318 | keyEntry.deviceId = event.getDeviceId(); | 
|  | 5319 | keyEntry.source = event.getSource(); | 
|  | 5320 | keyEntry.displayId = event.getDisplayId(); | 
|  | 5321 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
|  | 5322 | keyEntry.keyCode = fallbackKeyCode; | 
|  | 5323 | keyEntry.scanCode = event.getScanCode(); | 
|  | 5324 | keyEntry.metaState = event.getMetaState(); | 
|  | 5325 | keyEntry.repeatCount = event.getRepeatCount(); | 
|  | 5326 | keyEntry.downTime = event.getDownTime(); | 
|  | 5327 | keyEntry.syntheticRepeat = false; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5328 |  | 
|  | 5329 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5330 | ALOGD("Unhandled key event: Dispatching fallback key.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5331 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5332 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5333 | #endif | 
|  | 5334 | return true; // restart the event | 
|  | 5335 | } else { | 
|  | 5336 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
|  | 5337 | ALOGD("Unhandled key event: No fallback key."); | 
|  | 5338 | #endif | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5339 |  | 
|  | 5340 | // Report the key as unhandled, since there is no fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5341 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5342 | } | 
|  | 5343 | } | 
|  | 5344 | return false; | 
|  | 5345 | } | 
|  | 5346 |  | 
|  | 5347 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5348 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5349 | MotionEntry& motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5350 | return false; | 
|  | 5351 | } | 
|  | 5352 |  | 
|  | 5353 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { | 
|  | 5354 | mLock.unlock(); | 
|  | 5355 |  | 
|  | 5356 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); | 
|  | 5357 |  | 
|  | 5358 | mLock.lock(); | 
|  | 5359 | } | 
|  | 5360 |  | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5361 | KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { | 
|  | 5362 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5363 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 5364 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 5365 | entry.repeatCount, entry.downTime, entry.eventTime); | 
| Siarhei Vishniakou | 9757f78 | 2019-10-29 12:53:08 -0700 | [diff] [blame] | 5366 | return event; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5367 | } | 
|  | 5368 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5369 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, | 
|  | 5370 | const Connection& connection, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | // TODO Write some statistics about how long we spend waiting. | 
|  | 5372 | } | 
|  | 5373 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5374 | /** | 
|  | 5375 | * Report the touch event latency to the statsd server. | 
|  | 5376 | * Input events are reported for statistics if: | 
|  | 5377 | * - This is a touchscreen event | 
|  | 5378 | * - InputFilter is not enabled | 
|  | 5379 | * - Event is not injected or synthesized | 
|  | 5380 | * | 
|  | 5381 | * Statistics should be reported before calling addValue, to prevent a fresh new sample | 
|  | 5382 | * from getting aggregated with the "old" data. | 
|  | 5383 | */ | 
|  | 5384 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) | 
|  | 5385 | REQUIRES(mLock) { | 
|  | 5386 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && | 
|  | 5387 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; | 
|  | 5388 | if (!reportForStatistics) { | 
|  | 5389 | return; | 
|  | 5390 | } | 
|  | 5391 |  | 
|  | 5392 | if (mTouchStatistics.shouldReport()) { | 
|  | 5393 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), | 
|  | 5394 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), | 
|  | 5395 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); | 
|  | 5396 | mTouchStatistics.reset(); | 
|  | 5397 | } | 
|  | 5398 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); | 
|  | 5399 | mTouchStatistics.addValue(latencyMicros); | 
|  | 5400 | } | 
|  | 5401 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5402 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
|  | 5403 | if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5404 | ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5405 | } | 
|  | 5406 | } | 
|  | 5407 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5408 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5409 | if (ATRACE_ENABLED()) { | 
|  | 5410 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5411 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5412 | ATRACE_INT(counterName, connection->outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5413 | } | 
|  | 5414 | } | 
|  | 5415 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5416 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5417 | if (ATRACE_ENABLED()) { | 
|  | 5418 | char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5419 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5420 | ATRACE_INT(counterName, connection->waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5421 | } | 
|  | 5422 | } | 
|  | 5423 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5424 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5425 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5426 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5427 | dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5428 | dumpDispatchStateLocked(dump); | 
|  | 5429 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5430 | if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5431 | dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5432 | dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5433 | } | 
|  | 5434 | } | 
|  | 5435 |  | 
|  | 5436 | void InputDispatcher::monitor() { | 
|  | 5437 | // 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] | 5438 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5439 | mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5440 | mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5441 | } | 
|  | 5442 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5443 | /** | 
|  | 5444 | * Wake up the dispatcher and wait until it processes all events and commands. | 
|  | 5445 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
|  | 5446 | * this method can be safely called from any thread, as long as you've ensured that | 
|  | 5447 | * the work you are interested in completing has already been queued. | 
|  | 5448 | */ | 
|  | 5449 | bool InputDispatcher::waitForIdle() { | 
|  | 5450 | /** | 
|  | 5451 | * Timeout should represent the longest possible time that a device might spend processing | 
|  | 5452 | * events and commands. | 
|  | 5453 | */ | 
|  | 5454 | constexpr std::chrono::duration TIMEOUT = 100ms; | 
|  | 5455 | std::unique_lock lock(mLock); | 
|  | 5456 | mLooper->wake(); | 
|  | 5457 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
|  | 5458 | return result == std::cv_status::no_timeout; | 
|  | 5459 | } | 
|  | 5460 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5461 | /** | 
|  | 5462 | * Sets focus to the window identified by the token. This must be called | 
|  | 5463 | * after updating any input window handles. | 
|  | 5464 | * | 
|  | 5465 | * Params: | 
|  | 5466 | *  request.token - input channel token used to identify the window that should gain focus. | 
|  | 5467 | *  request.focusedToken - the token that the caller expects currently to be focused. If the | 
|  | 5468 | *  specified token does not match the currently focused window, this request will be dropped. | 
|  | 5469 | *  If the specified focused token matches the currently focused window, the call will succeed. | 
|  | 5470 | *  Set this to "null" if this call should succeed no matter what the currently focused token is. | 
|  | 5471 | *  request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) | 
|  | 5472 | *  when requesting the focus change. This determines which request gets | 
|  | 5473 | *  precedence if there is a focus change request from another source such as pointer down. | 
|  | 5474 | */ | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5475 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { | 
|  | 5476 | { // acquire lock | 
|  | 5477 | std::scoped_lock _l(mLock); | 
|  | 5478 |  | 
|  | 5479 | const int32_t displayId = request.displayId; | 
|  | 5480 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 5481 | if (request.focusedToken && oldFocusedToken != request.focusedToken) { | 
|  | 5482 | ALOGD_IF(DEBUG_FOCUS, | 
|  | 5483 | "setFocusedWindow on display %" PRId32 | 
|  | 5484 | " ignored, reason: focusedToken is not focused", | 
|  | 5485 | displayId); | 
|  | 5486 | return; | 
|  | 5487 | } | 
|  | 5488 |  | 
|  | 5489 | mPendingFocusRequests.erase(displayId); | 
|  | 5490 | FocusResult result = handleFocusRequestLocked(request); | 
|  | 5491 | if (result == FocusResult::NOT_VISIBLE) { | 
|  | 5492 | // The requested window is not currently visible. Wait for the window to become visible | 
|  | 5493 | // and then provide it focus. This is to handle situations where a user action triggers | 
|  | 5494 | // a new window to appear. We want to be able to queue any key events after the user | 
|  | 5495 | // action and deliver it to the newly focused window. In order for this to happen, we | 
|  | 5496 | // take focus from the currently focused window so key events can be queued. | 
|  | 5497 | ALOGD_IF(DEBUG_FOCUS, | 
|  | 5498 | "setFocusedWindow on display %" PRId32 | 
|  | 5499 | " pending, reason: window is not visible", | 
|  | 5500 | displayId); | 
|  | 5501 | mPendingFocusRequests[displayId] = request; | 
|  | 5502 | onFocusChangedLocked(oldFocusedToken, nullptr, displayId, | 
|  | 5503 | "setFocusedWindow_AwaitingWindowVisibility"); | 
|  | 5504 | } else if (result != FocusResult::OK) { | 
|  | 5505 | ALOGW("setFocusedWindow on display %" PRId32 " ignored, reason:%s", displayId, | 
|  | 5506 | typeToString(result)); | 
|  | 5507 | } | 
|  | 5508 | } // release lock | 
|  | 5509 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 5510 | mLooper->wake(); | 
|  | 5511 | } | 
|  | 5512 |  | 
|  | 5513 | InputDispatcher::FocusResult InputDispatcher::handleFocusRequestLocked( | 
|  | 5514 | const FocusRequest& request) { | 
|  | 5515 | const int32_t displayId = request.displayId; | 
|  | 5516 | const sp<IBinder> newFocusedToken = request.token; | 
|  | 5517 | const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
|  | 5518 |  | 
|  | 5519 | if (oldFocusedToken == request.token) { | 
|  | 5520 | ALOGD_IF(DEBUG_FOCUS, | 
|  | 5521 | "setFocusedWindow on display %" PRId32 " ignored, reason: already focused", | 
|  | 5522 | displayId); | 
|  | 5523 | return FocusResult::OK; | 
|  | 5524 | } | 
|  | 5525 |  | 
|  | 5526 | FocusResult result = checkTokenFocusableLocked(newFocusedToken, displayId); | 
|  | 5527 | if (result != FocusResult::OK) { | 
|  | 5528 | return result; | 
|  | 5529 | } | 
|  | 5530 |  | 
|  | 5531 | std::string_view reason = | 
|  | 5532 | (request.focusedToken) ? "setFocusedWindow_FocusCheck" : "setFocusedWindow"; | 
|  | 5533 | onFocusChangedLocked(oldFocusedToken, newFocusedToken, displayId, reason); | 
|  | 5534 | return FocusResult::OK; | 
|  | 5535 | } | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5536 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5537 | void InputDispatcher::onFocusChangedLocked(const sp<IBinder>& oldFocusedToken, | 
|  | 5538 | const sp<IBinder>& newFocusedToken, int32_t displayId, | 
|  | 5539 | std::string_view reason) { | 
|  | 5540 | if (oldFocusedToken) { | 
|  | 5541 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(oldFocusedToken); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5542 | if (focusedInputChannel) { | 
|  | 5543 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 5544 | "focus left window"); | 
|  | 5545 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5546 | enqueueFocusEventLocked(oldFocusedToken, false /*hasFocus*/, reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5547 | } | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5548 | mFocusedWindowTokenByDisplay.erase(displayId); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5549 | } | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5550 | if (newFocusedToken) { | 
|  | 5551 | mFocusedWindowTokenByDisplay[displayId] = newFocusedToken; | 
|  | 5552 | enqueueFocusEventLocked(newFocusedToken, true /*hasFocus*/, reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5553 | } | 
|  | 5554 |  | 
|  | 5555 | if (mFocusedDisplayId == displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5556 | notifyFocusChangedLocked(oldFocusedToken, newFocusedToken); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5557 | } | 
|  | 5558 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5559 |  | 
|  | 5560 | /** | 
|  | 5561 | * Checks if the window token can be focused on a display. The token can be focused if there is | 
|  | 5562 | * at least one window handle that is visible with the same token and all window handles with the | 
|  | 5563 | * same token are focusable. | 
|  | 5564 | * | 
|  | 5565 | * In the case of mirroring, two windows may share the same window token and their visibility | 
|  | 5566 | * might be different. Example, the mirrored window can cover the window its mirroring. However, | 
|  | 5567 | * we expect the focusability of the windows to match since its hard to reason why one window can | 
|  | 5568 | * receive focus events and the other cannot when both are backed by the same input channel. | 
|  | 5569 | */ | 
|  | 5570 | InputDispatcher::FocusResult InputDispatcher::checkTokenFocusableLocked(const sp<IBinder>& token, | 
|  | 5571 | int32_t displayId) const { | 
|  | 5572 | bool allWindowsAreFocusable = true; | 
|  | 5573 | bool visibleWindowFound = false; | 
|  | 5574 | bool windowFound = false; | 
|  | 5575 | for (const sp<InputWindowHandle>& window : getWindowHandlesLocked(displayId)) { | 
|  | 5576 | if (window->getToken() != token) { | 
|  | 5577 | continue; | 
|  | 5578 | } | 
|  | 5579 | windowFound = true; | 
|  | 5580 | if (window->getInfo()->visible) { | 
|  | 5581 | // Check if at least a single window is visible. | 
|  | 5582 | visibleWindowFound = true; | 
|  | 5583 | } | 
|  | 5584 | if (!window->getInfo()->focusable) { | 
|  | 5585 | // Check if all windows with the window token are focusable. | 
|  | 5586 | allWindowsAreFocusable = false; | 
|  | 5587 | break; | 
|  | 5588 | } | 
|  | 5589 | } | 
|  | 5590 |  | 
|  | 5591 | if (!windowFound) { | 
|  | 5592 | return FocusResult::NO_WINDOW; | 
|  | 5593 | } | 
|  | 5594 | if (!allWindowsAreFocusable) { | 
|  | 5595 | return FocusResult::NOT_FOCUSABLE; | 
|  | 5596 | } | 
|  | 5597 | if (!visibleWindowFound) { | 
|  | 5598 | return FocusResult::NOT_VISIBLE; | 
|  | 5599 | } | 
|  | 5600 |  | 
|  | 5601 | return FocusResult::OK; | 
|  | 5602 | } | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5603 | } // namespace android::inputdispatcher |