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 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 449 | // --- InputDispatcher --- |
| 450 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 451 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 452 | : mPolicy(policy), |
| 453 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 454 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 455 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 456 | mAppSwitchSawKeyDown(false), |
| 457 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 458 | mNextUnblockedEvent(nullptr), |
| 459 | mDispatchEnabled(false), |
| 460 | mDispatchFrozen(false), |
| 461 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 462 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 463 | // To avoid leaking stack in case that call never comes, and for tests, |
| 464 | // initialize it here anyways. |
| 465 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 466 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 467 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 468 | mFocusedWindowRequestedPointerCapture(false), |
| 469 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 470 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 471 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 472 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 473 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 474 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 475 | |
| 476 | policy->getDispatcherConfiguration(&mConfig); |
| 477 | } |
| 478 | |
| 479 | InputDispatcher::~InputDispatcher() { |
| 480 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 481 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 482 | |
| 483 | resetKeyRepeatLocked(); |
| 484 | releasePendingEventLocked(); |
| 485 | drainInboundQueueLocked(); |
| 486 | } |
| 487 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 488 | while (!mConnectionsByToken.empty()) { |
| 489 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 490 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 494 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 495 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 496 | return ALREADY_EXISTS; |
| 497 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 498 | mThread = std::make_unique<InputThread>( |
| 499 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 500 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 504 | if (mThread && mThread->isCallingThread()) { |
| 505 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 506 | return INVALID_OPERATION; |
| 507 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 508 | mThread.reset(); |
| 509 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 | void InputDispatcher::dispatchOnce() { |
| 513 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 514 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 515 | std::scoped_lock _l(mLock); |
| 516 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 517 | |
| 518 | // Run a dispatch loop if there are no pending commands. |
| 519 | // The dispatch loop might enqueue commands to run afterwards. |
| 520 | if (!haveCommandsLocked()) { |
| 521 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 522 | } |
| 523 | |
| 524 | // Run all pending commands if there are any. |
| 525 | // If any commands were run then force the next poll to wake up immediately. |
| 526 | if (runCommandsLockedInterruptible()) { |
| 527 | nextWakeupTime = LONG_LONG_MIN; |
| 528 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 529 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 530 | // If we are still waiting for ack on some events, |
| 531 | // we might have to wake up earlier to check if an app is anr'ing. |
| 532 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 533 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 534 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 535 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 536 | // pending or queued events |
| 537 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 538 | mDispatcherEnteredIdle.notify_all(); |
| 539 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | } // release lock |
| 541 | |
| 542 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 543 | nsecs_t currentTime = now(); |
| 544 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 545 | mLooper->pollOnce(timeoutMillis); |
| 546 | } |
| 547 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 548 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 549 | * Raise ANR if there is no focused window. |
| 550 | * Before the ANR is raised, do a final state check: |
| 551 | * 1. The currently focused application must be the same one we are waiting for. |
| 552 | * 2. Ensure we still don't have a focused window. |
| 553 | */ |
| 554 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 555 | // Check if the application that we are waiting for is still focused. |
| 556 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 557 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 558 | if (focusedApplication == nullptr || |
| 559 | focusedApplication->getApplicationToken() != |
| 560 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 561 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 562 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 563 | focusedApplication->getName().c_str()); |
| 564 | return; // The focused application has changed. |
| 565 | } |
| 566 | |
| 567 | const sp<InputWindowHandle>& focusedWindowHandle = |
| 568 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 569 | if (focusedWindowHandle != nullptr) { |
| 570 | return; // We now have a focused window. No need for ANR. |
| 571 | } |
| 572 | onAnrLocked(mAwaitedFocusedApplication); |
| 573 | } |
| 574 | |
| 575 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 576 | * Check if any of the connections' wait queues have events that are too old. |
| 577 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 578 | * Return the time at which we should wake up next. |
| 579 | */ |
| 580 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 581 | const nsecs_t currentTime = now(); |
| 582 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 583 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 584 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 585 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 586 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 587 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 588 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 589 | return LONG_LONG_MIN; |
| 590 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 591 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 592 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // Check if any connection ANRs are due |
| 597 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 598 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 599 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 600 | } |
| 601 | |
| 602 | // If we reached here, we have an unresponsive connection. |
| 603 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 604 | if (connection == nullptr) { |
| 605 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 606 | return nextAnrCheck; |
| 607 | } |
| 608 | connection->responsive = false; |
| 609 | // Stop waking up for this unresponsive connection |
| 610 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 611 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 612 | return LONG_LONG_MIN; |
| 613 | } |
| 614 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 615 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 616 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 617 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 618 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 619 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 620 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 624 | nsecs_t currentTime = now(); |
| 625 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 626 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 627 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 628 | // repeat if the device is just coming out of sleep. |
| 629 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | resetKeyRepeatLocked(); |
| 631 | } |
| 632 | |
| 633 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 634 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 635 | if (DEBUG_FOCUS) { |
| 636 | ALOGD("Dispatch frozen. Waiting some more."); |
| 637 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | |
| 641 | // Optimize latency of app switches. |
| 642 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 643 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 644 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 645 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 646 | *nextWakeupTime = mAppSwitchDueTime; |
| 647 | } |
| 648 | |
| 649 | // Ready to start a new event. |
| 650 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 651 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 652 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 653 | if (isAppSwitchDue) { |
| 654 | // The inbound queue is empty so the app switch key we were waiting |
| 655 | // for will never arrive. Stop waiting for it. |
| 656 | resetPendingAppSwitchLocked(false); |
| 657 | isAppSwitchDue = false; |
| 658 | } |
| 659 | |
| 660 | // Synthesize a key repeat if appropriate. |
| 661 | if (mKeyRepeatState.lastKeyEntry) { |
| 662 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 663 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 664 | } else { |
| 665 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 666 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Nothing to do if there is no pending event. |
| 672 | if (!mPendingEvent) { |
| 673 | return; |
| 674 | } |
| 675 | } else { |
| 676 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 677 | mPendingEvent = mInboundQueue.front(); |
| 678 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | traceInboundQueueLengthLocked(); |
| 680 | } |
| 681 | |
| 682 | // Poke user activity for this event. |
| 683 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 684 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // Now we have an event to dispatch. |
| 689 | // 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] | 690 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 692 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 693 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 694 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 695 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 696 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 700 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 704 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 705 | const ConfigurationChangedEntry& typedEntry = |
| 706 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 707 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 708 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 709 | break; |
| 710 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 711 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 712 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 713 | const DeviceResetEntry& typedEntry = |
| 714 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 715 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 716 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 717 | break; |
| 718 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 720 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 721 | std::shared_ptr<FocusEntry> typedEntry = |
| 722 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 723 | dispatchFocusLocked(currentTime, typedEntry); |
| 724 | done = true; |
| 725 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 726 | break; |
| 727 | } |
| 728 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 729 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 730 | const auto typedEntry = |
| 731 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 732 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 733 | done = true; |
| 734 | break; |
| 735 | } |
| 736 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 737 | case EventEntry::Type::DRAG: { |
| 738 | std::shared_ptr<DragEntry> typedEntry = |
| 739 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 740 | dispatchDragLocked(currentTime, typedEntry); |
| 741 | done = true; |
| 742 | break; |
| 743 | } |
| 744 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 745 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 746 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 747 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 748 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 749 | resetPendingAppSwitchLocked(true); |
| 750 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 751 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 752 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 753 | } |
| 754 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 755 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 756 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 757 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 758 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 759 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 761 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 765 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 766 | std::shared_ptr<MotionEntry> motionEntry = |
| 767 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 768 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 769 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 770 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 771 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 772 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 773 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 774 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 775 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 776 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 777 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 778 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 780 | |
| 781 | case EventEntry::Type::SENSOR: { |
| 782 | std::shared_ptr<SensorEntry> sensorEntry = |
| 783 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 784 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 785 | dropReason = DropReason::APP_SWITCH; |
| 786 | } |
| 787 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 788 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 789 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 790 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 791 | dropReason = DropReason::STALE; |
| 792 | } |
| 793 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 794 | done = true; |
| 795 | break; |
| 796 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 800 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 801 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 803 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | |
| 805 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 806 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 810 | /** |
| 811 | * Return true if the events preceding this incoming motion event should be dropped |
| 812 | * Return false otherwise (the default behaviour) |
| 813 | */ |
| 814 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 815 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 816 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 817 | |
| 818 | // Optimize case where the current application is unresponsive and the user |
| 819 | // decides to touch a window in a different application. |
| 820 | // If the application takes too long to catch up then we drop all events preceding |
| 821 | // the touch into the other window. |
| 822 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 823 | int32_t displayId = motionEntry.displayId; |
| 824 | int32_t x = static_cast<int32_t>( |
| 825 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 826 | int32_t y = static_cast<int32_t>( |
| 827 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 828 | sp<InputWindowHandle> touchedWindowHandle = |
| 829 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 830 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 831 | touchedWindowHandle->getApplicationToken() != |
| 832 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 833 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 834 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 835 | "for %s", |
| 836 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 837 | return true; |
| 838 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 839 | |
| 840 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 841 | std::vector<TouchedMonitor> gestureMonitors = |
| 842 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 843 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 844 | sp<Connection> connection = |
| 845 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 846 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 847 | // This monitor could take more input. Drop all events preceding this |
| 848 | // event, so that gesture monitor could get a chance to receive the stream |
| 849 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 850 | "responsive gesture monitor that may handle the event", |
| 851 | mAwaitedFocusedApplication->getName().c_str()); |
| 852 | return true; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 858 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 859 | // events to be processed before dispatching the key event. This is because these motion events |
| 860 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 861 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 862 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 863 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 864 | "just send the pending key event to the focused window."); |
| 865 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 866 | } |
| 867 | return false; |
| 868 | } |
| 869 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 870 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 871 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 872 | mInboundQueue.push_back(std::move(newEntry)); |
| 873 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 874 | traceInboundQueueLengthLocked(); |
| 875 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 876 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 877 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | // Optimize app switch latency. |
| 879 | // If the application takes too long to catch up then we drop all events preceding |
| 880 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 881 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 882 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 883 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 884 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 885 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 886 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 887 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 888 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 889 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 890 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 891 | mAppSwitchSawKeyDown = false; |
| 892 | needWake = true; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | break; |
| 897 | } |
| 898 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 899 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 900 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 901 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 902 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 903 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 904 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 906 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 907 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 908 | break; |
| 909 | } |
| 910 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 911 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 912 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 913 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 914 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 915 | // nothing to do |
| 916 | break; |
| 917 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | return needWake; |
| 921 | } |
| 922 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 923 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 924 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 925 | if (entry->type != EventEntry::Type::SENSOR) { |
| 926 | mRecentQueue.push_back(entry); |
| 927 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 928 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 929 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 933 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 934 | int32_t y, TouchState* touchState, |
| 935 | bool addOutsideTargets, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 936 | bool addPortalWindows, |
| 937 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 938 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 939 | LOG_ALWAYS_FATAL( |
| 940 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 943 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 944 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 945 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 946 | continue; |
| 947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 948 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 949 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 950 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 951 | |
| 952 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 953 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 954 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 955 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 957 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 958 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 959 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 960 | if (addPortalWindows) { |
| 961 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 962 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 963 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 964 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 965 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 967 | // Found window. |
| 968 | return windowHandle; |
| 969 | } |
| 970 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 971 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 972 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 973 | touchState->addOrUpdateWindow(windowHandle, |
| 974 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 975 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 976 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 977 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 978 | } |
| 979 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 980 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 983 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 984 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 985 | std::vector<TouchedMonitor> touchedMonitors; |
| 986 | |
| 987 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 988 | addGestureMonitors(monitors, touchedMonitors); |
| 989 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 990 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 991 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 992 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 993 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 994 | } |
| 995 | return touchedMonitors; |
| 996 | } |
| 997 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 998 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 999 | const char* reason; |
| 1000 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1001 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1003 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1005 | reason = "inbound event was dropped because the policy consumed it"; |
| 1006 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1007 | case DropReason::DISABLED: |
| 1008 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1009 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1010 | } |
| 1011 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1012 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1013 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1014 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1015 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1016 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1017 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1018 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1019 | "has started interacting with a different application."); |
| 1020 | reason = "inbound event was dropped because the current application is not responding " |
| 1021 | "and the user has started interacting with a different application"; |
| 1022 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1023 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1024 | ALOGI("Dropped event because it is stale."); |
| 1025 | reason = "inbound event was dropped because it is stale"; |
| 1026 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1027 | case DropReason::NO_POINTER_CAPTURE: |
| 1028 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1029 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1030 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1031 | case DropReason::NOT_DROPPED: { |
| 1032 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1033 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1034 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1037 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1038 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1040 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1041 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1042 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1043 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1044 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1045 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1046 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1047 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1048 | } else { |
| 1049 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1050 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1051 | } |
| 1052 | break; |
| 1053 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1054 | case EventEntry::Type::SENSOR: { |
| 1055 | break; |
| 1056 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1057 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1058 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1059 | break; |
| 1060 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1061 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1062 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1063 | case EventEntry::Type::DEVICE_RESET: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1064 | 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] | 1065 | break; |
| 1066 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1070 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1071 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1072 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1075 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1076 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1077 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1078 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1082 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1083 | } |
| 1084 | |
| 1085 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1086 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1087 | |
| 1088 | #if DEBUG_APP_SWITCH |
| 1089 | if (handled) { |
| 1090 | ALOGD("App switch has arrived."); |
| 1091 | } else { |
| 1092 | ALOGD("App switch was abandoned."); |
| 1093 | } |
| 1094 | #endif |
| 1095 | } |
| 1096 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1097 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1098 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1102 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1107 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1108 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1109 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1110 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1111 | |
| 1112 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1113 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1114 | return true; |
| 1115 | } |
| 1116 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1117 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1118 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1122 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1123 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1124 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1125 | releaseInboundEventLocked(entry); |
| 1126 | } |
| 1127 | traceInboundQueueLengthLocked(); |
| 1128 | } |
| 1129 | |
| 1130 | void InputDispatcher::releasePendingEventLocked() { |
| 1131 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1133 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1137 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1138 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1139 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1140 | #if DEBUG_DISPATCH_CYCLE |
| 1141 | ALOGD("Injected inbound event was dropped."); |
| 1142 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1143 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1144 | } |
| 1145 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1146 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | } |
| 1148 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1152 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1153 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1157 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1158 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1160 | uint32_t policyFlags = entry->policyFlags & |
| 1161 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1162 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1163 | std::shared_ptr<KeyEntry> newEntry = |
| 1164 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1165 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1166 | entry->flags, entry->keyCode, entry->scanCode, |
| 1167 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1168 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1169 | newEntry->syntheticRepeat = true; |
| 1170 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1172 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1175 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1176 | const ConfigurationChangedEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1178 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1179 | #endif |
| 1180 | |
| 1181 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1182 | resetKeyRepeatLocked(); |
| 1183 | |
| 1184 | // 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] | 1185 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1186 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1187 | commandEntry->eventTime = entry.eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1188 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
| 1191 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1192 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1193 | const DeviceResetEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1195 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1196 | entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1197 | #endif |
| 1198 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1199 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1200 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1201 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1202 | return true; |
| 1203 | } |
| 1204 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1205 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1206 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1207 | if (mPendingEvent != nullptr) { |
| 1208 | // Move the pending event to the front of the queue. This will give the chance |
| 1209 | // for the pending event to get dispatched to the newly focused window |
| 1210 | mInboundQueue.push_front(mPendingEvent); |
| 1211 | mPendingEvent = nullptr; |
| 1212 | } |
| 1213 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1214 | std::unique_ptr<FocusEntry> focusEntry = |
| 1215 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1216 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1217 | |
| 1218 | // This event should go to the front of the queue, but behind all other focus events |
| 1219 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1220 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1221 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1222 | [](const std::shared_ptr<EventEntry>& event) { |
| 1223 | return event->type == EventEntry::Type::FOCUS; |
| 1224 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1225 | |
| 1226 | // 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] | 1227 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1230 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1231 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1232 | if (channel == nullptr) { |
| 1233 | return; // Window has gone away |
| 1234 | } |
| 1235 | InputTarget target; |
| 1236 | target.inputChannel = channel; |
| 1237 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1238 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1239 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1240 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1241 | std::string reason = std::string("reason=").append(entry->reason); |
| 1242 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1243 | dispatchEventLocked(currentTime, entry, {target}); |
| 1244 | } |
| 1245 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1246 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1247 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1248 | DropReason& dropReason) { |
| 1249 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1250 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { |
| 1251 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); |
| 1252 | } |
| 1253 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1254 | // Pointer capture was already forcefully disabled because of focus change. |
| 1255 | dropReason = DropReason::NOT_DROPPED; |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | // Set drop reason for early returns |
| 1260 | dropReason = DropReason::NO_POINTER_CAPTURE; |
| 1261 | |
| 1262 | sp<IBinder> token; |
| 1263 | if (entry->pointerCaptureEnabled) { |
| 1264 | // Enable Pointer Capture |
| 1265 | if (!mFocusedWindowRequestedPointerCapture) { |
| 1266 | // This can happen if a window requests capture and immediately releases capture. |
| 1267 | ALOGW("No window requested Pointer Capture."); |
| 1268 | return; |
| 1269 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1270 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1271 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1272 | mWindowTokenWithPointerCapture = token; |
| 1273 | } else { |
| 1274 | // Disable Pointer Capture |
| 1275 | token = mWindowTokenWithPointerCapture; |
| 1276 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1277 | if (mFocusedWindowRequestedPointerCapture) { |
| 1278 | mFocusedWindowRequestedPointerCapture = false; |
| 1279 | setPointerCaptureLocked(false); |
| 1280 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | auto channel = getInputChannelLocked(token); |
| 1284 | if (channel == nullptr) { |
| 1285 | // Window has gone away, clean up Pointer Capture state. |
| 1286 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1287 | if (mFocusedWindowRequestedPointerCapture) { |
| 1288 | mFocusedWindowRequestedPointerCapture = false; |
| 1289 | setPointerCaptureLocked(false); |
| 1290 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1291 | return; |
| 1292 | } |
| 1293 | InputTarget target; |
| 1294 | target.inputChannel = channel; |
| 1295 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1296 | entry->dispatchInProgress = true; |
| 1297 | dispatchEventLocked(currentTime, entry, {target}); |
| 1298 | |
| 1299 | dropReason = DropReason::NOT_DROPPED; |
| 1300 | } |
| 1301 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1302 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1303 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1305 | if (!entry->dispatchInProgress) { |
| 1306 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1307 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1308 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1309 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1310 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1311 | // We have seen two identical key downs in a row which indicates that the device |
| 1312 | // driver is automatically generating key repeats itself. We take note of the |
| 1313 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1314 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1315 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1316 | // Make sure we don't get key down from a different device. If a different |
| 1317 | // device Id has same key pressed down, the new device Id will replace the |
| 1318 | // current one to hold the key repeat with repeat count reset. |
| 1319 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1320 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1321 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1322 | resetKeyRepeatLocked(); |
| 1323 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1324 | } else { |
| 1325 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1326 | resetKeyRepeatLocked(); |
| 1327 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1328 | } |
| 1329 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1330 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1331 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1332 | // 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] | 1333 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1334 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1335 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1336 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | resetKeyRepeatLocked(); |
| 1338 | } |
| 1339 | |
| 1340 | if (entry->repeatCount == 1) { |
| 1341 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1342 | } else { |
| 1343 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1344 | } |
| 1345 | |
| 1346 | entry->dispatchInProgress = true; |
| 1347 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1348 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | // Handle case where the policy asked us to try again later last time. |
| 1352 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1353 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1354 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1355 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1356 | } |
| 1357 | return false; // wait until next wakeup |
| 1358 | } |
| 1359 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1360 | entry->interceptKeyWakeupTime = 0; |
| 1361 | } |
| 1362 | |
| 1363 | // Give the policy a chance to intercept the key. |
| 1364 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1365 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1366 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1367 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1368 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1369 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1370 | commandEntry->connectionToken = focusedWindowToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1371 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1372 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1373 | return false; // wait for the command to run |
| 1374 | } else { |
| 1375 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1376 | } |
| 1377 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1378 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1379 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1384 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1385 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1386 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1387 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1388 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | return true; |
| 1390 | } |
| 1391 | |
| 1392 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1393 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1394 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1395 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1396 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | return false; |
| 1398 | } |
| 1399 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1400 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1401 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1402 | return true; |
| 1403 | } |
| 1404 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1405 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1406 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1407 | |
| 1408 | // Dispatch the key. |
| 1409 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1413 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1414 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1415 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1416 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1417 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1418 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1419 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1420 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1421 | #endif |
| 1422 | } |
| 1423 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1424 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { |
| 1425 | mLock.unlock(); |
| 1426 | |
| 1427 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; |
| 1428 | if (entry->accuracyChanged) { |
| 1429 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1430 | } |
| 1431 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1432 | entry->hwTimestamp, entry->values); |
| 1433 | mLock.lock(); |
| 1434 | } |
| 1435 | |
| 1436 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, |
| 1437 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 1438 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1439 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1440 | "source=0x%x, sensorType=%s", |
| 1441 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1442 | NamedEnum::string(entry->sensorType).c_str()); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1443 | #endif |
| 1444 | std::unique_ptr<CommandEntry> commandEntry = |
| 1445 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); |
| 1446 | commandEntry->sensorEntry = entry; |
| 1447 | postCommandLocked(std::move(commandEntry)); |
| 1448 | } |
| 1449 | |
| 1450 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
| 1451 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1452 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
| 1453 | NamedEnum::string(sensorType).c_str()); |
| 1454 | #endif |
| 1455 | { // acquire lock |
| 1456 | std::scoped_lock _l(mLock); |
| 1457 | |
| 1458 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1459 | std::shared_ptr<EventEntry> entry = *it; |
| 1460 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1461 | it = mInboundQueue.erase(it); |
| 1462 | releaseInboundEventLocked(entry); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1469 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1470 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1471 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1472 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1473 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | entry->dispatchInProgress = true; |
| 1475 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1476 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1480 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1481 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1482 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1483 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1484 | return true; |
| 1485 | } |
| 1486 | |
| 1487 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1488 | |
| 1489 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1490 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1491 | |
| 1492 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1493 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1494 | if (isPointerEvent) { |
| 1495 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1496 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1497 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1498 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1499 | } else { |
| 1500 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1501 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1502 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1504 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1505 | return false; |
| 1506 | } |
| 1507 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1508 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1509 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1510 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1511 | return true; |
| 1512 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1514 | CancelationOptions::Mode mode(isPointerEvent |
| 1515 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1516 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1517 | CancelationOptions options(mode, "input event injection failed"); |
| 1518 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 | return true; |
| 1520 | } |
| 1521 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1522 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1523 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1524 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1525 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1526 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1527 | mTouchStatesByDisplay.find(entry->displayId); |
| 1528 | if (it != mTouchStatesByDisplay.end()) { |
| 1529 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1530 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1531 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1532 | // the corresponding displays as well. |
| 1533 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1534 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1535 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1536 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 | // Dispatch the motion. |
| 1543 | if (conflictingPointerActions) { |
| 1544 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1545 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1547 | } |
| 1548 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1549 | return true; |
| 1550 | } |
| 1551 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1552 | void InputDispatcher::enqueueDragEventLocked(const sp<InputWindowHandle>& windowHandle, |
| 1553 | bool isExiting, const MotionEntry& motionEntry) { |
| 1554 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1555 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1556 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1557 | PointerCoords pointerCoords; |
| 1558 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1559 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1560 | |
| 1561 | std::unique_ptr<DragEntry> dragEntry = |
| 1562 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1563 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1564 | pointerCoords.getY()); |
| 1565 | |
| 1566 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1567 | } |
| 1568 | |
| 1569 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1570 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1571 | if (channel == nullptr) { |
| 1572 | return; // Window has gone away |
| 1573 | } |
| 1574 | InputTarget target; |
| 1575 | target.inputChannel = channel; |
| 1576 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1577 | entry->dispatchInProgress = true; |
| 1578 | dispatchEventLocked(currentTime, entry, {target}); |
| 1579 | } |
| 1580 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1581 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1582 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1583 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1584 | ", policyFlags=0x%x, " |
| 1585 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1586 | "metaState=0x%x, buttonState=0x%x," |
| 1587 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1588 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1589 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1590 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1592 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1593 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1594 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1595 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1596 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1597 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1598 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1599 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1600 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1601 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1602 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1603 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1604 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1605 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1606 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 | } |
| 1608 | #endif |
| 1609 | } |
| 1610 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1611 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1612 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1613 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1614 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 | #if DEBUG_DISPATCH_CYCLE |
| 1616 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1617 | #endif |
| 1618 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1619 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1620 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1622 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1623 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1625 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1626 | sp<Connection> connection = |
| 1627 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1628 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1629 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1631 | if (DEBUG_FOCUS) { |
| 1632 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1633 | "is no longer registered with the input dispatcher.", |
| 1634 | inputTarget.inputChannel->getName().c_str()); |
| 1635 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1640 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1641 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1642 | // If the policy decides to close the app, we will get a channel removal event via |
| 1643 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1644 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1645 | // pile up. |
| 1646 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1647 | connection->inputChannel->getName().c_str()); |
| 1648 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1649 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1650 | "application not responding"); |
| 1651 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1655 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1656 | if (DEBUG_FOCUS) { |
| 1657 | ALOGD("Resetting ANR timeouts."); |
| 1658 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 | |
| 1660 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1661 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1662 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1663 | } |
| 1664 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1665 | /** |
| 1666 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1667 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1668 | * Focused display is the display that the user most recently interacted with. |
| 1669 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1670 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1671 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1672 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1673 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1674 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1675 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1676 | break; |
| 1677 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1678 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1679 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1680 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1681 | break; |
| 1682 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1683 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1684 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1685 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1686 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1687 | case EventEntry::Type::SENSOR: |
| 1688 | case EventEntry::Type::DRAG: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1689 | 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] | 1690 | return ADISPLAY_ID_NONE; |
| 1691 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1692 | } |
| 1693 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1694 | } |
| 1695 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1696 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1697 | const char* focusedWindowName) { |
| 1698 | if (mAnrTracker.empty()) { |
| 1699 | // already processed all events that we waited for |
| 1700 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
| 1704 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1705 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1706 | // 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] | 1707 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1708 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1709 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1710 | return true; |
| 1711 | } |
| 1712 | |
| 1713 | // We still have pending events, and already started the timer |
| 1714 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1715 | return true; // Still waiting |
| 1716 | } |
| 1717 | |
| 1718 | // Waited too long, and some connection still hasn't processed all motions |
| 1719 | // Just send the key to the focused window |
| 1720 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1721 | focusedWindowName); |
| 1722 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1726 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1727 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1728 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1729 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1731 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1732 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1733 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1734 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1735 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | // If there is no currently focused window and no focused application |
| 1737 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1738 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1739 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1740 | "display %" PRId32 ".", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1741 | NamedEnum::string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1742 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1745 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1746 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1747 | // start interacting with another application via touch (app switch). This code can be removed |
| 1748 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1749 | // an app is expected to have a focused window. |
| 1750 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1751 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1752 | // 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] | 1753 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1754 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1755 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1756 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1757 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1758 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1759 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1760 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1761 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1762 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1763 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1764 | // Already raised ANR. Drop the event |
| 1765 | ALOGE("Dropping %s event because there is no focused window", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1766 | NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1767 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1768 | } else { |
| 1769 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1770 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | // we have a valid, non-null focused window |
| 1775 | resetNoFocusedWindowTimeoutLocked(); |
| 1776 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1777 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1778 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1779 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1782 | if (focusedWindowHandle->getInfo()->paused) { |
| 1783 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1784 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | // If the event is a key event, then we must wait for all previous events to |
| 1788 | // complete before delivering it because previous events may have the |
| 1789 | // side-effect of transferring focus to a different window and we want to |
| 1790 | // ensure that the following keys are sent to the new window. |
| 1791 | // |
| 1792 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1793 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1794 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1795 | // often anticipate pending UI changes when typing on a keyboard. |
| 1796 | // To obtain this behavior, we must serialize key events with respect to all |
| 1797 | // prior input events. |
| 1798 | if (entry.type == EventEntry::Type::KEY) { |
| 1799 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1800 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1801 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1802 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1806 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1807 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1808 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1809 | |
| 1810 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1811 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1814 | /** |
| 1815 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1816 | * that are currently unresponsive. |
| 1817 | */ |
| 1818 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1819 | const std::vector<TouchedMonitor>& monitors) const { |
| 1820 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1821 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1822 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1823 | sp<Connection> connection = getConnectionLocked( |
| 1824 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1825 | if (connection == nullptr) { |
| 1826 | ALOGE("Could not find connection for monitor %s", |
| 1827 | monitor.monitor.inputChannel->getName().c_str()); |
| 1828 | return false; |
| 1829 | } |
| 1830 | if (!connection->responsive) { |
| 1831 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1832 | connection->inputChannel->getName().c_str()); |
| 1833 | return false; |
| 1834 | } |
| 1835 | return true; |
| 1836 | }); |
| 1837 | return responsiveMonitors; |
| 1838 | } |
| 1839 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1840 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1841 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1842 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1843 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1844 | enum InjectionPermission { |
| 1845 | INJECTION_PERMISSION_UNKNOWN, |
| 1846 | INJECTION_PERMISSION_GRANTED, |
| 1847 | INJECTION_PERMISSION_DENIED |
| 1848 | }; |
| 1849 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | // For security reasons, we defer updating the touch state until we are sure that |
| 1851 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1852 | int32_t displayId = entry.displayId; |
| 1853 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1854 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1855 | |
| 1856 | // 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] | 1857 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1858 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1859 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1860 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1861 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1862 | // Copy current touch state into tempTouchState. |
| 1863 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1864 | // 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] | 1865 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1866 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1867 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1868 | mTouchStatesByDisplay.find(displayId); |
| 1869 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1870 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1871 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1874 | bool isSplit = tempTouchState.split; |
| 1875 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1876 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1877 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1878 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1879 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1880 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1881 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1882 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1883 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1884 | bool wrongDevice = false; |
| 1885 | if (newGesture) { |
| 1886 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1887 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1888 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1889 | "in display %" PRId32, |
| 1890 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1891 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1892 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1893 | switchedDevice = false; |
| 1894 | wrongDevice = true; |
| 1895 | goto Failed; |
| 1896 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1897 | tempTouchState.reset(); |
| 1898 | tempTouchState.down = down; |
| 1899 | tempTouchState.deviceId = entry.deviceId; |
| 1900 | tempTouchState.source = entry.source; |
| 1901 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1902 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1903 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1904 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1905 | "in display %" PRId32, |
| 1906 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1907 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1908 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1909 | switchedDevice = false; |
| 1910 | wrongDevice = true; |
| 1911 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1915 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1916 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1917 | int32_t x; |
| 1918 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1920 | // Always dispatch mouse events to cursor position. |
| 1921 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1922 | x = int32_t(entry.xCursorPosition); |
| 1923 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1924 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1925 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1926 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1927 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1928 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1929 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1930 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1931 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1932 | |
| 1933 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1934 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1935 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1936 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1938 | if (newTouchedWindowHandle != nullptr && |
| 1939 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1940 | // New window supports splitting, but we should never split mouse events. |
| 1941 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1942 | } else if (isSplit) { |
| 1943 | // New window does not support splitting but we have already split events. |
| 1944 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1945 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1949 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1950 | // 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] | 1951 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1954 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1955 | ALOGI("Not sending touch event to %s because it is paused", |
| 1956 | newTouchedWindowHandle->getName().c_str()); |
| 1957 | newTouchedWindowHandle = nullptr; |
| 1958 | } |
| 1959 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1960 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1961 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1962 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1963 | if (!isResponsive) { |
| 1964 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1965 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1966 | newTouchedWindowHandle = nullptr; |
| 1967 | } |
| 1968 | } |
| 1969 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1970 | // Drop events that can't be trusted due to occlusion |
| 1971 | if (newTouchedWindowHandle != nullptr && |
| 1972 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 1973 | TouchOcclusionInfo occlusionInfo = |
| 1974 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1975 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 1976 | if (DEBUG_TOUCH_OCCLUSION) { |
| 1977 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 1978 | for (const auto& log : occlusionInfo.debugInfo) { |
| 1979 | ALOGD("%s", log.c_str()); |
| 1980 | } |
| 1981 | } |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1982 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 1983 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 1984 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 1985 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 1986 | newTouchedWindowHandle = nullptr; |
| 1987 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1991 | // Also don't send the new touch event to unresponsive gesture monitors |
| 1992 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 1993 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1994 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 1995 | 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] | 1996 | "(%d, %d) in display %" PRId32 ".", |
| 1997 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1998 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1999 | goto Failed; |
| 2000 | } |
| 2001 | |
| 2002 | if (newTouchedWindowHandle != nullptr) { |
| 2003 | // Set target flags. |
| 2004 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2005 | if (isSplit) { |
| 2006 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2008 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2009 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2010 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2011 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2012 | } |
| 2013 | |
| 2014 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2015 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2016 | newHoverWindowHandle = nullptr; |
| 2017 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2018 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | // Update the temporary touch state. |
| 2022 | BitSet32 pointerIds; |
| 2023 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2024 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2025 | pointerIds.markBit(pointerId); |
| 2026 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2027 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2028 | } |
| 2029 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2030 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2031 | } else { |
| 2032 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2033 | |
| 2034 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2035 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2036 | if (DEBUG_FOCUS) { |
| 2037 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2038 | "dropped the pointer down event in display %" PRId32, |
| 2039 | displayId); |
| 2040 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2041 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2042 | goto Failed; |
| 2043 | } |
| 2044 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2045 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2046 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2047 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2048 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2049 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2050 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2051 | 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] | 2052 | |
| 2053 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2054 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2055 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2056 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2057 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2058 | if (DEBUG_FOCUS) { |
| 2059 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2060 | oldTouchedWindowHandle->getName().c_str(), |
| 2061 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2062 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2063 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2064 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2065 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2066 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2067 | |
| 2068 | // Make a slippery entrance into the new window. |
| 2069 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2070 | isSplit = true; |
| 2071 | } |
| 2072 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2073 | int32_t targetFlags = |
| 2074 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2075 | if (isSplit) { |
| 2076 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2077 | } |
| 2078 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2079 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2080 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2081 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | BitSet32 pointerIds; |
| 2085 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2086 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2087 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2088 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2094 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2095 | // when dispatching it as is to newTouchedWindowHandle. |
| 2096 | if (mLastHoverWindowHandle != nullptr && |
| 2097 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2098 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2099 | #if DEBUG_HOVER |
| 2100 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2101 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2102 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2103 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2104 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2107 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2108 | // when dispatching it as is to newTouchedWindowHandle. |
| 2109 | if (newHoverWindowHandle != nullptr && |
| 2110 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2111 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 | #if DEBUG_HOVER |
| 2113 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2114 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2115 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2116 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2117 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2118 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | // Check permission to inject into all touched foreground windows and ensure there |
| 2123 | // is at least one touched foreground window. |
| 2124 | { |
| 2125 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2126 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2127 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2128 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2129 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2130 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2131 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2132 | goto Failed; |
| 2133 | } |
| 2134 | } |
| 2135 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2136 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2137 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2138 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2139 | "%" PRId32 " or gesture monitor to receive it.", |
| 2140 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2141 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2142 | goto Failed; |
| 2143 | } |
| 2144 | |
| 2145 | // Permission granted to injection into all touched foreground windows. |
| 2146 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2147 | } |
| 2148 | |
| 2149 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2150 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2151 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2152 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2153 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2154 | if (foregroundWindowHandle) { |
| 2155 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2156 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2157 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2158 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 2159 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2160 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 2161 | InputTarget::FLAG_ZERO_COORDS, |
| 2162 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2163 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2169 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2170 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2171 | // of the touch gesture. |
| 2172 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2173 | // engine only supports touch events. We would need to add a mechanism similar |
| 2174 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2175 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2176 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2177 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2178 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2179 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2180 | getWindowHandlesLocked(displayId); |
| 2181 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2182 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2183 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2184 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2185 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2186 | .addOrUpdateWindow(windowHandle, |
| 2187 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2188 | InputTarget:: |
| 2189 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2190 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2191 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2198 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2200 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2202 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | } |
| 2204 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2205 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2206 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2207 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | // Drop the outside or hover touch windows since we will not care about them |
| 2211 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2212 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | |
| 2214 | Failed: |
| 2215 | // Check injection permission once and for all. |
| 2216 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2217 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2218 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2219 | } else { |
| 2220 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2221 | } |
| 2222 | } |
| 2223 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2224 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2225 | return injectionResult; |
| 2226 | } |
| 2227 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2228 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2229 | if (!wrongDevice) { |
| 2230 | if (switchedDevice) { |
| 2231 | if (DEBUG_FOCUS) { |
| 2232 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2233 | } |
| 2234 | *outConflictingPointerActions = true; |
| 2235 | } |
| 2236 | |
| 2237 | if (isHoverAction) { |
| 2238 | // Started hovering, therefore no longer down. |
| 2239 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2240 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2241 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2242 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2243 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2244 | *outConflictingPointerActions = true; |
| 2245 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2246 | tempTouchState.reset(); |
| 2247 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2248 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2249 | tempTouchState.deviceId = entry.deviceId; |
| 2250 | tempTouchState.source = entry.source; |
| 2251 | tempTouchState.displayId = displayId; |
| 2252 | } |
| 2253 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2254 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2255 | // All pointers up or canceled. |
| 2256 | tempTouchState.reset(); |
| 2257 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2258 | // First pointer went down. |
| 2259 | if (oldState && oldState->down) { |
| 2260 | if (DEBUG_FOCUS) { |
| 2261 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2262 | } |
| 2263 | *outConflictingPointerActions = true; |
| 2264 | } |
| 2265 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2266 | // One pointer went up. |
| 2267 | if (isSplit) { |
| 2268 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2269 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2270 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2271 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2272 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2273 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2274 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2275 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2276 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2277 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2278 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2279 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2280 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2281 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2282 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2283 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2284 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2285 | // Save changes unless the action was scroll in which case the temporary touch |
| 2286 | // state was only valid for this one action. |
| 2287 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2288 | if (tempTouchState.displayId >= 0) { |
| 2289 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2290 | } else { |
| 2291 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2292 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2293 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2294 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2295 | // Update hover state. |
| 2296 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2297 | } |
| 2298 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2299 | return injectionResult; |
| 2300 | } |
| 2301 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2302 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
| 2303 | const sp<InputWindowHandle> dropWindow = |
| 2304 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
| 2305 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2306 | true /*ignoreDragWindow*/); |
| 2307 | if (dropWindow) { |
| 2308 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
| 2309 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2310 | } else { |
| 2311 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2312 | } |
| 2313 | mDragState.reset(); |
| 2314 | } |
| 2315 | |
| 2316 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2317 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2318 | return; |
| 2319 | } |
| 2320 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2321 | if (!mDragState->isStartDrag) { |
| 2322 | mDragState->isStartDrag = true; |
| 2323 | mDragState->isStylusButtonDownAtStart = |
| 2324 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2325 | } |
| 2326 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2327 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2328 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2329 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2330 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2331 | // Handle the special case : stylus button no longer pressed. |
| 2332 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2333 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2334 | finishDragAndDrop(entry.displayId, x, y); |
| 2335 | return; |
| 2336 | } |
| 2337 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2338 | const sp<InputWindowHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2339 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2340 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2341 | true /*ignoreDragWindow*/); |
| 2342 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2343 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2344 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2345 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2346 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2347 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2348 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2349 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2350 | } |
| 2351 | // enqueue drag location if needed. |
| 2352 | if (hoverWindowHandle != nullptr) { |
| 2353 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2354 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2355 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2356 | finishDragAndDrop(entry.displayId, x, y); |
| 2357 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2358 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2359 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2363 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2364 | int32_t targetFlags, BitSet32 pointerIds, |
| 2365 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2366 | std::vector<InputTarget>::iterator it = |
| 2367 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2368 | [&windowHandle](const InputTarget& inputTarget) { |
| 2369 | return inputTarget.inputChannel->getConnectionToken() == |
| 2370 | windowHandle->getToken(); |
| 2371 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2372 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2373 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (it == inputTargets.end()) { |
| 2376 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2377 | std::shared_ptr<InputChannel> inputChannel = |
| 2378 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2379 | if (inputChannel == nullptr) { |
| 2380 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2381 | return; |
| 2382 | } |
| 2383 | inputTarget.inputChannel = inputChannel; |
| 2384 | inputTarget.flags = targetFlags; |
| 2385 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2386 | inputTarget.displaySize = |
| 2387 | vec2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2388 | inputTargets.push_back(inputTarget); |
| 2389 | it = inputTargets.end() - 1; |
| 2390 | } |
| 2391 | |
| 2392 | ALOG_ASSERT(it->flags == targetFlags); |
| 2393 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2394 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2395 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2398 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2399 | int32_t displayId, float xOffset, |
| 2400 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2401 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2402 | mGlobalMonitorsByDisplay.find(displayId); |
| 2403 | |
| 2404 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2405 | const std::vector<Monitor>& monitors = it->second; |
| 2406 | for (const Monitor& monitor : monitors) { |
| 2407 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2408 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2412 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2413 | float yOffset, |
| 2414 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2415 | InputTarget target; |
| 2416 | target.inputChannel = monitor.inputChannel; |
| 2417 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2418 | ui::Transform t; |
| 2419 | t.set(xOffset, yOffset); |
| 2420 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2421 | inputTargets.push_back(target); |
| 2422 | } |
| 2423 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2424 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2425 | const InjectionState* injectionState) { |
| 2426 | if (injectionState && |
| 2427 | (windowHandle == nullptr || |
| 2428 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2429 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2430 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | 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] | 2432 | "owned by uid %d", |
| 2433 | injectionState->injectorPid, injectionState->injectorUid, |
| 2434 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2435 | } else { |
| 2436 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2437 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2438 | } |
| 2439 | return false; |
| 2440 | } |
| 2441 | return true; |
| 2442 | } |
| 2443 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2444 | /** |
| 2445 | * Indicate whether one window handle should be considered as obscuring |
| 2446 | * another window handle. We only check a few preconditions. Actually |
| 2447 | * checking the bounds is left to the caller. |
| 2448 | */ |
| 2449 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2450 | const sp<InputWindowHandle>& otherHandle) { |
| 2451 | // Compare by token so cloned layers aren't counted |
| 2452 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2453 | return false; |
| 2454 | } |
| 2455 | auto info = windowHandle->getInfo(); |
| 2456 | auto otherInfo = otherHandle->getInfo(); |
| 2457 | if (!otherInfo->visible) { |
| 2458 | return false; |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2459 | } else if (otherInfo->alpha == 0 && |
| 2460 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 2461 | // Those act as if they were invisible, so we don't need to flag them. |
| 2462 | // We do want to potentially flag touchable windows even if they have 0 |
| 2463 | // opacity, since they can consume touches and alter the effects of the |
| 2464 | // user interaction (eg. apps that rely on |
| 2465 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2466 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2467 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2468 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2469 | // If ownerUid is the same we don't generate occlusion events as there |
| 2470 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2471 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2472 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2473 | return false; |
| 2474 | } else if (otherInfo->displayId != info->displayId) { |
| 2475 | return false; |
| 2476 | } |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2480 | /** |
| 2481 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2482 | * untrusted, one should check: |
| 2483 | * |
| 2484 | * 1. If result.hasBlockingOcclusion is true. |
| 2485 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2486 | * BLOCK_UNTRUSTED. |
| 2487 | * |
| 2488 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2489 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2490 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2491 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2492 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2493 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2494 | * |
| 2495 | * If neither of those is true, then it means the touch can be allowed. |
| 2496 | */ |
| 2497 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
| 2498 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2499 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2500 | int32_t displayId = windowInfo->displayId; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2501 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2502 | TouchOcclusionInfo info; |
| 2503 | info.hasBlockingOcclusion = false; |
| 2504 | info.obscuringOpacity = 0; |
| 2505 | info.obscuringUid = -1; |
| 2506 | std::map<int32_t, float> opacityByUid; |
| 2507 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| 2508 | if (windowHandle == otherHandle) { |
| 2509 | break; // All future windows are below us. Exit early. |
| 2510 | } |
| 2511 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2512 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2513 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2514 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2515 | info.debugInfo.push_back( |
| 2516 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2517 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2518 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2519 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2520 | // window's touch occlusion mode |
| 2521 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2522 | info.hasBlockingOcclusion = true; |
| 2523 | info.obscuringUid = otherInfo->ownerUid; |
| 2524 | info.obscuringPackage = otherInfo->packageName; |
| 2525 | break; |
| 2526 | } |
| 2527 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2528 | uint32_t uid = otherInfo->ownerUid; |
| 2529 | float opacity = |
| 2530 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2531 | // Given windows A and B: |
| 2532 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2533 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2534 | opacityByUid[uid] = opacity; |
| 2535 | if (opacity > info.obscuringOpacity) { |
| 2536 | info.obscuringOpacity = opacity; |
| 2537 | info.obscuringUid = uid; |
| 2538 | info.obscuringPackage = otherInfo->packageName; |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2543 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2544 | info.debugInfo.push_back( |
| 2545 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2546 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2547 | return info; |
| 2548 | } |
| 2549 | |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2550 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, |
| 2551 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2552 | return StringPrintf(INDENT2 |
| 2553 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2554 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2555 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2556 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2557 | (isTouchedWindow) ? "[TOUCHED] " : "", |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2558 | NamedEnum::string(info->type, "%" PRId32).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2559 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2560 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2561 | info->frameTop, info->frameRight, info->frameBottom, |
| 2562 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2563 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2564 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2565 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2568 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2569 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2570 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2571 | occlusionInfo.obscuringUid); |
| 2572 | return false; |
| 2573 | } |
| 2574 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2575 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2576 | "%.2f, maximum allowed = %.2f)", |
| 2577 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2578 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2579 | return false; |
| 2580 | } |
| 2581 | return true; |
| 2582 | } |
| 2583 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2584 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2585 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2586 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2587 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2588 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2589 | if (windowHandle == otherHandle) { |
| 2590 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2591 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2592 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2593 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2594 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2595 | return true; |
| 2596 | } |
| 2597 | } |
| 2598 | return false; |
| 2599 | } |
| 2600 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2601 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2602 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2603 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2604 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2605 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2606 | if (windowHandle == otherHandle) { |
| 2607 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2608 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2609 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2610 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2611 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2612 | return true; |
| 2613 | } |
| 2614 | } |
| 2615 | return false; |
| 2616 | } |
| 2617 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2618 | std::string InputDispatcher::getApplicationWindowLabel( |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2619 | const InputApplicationHandle* applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2621 | if (applicationHandle != nullptr) { |
| 2622 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2623 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2624 | } else { |
| 2625 | return applicationHandle->getName(); |
| 2626 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2627 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2628 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2629 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2630 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2634 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2635 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2636 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2637 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2638 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2639 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2640 | return; |
| 2641 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2642 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2643 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2644 | if (focusedWindowHandle != nullptr) { |
| 2645 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2646 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2647 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2648 | 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] | 2649 | #endif |
| 2650 | return; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2655 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2656 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2657 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2658 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2659 | return; |
| 2660 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2661 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2662 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2663 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2664 | } |
| 2665 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2666 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2667 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2668 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2669 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2670 | return; |
| 2671 | } |
| 2672 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2673 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2674 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2675 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2676 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2677 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2678 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2679 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2680 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2681 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2682 | NamedEnum::string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2683 | break; |
| 2684 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2685 | } |
| 2686 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2687 | std::unique_ptr<CommandEntry> commandEntry = |
| 2688 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2689 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2690 | commandEntry->userActivityEventType = eventType; |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2691 | commandEntry->displayId = displayId; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2692 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2696 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2697 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2698 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2699 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2700 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2701 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2702 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2703 | ATRACE_NAME(message.c_str()); |
| 2704 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2705 | #if DEBUG_DISPATCH_CYCLE |
| 2706 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2707 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2708 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2709 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2710 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2711 | #endif |
| 2712 | |
| 2713 | // Skip this event if the connection status is not normal. |
| 2714 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2715 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2716 | #if DEBUG_DISPATCH_CYCLE |
| 2717 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2718 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2719 | #endif |
| 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2724 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2725 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2726 | "Entry type %s should not have FLAG_SPLIT", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2727 | NamedEnum::string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2728 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2729 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2730 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2731 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2732 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2733 | if (!splitMotionEntry) { |
| 2734 | return; // split event was dropped |
| 2735 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2736 | if (DEBUG_FOCUS) { |
| 2737 | ALOGD("channel '%s' ~ Split motion event.", |
| 2738 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2739 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2740 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2741 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2742 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2743 | return; |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2748 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2749 | } |
| 2750 | |
| 2751 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2752 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2753 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2754 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2755 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2756 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2757 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2758 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2759 | ATRACE_NAME(message.c_str()); |
| 2760 | } |
| 2761 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2762 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2763 | |
| 2764 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2765 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2766 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2767 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2768 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2769 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2770 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2771 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2772 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2773 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2774 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2775 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2776 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2777 | |
| 2778 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2779 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | startDispatchCycleLocked(currentTime, connection); |
| 2781 | } |
| 2782 | } |
| 2783 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2784 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2785 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2786 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2787 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2788 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2789 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2790 | connection->getInputChannelName().c_str(), |
| 2791 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2792 | ATRACE_NAME(message.c_str()); |
| 2793 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2794 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2795 | if (!(inputTargetFlags & dispatchMode)) { |
| 2796 | return; |
| 2797 | } |
| 2798 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2799 | |
| 2800 | // This is a new event. |
| 2801 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2802 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2803 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2804 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2805 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2806 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2807 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2808 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2809 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2810 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2811 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2812 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2813 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2814 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2815 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2816 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2817 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2818 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2819 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2820 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2821 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2822 | return; // skip the inconsistent event |
| 2823 | } |
| 2824 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2825 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2826 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2827 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2828 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2829 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2830 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2831 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2832 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2833 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2834 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2835 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2836 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2837 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2838 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2839 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2840 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2841 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2842 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2843 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2844 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2845 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2846 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2847 | } |
| 2848 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2849 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2850 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2851 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2852 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2853 | "event", |
| 2854 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2855 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2856 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2857 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2858 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2859 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2860 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2861 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2862 | } |
| 2863 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2864 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2865 | } |
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.trackMotion(motionEntry, 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 motion " |
| 2871 | "event", |
| 2872 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2873 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2874 | return; // skip the inconsistent event |
| 2875 | } |
| 2876 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2877 | dispatchEntry->resolvedEventId = |
| 2878 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2879 | ? mIdGenerator.nextId() |
| 2880 | : motionEntry.id; |
| 2881 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2882 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2883 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2884 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2885 | ATRACE_NAME(message.c_str()); |
| 2886 | } |
| 2887 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2888 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 2889 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 2890 | // Skip reporting pointer down outside focus to the policy. |
| 2891 | break; |
| 2892 | } |
| 2893 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2894 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2895 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2896 | |
| 2897 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2898 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2899 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2900 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2901 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2902 | break; |
| 2903 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2904 | case EventEntry::Type::SENSOR: { |
| 2905 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 2906 | break; |
| 2907 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2908 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2909 | case EventEntry::Type::DEVICE_RESET: { |
| 2910 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2911 | NamedEnum::string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2912 | break; |
| 2913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | // Remember that we are waiting for this dispatch to complete. |
| 2917 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2918 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2922 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 2923 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2924 | } |
| 2925 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2926 | /** |
| 2927 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2928 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2929 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2930 | * We will see both launcher and wallpaper in that list. |
| 2931 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2932 | * until the set of interacted connections changes. |
| 2933 | * |
| 2934 | * The following items are skipped, to reduce the logspam: |
| 2935 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2936 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2937 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2938 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2939 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2940 | */ |
| 2941 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2942 | const std::vector<InputTarget>& targets) { |
| 2943 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2944 | if (entry.type == EventEntry::Type::KEY) { |
| 2945 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2946 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2947 | return; |
| 2948 | } |
| 2949 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2950 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2951 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2952 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2953 | return; |
| 2954 | } |
| 2955 | } else { |
| 2956 | return; // Not a key or a motion |
| 2957 | } |
| 2958 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 2959 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2960 | std::vector<sp<Connection>> newConnections; |
| 2961 | for (const InputTarget& target : targets) { |
| 2962 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2963 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2964 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2965 | } |
| 2966 | |
| 2967 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2968 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2969 | if (connection == nullptr) { |
| 2970 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2971 | } |
| 2972 | newConnectionTokens.insert(std::move(token)); |
| 2973 | newConnections.emplace_back(connection); |
| 2974 | } |
| 2975 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 2976 | return; // no change |
| 2977 | } |
| 2978 | mInteractionConnectionTokens = newConnectionTokens; |
| 2979 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2980 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2981 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2982 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2983 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2984 | std::string message = "Interaction with: " + targetList; |
| 2985 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2986 | message += "<none>"; |
| 2987 | } |
| 2988 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 2989 | } |
| 2990 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2991 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2992 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2993 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2994 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 2995 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2996 | return; |
| 2997 | } |
| 2998 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 2999 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3000 | if (focusedToken == token) { |
| 3001 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3002 | return; |
| 3003 | } |
| 3004 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3005 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 3006 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3007 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3008 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3012 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3013 | if (ATRACE_ENABLED()) { |
| 3014 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3015 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3016 | ATRACE_NAME(message.c_str()); |
| 3017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3018 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3019 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3020 | #endif |
| 3021 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3022 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3023 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3024 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3025 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3026 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3027 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3028 | |
| 3029 | // Publish the event. |
| 3030 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3031 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3032 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3033 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3034 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3035 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3036 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3037 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3038 | status = connection->inputPublisher |
| 3039 | .publishKeyEvent(dispatchEntry->seq, |
| 3040 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3041 | keyEntry.source, keyEntry.displayId, |
| 3042 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3043 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3044 | keyEntry.scanCode, keyEntry.metaState, |
| 3045 | keyEntry.repeatCount, keyEntry.downTime, |
| 3046 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3047 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3050 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3051 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3052 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3053 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3054 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3055 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3056 | // 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] | 3057 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3058 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3059 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3060 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3061 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3062 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3063 | // Don't apply window scale here since we don't want scale to affect raw |
| 3064 | // coordinates. The scale will be sent back to the client and applied |
| 3065 | // later when requesting relative coordinates. |
| 3066 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3067 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3068 | } |
| 3069 | usingCoords = scaledCoords; |
| 3070 | } |
| 3071 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3072 | // We don't want the dispatch target to know. |
| 3073 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3074 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3075 | scaledCoords[i].clear(); |
| 3076 | } |
| 3077 | usingCoords = scaledCoords; |
| 3078 | } |
| 3079 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3080 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3081 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3082 | |
| 3083 | // Publish the motion event. |
| 3084 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3085 | .publishMotionEvent(dispatchEntry->seq, |
| 3086 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3087 | motionEntry.deviceId, motionEntry.source, |
| 3088 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3089 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3090 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3091 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3092 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3093 | motionEntry.buttonState, |
| 3094 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3095 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3096 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3097 | motionEntry.xCursorPosition, |
| 3098 | motionEntry.yCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3099 | dispatchEntry->displaySize.x, |
| 3100 | dispatchEntry->displaySize.y, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3101 | motionEntry.downTime, motionEntry.eventTime, |
| 3102 | motionEntry.pointerCount, |
| 3103 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3104 | break; |
| 3105 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3106 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3107 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3108 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3109 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3110 | focusEntry.id, |
| 3111 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3112 | mInTouchMode); |
| 3113 | break; |
| 3114 | } |
| 3115 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3116 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3117 | const auto& captureEntry = |
| 3118 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3119 | status = connection->inputPublisher |
| 3120 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
| 3121 | captureEntry.pointerCaptureEnabled); |
| 3122 | break; |
| 3123 | } |
| 3124 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3125 | case EventEntry::Type::DRAG: { |
| 3126 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3127 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3128 | dragEntry.id, dragEntry.x, |
| 3129 | dragEntry.y, |
| 3130 | dragEntry.isExiting); |
| 3131 | break; |
| 3132 | } |
| 3133 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3134 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3135 | case EventEntry::Type::DEVICE_RESET: |
| 3136 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3137 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3138 | NamedEnum::string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3139 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3140 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | // Check the result. |
| 3144 | if (status) { |
| 3145 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3146 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3147 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3148 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3149 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3150 | "event to it, status=%s(%d)", |
| 3151 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3152 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3153 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3154 | } else { |
| 3155 | // Pipe is full and we are waiting for the app to finish process some events |
| 3156 | // before sending more events to it. |
| 3157 | #if DEBUG_DISPATCH_CYCLE |
| 3158 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3159 | "waiting for the application to catch up", |
| 3160 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3161 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3162 | } |
| 3163 | } else { |
| 3164 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3165 | "status=%s(%d)", |
| 3166 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3167 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3168 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3169 | } |
| 3170 | return; |
| 3171 | } |
| 3172 | |
| 3173 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3174 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3175 | connection->outboundQueue.end(), |
| 3176 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 3177 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3178 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3179 | if (connection->responsive) { |
| 3180 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3181 | connection->inputChannel->getConnectionToken()); |
| 3182 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 3183 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3187 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3188 | size_t size; |
| 3189 | switch (event.type) { |
| 3190 | case VerifiedInputEvent::Type::KEY: { |
| 3191 | size = sizeof(VerifiedKeyEvent); |
| 3192 | break; |
| 3193 | } |
| 3194 | case VerifiedInputEvent::Type::MOTION: { |
| 3195 | size = sizeof(VerifiedMotionEvent); |
| 3196 | break; |
| 3197 | } |
| 3198 | } |
| 3199 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3200 | return mHmacKeyManager.sign(start, size); |
| 3201 | } |
| 3202 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3203 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3204 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3205 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3206 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3207 | // Only sign events up and down events as the purely move events |
| 3208 | // are tied to their up/down counterparts so signing would be redundant. |
| 3209 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3210 | verifiedEvent.actionMasked = actionMasked; |
| 3211 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3212 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3213 | } |
| 3214 | return INVALID_HMAC; |
| 3215 | } |
| 3216 | |
| 3217 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3218 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3219 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3220 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3221 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3222 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3225 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3226 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3227 | bool handled, nsecs_t consumeTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3228 | #if DEBUG_DISPATCH_CYCLE |
| 3229 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3230 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3231 | #endif |
| 3232 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3233 | if (connection->status == Connection::STATUS_BROKEN || |
| 3234 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3235 | return; |
| 3236 | } |
| 3237 | |
| 3238 | // Notify other system components and prepare to start the next dispatch cycle. |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3239 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3243 | const sp<Connection>& connection, |
| 3244 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3245 | #if DEBUG_DISPATCH_CYCLE |
| 3246 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3247 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3248 | #endif |
| 3249 | |
| 3250 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3251 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 3252 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3253 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 3254 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3255 | |
| 3256 | // The connection appears to be unrecoverably broken. |
| 3257 | // Ignore already broken or zombie connections. |
| 3258 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3259 | connection->status = Connection::STATUS_BROKEN; |
| 3260 | |
| 3261 | if (notify) { |
| 3262 | // Notify other system components. |
| 3263 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3268 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3269 | while (!queue.empty()) { |
| 3270 | DispatchEntry* dispatchEntry = queue.front(); |
| 3271 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3272 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3276 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3277 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3278 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3279 | } |
| 3280 | delete dispatchEntry; |
| 3281 | } |
| 3282 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3283 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3284 | std::scoped_lock _l(mLock); |
| 3285 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3286 | if (connection == nullptr) { |
| 3287 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3288 | connectionToken.get(), events); |
| 3289 | return 0; // remove the callback |
| 3290 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3291 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3292 | bool notify; |
| 3293 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3294 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3295 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3296 | "events=0x%x", |
| 3297 | connection->getInputChannelName().c_str(), events); |
| 3298 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3299 | } |
| 3300 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3301 | nsecs_t currentTime = now(); |
| 3302 | bool gotOne = false; |
| 3303 | status_t status = OK; |
| 3304 | for (;;) { |
| 3305 | Result<InputPublisher::ConsumerResponse> result = |
| 3306 | connection->inputPublisher.receiveConsumerResponse(); |
| 3307 | if (!result.ok()) { |
| 3308 | status = result.error().code(); |
| 3309 | break; |
| 3310 | } |
| 3311 | |
| 3312 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3313 | const InputPublisher::Finished& finish = |
| 3314 | std::get<InputPublisher::Finished>(*result); |
| 3315 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3316 | finish.consumeTime); |
| 3317 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
| 3318 | // TODO(b/167947340): Report this data to LatencyTracker |
| 3319 | } |
| 3320 | gotOne = true; |
| 3321 | } |
| 3322 | if (gotOne) { |
| 3323 | runCommandsLockedInterruptible(); |
| 3324 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3325 | return 1; |
| 3326 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | } |
| 3328 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3329 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3330 | if (notify) { |
| 3331 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3332 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3333 | status); |
| 3334 | } |
| 3335 | } else { |
| 3336 | // Monitor channels are never explicitly unregistered. |
| 3337 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3338 | const bool stillHaveWindowHandle = |
| 3339 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3340 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3341 | if (notify) { |
| 3342 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3343 | connection->getInputChannelName().c_str(), events); |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | // Remove the channel. |
| 3348 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3349 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3350 | } |
| 3351 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3352 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3353 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3354 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3355 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3359 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3360 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3361 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3362 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3363 | } |
| 3364 | |
| 3365 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3366 | const CancelationOptions& options, |
| 3367 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3368 | for (const auto& it : monitorsByDisplay) { |
| 3369 | const std::vector<Monitor>& monitors = it.second; |
| 3370 | for (const Monitor& monitor : monitors) { |
| 3371 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3372 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3373 | } |
| 3374 | } |
| 3375 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3376 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3377 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3378 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3379 | if (connection == nullptr) { |
| 3380 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3381 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3382 | |
| 3383 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3387 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3388 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3389 | return; |
| 3390 | } |
| 3391 | |
| 3392 | nsecs_t currentTime = now(); |
| 3393 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3394 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3395 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3396 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3397 | if (cancelationEvents.empty()) { |
| 3398 | return; |
| 3399 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3400 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3401 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3402 | "with reality: %s, mode=%d.", |
| 3403 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3404 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3405 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3406 | |
| 3407 | InputTarget target; |
| 3408 | sp<InputWindowHandle> windowHandle = |
| 3409 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3410 | if (windowHandle != nullptr) { |
| 3411 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3412 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3413 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3414 | } |
| 3415 | target.inputChannel = connection->inputChannel; |
| 3416 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3417 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3418 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3419 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3420 | switch (cancelationEventEntry->type) { |
| 3421 | case EventEntry::Type::KEY: { |
| 3422 | logOutboundKeyDetails("cancel - ", |
| 3423 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3424 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3425 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3426 | case EventEntry::Type::MOTION: { |
| 3427 | logOutboundMotionDetails("cancel - ", |
| 3428 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3429 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3430 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3431 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3432 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3433 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3434 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3435 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3436 | break; |
| 3437 | } |
| 3438 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3439 | case EventEntry::Type::DEVICE_RESET: |
| 3440 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3441 | 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] | 3442 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3443 | break; |
| 3444 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3445 | } |
| 3446 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3447 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3448 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3449 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3450 | |
| 3451 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3454 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3455 | const sp<Connection>& connection) { |
| 3456 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3457 | return; |
| 3458 | } |
| 3459 | |
| 3460 | nsecs_t currentTime = now(); |
| 3461 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3462 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3463 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3464 | |
| 3465 | if (downEvents.empty()) { |
| 3466 | return; |
| 3467 | } |
| 3468 | |
| 3469 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3470 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3471 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3472 | #endif |
| 3473 | |
| 3474 | InputTarget target; |
| 3475 | sp<InputWindowHandle> windowHandle = |
| 3476 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3477 | if (windowHandle != nullptr) { |
| 3478 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3479 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3480 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3481 | } |
| 3482 | target.inputChannel = connection->inputChannel; |
| 3483 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3484 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3485 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3486 | switch (downEventEntry->type) { |
| 3487 | case EventEntry::Type::MOTION: { |
| 3488 | logOutboundMotionDetails("down - ", |
| 3489 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3490 | break; |
| 3491 | } |
| 3492 | |
| 3493 | case EventEntry::Type::KEY: |
| 3494 | case EventEntry::Type::FOCUS: |
| 3495 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3496 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3497 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3498 | case EventEntry::Type::SENSOR: |
| 3499 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3500 | 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] | 3501 | NamedEnum::string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3502 | break; |
| 3503 | } |
| 3504 | } |
| 3505 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3506 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3507 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | startDispatchCycleLocked(currentTime, connection); |
| 3511 | } |
| 3512 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3513 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3514 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3515 | ALOG_ASSERT(pointerIds.value != 0); |
| 3516 | |
| 3517 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3518 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3519 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3520 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3521 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | uint32_t splitPointerCount = 0; |
| 3523 | |
| 3524 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3525 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3527 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3529 | if (pointerIds.hasBit(pointerId)) { |
| 3530 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3531 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3532 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3533 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | splitPointerCount += 1; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | if (splitPointerCount != pointerIds.count()) { |
| 3539 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3540 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3541 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3542 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3543 | // in this way. |
| 3544 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3545 | "we expected there to be %d pointers. This probably means we received " |
| 3546 | "a broken sequence of pointer ids from the input device.", |
| 3547 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3548 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3549 | } |
| 3550 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3551 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3552 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3553 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3554 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3555 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3556 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3557 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3558 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3559 | if (pointerIds.hasBit(pointerId)) { |
| 3560 | if (pointerIds.count() == 1) { |
| 3561 | // The first/last pointer went down/up. |
| 3562 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3563 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3564 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3565 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3566 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3567 | } else { |
| 3568 | // A secondary pointer went down/up. |
| 3569 | uint32_t splitPointerIndex = 0; |
| 3570 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3571 | splitPointerIndex += 1; |
| 3572 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3573 | action = maskedAction | |
| 3574 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3575 | } |
| 3576 | } else { |
| 3577 | // An unrelated pointer changed. |
| 3578 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3579 | } |
| 3580 | } |
| 3581 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3582 | int32_t newId = mIdGenerator.nextId(); |
| 3583 | if (ATRACE_ENABLED()) { |
| 3584 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3585 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3586 | originalMotionEntry.id, newId); |
| 3587 | ATRACE_NAME(message.c_str()); |
| 3588 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3589 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3590 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3591 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3592 | originalMotionEntry.displayId, |
| 3593 | originalMotionEntry.policyFlags, action, |
| 3594 | originalMotionEntry.actionButton, |
| 3595 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3596 | originalMotionEntry.buttonState, |
| 3597 | originalMotionEntry.classification, |
| 3598 | originalMotionEntry.edgeFlags, |
| 3599 | originalMotionEntry.xPrecision, |
| 3600 | originalMotionEntry.yPrecision, |
| 3601 | originalMotionEntry.xCursorPosition, |
| 3602 | originalMotionEntry.yCursorPosition, |
| 3603 | originalMotionEntry.downTime, splitPointerCount, |
| 3604 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3605 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3606 | if (originalMotionEntry.injectionState) { |
| 3607 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3608 | splitMotionEntry->injectionState->refCount += 1; |
| 3609 | } |
| 3610 | |
| 3611 | return splitMotionEntry; |
| 3612 | } |
| 3613 | |
| 3614 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3615 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3616 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | #endif |
| 3618 | |
| 3619 | bool needWake; |
| 3620 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3621 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3622 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3623 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3624 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3625 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | } // release lock |
| 3627 | |
| 3628 | if (needWake) { |
| 3629 | mLooper->wake(); |
| 3630 | } |
| 3631 | } |
| 3632 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3633 | /** |
| 3634 | * If one of the meta shortcuts is detected, process them here: |
| 3635 | * Meta + Backspace -> generate BACK |
| 3636 | * Meta + Enter -> generate HOME |
| 3637 | * This will potentially overwrite keyCode and metaState. |
| 3638 | */ |
| 3639 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3640 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3641 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3642 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3643 | if (keyCode == AKEYCODE_DEL) { |
| 3644 | newKeyCode = AKEYCODE_BACK; |
| 3645 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3646 | newKeyCode = AKEYCODE_HOME; |
| 3647 | } |
| 3648 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3649 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3650 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3651 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3652 | keyCode = newKeyCode; |
| 3653 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3654 | } |
| 3655 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3656 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3657 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3658 | // 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] | 3659 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3660 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3661 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3662 | if (replacementIt != mReplacedKeys.end()) { |
| 3663 | keyCode = replacementIt->second; |
| 3664 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3665 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3670 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3671 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3672 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3673 | "policyFlags=0x%x, action=0x%x, " |
| 3674 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3675 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3676 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3677 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3678 | #endif |
| 3679 | if (!validateKeyEvent(args->action)) { |
| 3680 | return; |
| 3681 | } |
| 3682 | |
| 3683 | uint32_t policyFlags = args->policyFlags; |
| 3684 | int32_t flags = args->flags; |
| 3685 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3686 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3687 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3688 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3689 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3690 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3691 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3692 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3693 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3694 | metaState |= AMETA_FUNCTION_ON; |
| 3695 | } |
| 3696 | |
| 3697 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3698 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3699 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3700 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3701 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3702 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3703 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3704 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3705 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3706 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3707 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3709 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3710 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3711 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3712 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3713 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3714 | bool needWake; |
| 3715 | { // acquire lock |
| 3716 | mLock.lock(); |
| 3717 | |
| 3718 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3719 | mLock.unlock(); |
| 3720 | |
| 3721 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3722 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3723 | return; // event was consumed by the filter |
| 3724 | } |
| 3725 | |
| 3726 | mLock.lock(); |
| 3727 | } |
| 3728 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3729 | std::unique_ptr<KeyEntry> newEntry = |
| 3730 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3731 | args->displayId, policyFlags, args->action, flags, |
| 3732 | keyCode, args->scanCode, metaState, repeatCount, |
| 3733 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3734 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3735 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 | mLock.unlock(); |
| 3737 | } // release lock |
| 3738 | |
| 3739 | if (needWake) { |
| 3740 | mLooper->wake(); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3745 | return mInputFilterEnabled; |
| 3746 | } |
| 3747 | |
| 3748 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3749 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3750 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3751 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3752 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3753 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3754 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3755 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3756 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3757 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3758 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3759 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3760 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3761 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3762 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3763 | "orientation=%f", |
| 3764 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3765 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3766 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3767 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3768 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3769 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3770 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3771 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3772 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3773 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | } |
| 3775 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3776 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3777 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3778 | return; |
| 3779 | } |
| 3780 | |
| 3781 | uint32_t policyFlags = args->policyFlags; |
| 3782 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3783 | |
| 3784 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3785 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3786 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3787 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3790 | |
| 3791 | bool needWake; |
| 3792 | { // acquire lock |
| 3793 | mLock.lock(); |
| 3794 | |
| 3795 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3796 | mLock.unlock(); |
| 3797 | |
| 3798 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3799 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3800 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3801 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3802 | args->metaState, args->buttonState, args->classification, transform, |
| 3803 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3804 | args->yCursorPosition, AMOTION_EVENT_INVALID_DISPLAY_SIZE, |
| 3805 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3806 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3807 | |
| 3808 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3809 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3810 | return; // event was consumed by the filter |
| 3811 | } |
| 3812 | |
| 3813 | mLock.lock(); |
| 3814 | } |
| 3815 | |
| 3816 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3817 | std::unique_ptr<MotionEntry> newEntry = |
| 3818 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3819 | args->source, args->displayId, policyFlags, |
| 3820 | args->action, args->actionButton, args->flags, |
| 3821 | args->metaState, args->buttonState, |
| 3822 | args->classification, args->edgeFlags, |
| 3823 | args->xPrecision, args->yPrecision, |
| 3824 | args->xCursorPosition, args->yCursorPosition, |
| 3825 | args->downTime, args->pointerCount, |
| 3826 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3827 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3828 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3829 | mLock.unlock(); |
| 3830 | } // release lock |
| 3831 | |
| 3832 | if (needWake) { |
| 3833 | mLooper->wake(); |
| 3834 | } |
| 3835 | } |
| 3836 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3837 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
| 3838 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3839 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3840 | " sensorType=%s", |
| 3841 | args->id, args->eventTime, args->deviceId, args->source, |
| 3842 | NamedEnum::string(args->sensorType).c_str()); |
| 3843 | #endif |
| 3844 | |
| 3845 | bool needWake; |
| 3846 | { // acquire lock |
| 3847 | mLock.lock(); |
| 3848 | |
| 3849 | // Just enqueue a new sensor event. |
| 3850 | std::unique_ptr<SensorEntry> newEntry = |
| 3851 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 3852 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 3853 | args->sensorType, args->accuracy, |
| 3854 | args->accuracyChanged, args->values); |
| 3855 | |
| 3856 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 3857 | mLock.unlock(); |
| 3858 | } // release lock |
| 3859 | |
| 3860 | if (needWake) { |
| 3861 | mLooper->wake(); |
| 3862 | } |
| 3863 | } |
| 3864 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3865 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
| 3866 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3867 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 3868 | args->deviceId, args->isOn); |
| 3869 | #endif |
| 3870 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 3871 | } |
| 3872 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3874 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3878 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3879 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3880 | "switchMask=0x%08x", |
| 3881 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3882 | #endif |
| 3883 | |
| 3884 | uint32_t policyFlags = args->policyFlags; |
| 3885 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3886 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3890 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3891 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3892 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | #endif |
| 3894 | |
| 3895 | bool needWake; |
| 3896 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3897 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3898 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3899 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 3900 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 3901 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3902 | } // release lock |
| 3903 | |
| 3904 | if (needWake) { |
| 3905 | mLooper->wake(); |
| 3906 | } |
| 3907 | } |
| 3908 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3909 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
| 3910 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3911 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
| 3912 | args->enabled ? "true" : "false"); |
| 3913 | #endif |
| 3914 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3915 | bool needWake; |
| 3916 | { // acquire lock |
| 3917 | std::scoped_lock _l(mLock); |
| 3918 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
| 3919 | args->enabled); |
| 3920 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 3921 | } // release lock |
| 3922 | |
| 3923 | if (needWake) { |
| 3924 | mLooper->wake(); |
| 3925 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3926 | } |
| 3927 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3928 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 3929 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 3930 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3932 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3933 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3934 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3935 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3936 | 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] | 3937 | |
| 3938 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3939 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3940 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3941 | } |
| 3942 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3943 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3944 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3945 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3946 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3947 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3948 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3949 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3950 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3951 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3952 | int32_t flags = incomingKey.getFlags(); |
| 3953 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3954 | int32_t metaState = incomingKey.getMetaState(); |
| 3955 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3956 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3957 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3958 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3959 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3960 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3961 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3962 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3963 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3964 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3965 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3966 | |
| 3967 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3968 | android::base::Timer t; |
| 3969 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3970 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3971 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3972 | std::to_string(t.duration().count()).c_str()); |
| 3973 | } |
| 3974 | } |
| 3975 | |
| 3976 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3977 | std::unique_ptr<KeyEntry> injectedEntry = |
| 3978 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
| 3979 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
| 3980 | incomingKey.getDisplayId(), policyFlags, action, |
| 3981 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 3982 | incomingKey.getRepeatCount(), |
| 3983 | incomingKey.getDownTime()); |
| 3984 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3985 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3986 | } |
| 3987 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3988 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3989 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3990 | int32_t action = motionEvent->getAction(); |
| 3991 | size_t pointerCount = motionEvent->getPointerCount(); |
| 3992 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3993 | int32_t actionButton = motionEvent->getActionButton(); |
| 3994 | int32_t displayId = motionEvent->getDisplayId(); |
| 3995 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3996 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4000 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 4001 | android::base::Timer t; |
| 4002 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4003 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4004 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4005 | std::to_string(t.duration().count()).c_str()); |
| 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | mLock.lock(); |
| 4010 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 4011 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4012 | std::unique_ptr<MotionEntry> injectedEntry = |
| 4013 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, |
| 4014 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
| 4015 | motionEvent->getDisplayId(), policyFlags, action, |
| 4016 | actionButton, motionEvent->getFlags(), |
| 4017 | motionEvent->getMetaState(), |
| 4018 | motionEvent->getButtonState(), |
| 4019 | motionEvent->getClassification(), |
| 4020 | motionEvent->getEdgeFlags(), |
| 4021 | motionEvent->getXPrecision(), |
| 4022 | motionEvent->getYPrecision(), |
| 4023 | motionEvent->getRawXCursorPosition(), |
| 4024 | motionEvent->getRawYCursorPosition(), |
| 4025 | motionEvent->getDownTime(), |
| 4026 | uint32_t(pointerCount), pointerProperties, |
| 4027 | samplePointerCoords, motionEvent->getXOffset(), |
| 4028 | motionEvent->getYOffset()); |
| 4029 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4030 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 4031 | sampleEventTimes += 1; |
| 4032 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4033 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
| 4034 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, |
| 4035 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
| 4036 | motionEvent->getDisplayId(), policyFlags, |
| 4037 | action, actionButton, motionEvent->getFlags(), |
| 4038 | motionEvent->getMetaState(), |
| 4039 | motionEvent->getButtonState(), |
| 4040 | motionEvent->getClassification(), |
| 4041 | motionEvent->getEdgeFlags(), |
| 4042 | motionEvent->getXPrecision(), |
| 4043 | motionEvent->getYPrecision(), |
| 4044 | motionEvent->getRawXCursorPosition(), |
| 4045 | motionEvent->getRawYCursorPosition(), |
| 4046 | motionEvent->getDownTime(), |
| 4047 | uint32_t(pointerCount), pointerProperties, |
| 4048 | samplePointerCoords, |
| 4049 | motionEvent->getXOffset(), |
| 4050 | motionEvent->getYOffset()); |
| 4051 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4052 | } |
| 4053 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4054 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4055 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4056 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4057 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4058 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4062 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | injectionState->injectionIsAsync = true; |
| 4064 | } |
| 4065 | |
| 4066 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4067 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4068 | |
| 4069 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4070 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4071 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4072 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | mLock.unlock(); |
| 4076 | |
| 4077 | if (needWake) { |
| 4078 | mLooper->wake(); |
| 4079 | } |
| 4080 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4081 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4082 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4083 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4084 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4085 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4086 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4087 | } else { |
| 4088 | for (;;) { |
| 4089 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4090 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4091 | break; |
| 4092 | } |
| 4093 | |
| 4094 | nsecs_t remainingTimeout = endTime - now(); |
| 4095 | if (remainingTimeout <= 0) { |
| 4096 | #if DEBUG_INJECTION |
| 4097 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4098 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4099 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4100 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4101 | break; |
| 4102 | } |
| 4103 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4104 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4105 | } |
| 4106 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4107 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4108 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4109 | while (injectionState->pendingForegroundDispatches != 0) { |
| 4110 | #if DEBUG_INJECTION |
| 4111 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4112 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | #endif |
| 4114 | nsecs_t remainingTimeout = endTime - now(); |
| 4115 | if (remainingTimeout <= 0) { |
| 4116 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4117 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4118 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4120 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | break; |
| 4122 | } |
| 4123 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4124 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4125 | } |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | injectionState->release(); |
| 4130 | } // release lock |
| 4131 | |
| 4132 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4133 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4134 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | #endif |
| 4136 | |
| 4137 | return injectionResult; |
| 4138 | } |
| 4139 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4140 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4141 | std::array<uint8_t, 32> calculatedHmac; |
| 4142 | std::unique_ptr<VerifiedInputEvent> result; |
| 4143 | switch (event.getType()) { |
| 4144 | case AINPUT_EVENT_TYPE_KEY: { |
| 4145 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4146 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4147 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4148 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4149 | break; |
| 4150 | } |
| 4151 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4152 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4153 | VerifiedMotionEvent verifiedMotionEvent = |
| 4154 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4155 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4156 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4157 | break; |
| 4158 | } |
| 4159 | default: { |
| 4160 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4161 | return nullptr; |
| 4162 | } |
| 4163 | } |
| 4164 | if (calculatedHmac == INVALID_HMAC) { |
| 4165 | return nullptr; |
| 4166 | } |
| 4167 | if (calculatedHmac != event.getHmac()) { |
| 4168 | return nullptr; |
| 4169 | } |
| 4170 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4171 | } |
| 4172 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4173 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4174 | return injectorUid == 0 || |
| 4175 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4178 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4179 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4180 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4181 | if (injectionState) { |
| 4182 | #if DEBUG_INJECTION |
| 4183 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4184 | "injectorPid=%d, injectorUid=%d", |
| 4185 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4186 | #endif |
| 4187 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4188 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4189 | // Log the outcome since the injector did not wait for the injection result. |
| 4190 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4191 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4192 | ALOGV("Asynchronous input event injection succeeded."); |
| 4193 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4194 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4195 | ALOGW("Asynchronous input event injection failed."); |
| 4196 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4197 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4198 | ALOGW("Asynchronous input event injection permission denied."); |
| 4199 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4200 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4201 | ALOGW("Asynchronous input event injection timed out."); |
| 4202 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4203 | case InputEventInjectionResult::PENDING: |
| 4204 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4205 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4206 | } |
| 4207 | } |
| 4208 | |
| 4209 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4210 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4214 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4215 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | if (injectionState) { |
| 4217 | injectionState->pendingForegroundDispatches += 1; |
| 4218 | } |
| 4219 | } |
| 4220 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4221 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4222 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4223 | if (injectionState) { |
| 4224 | injectionState->pendingForegroundDispatches -= 1; |
| 4225 | |
| 4226 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4227 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4228 | } |
| 4229 | } |
| 4230 | } |
| 4231 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4232 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4233 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4234 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 4235 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4236 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4237 | } |
| 4238 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4239 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4240 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4241 | if (windowHandleToken == nullptr) { |
| 4242 | return nullptr; |
| 4243 | } |
| 4244 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4245 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4246 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4247 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4248 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4249 | return windowHandle; |
| 4250 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4251 | } |
| 4252 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4253 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4254 | } |
| 4255 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4256 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4257 | int displayId) const { |
| 4258 | if (windowHandleToken == nullptr) { |
| 4259 | return nullptr; |
| 4260 | } |
| 4261 | |
| 4262 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 4263 | if (windowHandle->getToken() == windowHandleToken) { |
| 4264 | return windowHandle; |
| 4265 | } |
| 4266 | } |
| 4267 | return nullptr; |
| 4268 | } |
| 4269 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4270 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
| 4271 | const sp<InputWindowHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4272 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4273 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4274 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4275 | if (handle->getId() == windowHandle->getId() && |
| 4276 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4277 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4278 | ALOGE("Found window %s in display %" PRId32 |
| 4279 | ", but it should belong to display %" PRId32, |
| 4280 | windowHandle->getName().c_str(), it.first, |
| 4281 | windowHandle->getInfo()->displayId); |
| 4282 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4283 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4284 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4285 | } |
| 4286 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4287 | return nullptr; |
| 4288 | } |
| 4289 | |
| 4290 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 4291 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4292 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4293 | } |
| 4294 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4295 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 4296 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4297 | const bool noInputChannel = |
| 4298 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4299 | if (connection != nullptr && noInputChannel) { |
| 4300 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4301 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4302 | return false; |
| 4303 | } |
| 4304 | |
| 4305 | if (connection == nullptr) { |
| 4306 | if (!noInputChannel) { |
| 4307 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4308 | } |
| 4309 | return false; |
| 4310 | } |
| 4311 | if (!connection->responsive) { |
| 4312 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4313 | return false; |
| 4314 | } |
| 4315 | return true; |
| 4316 | } |
| 4317 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4318 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4319 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4320 | auto connectionIt = mConnectionsByToken.find(token); |
| 4321 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4322 | return nullptr; |
| 4323 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4324 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4325 | } |
| 4326 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4327 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 4328 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 4329 | if (inputWindowHandles.empty()) { |
| 4330 | // Remove all handles on a display if there are no windows left. |
| 4331 | mWindowHandlesByDisplay.erase(displayId); |
| 4332 | return; |
| 4333 | } |
| 4334 | |
| 4335 | // Since we compare the pointer of input window handles across window updates, we need |
| 4336 | // to make sure the handle object for the same window stays unchanged across updates. |
| 4337 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4338 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4339 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4340 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | std::vector<sp<InputWindowHandle>> newHandles; |
| 4344 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 4345 | if (!handle->updateInfo()) { |
| 4346 | // handle no longer valid |
| 4347 | continue; |
| 4348 | } |
| 4349 | |
| 4350 | const InputWindowInfo* info = handle->getInfo(); |
| 4351 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 4352 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 4353 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4354 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4355 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 4356 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4357 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4358 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4359 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4360 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4361 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | if (info->displayId != displayId) { |
| 4365 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4366 | handle->getName().c_str(), displayId, info->displayId); |
| 4367 | continue; |
| 4368 | } |
| 4369 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4370 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4371 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4372 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4373 | oldHandle->updateFrom(handle); |
| 4374 | newHandles.push_back(oldHandle); |
| 4375 | } else { |
| 4376 | newHandles.push_back(handle); |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | // Insert or replace |
| 4381 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4382 | } |
| 4383 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4384 | void InputDispatcher::setInputWindows( |
| 4385 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 4386 | { // acquire lock |
| 4387 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4388 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4389 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4390 | } |
| 4391 | } |
| 4392 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4393 | mLooper->wake(); |
| 4394 | } |
| 4395 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4396 | /** |
| 4397 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4398 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4399 | * If set an empty list, remove all handles from the specific display. |
| 4400 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4401 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4402 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4403 | void InputDispatcher::setInputWindowsLocked( |
| 4404 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4405 | if (DEBUG_FOCUS) { |
| 4406 | std::string windowList; |
| 4407 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 4408 | windowList += iwh->getName() + " "; |
| 4409 | } |
| 4410 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4411 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4412 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4413 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4414 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 4415 | const bool noInputWindow = |
| 4416 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4417 | if (noInputWindow && window->getToken() != nullptr) { |
| 4418 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4419 | window->getName().c_str()); |
| 4420 | window->releaseChannel(); |
| 4421 | } |
| 4422 | } |
| 4423 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4424 | // Copy old handles for release if they are no longer present. |
| 4425 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4427 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4428 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4429 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 4430 | if (mLastHoverWindowHandle && |
| 4431 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4432 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4433 | mLastHoverWindowHandle = nullptr; |
| 4434 | } |
| 4435 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4436 | std::optional<FocusResolver::FocusChanges> changes = |
| 4437 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4438 | if (changes) { |
| 4439 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4440 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4441 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4442 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4443 | mTouchStatesByDisplay.find(displayId); |
| 4444 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4445 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4446 | for (size_t i = 0; i < state.windows.size();) { |
| 4447 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4448 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4449 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4450 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4451 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4452 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4453 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4454 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4455 | if (touchedInputChannel != nullptr) { |
| 4456 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4457 | "touched window was removed"); |
| 4458 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4460 | state.windows.erase(state.windows.begin() + i); |
| 4461 | } else { |
| 4462 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4463 | } |
| 4464 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4465 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4466 | // 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] | 4467 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4468 | if (mDragState && |
| 4469 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4470 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4471 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4472 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4473 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4474 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4475 | // Release information for windows that are no longer present. |
| 4476 | // This ensures that unused input channels are released promptly. |
| 4477 | // Otherwise, they might stick around until the window handle is destroyed |
| 4478 | // which might not happen until the next GC. |
| 4479 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4480 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4481 | if (DEBUG_FOCUS) { |
| 4482 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4483 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4484 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4485 | // To avoid making too many calls into the compat framework, only |
| 4486 | // check for window flags when windows are going away. |
| 4487 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4488 | // in order to gather some data about the flag usage |
| 4489 | if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { |
| 4490 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4491 | oldWindowHandle->getName().c_str()); |
| 4492 | if (mCompatService != nullptr) { |
| 4493 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4494 | oldWindowHandle->getInfo()->ownerUid); |
| 4495 | } |
| 4496 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4497 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4498 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4502 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4503 | if (DEBUG_FOCUS) { |
| 4504 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4505 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4506 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4507 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4508 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4509 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4510 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4511 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4512 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4513 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4514 | 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] | 4515 | } |
| 4516 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4517 | // Set the new application handle. |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4518 | if (inputApplicationHandle != nullptr) { |
| 4519 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4520 | } else { |
| 4521 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4522 | } |
| 4523 | |
| 4524 | // No matter what the old focused application was, stop waiting on it because it is |
| 4525 | // no longer focused. |
| 4526 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | } // release lock |
| 4528 | |
| 4529 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4530 | mLooper->wake(); |
| 4531 | } |
| 4532 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4533 | /** |
| 4534 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4535 | * the display not specified. |
| 4536 | * |
| 4537 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4538 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4539 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4540 | * display. The display-specified events won't be affected. |
| 4541 | */ |
| 4542 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4543 | if (DEBUG_FOCUS) { |
| 4544 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4545 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4546 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4547 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4548 | |
| 4549 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4550 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4551 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4552 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4553 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4554 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4555 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4556 | CancelationOptions |
| 4557 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4558 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4559 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4560 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4561 | } |
| 4562 | } |
| 4563 | mFocusedDisplayId = displayId; |
| 4564 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4565 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4566 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4567 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4568 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4569 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4570 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4571 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4572 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4573 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4574 | } |
| 4575 | } |
| 4576 | } |
| 4577 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4578 | if (DEBUG_FOCUS) { |
| 4579 | logDispatchStateLocked(); |
| 4580 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4581 | } // release lock |
| 4582 | |
| 4583 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4584 | mLooper->wake(); |
| 4585 | } |
| 4586 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4587 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4588 | if (DEBUG_FOCUS) { |
| 4589 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4591 | |
| 4592 | bool changed; |
| 4593 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4594 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4595 | |
| 4596 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4597 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4598 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | if (mDispatchEnabled && !enabled) { |
| 4602 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4603 | } |
| 4604 | |
| 4605 | mDispatchEnabled = enabled; |
| 4606 | mDispatchFrozen = frozen; |
| 4607 | changed = true; |
| 4608 | } else { |
| 4609 | changed = false; |
| 4610 | } |
| 4611 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4612 | if (DEBUG_FOCUS) { |
| 4613 | logDispatchStateLocked(); |
| 4614 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4615 | } // release lock |
| 4616 | |
| 4617 | if (changed) { |
| 4618 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4619 | mLooper->wake(); |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4624 | if (DEBUG_FOCUS) { |
| 4625 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4626 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4627 | |
| 4628 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4629 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4630 | |
| 4631 | if (mInputFilterEnabled == enabled) { |
| 4632 | return; |
| 4633 | } |
| 4634 | |
| 4635 | mInputFilterEnabled = enabled; |
| 4636 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4637 | } // release lock |
| 4638 | |
| 4639 | // Wake up poll loop since there might be work to do to drop everything. |
| 4640 | mLooper->wake(); |
| 4641 | } |
| 4642 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4643 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4644 | std::scoped_lock lock(mLock); |
| 4645 | mInTouchMode = inTouchMode; |
| 4646 | } |
| 4647 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4648 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4649 | if (opacity < 0 || opacity > 1) { |
| 4650 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4651 | return; |
| 4652 | } |
| 4653 | |
| 4654 | std::scoped_lock lock(mLock); |
| 4655 | mMaximumObscuringOpacityForTouch = opacity; |
| 4656 | } |
| 4657 | |
| 4658 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4659 | std::scoped_lock lock(mLock); |
| 4660 | mBlockUntrustedTouchesMode = mode; |
| 4661 | } |
| 4662 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4663 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4664 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4665 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4666 | if (DEBUG_FOCUS) { |
| 4667 | ALOGD("Trivial transfer to same window."); |
| 4668 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4669 | return true; |
| 4670 | } |
| 4671 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4672 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4673 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4674 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4675 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4676 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4677 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4678 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4679 | return false; |
| 4680 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4681 | if (DEBUG_FOCUS) { |
| 4682 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4683 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4684 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4685 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4686 | if (DEBUG_FOCUS) { |
| 4687 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4688 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4689 | return false; |
| 4690 | } |
| 4691 | |
| 4692 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4693 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4694 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4695 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4696 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4697 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4698 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4699 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4700 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4701 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4702 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4703 | int32_t newTargetFlags = oldTargetFlags & |
| 4704 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4705 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4706 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4707 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4708 | // Store the dragging window. |
| 4709 | if (isDragDrop) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4710 | mDragState = std::make_unique<DragState>(toWindowHandle); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4713 | found = true; |
| 4714 | goto Found; |
| 4715 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4716 | } |
| 4717 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4718 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4719 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4720 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4721 | if (DEBUG_FOCUS) { |
| 4722 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4723 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4724 | return false; |
| 4725 | } |
| 4726 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4727 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4728 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4729 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4730 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4731 | CancelationOptions |
| 4732 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4733 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4734 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4735 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | } |
| 4737 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4738 | if (DEBUG_FOCUS) { |
| 4739 | logDispatchStateLocked(); |
| 4740 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4741 | } // release lock |
| 4742 | |
| 4743 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4744 | mLooper->wake(); |
| 4745 | return true; |
| 4746 | } |
| 4747 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4748 | // Binder call |
| 4749 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 4750 | sp<IBinder> fromToken; |
| 4751 | { // acquire lock |
| 4752 | std::scoped_lock _l(mLock); |
| 4753 | |
| 4754 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); |
| 4755 | if (toWindowHandle == nullptr) { |
| 4756 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 4757 | return false; |
| 4758 | } |
| 4759 | |
| 4760 | const int32_t displayId = toWindowHandle->getInfo()->displayId; |
| 4761 | |
| 4762 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 4763 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 4764 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", |
| 4765 | displayId); |
| 4766 | return false; |
| 4767 | } |
| 4768 | |
| 4769 | TouchState& state = touchStateIt->second; |
| 4770 | if (state.windows.size() != 1) { |
| 4771 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", |
| 4772 | state.windows.size()); |
| 4773 | return false; |
| 4774 | } |
| 4775 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 4776 | fromToken = touchedWindow.windowHandle->getToken(); |
| 4777 | } // release lock |
| 4778 | |
| 4779 | return transferTouchFocus(fromToken, destChannelToken); |
| 4780 | } |
| 4781 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4782 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4783 | if (DEBUG_FOCUS) { |
| 4784 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4785 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4786 | |
| 4787 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4788 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4789 | |
| 4790 | resetKeyRepeatLocked(); |
| 4791 | releasePendingEventLocked(); |
| 4792 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4793 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4794 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4795 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4796 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4797 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4798 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4802 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4803 | dumpDispatchStateLocked(dump); |
| 4804 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4805 | std::istringstream stream(dump); |
| 4806 | std::string line; |
| 4807 | |
| 4808 | while (std::getline(stream, line, '\n')) { |
| 4809 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4813 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 4814 | std::string dump; |
| 4815 | |
| 4816 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", |
| 4817 | toString(mFocusedWindowRequestedPointerCapture)); |
| 4818 | |
| 4819 | std::string windowName = "None"; |
| 4820 | if (mWindowTokenWithPointerCapture) { |
| 4821 | const sp<InputWindowHandle> captureWindowHandle = |
| 4822 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 4823 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 4824 | : "token has capture without window"; |
| 4825 | } |
| 4826 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); |
| 4827 | |
| 4828 | return dump; |
| 4829 | } |
| 4830 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4831 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4832 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4833 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4834 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4835 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4836 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4837 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4838 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4839 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4840 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4841 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4842 | const std::chrono::duration timeout = |
| 4843 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4844 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4845 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4846 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4849 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4850 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4851 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4852 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4853 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4855 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4856 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4857 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4858 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4859 | 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] | 4860 | state.displayId, toString(state.down), toString(state.split), |
| 4861 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4862 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4863 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4864 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4865 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4866 | dump += StringPrintf(INDENT4 |
| 4867 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4868 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4869 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4870 | } |
| 4871 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4872 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4873 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4874 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4875 | dump += INDENT3 "Portal windows:\n"; |
| 4876 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4877 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4878 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4879 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4880 | } |
| 4881 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4882 | } |
| 4883 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4884 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4885 | } |
| 4886 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4887 | if (mDragState) { |
| 4888 | dump += StringPrintf(INDENT "DragState:\n"); |
| 4889 | mDragState->dump(dump, INDENT2); |
| 4890 | } |
| 4891 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4892 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4893 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4894 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4895 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4896 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4897 | dump += INDENT2 "Windows:\n"; |
| 4898 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4899 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4900 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4901 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4902 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4903 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4904 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4905 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4906 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4907 | "applicationInfo.name=%s, " |
| 4908 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4909 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4910 | i, windowInfo->name.c_str(), windowInfo->id, |
| 4911 | windowInfo->displayId, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4912 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4913 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4914 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4915 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4916 | windowInfo->flags.string().c_str(), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4917 | NamedEnum::string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4918 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4919 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4920 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4921 | windowInfo->applicationInfo.name.c_str(), |
| 4922 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4923 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4924 | dump += StringPrintf(", inputFeatures=%s", |
| 4925 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4926 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4927 | "ms, trustedOverlay=%s, hasToken=%s, " |
| 4928 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4929 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4930 | millis(windowInfo->dispatchingTimeout), |
| 4931 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4932 | toString(windowInfo->token != nullptr), |
| 4933 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4934 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4935 | } |
| 4936 | } else { |
| 4937 | dump += INDENT2 "Windows: <none>\n"; |
| 4938 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4939 | } |
| 4940 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4941 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4942 | } |
| 4943 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4944 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4945 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4946 | const std::vector<Monitor>& monitors = it.second; |
| 4947 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4948 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4949 | } |
| 4950 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4951 | const std::vector<Monitor>& monitors = it.second; |
| 4952 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4953 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4954 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4956 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | nsecs_t currentTime = now(); |
| 4960 | |
| 4961 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4962 | if (!mRecentQueue.empty()) { |
| 4963 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4964 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4965 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4966 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4967 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4968 | } |
| 4969 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4970 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | // Dump event currently being dispatched. |
| 4974 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4975 | dump += INDENT "PendingEvent:\n"; |
| 4976 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4977 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4978 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4979 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4980 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4981 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4985 | if (!mInboundQueue.empty()) { |
| 4986 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4987 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4988 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4989 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4990 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4991 | } |
| 4992 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4993 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4994 | } |
| 4995 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4996 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4997 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4998 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4999 | const KeyReplacement& replacement = pair.first; |
| 5000 | int32_t newKeyCode = pair.second; |
| 5001 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5002 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5003 | } |
| 5004 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5005 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5008 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5009 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5010 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5011 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5012 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5013 | connection->inputChannel->getFd().get(), |
| 5014 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5015 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5016 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5017 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5018 | if (!connection->outboundQueue.empty()) { |
| 5019 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5020 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5021 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5022 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5023 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5024 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5025 | } |
| 5026 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5027 | if (!connection->waitQueue.empty()) { |
| 5028 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5029 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5030 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5031 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5032 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5033 | } |
| 5034 | } |
| 5035 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5036 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5040 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5041 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5042 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5043 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5044 | } |
| 5045 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5046 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5047 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5048 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5049 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5050 | } |
| 5051 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5052 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5053 | const size_t numMonitors = monitors.size(); |
| 5054 | for (size_t i = 0; i < numMonitors; i++) { |
| 5055 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5056 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5057 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5058 | dump += "\n"; |
| 5059 | } |
| 5060 | } |
| 5061 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5062 | class LooperEventCallback : public LooperCallback { |
| 5063 | public: |
| 5064 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5065 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5066 | |
| 5067 | private: |
| 5068 | std::function<int(int events)> mCallback; |
| 5069 | }; |
| 5070 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5071 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5072 | #if DEBUG_CHANNEL_CREATION |
| 5073 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5074 | #endif |
| 5075 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5076 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5077 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5078 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5079 | |
| 5080 | if (result) { |
| 5081 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5082 | } |
| 5083 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5084 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5085 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5086 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5087 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5088 | sp<Connection> connection = |
| 5089 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5091 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5092 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5093 | } |
| 5094 | mConnectionsByToken.emplace(token, connection); |
| 5095 | |
| 5096 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5097 | this, std::placeholders::_1, token); |
| 5098 | |
| 5099 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5100 | } // release lock |
| 5101 | |
| 5102 | // Wake the looper because some connections have changed. |
| 5103 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5104 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5105 | } |
| 5106 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5107 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5108 | bool isGestureMonitor, |
| 5109 | const std::string& name, |
| 5110 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5111 | std::shared_ptr<InputChannel> serverChannel; |
| 5112 | std::unique_ptr<InputChannel> clientChannel; |
| 5113 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5114 | if (result) { |
| 5115 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5116 | } |
| 5117 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5118 | { // acquire lock |
| 5119 | std::scoped_lock _l(mLock); |
| 5120 | |
| 5121 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5122 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5123 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5126 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5127 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5128 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5129 | |
| 5130 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5131 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5132 | } |
| 5133 | mConnectionsByToken.emplace(token, connection); |
| 5134 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5135 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5136 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5137 | auto& monitorsByDisplay = |
| 5138 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5139 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5140 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5141 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5142 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5143 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5144 | // Wake the looper because some connections have changed. |
| 5145 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5146 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5147 | } |
| 5148 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5149 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5150 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5151 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5152 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5153 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5154 | if (status) { |
| 5155 | return status; |
| 5156 | } |
| 5157 | } // release lock |
| 5158 | |
| 5159 | // Wake the poll loop because removing the connection may have changed the current |
| 5160 | // synchronization state. |
| 5161 | mLooper->wake(); |
| 5162 | return OK; |
| 5163 | } |
| 5164 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5165 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5166 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5167 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5168 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5169 | // 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] | 5170 | return BAD_VALUE; |
| 5171 | } |
| 5172 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5173 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5174 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5175 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5176 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | } |
| 5178 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5179 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5180 | |
| 5181 | nsecs_t currentTime = now(); |
| 5182 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5183 | |
| 5184 | connection->status = Connection::STATUS_ZOMBIE; |
| 5185 | return OK; |
| 5186 | } |
| 5187 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5188 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5189 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5190 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5193 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5194 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5195 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5196 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5197 | std::vector<Monitor>& monitors = it->second; |
| 5198 | const size_t numMonitors = monitors.size(); |
| 5199 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5200 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5201 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5202 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5203 | monitors.erase(monitors.begin() + i); |
| 5204 | break; |
| 5205 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5206 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5207 | if (monitors.empty()) { |
| 5208 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5209 | } else { |
| 5210 | ++it; |
| 5211 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5212 | } |
| 5213 | } |
| 5214 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5215 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5216 | { // acquire lock |
| 5217 | std::scoped_lock _l(mLock); |
| 5218 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5219 | |
| 5220 | if (!foundDisplayId) { |
| 5221 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5222 | return BAD_VALUE; |
| 5223 | } |
| 5224 | int32_t displayId = foundDisplayId.value(); |
| 5225 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5226 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5227 | mTouchStatesByDisplay.find(displayId); |
| 5228 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5229 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5230 | return BAD_VALUE; |
| 5231 | } |
| 5232 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5233 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5234 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5235 | std::optional<int32_t> foundDeviceId; |
| 5236 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5237 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5238 | requestingChannel = touchedMonitor.monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5239 | foundDeviceId = state.deviceId; |
| 5240 | } |
| 5241 | } |
| 5242 | if (!foundDeviceId || !state.down) { |
| 5243 | 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] | 5244 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5245 | return BAD_VALUE; |
| 5246 | } |
| 5247 | int32_t deviceId = foundDeviceId.value(); |
| 5248 | |
| 5249 | // Send cancel events to all the input channels we're stealing from. |
| 5250 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5251 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5252 | options.deviceId = deviceId; |
| 5253 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5254 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5255 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5256 | std::shared_ptr<InputChannel> channel = |
| 5257 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5258 | if (channel != nullptr) { |
| 5259 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5260 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5261 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5262 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5263 | canceledWindows += "]"; |
| 5264 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5265 | canceledWindows.c_str()); |
| 5266 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5267 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5268 | state.filterNonMonitors(); |
| 5269 | } |
| 5270 | return OK; |
| 5271 | } |
| 5272 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5273 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5274 | { // acquire lock |
| 5275 | std::scoped_lock _l(mLock); |
| 5276 | if (DEBUG_FOCUS) { |
| 5277 | const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); |
| 5278 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5279 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5280 | : "token without window"); |
| 5281 | } |
| 5282 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5283 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5284 | if (focusedToken != windowToken) { |
| 5285 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5286 | enabled ? "enable" : "disable"); |
| 5287 | return; |
| 5288 | } |
| 5289 | |
| 5290 | if (enabled == mFocusedWindowRequestedPointerCapture) { |
| 5291 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5292 | "window has %s requested pointer capture.", |
| 5293 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5294 | return; |
| 5295 | } |
| 5296 | |
| 5297 | mFocusedWindowRequestedPointerCapture = enabled; |
| 5298 | setPointerCaptureLocked(enabled); |
| 5299 | } // release lock |
| 5300 | |
| 5301 | // Wake the thread to process command entries. |
| 5302 | mLooper->wake(); |
| 5303 | } |
| 5304 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5305 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5306 | const sp<IBinder>& token) { |
| 5307 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5308 | const std::vector<Monitor>& monitors = it.second; |
| 5309 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5310 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5311 | return it.first; |
| 5312 | } |
| 5313 | } |
| 5314 | } |
| 5315 | return std::nullopt; |
| 5316 | } |
| 5317 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5318 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5319 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5320 | if (gesturePid.has_value()) { |
| 5321 | return gesturePid; |
| 5322 | } |
| 5323 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5324 | } |
| 5325 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5326 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5327 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5328 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5329 | } |
| 5330 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5331 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5332 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5333 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5334 | } |
| 5335 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5336 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5337 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5338 | } |
| 5339 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5340 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5341 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5342 | if (connection == nullptr) { |
| 5343 | return "<nullptr>"; |
| 5344 | } |
| 5345 | return connection->getInputChannelName(); |
| 5346 | } |
| 5347 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5348 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5349 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5350 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5351 | } |
| 5352 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5353 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 5354 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5355 | bool handled, nsecs_t consumeTime) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5356 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5357 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5358 | commandEntry->connection = connection; |
| 5359 | commandEntry->eventTime = currentTime; |
| 5360 | commandEntry->seq = seq; |
| 5361 | commandEntry->handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5362 | commandEntry->consumeTime = consumeTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5363 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5364 | } |
| 5365 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5366 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 5367 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5368 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5369 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5371 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5372 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5373 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5374 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5375 | } |
| 5376 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5377 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 5378 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5379 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5380 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5381 | commandEntry->oldToken = oldToken; |
| 5382 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5383 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5384 | } |
| 5385 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5386 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { |
| 5387 | std::unique_ptr<CommandEntry> commandEntry = |
| 5388 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); |
| 5389 | commandEntry->newToken = token; |
| 5390 | commandEntry->x = x; |
| 5391 | commandEntry->y = y; |
| 5392 | postCommandLocked(std::move(commandEntry)); |
| 5393 | } |
| 5394 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5395 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5396 | if (connection == nullptr) { |
| 5397 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5398 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5399 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5400 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5401 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5402 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5403 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5404 | return; |
| 5405 | } |
| 5406 | /** |
| 5407 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5408 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5409 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5410 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5411 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5412 | * most useful. |
| 5413 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5414 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5415 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5416 | std::string reason = |
| 5417 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5418 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5419 | ns2ms(currentWait), |
| 5420 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5421 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5422 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5423 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5424 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5425 | |
| 5426 | // Stop waking up for events on this connection, it is already unresponsive |
| 5427 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5428 | } |
| 5429 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5430 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5431 | std::string reason = |
| 5432 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5433 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5434 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5435 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5436 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); |
| 5437 | commandEntry->inputApplicationHandle = std::move(application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5438 | postCommandLocked(std::move(commandEntry)); |
| 5439 | } |
| 5440 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5441 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 5442 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5443 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 5444 | commandEntry->obscuringPackage = obscuringPackage; |
| 5445 | postCommandLocked(std::move(commandEntry)); |
| 5446 | } |
| 5447 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5448 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 5449 | const std::string& reason) { |
| 5450 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5451 | updateLastAnrStateLocked(windowLabel, reason); |
| 5452 | } |
| 5453 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5454 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5455 | const std::string& reason) { |
| 5456 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5457 | updateLastAnrStateLocked(windowLabel, reason); |
| 5458 | } |
| 5459 | |
| 5460 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5461 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5462 | // 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] | 5463 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5464 | struct tm tm; |
| 5465 | localtime_r(&t, &tm); |
| 5466 | char timestr[64]; |
| 5467 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5468 | mLastAnrState.clear(); |
| 5469 | mLastAnrState += INDENT "ANR:\n"; |
| 5470 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5471 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5472 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5473 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5474 | } |
| 5475 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5476 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5477 | mLock.unlock(); |
| 5478 | |
| 5479 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 5480 | |
| 5481 | mLock.lock(); |
| 5482 | } |
| 5483 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5484 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5485 | sp<Connection> connection = commandEntry->connection; |
| 5486 | |
| 5487 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 5488 | mLock.unlock(); |
| 5489 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5490 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5491 | |
| 5492 | mLock.lock(); |
| 5493 | } |
| 5494 | } |
| 5495 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5496 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5497 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 5498 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5499 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5500 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5501 | mLock.lock(); |
| 5502 | } |
| 5503 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5504 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { |
| 5505 | sp<IBinder> newToken = commandEntry->newToken; |
| 5506 | mLock.unlock(); |
| 5507 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); |
| 5508 | mLock.lock(); |
| 5509 | } |
| 5510 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5511 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5512 | mLock.unlock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5513 | |
| 5514 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); |
| 5515 | |
| 5516 | mLock.lock(); |
| 5517 | } |
| 5518 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5519 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5520 | mLock.unlock(); |
| 5521 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5522 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5523 | |
| 5524 | mLock.lock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5525 | } |
| 5526 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5527 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5528 | mLock.unlock(); |
| 5529 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5530 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); |
| 5531 | |
| 5532 | mLock.lock(); |
| 5533 | } |
| 5534 | |
| 5535 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5536 | mLock.unlock(); |
| 5537 | |
| 5538 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); |
| 5539 | |
| 5540 | mLock.lock(); |
| 5541 | } |
| 5542 | |
| 5543 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5544 | mLock.unlock(); |
| 5545 | |
| 5546 | mPolicy->notifyMonitorResponsive(commandEntry->pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5547 | |
| 5548 | mLock.lock(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5549 | } |
| 5550 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5551 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 5552 | mLock.unlock(); |
| 5553 | |
| 5554 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 5555 | |
| 5556 | mLock.lock(); |
| 5557 | } |
| 5558 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5559 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5560 | CommandEntry* commandEntry) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5561 | KeyEntry& entry = *(commandEntry->keyEntry); |
| 5562 | KeyEvent event = createKeyEvent(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5563 | |
| 5564 | mLock.unlock(); |
| 5565 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5566 | android::base::Timer t; |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5567 | const sp<IBinder>& token = commandEntry->connectionToken; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5568 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5569 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5570 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5571 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5572 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5573 | |
| 5574 | mLock.lock(); |
| 5575 | |
| 5576 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5577 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5578 | } else if (!delay) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5579 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5580 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5581 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5582 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5583 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5584 | } |
| 5585 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5586 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5587 | mLock.unlock(); |
| 5588 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5589 | mLock.lock(); |
| 5590 | } |
| 5591 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5592 | /** |
| 5593 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5594 | * current time. |
| 5595 | */ |
| 5596 | static bool isConnectionResponsive(const Connection& connection) { |
| 5597 | const nsecs_t currentTime = now(); |
| 5598 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5599 | if (entry->timeoutTime < currentTime) { |
| 5600 | return false; |
| 5601 | } |
| 5602 | } |
| 5603 | return true; |
| 5604 | } |
| 5605 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5606 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5607 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5608 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5609 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5610 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5611 | |
| 5612 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5613 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5614 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5615 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5616 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5617 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5618 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5619 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5620 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5621 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5622 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5623 | // TODO(b/167947340): report event latency information to the policy |
| 5624 | // Example: mPolicy->reportFinishedEvent(commandEntry->eventId, eventEntry.eventTime, |
| 5625 | // dispatchEntry->deliveryTime, commandEntry->consumeTime, |
| 5626 | // finishTime); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5627 | |
| 5628 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5629 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5630 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5631 | restartEvent = |
| 5632 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5633 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5634 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5635 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5636 | handled); |
| 5637 | } else { |
| 5638 | restartEvent = false; |
| 5639 | } |
| 5640 | |
| 5641 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5642 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5643 | // contents of the wait queue to have been drained, so we need to double-check |
| 5644 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5645 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5646 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5647 | dispatchEntry = *dispatchEntryIt; |
| 5648 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5649 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5650 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5651 | if (!connection->responsive) { |
| 5652 | connection->responsive = isConnectionResponsive(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5653 | if (connection->responsive) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5654 | // The connection was unresponsive, and now it's responsive. |
| 5655 | processConnectionResponsiveLocked(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5656 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5657 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5658 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5659 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5660 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5661 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5662 | } else { |
| 5663 | releaseDispatchEntry(dispatchEntry); |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | // Start the next dispatch cycle for this connection. |
| 5668 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5669 | } |
| 5670 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5671 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
| 5672 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5673 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); |
| 5674 | monitorUnresponsiveCommand->pid = pid; |
| 5675 | monitorUnresponsiveCommand->reason = std::move(reason); |
| 5676 | postCommandLocked(std::move(monitorUnresponsiveCommand)); |
| 5677 | } |
| 5678 | |
| 5679 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, |
| 5680 | std::string reason) { |
| 5681 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5682 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); |
| 5683 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); |
| 5684 | windowUnresponsiveCommand->reason = std::move(reason); |
| 5685 | postCommandLocked(std::move(windowUnresponsiveCommand)); |
| 5686 | } |
| 5687 | |
| 5688 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
| 5689 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( |
| 5690 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); |
| 5691 | monitorResponsiveCommand->pid = pid; |
| 5692 | postCommandLocked(std::move(monitorResponsiveCommand)); |
| 5693 | } |
| 5694 | |
| 5695 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { |
| 5696 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( |
| 5697 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); |
| 5698 | windowResponsiveCommand->connectionToken = std::move(connectionToken); |
| 5699 | postCommandLocked(std::move(windowResponsiveCommand)); |
| 5700 | } |
| 5701 | |
| 5702 | /** |
| 5703 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5704 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5705 | * command entry to the command queue. |
| 5706 | */ |
| 5707 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5708 | std::string reason) { |
| 5709 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5710 | if (connection.monitor) { |
| 5711 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5712 | reason.c_str()); |
| 5713 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5714 | if (!pid.has_value()) { |
| 5715 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5716 | connection.inputChannel->getName().c_str()); |
| 5717 | return; |
| 5718 | } |
| 5719 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5720 | return; |
| 5721 | } |
| 5722 | // If not a monitor, must be a window |
| 5723 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5724 | reason.c_str()); |
| 5725 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5726 | } |
| 5727 | |
| 5728 | /** |
| 5729 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5730 | */ |
| 5731 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5732 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5733 | if (connection.monitor) { |
| 5734 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5735 | if (!pid.has_value()) { |
| 5736 | ALOGE("Could not find responsive monitor for connection %s", |
| 5737 | connection.inputChannel->getName().c_str()); |
| 5738 | return; |
| 5739 | } |
| 5740 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5741 | return; |
| 5742 | } |
| 5743 | // If not a monitor, must be a window |
| 5744 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5745 | } |
| 5746 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5747 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5748 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5749 | KeyEntry& keyEntry, bool handled) { |
| 5750 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5751 | if (!handled) { |
| 5752 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5753 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5754 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5755 | return false; |
| 5756 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5757 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5758 | // Get the fallback key state. |
| 5759 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5760 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5761 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5762 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5763 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5764 | } |
| 5765 | |
| 5766 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5767 | // If the application handles the original key for which we previously |
| 5768 | // generated a fallback or if the window is not a foreground window, |
| 5769 | // then cancel the associated fallback key, if any. |
| 5770 | if (fallbackKeyCode != -1) { |
| 5771 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5772 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5773 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5774 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5775 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5776 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5777 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5778 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5779 | |
| 5780 | mLock.unlock(); |
| 5781 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5782 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5783 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5784 | |
| 5785 | mLock.lock(); |
| 5786 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5787 | // Cancel the fallback key. |
| 5788 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5789 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5790 | "application handled the original non-fallback key " |
| 5791 | "or is no longer a foreground target, " |
| 5792 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5793 | options.keyCode = fallbackKeyCode; |
| 5794 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5795 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5796 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5797 | } |
| 5798 | } else { |
| 5799 | // If the application did not handle a non-fallback key, first check |
| 5800 | // that we are in a good state to perform unhandled key event processing |
| 5801 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5802 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5803 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5804 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5805 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5806 | "since this is not an initial down. " |
| 5807 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5808 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5809 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5810 | return false; |
| 5811 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5812 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5813 | // Dispatch the unhandled key to the policy. |
| 5814 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5815 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5816 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5817 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5818 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5819 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5820 | |
| 5821 | mLock.unlock(); |
| 5822 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5823 | bool fallback = |
| 5824 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5825 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5826 | |
| 5827 | mLock.lock(); |
| 5828 | |
| 5829 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5830 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5831 | return false; |
| 5832 | } |
| 5833 | |
| 5834 | // Latch the fallback keycode for this key on an initial down. |
| 5835 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5836 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5837 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5838 | fallbackKeyCode = event.getKeyCode(); |
| 5839 | } else { |
| 5840 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5841 | } |
| 5842 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5843 | } |
| 5844 | |
| 5845 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5846 | |
| 5847 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5848 | // We will continue to dispatch the key to the policy but we will no |
| 5849 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5850 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5851 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5852 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5853 | if (fallback) { |
| 5854 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5855 | "as a fallback for %d, but on the DOWN it had requested " |
| 5856 | "to send %d instead. Fallback canceled.", |
| 5857 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5858 | } else { |
| 5859 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5860 | "but on the DOWN it had requested to send %d. " |
| 5861 | "Fallback canceled.", |
| 5862 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5863 | } |
| 5864 | #endif |
| 5865 | |
| 5866 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5867 | "canceling fallback, policy no longer desires it"); |
| 5868 | options.keyCode = fallbackKeyCode; |
| 5869 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5870 | |
| 5871 | fallback = false; |
| 5872 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5873 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5874 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5875 | } |
| 5876 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5877 | |
| 5878 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5879 | { |
| 5880 | std::string msg; |
| 5881 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5882 | connection->inputState.getFallbackKeys(); |
| 5883 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5884 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5885 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5886 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5887 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5888 | } |
| 5889 | #endif |
| 5890 | |
| 5891 | if (fallback) { |
| 5892 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5893 | keyEntry.eventTime = event.getEventTime(); |
| 5894 | keyEntry.deviceId = event.getDeviceId(); |
| 5895 | keyEntry.source = event.getSource(); |
| 5896 | keyEntry.displayId = event.getDisplayId(); |
| 5897 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5898 | keyEntry.keyCode = fallbackKeyCode; |
| 5899 | keyEntry.scanCode = event.getScanCode(); |
| 5900 | keyEntry.metaState = event.getMetaState(); |
| 5901 | keyEntry.repeatCount = event.getRepeatCount(); |
| 5902 | keyEntry.downTime = event.getDownTime(); |
| 5903 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5904 | |
| 5905 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5906 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5907 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5908 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5909 | #endif |
| 5910 | return true; // restart the event |
| 5911 | } else { |
| 5912 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5913 | ALOGD("Unhandled key event: No fallback key."); |
| 5914 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5915 | |
| 5916 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5917 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5918 | } |
| 5919 | } |
| 5920 | return false; |
| 5921 | } |
| 5922 | |
| 5923 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5924 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5925 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5926 | return false; |
| 5927 | } |
| 5928 | |
| 5929 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5930 | mLock.unlock(); |
| 5931 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 5932 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, |
| 5933 | commandEntry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5934 | |
| 5935 | mLock.lock(); |
| 5936 | } |
| 5937 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5938 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5939 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5940 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5941 | } |
| 5942 | } |
| 5943 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5944 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5945 | if (ATRACE_ENABLED()) { |
| 5946 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5947 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 5948 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5949 | } |
| 5950 | } |
| 5951 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5952 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5953 | if (ATRACE_ENABLED()) { |
| 5954 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame^] | 5955 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 5956 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5957 | } |
| 5958 | } |
| 5959 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5960 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5961 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5962 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5963 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5964 | dumpDispatchStateLocked(dump); |
| 5965 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5966 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5967 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5968 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5969 | } |
| 5970 | } |
| 5971 | |
| 5972 | void InputDispatcher::monitor() { |
| 5973 | // 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] | 5974 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5975 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5976 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5977 | } |
| 5978 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5979 | /** |
| 5980 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5981 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5982 | * this method can be safely called from any thread, as long as you've ensured that |
| 5983 | * the work you are interested in completing has already been queued. |
| 5984 | */ |
| 5985 | bool InputDispatcher::waitForIdle() { |
| 5986 | /** |
| 5987 | * Timeout should represent the longest possible time that a device might spend processing |
| 5988 | * events and commands. |
| 5989 | */ |
| 5990 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5991 | std::unique_lock lock(mLock); |
| 5992 | mLooper->wake(); |
| 5993 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5994 | return result == std::cv_status::no_timeout; |
| 5995 | } |
| 5996 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5997 | /** |
| 5998 | * Sets focus to the window identified by the token. This must be called |
| 5999 | * after updating any input window handles. |
| 6000 | * |
| 6001 | * Params: |
| 6002 | * request.token - input channel token used to identify the window that should gain focus. |
| 6003 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6004 | * specified token does not match the currently focused window, this request will be dropped. |
| 6005 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6006 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6007 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6008 | * when requesting the focus change. This determines which request gets |
| 6009 | * precedence if there is a focus change request from another source such as pointer down. |
| 6010 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6011 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6012 | { // acquire lock |
| 6013 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6014 | std::optional<FocusResolver::FocusChanges> changes = |
| 6015 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6016 | if (changes) { |
| 6017 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6018 | } |
| 6019 | } // release lock |
| 6020 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6021 | mLooper->wake(); |
| 6022 | } |
| 6023 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6024 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6025 | if (changes.oldFocus) { |
| 6026 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6027 | if (focusedInputChannel) { |
| 6028 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6029 | "focus left window"); |
| 6030 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6031 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6032 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6033 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6034 | if (changes.newFocus) { |
| 6035 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6038 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6039 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6040 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6041 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6042 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6043 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6044 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6045 | // front. |
| 6046 | disablePointerCaptureForcedLocked(); |
| 6047 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6048 | if (mFocusedDisplayId == changes.displayId) { |
| 6049 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6050 | } |
| 6051 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6052 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6053 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
| 6054 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { |
| 6055 | return; |
| 6056 | } |
| 6057 | |
| 6058 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6059 | |
| 6060 | if (mFocusedWindowRequestedPointerCapture) { |
| 6061 | mFocusedWindowRequestedPointerCapture = false; |
| 6062 | setPointerCaptureLocked(false); |
| 6063 | } |
| 6064 | |
| 6065 | if (!mWindowTokenWithPointerCapture) { |
| 6066 | // No need to send capture changes because no window has capture. |
| 6067 | return; |
| 6068 | } |
| 6069 | |
| 6070 | if (mPendingEvent != nullptr) { |
| 6071 | // Move the pending event to the front of the queue. This will give the chance |
| 6072 | // for the pending event to be dropped if it is a captured event. |
| 6073 | mInboundQueue.push_front(mPendingEvent); |
| 6074 | mPendingEvent = nullptr; |
| 6075 | } |
| 6076 | |
| 6077 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
| 6078 | false /* hasCapture */); |
| 6079 | mInboundQueue.push_front(std::move(entry)); |
| 6080 | } |
| 6081 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6082 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { |
| 6083 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 6084 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); |
| 6085 | commandEntry->enabled = enabled; |
| 6086 | postCommandLocked(std::move(commandEntry)); |
| 6087 | } |
| 6088 | |
| 6089 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( |
| 6090 | android::inputdispatcher::CommandEntry* commandEntry) { |
| 6091 | mLock.unlock(); |
| 6092 | |
| 6093 | mPolicy->setPointerCapture(commandEntry->enabled); |
| 6094 | |
| 6095 | mLock.lock(); |
| 6096 | } |
| 6097 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6098 | } // namespace android::inputdispatcher |