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 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 50 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 51 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 52 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 53 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 54 | #include <binder/Binder.h> |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 55 | #include <binder/IServiceManager.h> |
| 56 | #include <com/android/internal/compat/IPlatformCompatNative.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 57 | #include <input/InputDevice.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 58 | #include <input/InputWindow.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 60 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 61 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 74 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 75 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | #define INDENT " " |
| 77 | #define INDENT2 " " |
| 78 | #define INDENT3 " " |
| 79 | #define INDENT4 " " |
| 80 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 81 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 82 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 83 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 84 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 85 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 86 | using android::os::InputEventInjectionResult; |
| 87 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 88 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 89 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 90 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | |
| 92 | // Default input dispatching timeout if there is no focused application or paused window |
| 93 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 94 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 95 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 96 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | |
| 98 | // Amount of time to allow for all pending events to be processed when an app switch |
| 99 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 100 | // 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] | 101 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 102 | |
| 103 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 104 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 105 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | // 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] | 108 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 109 | |
| 110 | // Log a warning when an interception call takes longer than this to process. |
| 111 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 113 | // Additional key latency in case a connection is still processing some motion events. |
| 114 | // This will help with the case when a user touched a button that opens a new window, |
| 115 | // and gives us the chance to dispatch the key to this new window. |
| 116 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 117 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 119 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 120 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 121 | // Event log tags. See EventLogTags.logtags for reference |
| 122 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 123 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 124 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 | static inline nsecs_t now() { |
| 126 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 127 | } |
| 128 | |
| 129 | static inline const char* toString(bool value) { |
| 130 | return value ? "true" : "false"; |
| 131 | } |
| 132 | |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 133 | static inline const std::string toString(sp<IBinder> binder) { |
| 134 | if (binder == nullptr) { |
| 135 | return "<null>"; |
| 136 | } |
| 137 | return StringPrintf("%p", binder.get()); |
| 138 | } |
| 139 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 141 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 142 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static bool isValidKeyAction(int32_t action) { |
| 146 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 147 | case AKEY_EVENT_ACTION_DOWN: |
| 148 | case AKEY_EVENT_ACTION_UP: |
| 149 | return true; |
| 150 | default: |
| 151 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 157 | ALOGE("Key event has invalid action code 0x%x", action); |
| 158 | return false; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 163 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 164 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 165 | case AMOTION_EVENT_ACTION_DOWN: |
| 166 | case AMOTION_EVENT_ACTION_UP: |
| 167 | case AMOTION_EVENT_ACTION_CANCEL: |
| 168 | case AMOTION_EVENT_ACTION_MOVE: |
| 169 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 170 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 171 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 172 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 173 | case AMOTION_EVENT_ACTION_SCROLL: |
| 174 | return true; |
| 175 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 176 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 177 | int32_t index = getMotionEventActionPointerIndex(action); |
| 178 | return index >= 0 && index < pointerCount; |
| 179 | } |
| 180 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 181 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 182 | return actionButton != 0; |
| 183 | default: |
| 184 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 188 | static int64_t millis(std::chrono::nanoseconds t) { |
| 189 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 190 | } |
| 191 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 192 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 193 | const PointerProperties* pointerProperties) { |
| 194 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 196 | return false; |
| 197 | } |
| 198 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 199 | 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] | 200 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | BitSet32 pointerIdBits; |
| 204 | for (size_t i = 0; i < pointerCount; i++) { |
| 205 | int32_t id = pointerProperties[i].id; |
| 206 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 207 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 208 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | if (pointerIdBits.hasBit(id)) { |
| 212 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 213 | return false; |
| 214 | } |
| 215 | pointerIdBits.markBit(id); |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 220 | static std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 222 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 225 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 226 | bool first = true; |
| 227 | Region::const_iterator cur = region.begin(); |
| 228 | Region::const_iterator const tail = region.end(); |
| 229 | while (cur != tail) { |
| 230 | if (first) { |
| 231 | first = false; |
| 232 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 233 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 235 | 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] | 236 | cur++; |
| 237 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 238 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 241 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
| 242 | constexpr size_t maxEntries = 50; // max events to print |
| 243 | constexpr size_t skipBegin = maxEntries / 2; |
| 244 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 245 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 246 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 247 | |
| 248 | std::string dump; |
| 249 | for (size_t i = 0; i < queue.size(); i++) { |
| 250 | const DispatchEntry& entry = *queue[i]; |
| 251 | if (i >= skipBegin && i < skipEnd) { |
| 252 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 253 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 254 | continue; |
| 255 | } |
| 256 | dump.append(INDENT4); |
| 257 | dump += entry.eventEntry->getDescription(); |
| 258 | dump += StringPrintf(", seq=%" PRIu32 |
| 259 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 260 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 261 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 262 | if (entry.deliveryTime != 0) { |
| 263 | // This entry was delivered, so add information on how long we've been waiting |
| 264 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 265 | } |
| 266 | dump.append("\n"); |
| 267 | } |
| 268 | return dump; |
| 269 | } |
| 270 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Find the entry in std::unordered_map by key, and return it. |
| 273 | * If the entry is not found, return a default constructed entry. |
| 274 | * |
| 275 | * Useful when the entries are vectors, since an empty vector will be returned |
| 276 | * if the entry is not found. |
| 277 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 278 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 279 | template <typename K, typename V> |
| 280 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 281 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 282 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 283 | } |
| 284 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 285 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 286 | if (first == second) { |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | if (first == nullptr || second == nullptr) { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | return first->getToken() == second->getToken(); |
| 295 | } |
| 296 | |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 297 | static bool haveSameApplicationToken(const InputWindowInfo* first, const InputWindowInfo* second) { |
| 298 | if (first == nullptr || second == nullptr) { |
| 299 | return false; |
| 300 | } |
| 301 | return first->applicationInfo.token != nullptr && |
| 302 | first->applicationInfo.token == second->applicationInfo.token; |
| 303 | } |
| 304 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 305 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 306 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 307 | } |
| 308 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 309 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 310 | std::shared_ptr<EventEntry> eventEntry, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 311 | int32_t inputTargetFlags) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 312 | if (eventEntry->type == EventEntry::Type::MOTION) { |
| 313 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 314 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) == 0) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 315 | const ui::Transform identityTransform; |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 316 | // Use identity transform for events that are not pointer events because their axes |
| 317 | // values do not represent on-screen coordinates, so they should not have any window |
| 318 | // transformations applied to them. |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 319 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 320 | 1.0f /*globalScaleFactor*/, |
| 321 | inputTarget.displaySize); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 325 | if (inputTarget.useDefaultPointerTransform()) { |
| 326 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 327 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 328 | inputTarget.globalScaleFactor, |
| 329 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 333 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 334 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 335 | std::vector<PointerCoords> pointerCoords; |
| 336 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 337 | |
| 338 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 339 | // 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] | 340 | // uses the transform for the normalized pointer. |
| 341 | const ui::Transform& firstPointerTransform = |
| 342 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 343 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 344 | |
| 345 | // Iterate through all pointers in the event to normalize against the first. |
| 346 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 347 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 348 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 349 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 350 | |
| 351 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 352 | // First, apply the current pointer's transform to update the coordinates into |
| 353 | // window space. |
| 354 | pointerCoords[pointerIndex].transform(currTransform); |
| 355 | // Next, apply the inverse transform of the normalized coordinates so the |
| 356 | // current coordinates are transformed into the normalized coordinate space. |
| 357 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 360 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 361 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 362 | motionEntry.deviceId, motionEntry.source, |
| 363 | motionEntry.displayId, motionEntry.policyFlags, |
| 364 | motionEntry.action, motionEntry.actionButton, |
| 365 | motionEntry.flags, motionEntry.metaState, |
| 366 | motionEntry.buttonState, motionEntry.classification, |
| 367 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 368 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 369 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 370 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 371 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 372 | |
| 373 | if (motionEntry.injectionState) { |
| 374 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 375 | combinedMotionEntry->injectionState->refCount += 1; |
| 376 | } |
| 377 | |
| 378 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 379 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 380 | firstPointerTransform, inputTarget.globalScaleFactor, |
| 381 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 382 | return dispatchEntry; |
| 383 | } |
| 384 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 385 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 386 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 387 | float yOffset = 0) { |
| 388 | if (monitors.empty()) { |
| 389 | return; |
| 390 | } |
| 391 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 392 | for (const Monitor& monitor : monitors) { |
| 393 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 394 | } |
| 395 | } |
| 396 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 397 | static status_t openInputChannelPair(const std::string& name, |
| 398 | std::shared_ptr<InputChannel>& serverChannel, |
| 399 | std::unique_ptr<InputChannel>& clientChannel) { |
| 400 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 401 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 402 | |
| 403 | serverChannel = std::move(uniqueServerChannel); |
| 404 | return result; |
| 405 | } |
| 406 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 407 | template <typename T> |
| 408 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 409 | if (lhs == nullptr && rhs == nullptr) { |
| 410 | return true; |
| 411 | } |
| 412 | if (lhs == nullptr || rhs == nullptr) { |
| 413 | return false; |
| 414 | } |
| 415 | return *lhs == *rhs; |
| 416 | } |
| 417 | |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 418 | static sp<IPlatformCompatNative> getCompatService() { |
| 419 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 420 | if (service == nullptr) { |
| 421 | ALOGE("Failed to link to compat service"); |
| 422 | return nullptr; |
| 423 | } |
| 424 | return interface_cast<IPlatformCompatNative>(service); |
| 425 | } |
| 426 | |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 427 | static KeyEvent createKeyEvent(const KeyEntry& entry) { |
| 428 | KeyEvent event; |
| 429 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 430 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 431 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 432 | return event; |
| 433 | } |
| 434 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 435 | static std::optional<int32_t> findMonitorPidByToken( |
| 436 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 437 | const sp<IBinder>& token) { |
| 438 | for (const auto& it : monitorsByDisplay) { |
| 439 | const std::vector<Monitor>& monitors = it.second; |
| 440 | for (const Monitor& monitor : monitors) { |
| 441 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 442 | return monitor.pid; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | return std::nullopt; |
| 447 | } |
| 448 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 449 | static bool shouldReportMetricsForConnection(const Connection& connection) { |
| 450 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 451 | // affect the statistics. |
| 452 | if (connection.monitor) { |
| 453 | return false; |
| 454 | } |
| 455 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 456 | if (!connection.responsive) { |
| 457 | return false; |
| 458 | } |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | static bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, |
| 463 | const Connection& connection) { |
| 464 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 465 | const int32_t& inputEventId = eventEntry.id; |
| 466 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 467 | // Event was transmuted |
| 468 | return false; |
| 469 | } |
| 470 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 471 | return false; |
| 472 | } |
| 473 | // Only track latency for events that originated from hardware |
| 474 | if (eventEntry.isSynthesized()) { |
| 475 | return false; |
| 476 | } |
| 477 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 478 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 479 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 480 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 481 | return false; |
| 482 | } |
| 483 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 484 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 485 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 486 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 487 | return false; |
| 488 | } |
| 489 | } else { |
| 490 | // Not a key or a motion |
| 491 | return false; |
| 492 | } |
| 493 | if (!shouldReportMetricsForConnection(connection)) { |
| 494 | return false; |
| 495 | } |
| 496 | return true; |
| 497 | } |
| 498 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 499 | // --- InputDispatcher --- |
| 500 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 501 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 502 | : mPolicy(policy), |
| 503 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 504 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 505 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 506 | mAppSwitchSawKeyDown(false), |
| 507 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 508 | mNextUnblockedEvent(nullptr), |
| 509 | mDispatchEnabled(false), |
| 510 | mDispatchFrozen(false), |
| 511 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 512 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 513 | // To avoid leaking stack in case that call never comes, and for tests, |
| 514 | // initialize it here anyways. |
| 515 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 516 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 517 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 518 | mFocusedWindowRequestedPointerCapture(false), |
| 519 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 520 | mLatencyTracker(&mEmptyProcessor), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 521 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 522 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 523 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 524 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 525 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 526 | |
| 527 | policy->getDispatcherConfiguration(&mConfig); |
| 528 | } |
| 529 | |
| 530 | InputDispatcher::~InputDispatcher() { |
| 531 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 532 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | |
| 534 | resetKeyRepeatLocked(); |
| 535 | releasePendingEventLocked(); |
| 536 | drainInboundQueueLocked(); |
| 537 | } |
| 538 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 539 | while (!mConnectionsByToken.empty()) { |
| 540 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 541 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 545 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 546 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 547 | return ALREADY_EXISTS; |
| 548 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 549 | mThread = std::make_unique<InputThread>( |
| 550 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 551 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 555 | if (mThread && mThread->isCallingThread()) { |
| 556 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 557 | return INVALID_OPERATION; |
| 558 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 559 | mThread.reset(); |
| 560 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | void InputDispatcher::dispatchOnce() { |
| 564 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 565 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 566 | std::scoped_lock _l(mLock); |
| 567 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | |
| 569 | // Run a dispatch loop if there are no pending commands. |
| 570 | // The dispatch loop might enqueue commands to run afterwards. |
| 571 | if (!haveCommandsLocked()) { |
| 572 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 573 | } |
| 574 | |
| 575 | // Run all pending commands if there are any. |
| 576 | // If any commands were run then force the next poll to wake up immediately. |
| 577 | if (runCommandsLockedInterruptible()) { |
| 578 | nextWakeupTime = LONG_LONG_MIN; |
| 579 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 580 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 581 | // If we are still waiting for ack on some events, |
| 582 | // we might have to wake up earlier to check if an app is anr'ing. |
| 583 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 584 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 585 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 586 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 587 | // pending or queued events |
| 588 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 589 | mDispatcherEnteredIdle.notify_all(); |
| 590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | } // release lock |
| 592 | |
| 593 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 594 | nsecs_t currentTime = now(); |
| 595 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 596 | mLooper->pollOnce(timeoutMillis); |
| 597 | } |
| 598 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 599 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 600 | * Raise ANR if there is no focused window. |
| 601 | * Before the ANR is raised, do a final state check: |
| 602 | * 1. The currently focused application must be the same one we are waiting for. |
| 603 | * 2. Ensure we still don't have a focused window. |
| 604 | */ |
| 605 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 606 | // Check if the application that we are waiting for is still focused. |
| 607 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 608 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 609 | if (focusedApplication == nullptr || |
| 610 | focusedApplication->getApplicationToken() != |
| 611 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 612 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 613 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 614 | focusedApplication->getName().c_str()); |
| 615 | return; // The focused application has changed. |
| 616 | } |
| 617 | |
| 618 | const sp<InputWindowHandle>& focusedWindowHandle = |
| 619 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 620 | if (focusedWindowHandle != nullptr) { |
| 621 | return; // We now have a focused window. No need for ANR. |
| 622 | } |
| 623 | onAnrLocked(mAwaitedFocusedApplication); |
| 624 | } |
| 625 | |
| 626 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 627 | * Check if any of the connections' wait queues have events that are too old. |
| 628 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 629 | * Return the time at which we should wake up next. |
| 630 | */ |
| 631 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 632 | const nsecs_t currentTime = now(); |
| 633 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 634 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 635 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 636 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 637 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 638 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 639 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 640 | return LONG_LONG_MIN; |
| 641 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 642 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 643 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Check if any connection ANRs are due |
| 648 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 649 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 650 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 651 | } |
| 652 | |
| 653 | // If we reached here, we have an unresponsive connection. |
| 654 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 655 | if (connection == nullptr) { |
| 656 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 657 | return nextAnrCheck; |
| 658 | } |
| 659 | connection->responsive = false; |
| 660 | // Stop waking up for this unresponsive connection |
| 661 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 662 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 663 | return LONG_LONG_MIN; |
| 664 | } |
| 665 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 666 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 667 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 668 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 669 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 670 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 671 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 675 | nsecs_t currentTime = now(); |
| 676 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 677 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 678 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 679 | // repeat if the device is just coming out of sleep. |
| 680 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | resetKeyRepeatLocked(); |
| 682 | } |
| 683 | |
| 684 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 685 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 686 | if (DEBUG_FOCUS) { |
| 687 | ALOGD("Dispatch frozen. Waiting some more."); |
| 688 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | return; |
| 690 | } |
| 691 | |
| 692 | // Optimize latency of app switches. |
| 693 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 694 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 695 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 696 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 697 | *nextWakeupTime = mAppSwitchDueTime; |
| 698 | } |
| 699 | |
| 700 | // Ready to start a new event. |
| 701 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 702 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 703 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 704 | if (isAppSwitchDue) { |
| 705 | // The inbound queue is empty so the app switch key we were waiting |
| 706 | // for will never arrive. Stop waiting for it. |
| 707 | resetPendingAppSwitchLocked(false); |
| 708 | isAppSwitchDue = false; |
| 709 | } |
| 710 | |
| 711 | // Synthesize a key repeat if appropriate. |
| 712 | if (mKeyRepeatState.lastKeyEntry) { |
| 713 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 714 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 715 | } else { |
| 716 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 717 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | // Nothing to do if there is no pending event. |
| 723 | if (!mPendingEvent) { |
| 724 | return; |
| 725 | } |
| 726 | } else { |
| 727 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 728 | mPendingEvent = mInboundQueue.front(); |
| 729 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 730 | traceInboundQueueLengthLocked(); |
| 731 | } |
| 732 | |
| 733 | // Poke user activity for this event. |
| 734 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 735 | pokeUserActivityLocked(*mPendingEvent); |
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 | // Now we have an event to dispatch. |
| 740 | // 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] | 741 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 743 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 744 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 745 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 747 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 751 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 755 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 756 | const ConfigurationChangedEntry& typedEntry = |
| 757 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 758 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 759 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 | break; |
| 761 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 762 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 763 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 764 | const DeviceResetEntry& typedEntry = |
| 765 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 766 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 767 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 768 | break; |
| 769 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 770 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 771 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 772 | std::shared_ptr<FocusEntry> typedEntry = |
| 773 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 774 | dispatchFocusLocked(currentTime, typedEntry); |
| 775 | done = true; |
| 776 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 777 | break; |
| 778 | } |
| 779 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 780 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 781 | const auto typedEntry = |
| 782 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 783 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 784 | done = true; |
| 785 | break; |
| 786 | } |
| 787 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 788 | case EventEntry::Type::DRAG: { |
| 789 | std::shared_ptr<DragEntry> typedEntry = |
| 790 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 791 | dispatchDragLocked(currentTime, typedEntry); |
| 792 | done = true; |
| 793 | break; |
| 794 | } |
| 795 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 796 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 797 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 798 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 799 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 800 | resetPendingAppSwitchLocked(true); |
| 801 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 802 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 803 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 806 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 807 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 808 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 809 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 810 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 811 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 812 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 816 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 817 | std::shared_ptr<MotionEntry> motionEntry = |
| 818 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 819 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 820 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 822 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 823 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 824 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 825 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 826 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 827 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 828 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 829 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 830 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 831 | |
| 832 | case EventEntry::Type::SENSOR: { |
| 833 | std::shared_ptr<SensorEntry> sensorEntry = |
| 834 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 835 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 836 | dropReason = DropReason::APP_SWITCH; |
| 837 | } |
| 838 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 839 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 840 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 841 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 842 | dropReason = DropReason::STALE; |
| 843 | } |
| 844 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 845 | done = true; |
| 846 | break; |
| 847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 851 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 852 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 853 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 854 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 855 | |
| 856 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 861 | /** |
| 862 | * Return true if the events preceding this incoming motion event should be dropped |
| 863 | * Return false otherwise (the default behaviour) |
| 864 | */ |
| 865 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 866 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 867 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 868 | |
| 869 | // Optimize case where the current application is unresponsive and the user |
| 870 | // decides to touch a window in a different application. |
| 871 | // If the application takes too long to catch up then we drop all events preceding |
| 872 | // the touch into the other window. |
| 873 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 874 | int32_t displayId = motionEntry.displayId; |
| 875 | int32_t x = static_cast<int32_t>( |
| 876 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 877 | int32_t y = static_cast<int32_t>( |
| 878 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 879 | sp<InputWindowHandle> touchedWindowHandle = |
| 880 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 881 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 882 | touchedWindowHandle->getApplicationToken() != |
| 883 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 884 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 885 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 886 | "for %s", |
| 887 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 888 | return true; |
| 889 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 890 | |
| 891 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 892 | std::vector<TouchedMonitor> gestureMonitors = |
| 893 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 894 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 895 | sp<Connection> connection = |
| 896 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 897 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 898 | // This monitor could take more input. Drop all events preceding this |
| 899 | // event, so that gesture monitor could get a chance to receive the stream |
| 900 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 901 | "responsive gesture monitor that may handle the event", |
| 902 | mAwaitedFocusedApplication->getName().c_str()); |
| 903 | return true; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 909 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 910 | // events to be processed before dispatching the key event. This is because these motion events |
| 911 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 912 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 913 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 914 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 915 | "just send the pending key event to the focused window."); |
| 916 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 917 | } |
| 918 | return false; |
| 919 | } |
| 920 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 921 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 922 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 923 | mInboundQueue.push_back(std::move(newEntry)); |
| 924 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | traceInboundQueueLengthLocked(); |
| 926 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 927 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 928 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 929 | // Optimize app switch latency. |
| 930 | // If the application takes too long to catch up then we drop all events preceding |
| 931 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 932 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 933 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 934 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 935 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 936 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 937 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 938 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 939 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 940 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 941 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 942 | mAppSwitchSawKeyDown = false; |
| 943 | needWake = true; |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | break; |
| 948 | } |
| 949 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 950 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 951 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 952 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 953 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 954 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 955 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 957 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 958 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 959 | break; |
| 960 | } |
| 961 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 962 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 963 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 964 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 965 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 966 | // nothing to do |
| 967 | break; |
| 968 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | return needWake; |
| 972 | } |
| 973 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 974 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 975 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 976 | if (entry->type != EventEntry::Type::SENSOR) { |
| 977 | mRecentQueue.push_back(entry); |
| 978 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 979 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 980 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 984 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 985 | int32_t y, TouchState* touchState, |
| 986 | bool addOutsideTargets, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 987 | bool addPortalWindows, |
| 988 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 989 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 990 | LOG_ALWAYS_FATAL( |
| 991 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 992 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 993 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 994 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 995 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 996 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 997 | continue; |
| 998 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 999 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 1000 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1001 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | |
| 1003 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1004 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1005 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 1006 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1007 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1008 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1009 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 1010 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1011 | if (addPortalWindows) { |
| 1012 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1013 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1014 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1015 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1016 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | // Found window. |
| 1019 | return windowHandle; |
| 1020 | } |
| 1021 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1022 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1023 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1024 | touchState->addOrUpdateWindow(windowHandle, |
| 1025 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1026 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1027 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1029 | } |
| 1030 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1031 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1034 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1035 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1036 | std::vector<TouchedMonitor> touchedMonitors; |
| 1037 | |
| 1038 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 1039 | addGestureMonitors(monitors, touchedMonitors); |
| 1040 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 1041 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 1042 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1043 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 1044 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1045 | } |
| 1046 | return touchedMonitors; |
| 1047 | } |
| 1048 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1049 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | const char* reason; |
| 1051 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1052 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1053 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1054 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1056 | reason = "inbound event was dropped because the policy consumed it"; |
| 1057 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1058 | case DropReason::DISABLED: |
| 1059 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1060 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1061 | } |
| 1062 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1063 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1064 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1065 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1066 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1067 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1068 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1069 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1070 | "has started interacting with a different application."); |
| 1071 | reason = "inbound event was dropped because the current application is not responding " |
| 1072 | "and the user has started interacting with a different application"; |
| 1073 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1074 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1075 | ALOGI("Dropped event because it is stale."); |
| 1076 | reason = "inbound event was dropped because it is stale"; |
| 1077 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1078 | case DropReason::NO_POINTER_CAPTURE: |
| 1079 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1080 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1081 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1082 | case DropReason::NOT_DROPPED: { |
| 1083 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1084 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1085 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1088 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1089 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1090 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1091 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1092 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1093 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1094 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1095 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1096 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1097 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1098 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1099 | } else { |
| 1100 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1101 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1102 | } |
| 1103 | break; |
| 1104 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1105 | case EventEntry::Type::SENSOR: { |
| 1106 | break; |
| 1107 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1108 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1109 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1110 | break; |
| 1111 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1112 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1113 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1114 | case EventEntry::Type::DEVICE_RESET: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1115 | LOG_ALWAYS_FATAL("Should not drop %s events", NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1116 | break; |
| 1117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1121 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1122 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1123 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1126 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1127 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1128 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1129 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1133 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1134 | } |
| 1135 | |
| 1136 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1137 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1138 | |
| 1139 | #if DEBUG_APP_SWITCH |
| 1140 | if (handled) { |
| 1141 | ALOGD("App switch has arrived."); |
| 1142 | } else { |
| 1143 | ALOGD("App switch was abandoned."); |
| 1144 | } |
| 1145 | #endif |
| 1146 | } |
| 1147 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1149 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1153 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | return false; |
| 1155 | } |
| 1156 | |
| 1157 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1158 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1159 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1160 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1161 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1162 | |
| 1163 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1164 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | return true; |
| 1166 | } |
| 1167 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1168 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1169 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1173 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1174 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1175 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1176 | releaseInboundEventLocked(entry); |
| 1177 | } |
| 1178 | traceInboundQueueLengthLocked(); |
| 1179 | } |
| 1180 | |
| 1181 | void InputDispatcher::releasePendingEventLocked() { |
| 1182 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1184 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1188 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1190 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | #if DEBUG_DISPATCH_CYCLE |
| 1192 | ALOGD("Injected inbound event was dropped."); |
| 1193 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1194 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1195 | } |
| 1196 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1197 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | } |
| 1199 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1203 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1204 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1208 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1209 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1211 | uint32_t policyFlags = entry->policyFlags & |
| 1212 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1213 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1214 | std::shared_ptr<KeyEntry> newEntry = |
| 1215 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1216 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1217 | entry->flags, entry->keyCode, entry->scanCode, |
| 1218 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1220 | newEntry->syntheticRepeat = true; |
| 1221 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1223 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1226 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1227 | const ConfigurationChangedEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1229 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | #endif |
| 1231 | |
| 1232 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1233 | resetKeyRepeatLocked(); |
| 1234 | |
| 1235 | // 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] | 1236 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1237 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1238 | commandEntry->eventTime = entry.eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1239 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | return true; |
| 1241 | } |
| 1242 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1243 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1244 | const DeviceResetEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1246 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1247 | entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | #endif |
| 1249 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1250 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1251 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1253 | return true; |
| 1254 | } |
| 1255 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1256 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1257 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1258 | if (mPendingEvent != nullptr) { |
| 1259 | // Move the pending event to the front of the queue. This will give the chance |
| 1260 | // for the pending event to get dispatched to the newly focused window |
| 1261 | mInboundQueue.push_front(mPendingEvent); |
| 1262 | mPendingEvent = nullptr; |
| 1263 | } |
| 1264 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1265 | std::unique_ptr<FocusEntry> focusEntry = |
| 1266 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1267 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1268 | |
| 1269 | // This event should go to the front of the queue, but behind all other focus events |
| 1270 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1271 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1272 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1273 | [](const std::shared_ptr<EventEntry>& event) { |
| 1274 | return event->type == EventEntry::Type::FOCUS; |
| 1275 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1276 | |
| 1277 | // 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] | 1278 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1281 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1282 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1283 | if (channel == nullptr) { |
| 1284 | return; // Window has gone away |
| 1285 | } |
| 1286 | InputTarget target; |
| 1287 | target.inputChannel = channel; |
| 1288 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1289 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1290 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1291 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1292 | std::string reason = std::string("reason=").append(entry->reason); |
| 1293 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1294 | dispatchEventLocked(currentTime, entry, {target}); |
| 1295 | } |
| 1296 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1297 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1298 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1299 | DropReason& dropReason) { |
| 1300 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1301 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { |
| 1302 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); |
| 1303 | } |
| 1304 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1305 | // Pointer capture was already forcefully disabled because of focus change. |
| 1306 | dropReason = DropReason::NOT_DROPPED; |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | // Set drop reason for early returns |
| 1311 | dropReason = DropReason::NO_POINTER_CAPTURE; |
| 1312 | |
| 1313 | sp<IBinder> token; |
| 1314 | if (entry->pointerCaptureEnabled) { |
| 1315 | // Enable Pointer Capture |
| 1316 | if (!mFocusedWindowRequestedPointerCapture) { |
| 1317 | // This can happen if a window requests capture and immediately releases capture. |
| 1318 | ALOGW("No window requested Pointer Capture."); |
| 1319 | return; |
| 1320 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1321 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1322 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1323 | mWindowTokenWithPointerCapture = token; |
| 1324 | } else { |
| 1325 | // Disable Pointer Capture |
| 1326 | token = mWindowTokenWithPointerCapture; |
| 1327 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1328 | if (mFocusedWindowRequestedPointerCapture) { |
| 1329 | mFocusedWindowRequestedPointerCapture = false; |
| 1330 | setPointerCaptureLocked(false); |
| 1331 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | auto channel = getInputChannelLocked(token); |
| 1335 | if (channel == nullptr) { |
| 1336 | // Window has gone away, clean up Pointer Capture state. |
| 1337 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1338 | if (mFocusedWindowRequestedPointerCapture) { |
| 1339 | mFocusedWindowRequestedPointerCapture = false; |
| 1340 | setPointerCaptureLocked(false); |
| 1341 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1342 | return; |
| 1343 | } |
| 1344 | InputTarget target; |
| 1345 | target.inputChannel = channel; |
| 1346 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1347 | entry->dispatchInProgress = true; |
| 1348 | dispatchEventLocked(currentTime, entry, {target}); |
| 1349 | |
| 1350 | dropReason = DropReason::NOT_DROPPED; |
| 1351 | } |
| 1352 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1353 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1354 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1355 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1356 | if (!entry->dispatchInProgress) { |
| 1357 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1358 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1359 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1360 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1361 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1362 | // We have seen two identical key downs in a row which indicates that the device |
| 1363 | // driver is automatically generating key repeats itself. We take note of the |
| 1364 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1365 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1366 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1367 | // Make sure we don't get key down from a different device. If a different |
| 1368 | // device Id has same key pressed down, the new device Id will replace the |
| 1369 | // current one to hold the key repeat with repeat count reset. |
| 1370 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1371 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1372 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1373 | resetKeyRepeatLocked(); |
| 1374 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1375 | } else { |
| 1376 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1377 | resetKeyRepeatLocked(); |
| 1378 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1379 | } |
| 1380 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1381 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1382 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1383 | // 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] | 1384 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1385 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1386 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1387 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1388 | resetKeyRepeatLocked(); |
| 1389 | } |
| 1390 | |
| 1391 | if (entry->repeatCount == 1) { |
| 1392 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1393 | } else { |
| 1394 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1395 | } |
| 1396 | |
| 1397 | entry->dispatchInProgress = true; |
| 1398 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1399 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | // Handle case where the policy asked us to try again later last time. |
| 1403 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1404 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1405 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1406 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1407 | } |
| 1408 | return false; // wait until next wakeup |
| 1409 | } |
| 1410 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1411 | entry->interceptKeyWakeupTime = 0; |
| 1412 | } |
| 1413 | |
| 1414 | // Give the policy a chance to intercept the key. |
| 1415 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1416 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1417 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1418 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1419 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1420 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1421 | commandEntry->connectionToken = focusedWindowToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1422 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1423 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1424 | return false; // wait for the command to run |
| 1425 | } else { |
| 1426 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1427 | } |
| 1428 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1429 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1430 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1435 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1436 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1437 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1438 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1439 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | return true; |
| 1441 | } |
| 1442 | |
| 1443 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1444 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1445 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1446 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1447 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1448 | return false; |
| 1449 | } |
| 1450 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1451 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1452 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1453 | return true; |
| 1454 | } |
| 1455 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1456 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1457 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1458 | |
| 1459 | // Dispatch the key. |
| 1460 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1461 | return true; |
| 1462 | } |
| 1463 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1464 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1466 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1467 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1468 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1469 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1470 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1471 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1472 | #endif |
| 1473 | } |
| 1474 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1475 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { |
| 1476 | mLock.unlock(); |
| 1477 | |
| 1478 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; |
| 1479 | if (entry->accuracyChanged) { |
| 1480 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1481 | } |
| 1482 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1483 | entry->hwTimestamp, entry->values); |
| 1484 | mLock.lock(); |
| 1485 | } |
| 1486 | |
| 1487 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, |
| 1488 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 1489 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1490 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1491 | "source=0x%x, sensorType=%s", |
| 1492 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1493 | NamedEnum::string(entry->sensorType).c_str()); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1494 | #endif |
| 1495 | std::unique_ptr<CommandEntry> commandEntry = |
| 1496 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); |
| 1497 | commandEntry->sensorEntry = entry; |
| 1498 | postCommandLocked(std::move(commandEntry)); |
| 1499 | } |
| 1500 | |
| 1501 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
| 1502 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1503 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
| 1504 | NamedEnum::string(sensorType).c_str()); |
| 1505 | #endif |
| 1506 | { // acquire lock |
| 1507 | std::scoped_lock _l(mLock); |
| 1508 | |
| 1509 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1510 | std::shared_ptr<EventEntry> entry = *it; |
| 1511 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1512 | it = mInboundQueue.erase(it); |
| 1513 | releaseInboundEventLocked(entry); |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | return true; |
| 1518 | } |
| 1519 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1520 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1521 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1522 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1524 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 | entry->dispatchInProgress = true; |
| 1526 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1527 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1531 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1532 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1533 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1534 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1535 | return true; |
| 1536 | } |
| 1537 | |
| 1538 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1539 | |
| 1540 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1541 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 | |
| 1543 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1544 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1545 | if (isPointerEvent) { |
| 1546 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1547 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1548 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1549 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1550 | } else { |
| 1551 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1552 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1553 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1555 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1556 | return false; |
| 1557 | } |
| 1558 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1559 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1560 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1561 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1562 | return true; |
| 1563 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1564 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1565 | CancelationOptions::Mode mode(isPointerEvent |
| 1566 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1567 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1568 | CancelationOptions options(mode, "input event injection failed"); |
| 1569 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1570 | return true; |
| 1571 | } |
| 1572 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1573 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1574 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1575 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1576 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1577 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1578 | mTouchStatesByDisplay.find(entry->displayId); |
| 1579 | if (it != mTouchStatesByDisplay.end()) { |
| 1580 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1581 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1582 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1583 | // the corresponding displays as well. |
| 1584 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1585 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1586 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1587 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1593 | // Dispatch the motion. |
| 1594 | if (conflictingPointerActions) { |
| 1595 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1596 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1598 | } |
| 1599 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1600 | return true; |
| 1601 | } |
| 1602 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1603 | void InputDispatcher::enqueueDragEventLocked(const sp<InputWindowHandle>& windowHandle, |
| 1604 | bool isExiting, const MotionEntry& motionEntry) { |
| 1605 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1606 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1607 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1608 | PointerCoords pointerCoords; |
| 1609 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1610 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1611 | |
| 1612 | std::unique_ptr<DragEntry> dragEntry = |
| 1613 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1614 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1615 | pointerCoords.getY()); |
| 1616 | |
| 1617 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1618 | } |
| 1619 | |
| 1620 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1621 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1622 | if (channel == nullptr) { |
| 1623 | return; // Window has gone away |
| 1624 | } |
| 1625 | InputTarget target; |
| 1626 | target.inputChannel = channel; |
| 1627 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1628 | entry->dispatchInProgress = true; |
| 1629 | dispatchEventLocked(currentTime, entry, {target}); |
| 1630 | } |
| 1631 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1632 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1633 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1634 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1635 | ", policyFlags=0x%x, " |
| 1636 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1637 | "metaState=0x%x, buttonState=0x%x," |
| 1638 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1639 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1640 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1641 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1643 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1645 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1646 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1647 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1648 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1649 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1650 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1651 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1652 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1653 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1654 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1655 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1656 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1657 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1658 | } |
| 1659 | #endif |
| 1660 | } |
| 1661 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1662 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1663 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1664 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1665 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | #if DEBUG_DISPATCH_CYCLE |
| 1667 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1668 | #endif |
| 1669 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1670 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1671 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1672 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1673 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1674 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1676 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1677 | sp<Connection> connection = |
| 1678 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1679 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1680 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1681 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1682 | if (DEBUG_FOCUS) { |
| 1683 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1684 | "is no longer registered with the input dispatcher.", |
| 1685 | inputTarget.inputChannel->getName().c_str()); |
| 1686 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1691 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1692 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1693 | // If the policy decides to close the app, we will get a channel removal event via |
| 1694 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1695 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1696 | // pile up. |
| 1697 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1698 | connection->inputChannel->getName().c_str()); |
| 1699 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1700 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1701 | "application not responding"); |
| 1702 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1706 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1707 | if (DEBUG_FOCUS) { |
| 1708 | ALOGD("Resetting ANR timeouts."); |
| 1709 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1710 | |
| 1711 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1712 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1713 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1716 | /** |
| 1717 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1718 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1719 | * Focused display is the display that the user most recently interacted with. |
| 1720 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1721 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1722 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1723 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1724 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1725 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1726 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1727 | break; |
| 1728 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1729 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1730 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1731 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1732 | break; |
| 1733 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1734 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1735 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1736 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1737 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1738 | case EventEntry::Type::SENSOR: |
| 1739 | case EventEntry::Type::DRAG: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1740 | ALOGE("%s events do not have a target display", NamedEnum::string(entry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1741 | return ADISPLAY_ID_NONE; |
| 1742 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1743 | } |
| 1744 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1745 | } |
| 1746 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1747 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1748 | const char* focusedWindowName) { |
| 1749 | if (mAnrTracker.empty()) { |
| 1750 | // already processed all events that we waited for |
| 1751 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
| 1755 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1756 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1757 | // Wait to send key because there are unprocessed events that may cause focus to change |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1758 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1759 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1760 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1761 | return true; |
| 1762 | } |
| 1763 | |
| 1764 | // We still have pending events, and already started the timer |
| 1765 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1766 | return true; // Still waiting |
| 1767 | } |
| 1768 | |
| 1769 | // Waited too long, and some connection still hasn't processed all motions |
| 1770 | // Just send the key to the focused window |
| 1771 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1772 | focusedWindowName); |
| 1773 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1774 | return false; |
| 1775 | } |
| 1776 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1777 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1778 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1779 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1780 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1782 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1783 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1784 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1785 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1786 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1787 | // If there is no currently focused window and no focused application |
| 1788 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1789 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1790 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1791 | "display %" PRId32 ".", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1792 | NamedEnum::string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1793 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 | } |
| 1795 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1796 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1797 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1798 | // start interacting with another application via touch (app switch). This code can be removed |
| 1799 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1800 | // an app is expected to have a focused window. |
| 1801 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1802 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1803 | // 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] | 1804 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1805 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1806 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1807 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1808 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1809 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1810 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1811 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1812 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1813 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1814 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1815 | // Already raised ANR. Drop the event |
| 1816 | ALOGE("Dropping %s event because there is no focused window", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1817 | NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1818 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1819 | } else { |
| 1820 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1821 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | // we have a valid, non-null focused window |
| 1826 | resetNoFocusedWindowTimeoutLocked(); |
| 1827 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1829 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1830 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | } |
| 1832 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1833 | if (focusedWindowHandle->getInfo()->paused) { |
| 1834 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1835 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | // If the event is a key event, then we must wait for all previous events to |
| 1839 | // complete before delivering it because previous events may have the |
| 1840 | // side-effect of transferring focus to a different window and we want to |
| 1841 | // ensure that the following keys are sent to the new window. |
| 1842 | // |
| 1843 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1844 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1845 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1846 | // often anticipate pending UI changes when typing on a keyboard. |
| 1847 | // To obtain this behavior, we must serialize key events with respect to all |
| 1848 | // prior input events. |
| 1849 | if (entry.type == EventEntry::Type::KEY) { |
| 1850 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1851 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1852 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1853 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1857 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1858 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1859 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1860 | |
| 1861 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1862 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1865 | /** |
| 1866 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1867 | * that are currently unresponsive. |
| 1868 | */ |
| 1869 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1870 | const std::vector<TouchedMonitor>& monitors) const { |
| 1871 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1872 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1873 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1874 | sp<Connection> connection = getConnectionLocked( |
| 1875 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1876 | if (connection == nullptr) { |
| 1877 | ALOGE("Could not find connection for monitor %s", |
| 1878 | monitor.monitor.inputChannel->getName().c_str()); |
| 1879 | return false; |
| 1880 | } |
| 1881 | if (!connection->responsive) { |
| 1882 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1883 | connection->inputChannel->getName().c_str()); |
| 1884 | return false; |
| 1885 | } |
| 1886 | return true; |
| 1887 | }); |
| 1888 | return responsiveMonitors; |
| 1889 | } |
| 1890 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1891 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1892 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1893 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1894 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1895 | enum InjectionPermission { |
| 1896 | INJECTION_PERMISSION_UNKNOWN, |
| 1897 | INJECTION_PERMISSION_GRANTED, |
| 1898 | INJECTION_PERMISSION_DENIED |
| 1899 | }; |
| 1900 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1901 | // For security reasons, we defer updating the touch state until we are sure that |
| 1902 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1903 | int32_t displayId = entry.displayId; |
| 1904 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1905 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1906 | |
| 1907 | // 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] | 1908 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1909 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1910 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1911 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1912 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1913 | // Copy current touch state into tempTouchState. |
| 1914 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1915 | // 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] | 1916 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1917 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1918 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1919 | mTouchStatesByDisplay.find(displayId); |
| 1920 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1921 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1922 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1923 | } |
| 1924 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1925 | bool isSplit = tempTouchState.split; |
| 1926 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1927 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1928 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1929 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1930 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1931 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1932 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1933 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1934 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | bool wrongDevice = false; |
| 1936 | if (newGesture) { |
| 1937 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1938 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1939 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1940 | "in display %" PRId32, |
| 1941 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1942 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1943 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1944 | switchedDevice = false; |
| 1945 | wrongDevice = true; |
| 1946 | goto Failed; |
| 1947 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1948 | tempTouchState.reset(); |
| 1949 | tempTouchState.down = down; |
| 1950 | tempTouchState.deviceId = entry.deviceId; |
| 1951 | tempTouchState.source = entry.source; |
| 1952 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1953 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1954 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1955 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1956 | "in display %" PRId32, |
| 1957 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1958 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1959 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1960 | switchedDevice = false; |
| 1961 | wrongDevice = true; |
| 1962 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1966 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1967 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1968 | int32_t x; |
| 1969 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1970 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1971 | // Always dispatch mouse events to cursor position. |
| 1972 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1973 | x = int32_t(entry.xCursorPosition); |
| 1974 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1975 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1976 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1977 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1978 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1979 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1980 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1981 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1982 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1983 | |
| 1984 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1985 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1986 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1988 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1989 | if (newTouchedWindowHandle != nullptr && |
| 1990 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1991 | // New window supports splitting, but we should never split mouse events. |
| 1992 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1993 | } else if (isSplit) { |
| 1994 | // New window does not support splitting but we have already split events. |
| 1995 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1996 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2000 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | // 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] | 2002 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2005 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2006 | ALOGI("Not sending touch event to %s because it is paused", |
| 2007 | newTouchedWindowHandle->getName().c_str()); |
| 2008 | newTouchedWindowHandle = nullptr; |
| 2009 | } |
| 2010 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2011 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2012 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2013 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2014 | if (!isResponsive) { |
| 2015 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2016 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2017 | newTouchedWindowHandle = nullptr; |
| 2018 | } |
| 2019 | } |
| 2020 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2021 | // Drop events that can't be trusted due to occlusion |
| 2022 | if (newTouchedWindowHandle != nullptr && |
| 2023 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2024 | TouchOcclusionInfo occlusionInfo = |
| 2025 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2026 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2027 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2028 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2029 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2030 | ALOGD("%s", log.c_str()); |
| 2031 | } |
| 2032 | } |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2033 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 2034 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2035 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2036 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2037 | newTouchedWindowHandle = nullptr; |
| 2038 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2042 | // Also don't send the new touch event to unresponsive gesture monitors |
| 2043 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 2044 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2045 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2046 | 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] | 2047 | "(%d, %d) in display %" PRId32 ".", |
| 2048 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2049 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2050 | goto Failed; |
| 2051 | } |
| 2052 | |
| 2053 | if (newTouchedWindowHandle != nullptr) { |
| 2054 | // Set target flags. |
| 2055 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2056 | if (isSplit) { |
| 2057 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2058 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2059 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2060 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2061 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2062 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2063 | } |
| 2064 | |
| 2065 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2066 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2067 | newHoverWindowHandle = nullptr; |
| 2068 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2069 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | // Update the temporary touch state. |
| 2073 | BitSet32 pointerIds; |
| 2074 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2075 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2076 | pointerIds.markBit(pointerId); |
| 2077 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2078 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2081 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2082 | } else { |
| 2083 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2084 | |
| 2085 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2086 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2087 | if (DEBUG_FOCUS) { |
| 2088 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2089 | "dropped the pointer down event in display %" PRId32, |
| 2090 | displayId); |
| 2091 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2092 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2093 | goto Failed; |
| 2094 | } |
| 2095 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2096 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2097 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2098 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2099 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2100 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2101 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2102 | 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] | 2103 | |
| 2104 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2105 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2106 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2107 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2108 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2109 | if (DEBUG_FOCUS) { |
| 2110 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2111 | oldTouchedWindowHandle->getName().c_str(), |
| 2112 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2113 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2114 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2115 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2116 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2117 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2118 | |
| 2119 | // Make a slippery entrance into the new window. |
| 2120 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2121 | isSplit = true; |
| 2122 | } |
| 2123 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2124 | int32_t targetFlags = |
| 2125 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2126 | if (isSplit) { |
| 2127 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2128 | } |
| 2129 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2130 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2131 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2132 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | BitSet32 pointerIds; |
| 2136 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2137 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2138 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2139 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2145 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2146 | // when dispatching it as is to newTouchedWindowHandle. |
| 2147 | if (mLastHoverWindowHandle != nullptr && |
| 2148 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2149 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 | #if DEBUG_HOVER |
| 2151 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2152 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2153 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2154 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2155 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2158 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2159 | // when dispatching it as is to newTouchedWindowHandle. |
| 2160 | if (newHoverWindowHandle != nullptr && |
| 2161 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2162 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2163 | #if DEBUG_HOVER |
| 2164 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2165 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2166 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2167 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2168 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2169 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | // Check permission to inject into all touched foreground windows and ensure there |
| 2174 | // is at least one touched foreground window. |
| 2175 | { |
| 2176 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2177 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2178 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2179 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2180 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2181 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2182 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2183 | goto Failed; |
| 2184 | } |
| 2185 | } |
| 2186 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2187 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2188 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2189 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2190 | "%" PRId32 " or gesture monitor to receive it.", |
| 2191 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2192 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | goto Failed; |
| 2194 | } |
| 2195 | |
| 2196 | // Permission granted to injection into all touched foreground windows. |
| 2197 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2198 | } |
| 2199 | |
| 2200 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2201 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2202 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2203 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2204 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2205 | if (foregroundWindowHandle) { |
| 2206 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2207 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2208 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2209 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 2210 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2211 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 2212 | InputTarget::FLAG_ZERO_COORDS, |
| 2213 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2214 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2220 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2221 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2222 | // of the touch gesture. |
| 2223 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2224 | // engine only supports touch events. We would need to add a mechanism similar |
| 2225 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2226 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2227 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2228 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2229 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2230 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2231 | getWindowHandlesLocked(displayId); |
| 2232 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2233 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2234 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2235 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2236 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2237 | .addOrUpdateWindow(windowHandle, |
| 2238 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2239 | InputTarget:: |
| 2240 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2241 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2242 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2249 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2250 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2251 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2253 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2254 | } |
| 2255 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2256 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2257 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2258 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2261 | // Drop the outside or hover touch windows since we will not care about them |
| 2262 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2263 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 | |
| 2265 | Failed: |
| 2266 | // Check injection permission once and for all. |
| 2267 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2268 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2270 | } else { |
| 2271 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2272 | } |
| 2273 | } |
| 2274 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2275 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2276 | return injectionResult; |
| 2277 | } |
| 2278 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2279 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2280 | if (!wrongDevice) { |
| 2281 | if (switchedDevice) { |
| 2282 | if (DEBUG_FOCUS) { |
| 2283 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2284 | } |
| 2285 | *outConflictingPointerActions = true; |
| 2286 | } |
| 2287 | |
| 2288 | if (isHoverAction) { |
| 2289 | // Started hovering, therefore no longer down. |
| 2290 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2291 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2292 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2293 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2294 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2295 | *outConflictingPointerActions = true; |
| 2296 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2297 | tempTouchState.reset(); |
| 2298 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2299 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2300 | tempTouchState.deviceId = entry.deviceId; |
| 2301 | tempTouchState.source = entry.source; |
| 2302 | tempTouchState.displayId = displayId; |
| 2303 | } |
| 2304 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2305 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2306 | // All pointers up or canceled. |
| 2307 | tempTouchState.reset(); |
| 2308 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2309 | // First pointer went down. |
| 2310 | if (oldState && oldState->down) { |
| 2311 | if (DEBUG_FOCUS) { |
| 2312 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2313 | } |
| 2314 | *outConflictingPointerActions = true; |
| 2315 | } |
| 2316 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2317 | // One pointer went up. |
| 2318 | if (isSplit) { |
| 2319 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2320 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2321 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2322 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2323 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2324 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2325 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2326 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2327 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2328 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2329 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2330 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2331 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2333 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2334 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2335 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2336 | // Save changes unless the action was scroll in which case the temporary touch |
| 2337 | // state was only valid for this one action. |
| 2338 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2339 | if (tempTouchState.displayId >= 0) { |
| 2340 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2341 | } else { |
| 2342 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2343 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2344 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2345 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2346 | // Update hover state. |
| 2347 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2350 | return injectionResult; |
| 2351 | } |
| 2352 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2353 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
| 2354 | const sp<InputWindowHandle> dropWindow = |
| 2355 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
| 2356 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2357 | true /*ignoreDragWindow*/); |
| 2358 | if (dropWindow) { |
| 2359 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
| 2360 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2361 | } else { |
| 2362 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2363 | } |
| 2364 | mDragState.reset(); |
| 2365 | } |
| 2366 | |
| 2367 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2368 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2369 | return; |
| 2370 | } |
| 2371 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2372 | if (!mDragState->isStartDrag) { |
| 2373 | mDragState->isStartDrag = true; |
| 2374 | mDragState->isStylusButtonDownAtStart = |
| 2375 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2376 | } |
| 2377 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2378 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2379 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2380 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2381 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2382 | // Handle the special case : stylus button no longer pressed. |
| 2383 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2384 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2385 | finishDragAndDrop(entry.displayId, x, y); |
| 2386 | return; |
| 2387 | } |
| 2388 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2389 | const sp<InputWindowHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2390 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2391 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2392 | true /*ignoreDragWindow*/); |
| 2393 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2394 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2395 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2396 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2397 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2398 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2399 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2400 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2401 | } |
| 2402 | // enqueue drag location if needed. |
| 2403 | if (hoverWindowHandle != nullptr) { |
| 2404 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2405 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2406 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2407 | finishDragAndDrop(entry.displayId, x, y); |
| 2408 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2409 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2410 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2414 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2415 | int32_t targetFlags, BitSet32 pointerIds, |
| 2416 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2417 | std::vector<InputTarget>::iterator it = |
| 2418 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2419 | [&windowHandle](const InputTarget& inputTarget) { |
| 2420 | return inputTarget.inputChannel->getConnectionToken() == |
| 2421 | windowHandle->getToken(); |
| 2422 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2423 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2424 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2425 | |
| 2426 | if (it == inputTargets.end()) { |
| 2427 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2428 | std::shared_ptr<InputChannel> inputChannel = |
| 2429 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2430 | if (inputChannel == nullptr) { |
| 2431 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2432 | return; |
| 2433 | } |
| 2434 | inputTarget.inputChannel = inputChannel; |
| 2435 | inputTarget.flags = targetFlags; |
| 2436 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2437 | inputTarget.displaySize = |
| 2438 | vec2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2439 | inputTargets.push_back(inputTarget); |
| 2440 | it = inputTargets.end() - 1; |
| 2441 | } |
| 2442 | |
| 2443 | ALOG_ASSERT(it->flags == targetFlags); |
| 2444 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2445 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2446 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2447 | } |
| 2448 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2449 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2450 | int32_t displayId, float xOffset, |
| 2451 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2452 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2453 | mGlobalMonitorsByDisplay.find(displayId); |
| 2454 | |
| 2455 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2456 | const std::vector<Monitor>& monitors = it->second; |
| 2457 | for (const Monitor& monitor : monitors) { |
| 2458 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2459 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2463 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2464 | float yOffset, |
| 2465 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2466 | InputTarget target; |
| 2467 | target.inputChannel = monitor.inputChannel; |
| 2468 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2469 | ui::Transform t; |
| 2470 | t.set(xOffset, yOffset); |
| 2471 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2472 | inputTargets.push_back(target); |
| 2473 | } |
| 2474 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2475 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2476 | const InjectionState* injectionState) { |
| 2477 | if (injectionState && |
| 2478 | (windowHandle == nullptr || |
| 2479 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2480 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2481 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2482 | 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] | 2483 | "owned by uid %d", |
| 2484 | injectionState->injectorPid, injectionState->injectorUid, |
| 2485 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2486 | } else { |
| 2487 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2488 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2489 | } |
| 2490 | return false; |
| 2491 | } |
| 2492 | return true; |
| 2493 | } |
| 2494 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2495 | /** |
| 2496 | * Indicate whether one window handle should be considered as obscuring |
| 2497 | * another window handle. We only check a few preconditions. Actually |
| 2498 | * checking the bounds is left to the caller. |
| 2499 | */ |
| 2500 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2501 | const sp<InputWindowHandle>& otherHandle) { |
| 2502 | // Compare by token so cloned layers aren't counted |
| 2503 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2504 | return false; |
| 2505 | } |
| 2506 | auto info = windowHandle->getInfo(); |
| 2507 | auto otherInfo = otherHandle->getInfo(); |
| 2508 | if (!otherInfo->visible) { |
| 2509 | return false; |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2510 | } else if (otherInfo->alpha == 0 && |
| 2511 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 2512 | // Those act as if they were invisible, so we don't need to flag them. |
| 2513 | // We do want to potentially flag touchable windows even if they have 0 |
| 2514 | // opacity, since they can consume touches and alter the effects of the |
| 2515 | // user interaction (eg. apps that rely on |
| 2516 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2517 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2518 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2519 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2520 | // If ownerUid is the same we don't generate occlusion events as there |
| 2521 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2522 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2523 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2524 | return false; |
| 2525 | } else if (otherInfo->displayId != info->displayId) { |
| 2526 | return false; |
| 2527 | } |
| 2528 | return true; |
| 2529 | } |
| 2530 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2531 | /** |
| 2532 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2533 | * untrusted, one should check: |
| 2534 | * |
| 2535 | * 1. If result.hasBlockingOcclusion is true. |
| 2536 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2537 | * BLOCK_UNTRUSTED. |
| 2538 | * |
| 2539 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2540 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2541 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2542 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2543 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2544 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2545 | * |
| 2546 | * If neither of those is true, then it means the touch can be allowed. |
| 2547 | */ |
| 2548 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
| 2549 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2550 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2551 | int32_t displayId = windowInfo->displayId; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2552 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2553 | TouchOcclusionInfo info; |
| 2554 | info.hasBlockingOcclusion = false; |
| 2555 | info.obscuringOpacity = 0; |
| 2556 | info.obscuringUid = -1; |
| 2557 | std::map<int32_t, float> opacityByUid; |
| 2558 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| 2559 | if (windowHandle == otherHandle) { |
| 2560 | break; // All future windows are below us. Exit early. |
| 2561 | } |
| 2562 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2563 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2564 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2565 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2566 | info.debugInfo.push_back( |
| 2567 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2568 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2569 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2570 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2571 | // window's touch occlusion mode |
| 2572 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2573 | info.hasBlockingOcclusion = true; |
| 2574 | info.obscuringUid = otherInfo->ownerUid; |
| 2575 | info.obscuringPackage = otherInfo->packageName; |
| 2576 | break; |
| 2577 | } |
| 2578 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2579 | uint32_t uid = otherInfo->ownerUid; |
| 2580 | float opacity = |
| 2581 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2582 | // Given windows A and B: |
| 2583 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2584 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2585 | opacityByUid[uid] = opacity; |
| 2586 | if (opacity > info.obscuringOpacity) { |
| 2587 | info.obscuringOpacity = opacity; |
| 2588 | info.obscuringUid = uid; |
| 2589 | info.obscuringPackage = otherInfo->packageName; |
| 2590 | } |
| 2591 | } |
| 2592 | } |
| 2593 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2594 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2595 | info.debugInfo.push_back( |
| 2596 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2597 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2598 | return info; |
| 2599 | } |
| 2600 | |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2601 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, |
| 2602 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2603 | return StringPrintf(INDENT2 |
| 2604 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2605 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2606 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2607 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2608 | (isTouchedWindow) ? "[TOUCHED] " : "", |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2609 | NamedEnum::string(info->type, "%" PRId32).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2610 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2611 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2612 | info->frameTop, info->frameRight, info->frameBottom, |
| 2613 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2614 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2615 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2616 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2619 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2620 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2621 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2622 | occlusionInfo.obscuringUid); |
| 2623 | return false; |
| 2624 | } |
| 2625 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2626 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2627 | "%.2f, maximum allowed = %.2f)", |
| 2628 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2629 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2630 | return false; |
| 2631 | } |
| 2632 | return true; |
| 2633 | } |
| 2634 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2635 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2636 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2637 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2638 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2639 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2640 | if (windowHandle == otherHandle) { |
| 2641 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2643 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2644 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2645 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2646 | return true; |
| 2647 | } |
| 2648 | } |
| 2649 | return false; |
| 2650 | } |
| 2651 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2652 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2653 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2654 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2655 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2656 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2657 | if (windowHandle == otherHandle) { |
| 2658 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2659 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2660 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2661 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2662 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2663 | return true; |
| 2664 | } |
| 2665 | } |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2669 | std::string InputDispatcher::getApplicationWindowLabel( |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2670 | const InputApplicationHandle* applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2671 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2672 | if (applicationHandle != nullptr) { |
| 2673 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2674 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2675 | } else { |
| 2676 | return applicationHandle->getName(); |
| 2677 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2678 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2679 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2680 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2681 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2682 | } |
| 2683 | } |
| 2684 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2685 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2686 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2687 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2688 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2689 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2690 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2691 | return; |
| 2692 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2693 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2694 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2695 | if (focusedWindowHandle != nullptr) { |
| 2696 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2697 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2698 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2699 | 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] | 2700 | #endif |
| 2701 | return; |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2706 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2707 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2708 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2709 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2710 | return; |
| 2711 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2712 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2713 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2714 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2715 | } |
| 2716 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2718 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2719 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2720 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2721 | return; |
| 2722 | } |
| 2723 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2724 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2725 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2726 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2727 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2728 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2729 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2730 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2731 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2732 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2733 | NamedEnum::string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2734 | break; |
| 2735 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2736 | } |
| 2737 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2738 | std::unique_ptr<CommandEntry> commandEntry = |
| 2739 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2740 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2741 | commandEntry->userActivityEventType = eventType; |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2742 | commandEntry->displayId = displayId; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2743 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2747 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2748 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2749 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2750 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2751 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2752 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2753 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2754 | ATRACE_NAME(message.c_str()); |
| 2755 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2756 | #if DEBUG_DISPATCH_CYCLE |
| 2757 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2758 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2759 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2760 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2761 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | #endif |
| 2763 | |
| 2764 | // Skip this event if the connection status is not normal. |
| 2765 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2766 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2767 | #if DEBUG_DISPATCH_CYCLE |
| 2768 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2769 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2770 | #endif |
| 2771 | return; |
| 2772 | } |
| 2773 | |
| 2774 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2775 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2776 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2777 | "Entry type %s should not have FLAG_SPLIT", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2778 | NamedEnum::string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2779 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2780 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2781 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2782 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2783 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2784 | if (!splitMotionEntry) { |
| 2785 | return; // split event was dropped |
| 2786 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2787 | if (DEBUG_FOCUS) { |
| 2788 | ALOGD("channel '%s' ~ Split motion event.", |
| 2789 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2790 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2791 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2792 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2793 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2794 | return; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2799 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2800 | } |
| 2801 | |
| 2802 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2803 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2804 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2805 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2806 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2807 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2808 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2809 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2810 | ATRACE_NAME(message.c_str()); |
| 2811 | } |
| 2812 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2813 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2814 | |
| 2815 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2816 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2817 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2818 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2819 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2820 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2821 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2822 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2824 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2825 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2826 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2827 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2828 | |
| 2829 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2830 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | startDispatchCycleLocked(currentTime, connection); |
| 2832 | } |
| 2833 | } |
| 2834 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2835 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2836 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2837 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2838 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2839 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2840 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2841 | connection->getInputChannelName().c_str(), |
| 2842 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2843 | ATRACE_NAME(message.c_str()); |
| 2844 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2845 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2846 | if (!(inputTargetFlags & dispatchMode)) { |
| 2847 | return; |
| 2848 | } |
| 2849 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2850 | |
| 2851 | // This is a new event. |
| 2852 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2853 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2854 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2855 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2856 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2857 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2858 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2859 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2860 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2861 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2862 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2863 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2864 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2865 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2866 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2867 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2868 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2869 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2870 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2871 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2872 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2873 | return; // skip the inconsistent event |
| 2874 | } |
| 2875 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2876 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2877 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2878 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2879 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2880 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2881 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2882 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2883 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2884 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2885 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2886 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2887 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2888 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2889 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2890 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2891 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2892 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2893 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2894 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2895 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2896 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2897 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2898 | } |
| 2899 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2900 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2901 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2902 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2903 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2904 | "event", |
| 2905 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2906 | #endif |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 2907 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2908 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2909 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2910 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2911 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2912 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2913 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2914 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2915 | } |
| 2916 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2917 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2918 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2919 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2920 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2921 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2922 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2923 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2924 | "event", |
| 2925 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2926 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2927 | return; // skip the inconsistent event |
| 2928 | } |
| 2929 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2930 | dispatchEntry->resolvedEventId = |
| 2931 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2932 | ? mIdGenerator.nextId() |
| 2933 | : motionEntry.id; |
| 2934 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2935 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2936 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2937 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2938 | ATRACE_NAME(message.c_str()); |
| 2939 | } |
| 2940 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2941 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 2942 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 2943 | // Skip reporting pointer down outside focus to the policy. |
| 2944 | break; |
| 2945 | } |
| 2946 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2947 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2948 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2949 | |
| 2950 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2951 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2952 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2953 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2954 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2955 | break; |
| 2956 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2957 | case EventEntry::Type::SENSOR: { |
| 2958 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 2959 | break; |
| 2960 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2961 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2962 | case EventEntry::Type::DEVICE_RESET: { |
| 2963 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2964 | NamedEnum::string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2965 | break; |
| 2966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | // Remember that we are waiting for this dispatch to complete. |
| 2970 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2971 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2975 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 2976 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2977 | } |
| 2978 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2979 | /** |
| 2980 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2981 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2982 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2983 | * We will see both launcher and wallpaper in that list. |
| 2984 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2985 | * until the set of interacted connections changes. |
| 2986 | * |
| 2987 | * The following items are skipped, to reduce the logspam: |
| 2988 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2989 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2990 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2991 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2992 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2993 | */ |
| 2994 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2995 | const std::vector<InputTarget>& targets) { |
| 2996 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2997 | if (entry.type == EventEntry::Type::KEY) { |
| 2998 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2999 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3000 | return; |
| 3001 | } |
| 3002 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3003 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3004 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3005 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3006 | return; |
| 3007 | } |
| 3008 | } else { |
| 3009 | return; // Not a key or a motion |
| 3010 | } |
| 3011 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3012 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3013 | std::vector<sp<Connection>> newConnections; |
| 3014 | for (const InputTarget& target : targets) { |
| 3015 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3016 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3017 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3018 | } |
| 3019 | |
| 3020 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3021 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3022 | if (connection == nullptr) { |
| 3023 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3024 | } |
| 3025 | newConnectionTokens.insert(std::move(token)); |
| 3026 | newConnections.emplace_back(connection); |
| 3027 | } |
| 3028 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3029 | return; // no change |
| 3030 | } |
| 3031 | mInteractionConnectionTokens = newConnectionTokens; |
| 3032 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3033 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3034 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3035 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3036 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3037 | std::string message = "Interaction with: " + targetList; |
| 3038 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3039 | message += "<none>"; |
| 3040 | } |
| 3041 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3042 | } |
| 3043 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3044 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3045 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3046 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3047 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3048 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3049 | return; |
| 3050 | } |
| 3051 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3052 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3053 | if (focusedToken == token) { |
| 3054 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3055 | return; |
| 3056 | } |
| 3057 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3058 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 3059 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3060 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3061 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3065 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3066 | if (ATRACE_ENABLED()) { |
| 3067 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3068 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3069 | ATRACE_NAME(message.c_str()); |
| 3070 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3071 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3072 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3073 | #endif |
| 3074 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3075 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3076 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3077 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3078 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3079 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3080 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3081 | |
| 3082 | // Publish the event. |
| 3083 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3084 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3085 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3086 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3087 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3088 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3089 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3090 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3091 | status = connection->inputPublisher |
| 3092 | .publishKeyEvent(dispatchEntry->seq, |
| 3093 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3094 | keyEntry.source, keyEntry.displayId, |
| 3095 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3096 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3097 | keyEntry.scanCode, keyEntry.metaState, |
| 3098 | keyEntry.repeatCount, keyEntry.downTime, |
| 3099 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3100 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3101 | } |
| 3102 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3103 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3104 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3105 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3106 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3107 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3108 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3109 | // 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] | 3110 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3111 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3112 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3113 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3114 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3115 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3116 | // Don't apply window scale here since we don't want scale to affect raw |
| 3117 | // coordinates. The scale will be sent back to the client and applied |
| 3118 | // later when requesting relative coordinates. |
| 3119 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3120 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3121 | } |
| 3122 | usingCoords = scaledCoords; |
| 3123 | } |
| 3124 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3125 | // We don't want the dispatch target to know. |
| 3126 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3127 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3128 | scaledCoords[i].clear(); |
| 3129 | } |
| 3130 | usingCoords = scaledCoords; |
| 3131 | } |
| 3132 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3133 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3134 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3135 | |
| 3136 | // Publish the motion event. |
| 3137 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3138 | .publishMotionEvent(dispatchEntry->seq, |
| 3139 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3140 | motionEntry.deviceId, motionEntry.source, |
| 3141 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3142 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3143 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3144 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3145 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3146 | motionEntry.buttonState, |
| 3147 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3148 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3149 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3150 | motionEntry.xCursorPosition, |
| 3151 | motionEntry.yCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3152 | dispatchEntry->displaySize.x, |
| 3153 | dispatchEntry->displaySize.y, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3154 | motionEntry.downTime, motionEntry.eventTime, |
| 3155 | motionEntry.pointerCount, |
| 3156 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3157 | break; |
| 3158 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3159 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3160 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3161 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3162 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3163 | focusEntry.id, |
| 3164 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3165 | mInTouchMode); |
| 3166 | break; |
| 3167 | } |
| 3168 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3169 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3170 | const auto& captureEntry = |
| 3171 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3172 | status = connection->inputPublisher |
| 3173 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
| 3174 | captureEntry.pointerCaptureEnabled); |
| 3175 | break; |
| 3176 | } |
| 3177 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3178 | case EventEntry::Type::DRAG: { |
| 3179 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3180 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3181 | dragEntry.id, dragEntry.x, |
| 3182 | dragEntry.y, |
| 3183 | dragEntry.isExiting); |
| 3184 | break; |
| 3185 | } |
| 3186 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3187 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3188 | case EventEntry::Type::DEVICE_RESET: |
| 3189 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3190 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3191 | NamedEnum::string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3192 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | // Check the result. |
| 3197 | if (status) { |
| 3198 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3199 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3200 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3201 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3202 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3203 | "event to it, status=%s(%d)", |
| 3204 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3205 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3206 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3207 | } else { |
| 3208 | // Pipe is full and we are waiting for the app to finish process some events |
| 3209 | // before sending more events to it. |
| 3210 | #if DEBUG_DISPATCH_CYCLE |
| 3211 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3212 | "waiting for the application to catch up", |
| 3213 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3214 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3215 | } |
| 3216 | } else { |
| 3217 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3218 | "status=%s(%d)", |
| 3219 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3220 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3221 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3222 | } |
| 3223 | return; |
| 3224 | } |
| 3225 | |
| 3226 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3227 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3228 | connection->outboundQueue.end(), |
| 3229 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3230 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3231 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3232 | if (connection->responsive) { |
| 3233 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3234 | connection->inputChannel->getConnectionToken()); |
| 3235 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3236 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3237 | } |
| 3238 | } |
| 3239 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3240 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3241 | size_t size; |
| 3242 | switch (event.type) { |
| 3243 | case VerifiedInputEvent::Type::KEY: { |
| 3244 | size = sizeof(VerifiedKeyEvent); |
| 3245 | break; |
| 3246 | } |
| 3247 | case VerifiedInputEvent::Type::MOTION: { |
| 3248 | size = sizeof(VerifiedMotionEvent); |
| 3249 | break; |
| 3250 | } |
| 3251 | } |
| 3252 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3253 | return mHmacKeyManager.sign(start, size); |
| 3254 | } |
| 3255 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3256 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3257 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3258 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3259 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3260 | // Only sign events up and down events as the purely move events |
| 3261 | // are tied to their up/down counterparts so signing would be redundant. |
| 3262 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3263 | verifiedEvent.actionMasked = actionMasked; |
| 3264 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3265 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3266 | } |
| 3267 | return INVALID_HMAC; |
| 3268 | } |
| 3269 | |
| 3270 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3271 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3272 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3273 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3274 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3275 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3276 | } |
| 3277 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3278 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3279 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3280 | bool handled, nsecs_t consumeTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | #if DEBUG_DISPATCH_CYCLE |
| 3282 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3283 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3284 | #endif |
| 3285 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3286 | if (connection->status == Connection::STATUS_BROKEN || |
| 3287 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3288 | return; |
| 3289 | } |
| 3290 | |
| 3291 | // Notify other system components and prepare to start the next dispatch cycle. |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3292 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3296 | const sp<Connection>& connection, |
| 3297 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | #if DEBUG_DISPATCH_CYCLE |
| 3299 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3300 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3301 | #endif |
| 3302 | |
| 3303 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3304 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3305 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3306 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3307 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 | |
| 3309 | // The connection appears to be unrecoverably broken. |
| 3310 | // Ignore already broken or zombie connections. |
| 3311 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3312 | connection->status = Connection::STATUS_BROKEN; |
| 3313 | |
| 3314 | if (notify) { |
| 3315 | // Notify other system components. |
| 3316 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 3317 | } |
| 3318 | } |
| 3319 | } |
| 3320 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3321 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3322 | while (!queue.empty()) { |
| 3323 | DispatchEntry* dispatchEntry = queue.front(); |
| 3324 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3325 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3329 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3330 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3331 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3332 | } |
| 3333 | delete dispatchEntry; |
| 3334 | } |
| 3335 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3336 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3337 | std::scoped_lock _l(mLock); |
| 3338 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3339 | if (connection == nullptr) { |
| 3340 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3341 | connectionToken.get(), events); |
| 3342 | return 0; // remove the callback |
| 3343 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3344 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3345 | bool notify; |
| 3346 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3347 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3348 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3349 | "events=0x%x", |
| 3350 | connection->getInputChannelName().c_str(), events); |
| 3351 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | } |
| 3353 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3354 | nsecs_t currentTime = now(); |
| 3355 | bool gotOne = false; |
| 3356 | status_t status = OK; |
| 3357 | for (;;) { |
| 3358 | Result<InputPublisher::ConsumerResponse> result = |
| 3359 | connection->inputPublisher.receiveConsumerResponse(); |
| 3360 | if (!result.ok()) { |
| 3361 | status = result.error().code(); |
| 3362 | break; |
| 3363 | } |
| 3364 | |
| 3365 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3366 | const InputPublisher::Finished& finish = |
| 3367 | std::get<InputPublisher::Finished>(*result); |
| 3368 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3369 | finish.consumeTime); |
| 3370 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 3371 | if (shouldReportMetricsForConnection(*connection)) { |
| 3372 | const InputPublisher::Timeline& timeline = |
| 3373 | std::get<InputPublisher::Timeline>(*result); |
| 3374 | mLatencyTracker |
| 3375 | .trackGraphicsLatency(timeline.inputEventId, |
| 3376 | connection->inputChannel->getConnectionToken(), |
| 3377 | std::move(timeline.graphicsTimeline)); |
| 3378 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3379 | } |
| 3380 | gotOne = true; |
| 3381 | } |
| 3382 | if (gotOne) { |
| 3383 | runCommandsLockedInterruptible(); |
| 3384 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3385 | return 1; |
| 3386 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3387 | } |
| 3388 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3389 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3390 | if (notify) { |
| 3391 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3392 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3393 | status); |
| 3394 | } |
| 3395 | } else { |
| 3396 | // Monitor channels are never explicitly unregistered. |
| 3397 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3398 | const bool stillHaveWindowHandle = |
| 3399 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3400 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3401 | if (notify) { |
| 3402 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3403 | connection->getInputChannelName().c_str(), events); |
| 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | // Remove the channel. |
| 3408 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3409 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3410 | } |
| 3411 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3412 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3414 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3415 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3416 | } |
| 3417 | } |
| 3418 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3419 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3420 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3421 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3422 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3423 | } |
| 3424 | |
| 3425 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3426 | const CancelationOptions& options, |
| 3427 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3428 | for (const auto& it : monitorsByDisplay) { |
| 3429 | const std::vector<Monitor>& monitors = it.second; |
| 3430 | for (const Monitor& monitor : monitors) { |
| 3431 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3432 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3436 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3437 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3438 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3439 | if (connection == nullptr) { |
| 3440 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3441 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3442 | |
| 3443 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3447 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3448 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3449 | return; |
| 3450 | } |
| 3451 | |
| 3452 | nsecs_t currentTime = now(); |
| 3453 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3454 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3455 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3456 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3457 | if (cancelationEvents.empty()) { |
| 3458 | return; |
| 3459 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3460 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3461 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3462 | "with reality: %s, mode=%d.", |
| 3463 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3464 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3465 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3466 | |
| 3467 | InputTarget target; |
| 3468 | sp<InputWindowHandle> windowHandle = |
| 3469 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3470 | if (windowHandle != nullptr) { |
| 3471 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3472 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3473 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3474 | } |
| 3475 | target.inputChannel = connection->inputChannel; |
| 3476 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3477 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3478 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3479 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3480 | switch (cancelationEventEntry->type) { |
| 3481 | case EventEntry::Type::KEY: { |
| 3482 | logOutboundKeyDetails("cancel - ", |
| 3483 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3484 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3485 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3486 | case EventEntry::Type::MOTION: { |
| 3487 | logOutboundMotionDetails("cancel - ", |
| 3488 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3489 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3490 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3491 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3492 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3493 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3494 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3495 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3496 | break; |
| 3497 | } |
| 3498 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3499 | case EventEntry::Type::DEVICE_RESET: |
| 3500 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3501 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3502 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3503 | break; |
| 3504 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | } |
| 3506 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3507 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3508 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3510 | |
| 3511 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | } |
| 3513 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3514 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3515 | const sp<Connection>& connection) { |
| 3516 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3517 | return; |
| 3518 | } |
| 3519 | |
| 3520 | nsecs_t currentTime = now(); |
| 3521 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3522 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3523 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3524 | |
| 3525 | if (downEvents.empty()) { |
| 3526 | return; |
| 3527 | } |
| 3528 | |
| 3529 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3530 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3531 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3532 | #endif |
| 3533 | |
| 3534 | InputTarget target; |
| 3535 | sp<InputWindowHandle> windowHandle = |
| 3536 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3537 | if (windowHandle != nullptr) { |
| 3538 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3539 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3540 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3541 | } |
| 3542 | target.inputChannel = connection->inputChannel; |
| 3543 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3544 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3545 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3546 | switch (downEventEntry->type) { |
| 3547 | case EventEntry::Type::MOTION: { |
| 3548 | logOutboundMotionDetails("down - ", |
| 3549 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3550 | break; |
| 3551 | } |
| 3552 | |
| 3553 | case EventEntry::Type::KEY: |
| 3554 | case EventEntry::Type::FOCUS: |
| 3555 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3556 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3557 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3558 | case EventEntry::Type::SENSOR: |
| 3559 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3560 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3561 | NamedEnum::string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3562 | break; |
| 3563 | } |
| 3564 | } |
| 3565 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3566 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3567 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | startDispatchCycleLocked(currentTime, connection); |
| 3571 | } |
| 3572 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3573 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3574 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3575 | ALOG_ASSERT(pointerIds.value != 0); |
| 3576 | |
| 3577 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3578 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3579 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3580 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3581 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3582 | uint32_t splitPointerCount = 0; |
| 3583 | |
| 3584 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3585 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3587 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3588 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3589 | if (pointerIds.hasBit(pointerId)) { |
| 3590 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3591 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3592 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3593 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3594 | splitPointerCount += 1; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | if (splitPointerCount != pointerIds.count()) { |
| 3599 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3600 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3601 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3602 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3603 | // in this way. |
| 3604 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3605 | "we expected there to be %d pointers. This probably means we received " |
| 3606 | "a broken sequence of pointer ids from the input device.", |
| 3607 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3608 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3609 | } |
| 3610 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3611 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3612 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3613 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3614 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3615 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3616 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3617 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3618 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3619 | if (pointerIds.hasBit(pointerId)) { |
| 3620 | if (pointerIds.count() == 1) { |
| 3621 | // The first/last pointer went down/up. |
| 3622 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3623 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3624 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3625 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3626 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3627 | } else { |
| 3628 | // A secondary pointer went down/up. |
| 3629 | uint32_t splitPointerIndex = 0; |
| 3630 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3631 | splitPointerIndex += 1; |
| 3632 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3633 | action = maskedAction | |
| 3634 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | } |
| 3636 | } else { |
| 3637 | // An unrelated pointer changed. |
| 3638 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3639 | } |
| 3640 | } |
| 3641 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3642 | int32_t newId = mIdGenerator.nextId(); |
| 3643 | if (ATRACE_ENABLED()) { |
| 3644 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3645 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3646 | originalMotionEntry.id, newId); |
| 3647 | ATRACE_NAME(message.c_str()); |
| 3648 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3649 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3650 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3651 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3652 | originalMotionEntry.displayId, |
| 3653 | originalMotionEntry.policyFlags, action, |
| 3654 | originalMotionEntry.actionButton, |
| 3655 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3656 | originalMotionEntry.buttonState, |
| 3657 | originalMotionEntry.classification, |
| 3658 | originalMotionEntry.edgeFlags, |
| 3659 | originalMotionEntry.xPrecision, |
| 3660 | originalMotionEntry.yPrecision, |
| 3661 | originalMotionEntry.xCursorPosition, |
| 3662 | originalMotionEntry.yCursorPosition, |
| 3663 | originalMotionEntry.downTime, splitPointerCount, |
| 3664 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3665 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3666 | if (originalMotionEntry.injectionState) { |
| 3667 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3668 | splitMotionEntry->injectionState->refCount += 1; |
| 3669 | } |
| 3670 | |
| 3671 | return splitMotionEntry; |
| 3672 | } |
| 3673 | |
| 3674 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3675 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3676 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | #endif |
| 3678 | |
| 3679 | bool needWake; |
| 3680 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3681 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3682 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3683 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3684 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3685 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3686 | } // release lock |
| 3687 | |
| 3688 | if (needWake) { |
| 3689 | mLooper->wake(); |
| 3690 | } |
| 3691 | } |
| 3692 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3693 | /** |
| 3694 | * If one of the meta shortcuts is detected, process them here: |
| 3695 | * Meta + Backspace -> generate BACK |
| 3696 | * Meta + Enter -> generate HOME |
| 3697 | * This will potentially overwrite keyCode and metaState. |
| 3698 | */ |
| 3699 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3700 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3701 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3702 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3703 | if (keyCode == AKEYCODE_DEL) { |
| 3704 | newKeyCode = AKEYCODE_BACK; |
| 3705 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3706 | newKeyCode = AKEYCODE_HOME; |
| 3707 | } |
| 3708 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3709 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3710 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3711 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3712 | keyCode = newKeyCode; |
| 3713 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3714 | } |
| 3715 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3716 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3717 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3718 | // 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] | 3719 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3720 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3721 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3722 | if (replacementIt != mReplacedKeys.end()) { |
| 3723 | keyCode = replacementIt->second; |
| 3724 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3725 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3726 | } |
| 3727 | } |
| 3728 | } |
| 3729 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3731 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3732 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3733 | "policyFlags=0x%x, action=0x%x, " |
| 3734 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3735 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3736 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3737 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3738 | #endif |
| 3739 | if (!validateKeyEvent(args->action)) { |
| 3740 | return; |
| 3741 | } |
| 3742 | |
| 3743 | uint32_t policyFlags = args->policyFlags; |
| 3744 | int32_t flags = args->flags; |
| 3745 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3746 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3747 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3748 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3749 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3750 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3751 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3752 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3753 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3754 | metaState |= AMETA_FUNCTION_ON; |
| 3755 | } |
| 3756 | |
| 3757 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3758 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3759 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3760 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3761 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3762 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3763 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3764 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3765 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3766 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3767 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3768 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3769 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3770 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3771 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3772 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | bool needWake; |
| 3775 | { // acquire lock |
| 3776 | mLock.lock(); |
| 3777 | |
| 3778 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3779 | mLock.unlock(); |
| 3780 | |
| 3781 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3782 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3783 | return; // event was consumed by the filter |
| 3784 | } |
| 3785 | |
| 3786 | mLock.lock(); |
| 3787 | } |
| 3788 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3789 | std::unique_ptr<KeyEntry> newEntry = |
| 3790 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3791 | args->displayId, policyFlags, args->action, flags, |
| 3792 | keyCode, args->scanCode, metaState, repeatCount, |
| 3793 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3794 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3795 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | mLock.unlock(); |
| 3797 | } // release lock |
| 3798 | |
| 3799 | if (needWake) { |
| 3800 | mLooper->wake(); |
| 3801 | } |
| 3802 | } |
| 3803 | |
| 3804 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3805 | return mInputFilterEnabled; |
| 3806 | } |
| 3807 | |
| 3808 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3809 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3810 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3811 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3812 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3813 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3814 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3815 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3816 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3817 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3818 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3819 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3820 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3821 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3822 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3823 | "orientation=%f", |
| 3824 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3825 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3826 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3827 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3828 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3829 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3830 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3831 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3832 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3833 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | } |
| 3835 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3836 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3837 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3838 | return; |
| 3839 | } |
| 3840 | |
| 3841 | uint32_t policyFlags = args->policyFlags; |
| 3842 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3843 | |
| 3844 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3845 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3846 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3847 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3848 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3849 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3850 | |
| 3851 | bool needWake; |
| 3852 | { // acquire lock |
| 3853 | mLock.lock(); |
| 3854 | |
| 3855 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3856 | mLock.unlock(); |
| 3857 | |
| 3858 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3859 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3860 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3861 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3862 | args->metaState, args->buttonState, args->classification, transform, |
| 3863 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3864 | args->yCursorPosition, AMOTION_EVENT_INVALID_DISPLAY_SIZE, |
| 3865 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3866 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3867 | |
| 3868 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3869 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3870 | return; // event was consumed by the filter |
| 3871 | } |
| 3872 | |
| 3873 | mLock.lock(); |
| 3874 | } |
| 3875 | |
| 3876 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3877 | std::unique_ptr<MotionEntry> newEntry = |
| 3878 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3879 | args->source, args->displayId, policyFlags, |
| 3880 | args->action, args->actionButton, args->flags, |
| 3881 | args->metaState, args->buttonState, |
| 3882 | args->classification, args->edgeFlags, |
| 3883 | args->xPrecision, args->yPrecision, |
| 3884 | args->xCursorPosition, args->yCursorPosition, |
| 3885 | args->downTime, args->pointerCount, |
| 3886 | args->pointerProperties, args->pointerCoords, 0, 0); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 3887 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 3888 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 3889 | !mInputFilterEnabled) { |
| 3890 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 3891 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 3892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3894 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3895 | mLock.unlock(); |
| 3896 | } // release lock |
| 3897 | |
| 3898 | if (needWake) { |
| 3899 | mLooper->wake(); |
| 3900 | } |
| 3901 | } |
| 3902 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3903 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
| 3904 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3905 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3906 | " sensorType=%s", |
| 3907 | args->id, args->eventTime, args->deviceId, args->source, |
| 3908 | NamedEnum::string(args->sensorType).c_str()); |
| 3909 | #endif |
| 3910 | |
| 3911 | bool needWake; |
| 3912 | { // acquire lock |
| 3913 | mLock.lock(); |
| 3914 | |
| 3915 | // Just enqueue a new sensor event. |
| 3916 | std::unique_ptr<SensorEntry> newEntry = |
| 3917 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 3918 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 3919 | args->sensorType, args->accuracy, |
| 3920 | args->accuracyChanged, args->values); |
| 3921 | |
| 3922 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 3923 | mLock.unlock(); |
| 3924 | } // release lock |
| 3925 | |
| 3926 | if (needWake) { |
| 3927 | mLooper->wake(); |
| 3928 | } |
| 3929 | } |
| 3930 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3931 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
| 3932 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3933 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 3934 | args->deviceId, args->isOn); |
| 3935 | #endif |
| 3936 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 3937 | } |
| 3938 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3939 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3940 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3944 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3945 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3946 | "switchMask=0x%08x", |
| 3947 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3948 | #endif |
| 3949 | |
| 3950 | uint32_t policyFlags = args->policyFlags; |
| 3951 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3952 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3956 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3957 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3958 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | #endif |
| 3960 | |
| 3961 | bool needWake; |
| 3962 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3963 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3965 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 3966 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 3967 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | } // release lock |
| 3969 | |
| 3970 | if (needWake) { |
| 3971 | mLooper->wake(); |
| 3972 | } |
| 3973 | } |
| 3974 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3975 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
| 3976 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3977 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
| 3978 | args->enabled ? "true" : "false"); |
| 3979 | #endif |
| 3980 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3981 | bool needWake; |
| 3982 | { // acquire lock |
| 3983 | std::scoped_lock _l(mLock); |
| 3984 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
| 3985 | args->enabled); |
| 3986 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 3987 | } // release lock |
| 3988 | |
| 3989 | if (needWake) { |
| 3990 | mLooper->wake(); |
| 3991 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3992 | } |
| 3993 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3994 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 3995 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 3996 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3997 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3998 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3999 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4000 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4002 | 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] | 4003 | |
| 4004 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4005 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4006 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4007 | } |
| 4008 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4009 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4010 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4011 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4012 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4013 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4014 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4015 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4016 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4017 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4018 | int32_t flags = incomingKey.getFlags(); |
| 4019 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4020 | int32_t metaState = incomingKey.getMetaState(); |
| 4021 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4022 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4023 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 4024 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4025 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4026 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4027 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4028 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4029 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4030 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4031 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4032 | |
| 4033 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4034 | android::base::Timer t; |
| 4035 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4036 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4037 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4038 | std::to_string(t.duration().count()).c_str()); |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4043 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4044 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
| 4045 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
| 4046 | incomingKey.getDisplayId(), policyFlags, action, |
| 4047 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4048 | incomingKey.getRepeatCount(), |
| 4049 | incomingKey.getDownTime()); |
| 4050 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4051 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 | } |
| 4053 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4054 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4055 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 4056 | int32_t action = motionEvent->getAction(); |
| 4057 | size_t pointerCount = motionEvent->getPointerCount(); |
| 4058 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 4059 | int32_t actionButton = motionEvent->getActionButton(); |
| 4060 | int32_t displayId = motionEvent->getDisplayId(); |
| 4061 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4062 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4066 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 4067 | android::base::Timer t; |
| 4068 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4069 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4070 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4071 | std::to_string(t.duration().count()).c_str()); |
| 4072 | } |
| 4073 | } |
| 4074 | |
| 4075 | mLock.lock(); |
| 4076 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 4077 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4078 | std::unique_ptr<MotionEntry> injectedEntry = |
| 4079 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, |
| 4080 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
| 4081 | motionEvent->getDisplayId(), policyFlags, action, |
| 4082 | actionButton, motionEvent->getFlags(), |
| 4083 | motionEvent->getMetaState(), |
| 4084 | motionEvent->getButtonState(), |
| 4085 | motionEvent->getClassification(), |
| 4086 | motionEvent->getEdgeFlags(), |
| 4087 | motionEvent->getXPrecision(), |
| 4088 | motionEvent->getYPrecision(), |
| 4089 | motionEvent->getRawXCursorPosition(), |
| 4090 | motionEvent->getRawYCursorPosition(), |
| 4091 | motionEvent->getDownTime(), |
| 4092 | uint32_t(pointerCount), pointerProperties, |
| 4093 | samplePointerCoords, motionEvent->getXOffset(), |
| 4094 | motionEvent->getYOffset()); |
| 4095 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4096 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 4097 | sampleEventTimes += 1; |
| 4098 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4099 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
| 4100 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, |
| 4101 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
| 4102 | motionEvent->getDisplayId(), policyFlags, |
| 4103 | action, actionButton, motionEvent->getFlags(), |
| 4104 | motionEvent->getMetaState(), |
| 4105 | motionEvent->getButtonState(), |
| 4106 | motionEvent->getClassification(), |
| 4107 | motionEvent->getEdgeFlags(), |
| 4108 | motionEvent->getXPrecision(), |
| 4109 | motionEvent->getYPrecision(), |
| 4110 | motionEvent->getRawXCursorPosition(), |
| 4111 | motionEvent->getRawYCursorPosition(), |
| 4112 | motionEvent->getDownTime(), |
| 4113 | uint32_t(pointerCount), pointerProperties, |
| 4114 | samplePointerCoords, |
| 4115 | motionEvent->getXOffset(), |
| 4116 | motionEvent->getYOffset()); |
| 4117 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4118 | } |
| 4119 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4122 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4123 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4124 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4128 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4129 | injectionState->injectionIsAsync = true; |
| 4130 | } |
| 4131 | |
| 4132 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4133 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4134 | |
| 4135 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4136 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4137 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4138 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | mLock.unlock(); |
| 4142 | |
| 4143 | if (needWake) { |
| 4144 | mLooper->wake(); |
| 4145 | } |
| 4146 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4147 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4148 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4149 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4150 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4151 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4152 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | } else { |
| 4154 | for (;;) { |
| 4155 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4156 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | break; |
| 4158 | } |
| 4159 | |
| 4160 | nsecs_t remainingTimeout = endTime - now(); |
| 4161 | if (remainingTimeout <= 0) { |
| 4162 | #if DEBUG_INJECTION |
| 4163 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4164 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4165 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4166 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 | break; |
| 4168 | } |
| 4169 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4170 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4171 | } |
| 4172 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4173 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4174 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4175 | while (injectionState->pendingForegroundDispatches != 0) { |
| 4176 | #if DEBUG_INJECTION |
| 4177 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4178 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4179 | #endif |
| 4180 | nsecs_t remainingTimeout = endTime - now(); |
| 4181 | if (remainingTimeout <= 0) { |
| 4182 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4183 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4184 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4185 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4186 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | break; |
| 4188 | } |
| 4189 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4190 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | } |
| 4192 | } |
| 4193 | } |
| 4194 | |
| 4195 | injectionState->release(); |
| 4196 | } // release lock |
| 4197 | |
| 4198 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4199 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4200 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4201 | #endif |
| 4202 | |
| 4203 | return injectionResult; |
| 4204 | } |
| 4205 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4206 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4207 | std::array<uint8_t, 32> calculatedHmac; |
| 4208 | std::unique_ptr<VerifiedInputEvent> result; |
| 4209 | switch (event.getType()) { |
| 4210 | case AINPUT_EVENT_TYPE_KEY: { |
| 4211 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4212 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4213 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4214 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4215 | break; |
| 4216 | } |
| 4217 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4218 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4219 | VerifiedMotionEvent verifiedMotionEvent = |
| 4220 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4221 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4222 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4223 | break; |
| 4224 | } |
| 4225 | default: { |
| 4226 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4227 | return nullptr; |
| 4228 | } |
| 4229 | } |
| 4230 | if (calculatedHmac == INVALID_HMAC) { |
| 4231 | return nullptr; |
| 4232 | } |
| 4233 | if (calculatedHmac != event.getHmac()) { |
| 4234 | return nullptr; |
| 4235 | } |
| 4236 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4237 | } |
| 4238 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4239 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4240 | return injectorUid == 0 || |
| 4241 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | } |
| 4243 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4244 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4245 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4246 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4247 | if (injectionState) { |
| 4248 | #if DEBUG_INJECTION |
| 4249 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4250 | "injectorPid=%d, injectorUid=%d", |
| 4251 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4252 | #endif |
| 4253 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4254 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4255 | // Log the outcome since the injector did not wait for the injection result. |
| 4256 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4257 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4258 | ALOGV("Asynchronous input event injection succeeded."); |
| 4259 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4260 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4261 | ALOGW("Asynchronous input event injection failed."); |
| 4262 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4263 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4264 | ALOGW("Asynchronous input event injection permission denied."); |
| 4265 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4266 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4267 | ALOGW("Asynchronous input event injection timed out."); |
| 4268 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4269 | case InputEventInjectionResult::PENDING: |
| 4270 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4271 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4276 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | } |
| 4278 | } |
| 4279 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4280 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4281 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | if (injectionState) { |
| 4283 | injectionState->pendingForegroundDispatches += 1; |
| 4284 | } |
| 4285 | } |
| 4286 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4287 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4288 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4289 | if (injectionState) { |
| 4290 | injectionState->pendingForegroundDispatches -= 1; |
| 4291 | |
| 4292 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4293 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | } |
| 4297 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4298 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4299 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4300 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 4301 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4302 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4303 | } |
| 4304 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4305 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4306 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4307 | if (windowHandleToken == nullptr) { |
| 4308 | return nullptr; |
| 4309 | } |
| 4310 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4311 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4312 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4313 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4314 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4315 | return windowHandle; |
| 4316 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | } |
| 4318 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4319 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } |
| 4321 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4322 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4323 | int displayId) const { |
| 4324 | if (windowHandleToken == nullptr) { |
| 4325 | return nullptr; |
| 4326 | } |
| 4327 | |
| 4328 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 4329 | if (windowHandle->getToken() == windowHandleToken) { |
| 4330 | return windowHandle; |
| 4331 | } |
| 4332 | } |
| 4333 | return nullptr; |
| 4334 | } |
| 4335 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4336 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
| 4337 | const sp<InputWindowHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4338 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4339 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4340 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4341 | if (handle->getId() == windowHandle->getId() && |
| 4342 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4343 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4344 | ALOGE("Found window %s in display %" PRId32 |
| 4345 | ", but it should belong to display %" PRId32, |
| 4346 | windowHandle->getName().c_str(), it.first, |
| 4347 | windowHandle->getInfo()->displayId); |
| 4348 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4349 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4350 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 | } |
| 4352 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4353 | return nullptr; |
| 4354 | } |
| 4355 | |
| 4356 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 4357 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4358 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4359 | } |
| 4360 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4361 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 4362 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4363 | const bool noInputChannel = |
| 4364 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4365 | if (connection != nullptr && noInputChannel) { |
| 4366 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4367 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4368 | return false; |
| 4369 | } |
| 4370 | |
| 4371 | if (connection == nullptr) { |
| 4372 | if (!noInputChannel) { |
| 4373 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4374 | } |
| 4375 | return false; |
| 4376 | } |
| 4377 | if (!connection->responsive) { |
| 4378 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4379 | return false; |
| 4380 | } |
| 4381 | return true; |
| 4382 | } |
| 4383 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4384 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4385 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4386 | auto connectionIt = mConnectionsByToken.find(token); |
| 4387 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4388 | return nullptr; |
| 4389 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4390 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4391 | } |
| 4392 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4393 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 4394 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 4395 | if (inputWindowHandles.empty()) { |
| 4396 | // Remove all handles on a display if there are no windows left. |
| 4397 | mWindowHandlesByDisplay.erase(displayId); |
| 4398 | return; |
| 4399 | } |
| 4400 | |
| 4401 | // Since we compare the pointer of input window handles across window updates, we need |
| 4402 | // to make sure the handle object for the same window stays unchanged across updates. |
| 4403 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4404 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4405 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4406 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | std::vector<sp<InputWindowHandle>> newHandles; |
| 4410 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 4411 | if (!handle->updateInfo()) { |
| 4412 | // handle no longer valid |
| 4413 | continue; |
| 4414 | } |
| 4415 | |
| 4416 | const InputWindowInfo* info = handle->getInfo(); |
| 4417 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 4418 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 4419 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4420 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4421 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 4422 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4423 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4424 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4425 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4426 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4427 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | if (info->displayId != displayId) { |
| 4431 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4432 | handle->getName().c_str(), displayId, info->displayId); |
| 4433 | continue; |
| 4434 | } |
| 4435 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4436 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4437 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4438 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4439 | oldHandle->updateFrom(handle); |
| 4440 | newHandles.push_back(oldHandle); |
| 4441 | } else { |
| 4442 | newHandles.push_back(handle); |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | // Insert or replace |
| 4447 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4448 | } |
| 4449 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4450 | void InputDispatcher::setInputWindows( |
| 4451 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 4452 | { // acquire lock |
| 4453 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4454 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4455 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4459 | mLooper->wake(); |
| 4460 | } |
| 4461 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4462 | /** |
| 4463 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4464 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4465 | * If set an empty list, remove all handles from the specific display. |
| 4466 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4467 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4468 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4469 | void InputDispatcher::setInputWindowsLocked( |
| 4470 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4471 | if (DEBUG_FOCUS) { |
| 4472 | std::string windowList; |
| 4473 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 4474 | windowList += iwh->getName() + " "; |
| 4475 | } |
| 4476 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4479 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4480 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 4481 | const bool noInputWindow = |
| 4482 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4483 | if (noInputWindow && window->getToken() != nullptr) { |
| 4484 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4485 | window->getName().c_str()); |
| 4486 | window->releaseChannel(); |
| 4487 | } |
| 4488 | } |
| 4489 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4490 | // Copy old handles for release if they are no longer present. |
| 4491 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4492 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4493 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4494 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4495 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 4496 | if (mLastHoverWindowHandle && |
| 4497 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4498 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4499 | mLastHoverWindowHandle = nullptr; |
| 4500 | } |
| 4501 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4502 | std::optional<FocusResolver::FocusChanges> changes = |
| 4503 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4504 | if (changes) { |
| 4505 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4506 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4507 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4508 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4509 | mTouchStatesByDisplay.find(displayId); |
| 4510 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4511 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4512 | for (size_t i = 0; i < state.windows.size();) { |
| 4513 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4514 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4515 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4516 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4517 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4518 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4519 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4520 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4521 | if (touchedInputChannel != nullptr) { |
| 4522 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4523 | "touched window was removed"); |
| 4524 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4525 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4526 | state.windows.erase(state.windows.begin() + i); |
| 4527 | } else { |
| 4528 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | } |
| 4530 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4531 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4532 | // If drag window is gone, it would receive a cancel event and broadcast the DRAG_END. We |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4533 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4534 | if (mDragState && |
| 4535 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4536 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4537 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4538 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4539 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4540 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4541 | // Release information for windows that are no longer present. |
| 4542 | // This ensures that unused input channels are released promptly. |
| 4543 | // Otherwise, they might stick around until the window handle is destroyed |
| 4544 | // which might not happen until the next GC. |
| 4545 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4546 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4547 | if (DEBUG_FOCUS) { |
| 4548 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4549 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4550 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4551 | // To avoid making too many calls into the compat framework, only |
| 4552 | // check for window flags when windows are going away. |
| 4553 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4554 | // in order to gather some data about the flag usage |
| 4555 | if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { |
| 4556 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4557 | oldWindowHandle->getName().c_str()); |
| 4558 | if (mCompatService != nullptr) { |
| 4559 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4560 | oldWindowHandle->getInfo()->ownerUid); |
| 4561 | } |
| 4562 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4563 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4564 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4568 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4569 | if (DEBUG_FOCUS) { |
| 4570 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4571 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4572 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4573 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4574 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4575 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4576 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4577 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4578 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4579 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4580 | 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] | 4581 | } |
| 4582 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4583 | // Set the new application handle. |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4584 | if (inputApplicationHandle != nullptr) { |
| 4585 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4586 | } else { |
| 4587 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4588 | } |
| 4589 | |
| 4590 | // No matter what the old focused application was, stop waiting on it because it is |
| 4591 | // no longer focused. |
| 4592 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4593 | } // release lock |
| 4594 | |
| 4595 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4596 | mLooper->wake(); |
| 4597 | } |
| 4598 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4599 | /** |
| 4600 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4601 | * the display not specified. |
| 4602 | * |
| 4603 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4604 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4605 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4606 | * display. The display-specified events won't be affected. |
| 4607 | */ |
| 4608 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4609 | if (DEBUG_FOCUS) { |
| 4610 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4611 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4612 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4613 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4614 | |
| 4615 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4616 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4617 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4618 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4619 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4620 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4621 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4622 | CancelationOptions |
| 4623 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4624 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4625 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4626 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4627 | } |
| 4628 | } |
| 4629 | mFocusedDisplayId = displayId; |
| 4630 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4631 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4632 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4633 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4634 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4635 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4636 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4637 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4638 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4639 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4640 | } |
| 4641 | } |
| 4642 | } |
| 4643 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4644 | if (DEBUG_FOCUS) { |
| 4645 | logDispatchStateLocked(); |
| 4646 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4647 | } // release lock |
| 4648 | |
| 4649 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4650 | mLooper->wake(); |
| 4651 | } |
| 4652 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4653 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4654 | if (DEBUG_FOCUS) { |
| 4655 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4656 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4657 | |
| 4658 | bool changed; |
| 4659 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4660 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4661 | |
| 4662 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4663 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4664 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | if (mDispatchEnabled && !enabled) { |
| 4668 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4669 | } |
| 4670 | |
| 4671 | mDispatchEnabled = enabled; |
| 4672 | mDispatchFrozen = frozen; |
| 4673 | changed = true; |
| 4674 | } else { |
| 4675 | changed = false; |
| 4676 | } |
| 4677 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4678 | if (DEBUG_FOCUS) { |
| 4679 | logDispatchStateLocked(); |
| 4680 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4681 | } // release lock |
| 4682 | |
| 4683 | if (changed) { |
| 4684 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4685 | mLooper->wake(); |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4690 | if (DEBUG_FOCUS) { |
| 4691 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4692 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4693 | |
| 4694 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4695 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4696 | |
| 4697 | if (mInputFilterEnabled == enabled) { |
| 4698 | return; |
| 4699 | } |
| 4700 | |
| 4701 | mInputFilterEnabled = enabled; |
| 4702 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4703 | } // release lock |
| 4704 | |
| 4705 | // Wake up poll loop since there might be work to do to drop everything. |
| 4706 | mLooper->wake(); |
| 4707 | } |
| 4708 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4709 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4710 | std::scoped_lock lock(mLock); |
| 4711 | mInTouchMode = inTouchMode; |
| 4712 | } |
| 4713 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4714 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4715 | if (opacity < 0 || opacity > 1) { |
| 4716 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4717 | return; |
| 4718 | } |
| 4719 | |
| 4720 | std::scoped_lock lock(mLock); |
| 4721 | mMaximumObscuringOpacityForTouch = opacity; |
| 4722 | } |
| 4723 | |
| 4724 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4725 | std::scoped_lock lock(mLock); |
| 4726 | mBlockUntrustedTouchesMode = mode; |
| 4727 | } |
| 4728 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4729 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4730 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4731 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4732 | if (DEBUG_FOCUS) { |
| 4733 | ALOGD("Trivial transfer to same window."); |
| 4734 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4735 | return true; |
| 4736 | } |
| 4737 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4739 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4740 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4741 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4742 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4743 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4744 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4745 | return false; |
| 4746 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4747 | if (DEBUG_FOCUS) { |
| 4748 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4749 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4750 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4751 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4752 | if (DEBUG_FOCUS) { |
| 4753 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4754 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4755 | return false; |
| 4756 | } |
| 4757 | |
| 4758 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4759 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4760 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4761 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4762 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4763 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4764 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4765 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4766 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4767 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4768 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4769 | int32_t newTargetFlags = oldTargetFlags & |
| 4770 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4771 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4772 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4774 | // Store the dragging window. |
| 4775 | if (isDragDrop) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4776 | mDragState = std::make_unique<DragState>(toWindowHandle); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4777 | } |
| 4778 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4779 | found = true; |
| 4780 | goto Found; |
| 4781 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4782 | } |
| 4783 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4784 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4785 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4786 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4787 | if (DEBUG_FOCUS) { |
| 4788 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4790 | return false; |
| 4791 | } |
| 4792 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4793 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4794 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4795 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4796 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4797 | CancelationOptions |
| 4798 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4799 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4800 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4801 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4802 | } |
| 4803 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4804 | if (DEBUG_FOCUS) { |
| 4805 | logDispatchStateLocked(); |
| 4806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4807 | } // release lock |
| 4808 | |
| 4809 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4810 | mLooper->wake(); |
| 4811 | return true; |
| 4812 | } |
| 4813 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4814 | // Binder call |
| 4815 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 4816 | sp<IBinder> fromToken; |
| 4817 | { // acquire lock |
| 4818 | std::scoped_lock _l(mLock); |
| 4819 | |
| 4820 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); |
| 4821 | if (toWindowHandle == nullptr) { |
| 4822 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 4823 | return false; |
| 4824 | } |
| 4825 | |
| 4826 | const int32_t displayId = toWindowHandle->getInfo()->displayId; |
| 4827 | |
| 4828 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 4829 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 4830 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", |
| 4831 | displayId); |
| 4832 | return false; |
| 4833 | } |
| 4834 | |
| 4835 | TouchState& state = touchStateIt->second; |
| 4836 | if (state.windows.size() != 1) { |
| 4837 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", |
| 4838 | state.windows.size()); |
| 4839 | return false; |
| 4840 | } |
| 4841 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 4842 | fromToken = touchedWindow.windowHandle->getToken(); |
| 4843 | } // release lock |
| 4844 | |
| 4845 | return transferTouchFocus(fromToken, destChannelToken); |
| 4846 | } |
| 4847 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4849 | if (DEBUG_FOCUS) { |
| 4850 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4851 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4852 | |
| 4853 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4854 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4855 | |
| 4856 | resetKeyRepeatLocked(); |
| 4857 | releasePendingEventLocked(); |
| 4858 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4859 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4861 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4862 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4863 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4864 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4868 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4869 | dumpDispatchStateLocked(dump); |
| 4870 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4871 | std::istringstream stream(dump); |
| 4872 | std::string line; |
| 4873 | |
| 4874 | while (std::getline(stream, line, '\n')) { |
| 4875 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4876 | } |
| 4877 | } |
| 4878 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4879 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 4880 | std::string dump; |
| 4881 | |
| 4882 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", |
| 4883 | toString(mFocusedWindowRequestedPointerCapture)); |
| 4884 | |
| 4885 | std::string windowName = "None"; |
| 4886 | if (mWindowTokenWithPointerCapture) { |
| 4887 | const sp<InputWindowHandle> captureWindowHandle = |
| 4888 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 4889 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 4890 | : "token has capture without window"; |
| 4891 | } |
| 4892 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); |
| 4893 | |
| 4894 | return dump; |
| 4895 | } |
| 4896 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4897 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4898 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4899 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4900 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4901 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4902 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4903 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4904 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4905 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4906 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4907 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4908 | const std::chrono::duration timeout = |
| 4909 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4910 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4911 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4912 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4914 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4915 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4916 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4917 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4918 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4919 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4920 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4921 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4922 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4923 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4924 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4925 | 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] | 4926 | state.displayId, toString(state.down), toString(state.split), |
| 4927 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4928 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4929 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4930 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4931 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4932 | dump += StringPrintf(INDENT4 |
| 4933 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4934 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4935 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4936 | } |
| 4937 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4938 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4939 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4940 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4941 | dump += INDENT3 "Portal windows:\n"; |
| 4942 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4943 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4944 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4945 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4946 | } |
| 4947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | } |
| 4949 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4950 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4951 | } |
| 4952 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4953 | if (mDragState) { |
| 4954 | dump += StringPrintf(INDENT "DragState:\n"); |
| 4955 | mDragState->dump(dump, INDENT2); |
| 4956 | } |
| 4957 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4958 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4959 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4960 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4961 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4962 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4963 | dump += INDENT2 "Windows:\n"; |
| 4964 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4965 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4966 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4967 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4968 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4969 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4970 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4971 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4972 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4973 | "applicationInfo.name=%s, " |
| 4974 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4975 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4976 | i, windowInfo->name.c_str(), windowInfo->id, |
| 4977 | windowInfo->displayId, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4978 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4979 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4980 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4981 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4982 | windowInfo->flags.string().c_str(), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4983 | NamedEnum::string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4984 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4985 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4986 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4987 | windowInfo->applicationInfo.name.c_str(), |
| 4988 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4989 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4990 | dump += StringPrintf(", inputFeatures=%s", |
| 4991 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4992 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4993 | "ms, trustedOverlay=%s, hasToken=%s, " |
| 4994 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4995 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4996 | millis(windowInfo->dispatchingTimeout), |
| 4997 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4998 | toString(windowInfo->token != nullptr), |
| 4999 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5000 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5001 | } |
| 5002 | } else { |
| 5003 | dump += INDENT2 "Windows: <none>\n"; |
| 5004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5005 | } |
| 5006 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5007 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | } |
| 5009 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5010 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5011 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5012 | const std::vector<Monitor>& monitors = it.second; |
| 5013 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5014 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5015 | } |
| 5016 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5017 | const std::vector<Monitor>& monitors = it.second; |
| 5018 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5019 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5020 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5021 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5022 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | nsecs_t currentTime = now(); |
| 5026 | |
| 5027 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5028 | if (!mRecentQueue.empty()) { |
| 5029 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5030 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5031 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5032 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5033 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5034 | } |
| 5035 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5036 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | // Dump event currently being dispatched. |
| 5040 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5041 | dump += INDENT "PendingEvent:\n"; |
| 5042 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5043 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5044 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5045 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5046 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5047 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5051 | if (!mInboundQueue.empty()) { |
| 5052 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5053 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5054 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5055 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5056 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5057 | } |
| 5058 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5059 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5060 | } |
| 5061 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5062 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5063 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5064 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5065 | const KeyReplacement& replacement = pair.first; |
| 5066 | int32_t newKeyCode = pair.second; |
| 5067 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5068 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5069 | } |
| 5070 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5071 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5074 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5075 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5076 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5077 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5078 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5079 | connection->inputChannel->getFd().get(), |
| 5080 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5081 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5082 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5083 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5084 | if (!connection->outboundQueue.empty()) { |
| 5085 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5086 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5087 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5088 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5089 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5090 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5091 | } |
| 5092 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5093 | if (!connection->waitQueue.empty()) { |
| 5094 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5095 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5096 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5097 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5098 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5102 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5103 | } |
| 5104 | |
| 5105 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5106 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5107 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5108 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5109 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | } |
| 5111 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5112 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5113 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5114 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5115 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 5116 | dump += mLatencyTracker.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5117 | } |
| 5118 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5119 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5120 | const size_t numMonitors = monitors.size(); |
| 5121 | for (size_t i = 0; i < numMonitors; i++) { |
| 5122 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5123 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5124 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5125 | dump += "\n"; |
| 5126 | } |
| 5127 | } |
| 5128 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5129 | class LooperEventCallback : public LooperCallback { |
| 5130 | public: |
| 5131 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5132 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5133 | |
| 5134 | private: |
| 5135 | std::function<int(int events)> mCallback; |
| 5136 | }; |
| 5137 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5138 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5139 | #if DEBUG_CHANNEL_CREATION |
| 5140 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5141 | #endif |
| 5142 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5143 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5144 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5145 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5146 | |
| 5147 | if (result) { |
| 5148 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5149 | } |
| 5150 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5151 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5152 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5153 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5154 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5155 | sp<Connection> connection = |
| 5156 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5158 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5159 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5160 | } |
| 5161 | mConnectionsByToken.emplace(token, connection); |
| 5162 | |
| 5163 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5164 | this, std::placeholders::_1, token); |
| 5165 | |
| 5166 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5167 | } // release lock |
| 5168 | |
| 5169 | // Wake the looper because some connections have changed. |
| 5170 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5171 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | } |
| 5173 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5174 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5175 | bool isGestureMonitor, |
| 5176 | const std::string& name, |
| 5177 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5178 | std::shared_ptr<InputChannel> serverChannel; |
| 5179 | std::unique_ptr<InputChannel> clientChannel; |
| 5180 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5181 | if (result) { |
| 5182 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5183 | } |
| 5184 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5185 | { // acquire lock |
| 5186 | std::scoped_lock _l(mLock); |
| 5187 | |
| 5188 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5189 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5190 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5193 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5194 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5195 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5196 | |
| 5197 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5198 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5199 | } |
| 5200 | mConnectionsByToken.emplace(token, connection); |
| 5201 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5202 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5203 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5204 | auto& monitorsByDisplay = |
| 5205 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5206 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5207 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5208 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5209 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5210 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5211 | // Wake the looper because some connections have changed. |
| 5212 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5213 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5216 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5218 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5219 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5220 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5221 | if (status) { |
| 5222 | return status; |
| 5223 | } |
| 5224 | } // release lock |
| 5225 | |
| 5226 | // Wake the poll loop because removing the connection may have changed the current |
| 5227 | // synchronization state. |
| 5228 | mLooper->wake(); |
| 5229 | return OK; |
| 5230 | } |
| 5231 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5232 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5233 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5234 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5235 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5236 | // Connection can be removed via socket hang up or an explicit call to 'removeInputChannel' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5237 | return BAD_VALUE; |
| 5238 | } |
| 5239 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5240 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5241 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5242 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5243 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5244 | } |
| 5245 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5246 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5247 | |
| 5248 | nsecs_t currentTime = now(); |
| 5249 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5250 | |
| 5251 | connection->status = Connection::STATUS_ZOMBIE; |
| 5252 | return OK; |
| 5253 | } |
| 5254 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5255 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5256 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5257 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5258 | } |
| 5259 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5260 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5261 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5262 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5263 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5264 | std::vector<Monitor>& monitors = it->second; |
| 5265 | const size_t numMonitors = monitors.size(); |
| 5266 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5267 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5268 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5269 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5270 | monitors.erase(monitors.begin() + i); |
| 5271 | break; |
| 5272 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5273 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5274 | if (monitors.empty()) { |
| 5275 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5276 | } else { |
| 5277 | ++it; |
| 5278 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5279 | } |
| 5280 | } |
| 5281 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5282 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5283 | { // acquire lock |
| 5284 | std::scoped_lock _l(mLock); |
| 5285 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5286 | |
| 5287 | if (!foundDisplayId) { |
| 5288 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5289 | return BAD_VALUE; |
| 5290 | } |
| 5291 | int32_t displayId = foundDisplayId.value(); |
| 5292 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5293 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5294 | mTouchStatesByDisplay.find(displayId); |
| 5295 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5296 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5297 | return BAD_VALUE; |
| 5298 | } |
| 5299 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5300 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5301 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5302 | std::optional<int32_t> foundDeviceId; |
| 5303 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5304 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5305 | requestingChannel = touchedMonitor.monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5306 | foundDeviceId = state.deviceId; |
| 5307 | } |
| 5308 | } |
| 5309 | if (!foundDeviceId || !state.down) { |
| 5310 | 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] | 5311 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5312 | return BAD_VALUE; |
| 5313 | } |
| 5314 | int32_t deviceId = foundDeviceId.value(); |
| 5315 | |
| 5316 | // Send cancel events to all the input channels we're stealing from. |
| 5317 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5318 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5319 | options.deviceId = deviceId; |
| 5320 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5321 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5322 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5323 | std::shared_ptr<InputChannel> channel = |
| 5324 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5325 | if (channel != nullptr) { |
| 5326 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5327 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5328 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5329 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5330 | canceledWindows += "]"; |
| 5331 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5332 | canceledWindows.c_str()); |
| 5333 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5334 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5335 | state.filterNonMonitors(); |
| 5336 | } |
| 5337 | return OK; |
| 5338 | } |
| 5339 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5340 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5341 | { // acquire lock |
| 5342 | std::scoped_lock _l(mLock); |
| 5343 | if (DEBUG_FOCUS) { |
| 5344 | const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); |
| 5345 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5346 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5347 | : "token without window"); |
| 5348 | } |
| 5349 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5350 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5351 | if (focusedToken != windowToken) { |
| 5352 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5353 | enabled ? "enable" : "disable"); |
| 5354 | return; |
| 5355 | } |
| 5356 | |
| 5357 | if (enabled == mFocusedWindowRequestedPointerCapture) { |
| 5358 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5359 | "window has %s requested pointer capture.", |
| 5360 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5361 | return; |
| 5362 | } |
| 5363 | |
| 5364 | mFocusedWindowRequestedPointerCapture = enabled; |
| 5365 | setPointerCaptureLocked(enabled); |
| 5366 | } // release lock |
| 5367 | |
| 5368 | // Wake the thread to process command entries. |
| 5369 | mLooper->wake(); |
| 5370 | } |
| 5371 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5372 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5373 | const sp<IBinder>& token) { |
| 5374 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5375 | const std::vector<Monitor>& monitors = it.second; |
| 5376 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5377 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5378 | return it.first; |
| 5379 | } |
| 5380 | } |
| 5381 | } |
| 5382 | return std::nullopt; |
| 5383 | } |
| 5384 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5385 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5386 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5387 | if (gesturePid.has_value()) { |
| 5388 | return gesturePid; |
| 5389 | } |
| 5390 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5391 | } |
| 5392 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5393 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5394 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5395 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5396 | } |
| 5397 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5398 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5399 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5400 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5401 | } |
| 5402 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5403 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5404 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5405 | } |
| 5406 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5407 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5408 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5409 | if (connection == nullptr) { |
| 5410 | return "<nullptr>"; |
| 5411 | } |
| 5412 | return connection->getInputChannelName(); |
| 5413 | } |
| 5414 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5415 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5416 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5417 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5418 | } |
| 5419 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5420 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 5421 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5422 | bool handled, nsecs_t consumeTime) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5423 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5424 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5425 | commandEntry->connection = connection; |
| 5426 | commandEntry->eventTime = currentTime; |
| 5427 | commandEntry->seq = seq; |
| 5428 | commandEntry->handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5429 | commandEntry->consumeTime = consumeTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5430 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5431 | } |
| 5432 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5433 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 5434 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5435 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5436 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5437 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5438 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5439 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5440 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5441 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5442 | } |
| 5443 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5444 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 5445 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5446 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5447 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5448 | commandEntry->oldToken = oldToken; |
| 5449 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5450 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5451 | } |
| 5452 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5453 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { |
| 5454 | std::unique_ptr<CommandEntry> commandEntry = |
| 5455 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); |
| 5456 | commandEntry->newToken = token; |
| 5457 | commandEntry->x = x; |
| 5458 | commandEntry->y = y; |
| 5459 | postCommandLocked(std::move(commandEntry)); |
| 5460 | } |
| 5461 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5462 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5463 | if (connection == nullptr) { |
| 5464 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5465 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5466 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5467 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5468 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5469 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5470 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5471 | return; |
| 5472 | } |
| 5473 | /** |
| 5474 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5475 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5476 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5477 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5478 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5479 | * most useful. |
| 5480 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5481 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5482 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5483 | std::string reason = |
| 5484 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5485 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5486 | ns2ms(currentWait), |
| 5487 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5488 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5489 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5490 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5491 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5492 | |
| 5493 | // Stop waking up for events on this connection, it is already unresponsive |
| 5494 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5495 | } |
| 5496 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5497 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5498 | std::string reason = |
| 5499 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5500 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5501 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5502 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5503 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); |
| 5504 | commandEntry->inputApplicationHandle = std::move(application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5505 | postCommandLocked(std::move(commandEntry)); |
| 5506 | } |
| 5507 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5508 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 5509 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5510 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 5511 | commandEntry->obscuringPackage = obscuringPackage; |
| 5512 | postCommandLocked(std::move(commandEntry)); |
| 5513 | } |
| 5514 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5515 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 5516 | const std::string& reason) { |
| 5517 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5518 | updateLastAnrStateLocked(windowLabel, reason); |
| 5519 | } |
| 5520 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5521 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5522 | const std::string& reason) { |
| 5523 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5524 | updateLastAnrStateLocked(windowLabel, reason); |
| 5525 | } |
| 5526 | |
| 5527 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5528 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5529 | // 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] | 5530 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5531 | struct tm tm; |
| 5532 | localtime_r(&t, &tm); |
| 5533 | char timestr[64]; |
| 5534 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5535 | mLastAnrState.clear(); |
| 5536 | mLastAnrState += INDENT "ANR:\n"; |
| 5537 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5538 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5539 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5540 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5541 | } |
| 5542 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5543 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5544 | mLock.unlock(); |
| 5545 | |
| 5546 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 5547 | |
| 5548 | mLock.lock(); |
| 5549 | } |
| 5550 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5551 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5552 | sp<Connection> connection = commandEntry->connection; |
| 5553 | |
| 5554 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 5555 | mLock.unlock(); |
| 5556 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5557 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5558 | |
| 5559 | mLock.lock(); |
| 5560 | } |
| 5561 | } |
| 5562 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5563 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5564 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 5565 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5566 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5567 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5568 | mLock.lock(); |
| 5569 | } |
| 5570 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5571 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { |
| 5572 | sp<IBinder> newToken = commandEntry->newToken; |
| 5573 | mLock.unlock(); |
| 5574 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); |
| 5575 | mLock.lock(); |
| 5576 | } |
| 5577 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5578 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5579 | mLock.unlock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5580 | |
| 5581 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); |
| 5582 | |
| 5583 | mLock.lock(); |
| 5584 | } |
| 5585 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5586 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5587 | mLock.unlock(); |
| 5588 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5589 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5590 | |
| 5591 | mLock.lock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5592 | } |
| 5593 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5594 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5595 | mLock.unlock(); |
| 5596 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5597 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); |
| 5598 | |
| 5599 | mLock.lock(); |
| 5600 | } |
| 5601 | |
| 5602 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5603 | mLock.unlock(); |
| 5604 | |
| 5605 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); |
| 5606 | |
| 5607 | mLock.lock(); |
| 5608 | } |
| 5609 | |
| 5610 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5611 | mLock.unlock(); |
| 5612 | |
| 5613 | mPolicy->notifyMonitorResponsive(commandEntry->pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5614 | |
| 5615 | mLock.lock(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5616 | } |
| 5617 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5618 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 5619 | mLock.unlock(); |
| 5620 | |
| 5621 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 5622 | |
| 5623 | mLock.lock(); |
| 5624 | } |
| 5625 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5626 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5627 | CommandEntry* commandEntry) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5628 | KeyEntry& entry = *(commandEntry->keyEntry); |
| 5629 | KeyEvent event = createKeyEvent(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5630 | |
| 5631 | mLock.unlock(); |
| 5632 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5633 | android::base::Timer t; |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5634 | const sp<IBinder>& token = commandEntry->connectionToken; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5635 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5636 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5637 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5638 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5639 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5640 | |
| 5641 | mLock.lock(); |
| 5642 | |
| 5643 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5644 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5645 | } else if (!delay) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5646 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5647 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5648 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5649 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5650 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5651 | } |
| 5652 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5653 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5654 | mLock.unlock(); |
| 5655 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5656 | mLock.lock(); |
| 5657 | } |
| 5658 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5659 | /** |
| 5660 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5661 | * current time. |
| 5662 | */ |
| 5663 | static bool isConnectionResponsive(const Connection& connection) { |
| 5664 | const nsecs_t currentTime = now(); |
| 5665 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5666 | if (entry->timeoutTime < currentTime) { |
| 5667 | return false; |
| 5668 | } |
| 5669 | } |
| 5670 | return true; |
| 5671 | } |
| 5672 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5673 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5674 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5675 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5676 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5677 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5678 | |
| 5679 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5680 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5681 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5682 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5683 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5684 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5685 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5686 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5687 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5688 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5689 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame^] | 5690 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5691 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5692 | connection->inputChannel->getConnectionToken(), |
| 5693 | dispatchEntry->deliveryTime, commandEntry->consumeTime, |
| 5694 | finishTime); |
| 5695 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5696 | |
| 5697 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5698 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5699 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5700 | restartEvent = |
| 5701 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5702 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5703 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5704 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5705 | handled); |
| 5706 | } else { |
| 5707 | restartEvent = false; |
| 5708 | } |
| 5709 | |
| 5710 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5711 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5712 | // contents of the wait queue to have been drained, so we need to double-check |
| 5713 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5714 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5715 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5716 | dispatchEntry = *dispatchEntryIt; |
| 5717 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5718 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5719 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5720 | if (!connection->responsive) { |
| 5721 | connection->responsive = isConnectionResponsive(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5722 | if (connection->responsive) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5723 | // The connection was unresponsive, and now it's responsive. |
| 5724 | processConnectionResponsiveLocked(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5725 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5726 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5727 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5728 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5729 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5730 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5731 | } else { |
| 5732 | releaseDispatchEntry(dispatchEntry); |
| 5733 | } |
| 5734 | } |
| 5735 | |
| 5736 | // Start the next dispatch cycle for this connection. |
| 5737 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5738 | } |
| 5739 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5740 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
| 5741 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5742 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); |
| 5743 | monitorUnresponsiveCommand->pid = pid; |
| 5744 | monitorUnresponsiveCommand->reason = std::move(reason); |
| 5745 | postCommandLocked(std::move(monitorUnresponsiveCommand)); |
| 5746 | } |
| 5747 | |
| 5748 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, |
| 5749 | std::string reason) { |
| 5750 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5751 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); |
| 5752 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); |
| 5753 | windowUnresponsiveCommand->reason = std::move(reason); |
| 5754 | postCommandLocked(std::move(windowUnresponsiveCommand)); |
| 5755 | } |
| 5756 | |
| 5757 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
| 5758 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( |
| 5759 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); |
| 5760 | monitorResponsiveCommand->pid = pid; |
| 5761 | postCommandLocked(std::move(monitorResponsiveCommand)); |
| 5762 | } |
| 5763 | |
| 5764 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { |
| 5765 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( |
| 5766 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); |
| 5767 | windowResponsiveCommand->connectionToken = std::move(connectionToken); |
| 5768 | postCommandLocked(std::move(windowResponsiveCommand)); |
| 5769 | } |
| 5770 | |
| 5771 | /** |
| 5772 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5773 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5774 | * command entry to the command queue. |
| 5775 | */ |
| 5776 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5777 | std::string reason) { |
| 5778 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5779 | if (connection.monitor) { |
| 5780 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5781 | reason.c_str()); |
| 5782 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5783 | if (!pid.has_value()) { |
| 5784 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5785 | connection.inputChannel->getName().c_str()); |
| 5786 | return; |
| 5787 | } |
| 5788 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5789 | return; |
| 5790 | } |
| 5791 | // If not a monitor, must be a window |
| 5792 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5793 | reason.c_str()); |
| 5794 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5795 | } |
| 5796 | |
| 5797 | /** |
| 5798 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5799 | */ |
| 5800 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5801 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5802 | if (connection.monitor) { |
| 5803 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5804 | if (!pid.has_value()) { |
| 5805 | ALOGE("Could not find responsive monitor for connection %s", |
| 5806 | connection.inputChannel->getName().c_str()); |
| 5807 | return; |
| 5808 | } |
| 5809 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5810 | return; |
| 5811 | } |
| 5812 | // If not a monitor, must be a window |
| 5813 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5814 | } |
| 5815 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5816 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5817 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5818 | KeyEntry& keyEntry, bool handled) { |
| 5819 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5820 | if (!handled) { |
| 5821 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5822 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5823 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5824 | return false; |
| 5825 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5826 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5827 | // Get the fallback key state. |
| 5828 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5829 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5830 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5831 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5832 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5833 | } |
| 5834 | |
| 5835 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5836 | // If the application handles the original key for which we previously |
| 5837 | // generated a fallback or if the window is not a foreground window, |
| 5838 | // then cancel the associated fallback key, if any. |
| 5839 | if (fallbackKeyCode != -1) { |
| 5840 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5841 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5842 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5843 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5844 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5845 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5846 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5847 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5848 | |
| 5849 | mLock.unlock(); |
| 5850 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5851 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5852 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5853 | |
| 5854 | mLock.lock(); |
| 5855 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5856 | // Cancel the fallback key. |
| 5857 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5858 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5859 | "application handled the original non-fallback key " |
| 5860 | "or is no longer a foreground target, " |
| 5861 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5862 | options.keyCode = fallbackKeyCode; |
| 5863 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5864 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5865 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5866 | } |
| 5867 | } else { |
| 5868 | // If the application did not handle a non-fallback key, first check |
| 5869 | // that we are in a good state to perform unhandled key event processing |
| 5870 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5871 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5872 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5873 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5874 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5875 | "since this is not an initial down. " |
| 5876 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5877 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5878 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5879 | return false; |
| 5880 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5881 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5882 | // Dispatch the unhandled key to the policy. |
| 5883 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5884 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5885 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5886 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5887 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5888 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5889 | |
| 5890 | mLock.unlock(); |
| 5891 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5892 | bool fallback = |
| 5893 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5894 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5895 | |
| 5896 | mLock.lock(); |
| 5897 | |
| 5898 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5899 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5900 | return false; |
| 5901 | } |
| 5902 | |
| 5903 | // Latch the fallback keycode for this key on an initial down. |
| 5904 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5905 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5906 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5907 | fallbackKeyCode = event.getKeyCode(); |
| 5908 | } else { |
| 5909 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5910 | } |
| 5911 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5912 | } |
| 5913 | |
| 5914 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5915 | |
| 5916 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5917 | // We will continue to dispatch the key to the policy but we will no |
| 5918 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5919 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5920 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5921 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5922 | if (fallback) { |
| 5923 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5924 | "as a fallback for %d, but on the DOWN it had requested " |
| 5925 | "to send %d instead. Fallback canceled.", |
| 5926 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5927 | } else { |
| 5928 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5929 | "but on the DOWN it had requested to send %d. " |
| 5930 | "Fallback canceled.", |
| 5931 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5932 | } |
| 5933 | #endif |
| 5934 | |
| 5935 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5936 | "canceling fallback, policy no longer desires it"); |
| 5937 | options.keyCode = fallbackKeyCode; |
| 5938 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5939 | |
| 5940 | fallback = false; |
| 5941 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5942 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5943 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5944 | } |
| 5945 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5946 | |
| 5947 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5948 | { |
| 5949 | std::string msg; |
| 5950 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5951 | connection->inputState.getFallbackKeys(); |
| 5952 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5953 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5954 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5955 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5956 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5957 | } |
| 5958 | #endif |
| 5959 | |
| 5960 | if (fallback) { |
| 5961 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5962 | keyEntry.eventTime = event.getEventTime(); |
| 5963 | keyEntry.deviceId = event.getDeviceId(); |
| 5964 | keyEntry.source = event.getSource(); |
| 5965 | keyEntry.displayId = event.getDisplayId(); |
| 5966 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5967 | keyEntry.keyCode = fallbackKeyCode; |
| 5968 | keyEntry.scanCode = event.getScanCode(); |
| 5969 | keyEntry.metaState = event.getMetaState(); |
| 5970 | keyEntry.repeatCount = event.getRepeatCount(); |
| 5971 | keyEntry.downTime = event.getDownTime(); |
| 5972 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5973 | |
| 5974 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5975 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5976 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5977 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5978 | #endif |
| 5979 | return true; // restart the event |
| 5980 | } else { |
| 5981 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5982 | ALOGD("Unhandled key event: No fallback key."); |
| 5983 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5984 | |
| 5985 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5986 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5987 | } |
| 5988 | } |
| 5989 | return false; |
| 5990 | } |
| 5991 | |
| 5992 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5993 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5994 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5995 | return false; |
| 5996 | } |
| 5997 | |
| 5998 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5999 | mLock.unlock(); |
| 6000 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 6001 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, |
| 6002 | commandEntry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6003 | |
| 6004 | mLock.lock(); |
| 6005 | } |
| 6006 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6007 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6008 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6009 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6010 | } |
| 6011 | } |
| 6012 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6013 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6014 | if (ATRACE_ENABLED()) { |
| 6015 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6016 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6017 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6018 | } |
| 6019 | } |
| 6020 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6021 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6022 | if (ATRACE_ENABLED()) { |
| 6023 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6024 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6025 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6026 | } |
| 6027 | } |
| 6028 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6029 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6030 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6031 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6032 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6033 | dumpDispatchStateLocked(dump); |
| 6034 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6035 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6036 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6037 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | void InputDispatcher::monitor() { |
| 6042 | // 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] | 6043 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6044 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6045 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6046 | } |
| 6047 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6048 | /** |
| 6049 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6050 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6051 | * this method can be safely called from any thread, as long as you've ensured that |
| 6052 | * the work you are interested in completing has already been queued. |
| 6053 | */ |
| 6054 | bool InputDispatcher::waitForIdle() { |
| 6055 | /** |
| 6056 | * Timeout should represent the longest possible time that a device might spend processing |
| 6057 | * events and commands. |
| 6058 | */ |
| 6059 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6060 | std::unique_lock lock(mLock); |
| 6061 | mLooper->wake(); |
| 6062 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6063 | return result == std::cv_status::no_timeout; |
| 6064 | } |
| 6065 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6066 | /** |
| 6067 | * Sets focus to the window identified by the token. This must be called |
| 6068 | * after updating any input window handles. |
| 6069 | * |
| 6070 | * Params: |
| 6071 | * request.token - input channel token used to identify the window that should gain focus. |
| 6072 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6073 | * specified token does not match the currently focused window, this request will be dropped. |
| 6074 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6075 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6076 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6077 | * when requesting the focus change. This determines which request gets |
| 6078 | * precedence if there is a focus change request from another source such as pointer down. |
| 6079 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6080 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6081 | { // acquire lock |
| 6082 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6083 | std::optional<FocusResolver::FocusChanges> changes = |
| 6084 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6085 | if (changes) { |
| 6086 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6087 | } |
| 6088 | } // release lock |
| 6089 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6090 | mLooper->wake(); |
| 6091 | } |
| 6092 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6093 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6094 | if (changes.oldFocus) { |
| 6095 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6096 | if (focusedInputChannel) { |
| 6097 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6098 | "focus left window"); |
| 6099 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6100 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6101 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6102 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6103 | if (changes.newFocus) { |
| 6104 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6105 | } |
| 6106 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6107 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6108 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6109 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6110 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6111 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6112 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6113 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6114 | // front. |
| 6115 | disablePointerCaptureForcedLocked(); |
| 6116 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6117 | if (mFocusedDisplayId == changes.displayId) { |
| 6118 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6119 | } |
| 6120 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6121 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6122 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
| 6123 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { |
| 6124 | return; |
| 6125 | } |
| 6126 | |
| 6127 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6128 | |
| 6129 | if (mFocusedWindowRequestedPointerCapture) { |
| 6130 | mFocusedWindowRequestedPointerCapture = false; |
| 6131 | setPointerCaptureLocked(false); |
| 6132 | } |
| 6133 | |
| 6134 | if (!mWindowTokenWithPointerCapture) { |
| 6135 | // No need to send capture changes because no window has capture. |
| 6136 | return; |
| 6137 | } |
| 6138 | |
| 6139 | if (mPendingEvent != nullptr) { |
| 6140 | // Move the pending event to the front of the queue. This will give the chance |
| 6141 | // for the pending event to be dropped if it is a captured event. |
| 6142 | mInboundQueue.push_front(mPendingEvent); |
| 6143 | mPendingEvent = nullptr; |
| 6144 | } |
| 6145 | |
| 6146 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
| 6147 | false /* hasCapture */); |
| 6148 | mInboundQueue.push_front(std::move(entry)); |
| 6149 | } |
| 6150 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6151 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { |
| 6152 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 6153 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); |
| 6154 | commandEntry->enabled = enabled; |
| 6155 | postCommandLocked(std::move(commandEntry)); |
| 6156 | } |
| 6157 | |
| 6158 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( |
| 6159 | android::inputdispatcher::CommandEntry* commandEntry) { |
| 6160 | mLock.unlock(); |
| 6161 | |
| 6162 | mPolicy->setPointerCapture(commandEntry->enabled); |
| 6163 | |
| 6164 | mLock.lock(); |
| 6165 | } |
| 6166 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6167 | } // namespace android::inputdispatcher |