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 <statslog.h> |
| 63 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 64 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 65 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 66 | #include <cerrno> |
| 67 | #include <cinttypes> |
| 68 | #include <climits> |
| 69 | #include <cstddef> |
| 70 | #include <ctime> |
| 71 | #include <queue> |
| 72 | #include <sstream> |
| 73 | |
| 74 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 75 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 76 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | #define INDENT " " |
| 78 | #define INDENT2 " " |
| 79 | #define INDENT3 " " |
| 80 | #define INDENT4 " " |
| 81 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 82 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 83 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 84 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 85 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 86 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 87 | using android::os::InputEventInjectionResult; |
| 88 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 89 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 90 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 91 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
| 93 | // Default input dispatching timeout if there is no focused application or paused window |
| 94 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 95 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 96 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 97 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | |
| 99 | // Amount of time to allow for all pending events to be processed when an app switch |
| 100 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 101 | // 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] | 102 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | |
| 104 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 105 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 106 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | // 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] | 109 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 110 | |
| 111 | // Log a warning when an interception call takes longer than this to process. |
| 112 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 113 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 114 | // Additional key latency in case a connection is still processing some motion events. |
| 115 | // This will help with the case when a user touched a button that opens a new window, |
| 116 | // and gives us the chance to dispatch the key to this new window. |
| 117 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 118 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 120 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 121 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 122 | // Event log tags. See EventLogTags.logtags for reference |
| 123 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 124 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 125 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | static inline nsecs_t now() { |
| 127 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 128 | } |
| 129 | |
| 130 | static inline const char* toString(bool value) { |
| 131 | return value ? "true" : "false"; |
| 132 | } |
| 133 | |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 134 | static inline const std::string toString(sp<IBinder> binder) { |
| 135 | if (binder == nullptr) { |
| 136 | return "<null>"; |
| 137 | } |
| 138 | return StringPrintf("%p", binder.get()); |
| 139 | } |
| 140 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 142 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 143 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static bool isValidKeyAction(int32_t action) { |
| 147 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 148 | case AKEY_EVENT_ACTION_DOWN: |
| 149 | case AKEY_EVENT_ACTION_UP: |
| 150 | return true; |
| 151 | default: |
| 152 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 157 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 158 | ALOGE("Key event has invalid action code 0x%x", action); |
| 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 164 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 166 | case AMOTION_EVENT_ACTION_DOWN: |
| 167 | case AMOTION_EVENT_ACTION_UP: |
| 168 | case AMOTION_EVENT_ACTION_CANCEL: |
| 169 | case AMOTION_EVENT_ACTION_MOVE: |
| 170 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 171 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 172 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 173 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 174 | case AMOTION_EVENT_ACTION_SCROLL: |
| 175 | return true; |
| 176 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 177 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 178 | int32_t index = getMotionEventActionPointerIndex(action); |
| 179 | return index >= 0 && index < pointerCount; |
| 180 | } |
| 181 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 182 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 183 | return actionButton != 0; |
| 184 | default: |
| 185 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 189 | static int64_t millis(std::chrono::nanoseconds t) { |
| 190 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 191 | } |
| 192 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 193 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 194 | const PointerProperties* pointerProperties) { |
| 195 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 196 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 197 | return false; |
| 198 | } |
| 199 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 200 | 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] | 201 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | BitSet32 pointerIdBits; |
| 205 | for (size_t i = 0; i < pointerCount; i++) { |
| 206 | int32_t id = pointerProperties[i].id; |
| 207 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 208 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 209 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | if (pointerIdBits.hasBit(id)) { |
| 213 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 214 | return false; |
| 215 | } |
| 216 | pointerIdBits.markBit(id); |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 221 | static std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 223 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 226 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 227 | bool first = true; |
| 228 | Region::const_iterator cur = region.begin(); |
| 229 | Region::const_iterator const tail = region.end(); |
| 230 | while (cur != tail) { |
| 231 | if (first) { |
| 232 | first = false; |
| 233 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 234 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 235 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 236 | 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] | 237 | cur++; |
| 238 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 239 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 242 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
| 243 | constexpr size_t maxEntries = 50; // max events to print |
| 244 | constexpr size_t skipBegin = maxEntries / 2; |
| 245 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 246 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 247 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 248 | |
| 249 | std::string dump; |
| 250 | for (size_t i = 0; i < queue.size(); i++) { |
| 251 | const DispatchEntry& entry = *queue[i]; |
| 252 | if (i >= skipBegin && i < skipEnd) { |
| 253 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 254 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 255 | continue; |
| 256 | } |
| 257 | dump.append(INDENT4); |
| 258 | dump += entry.eventEntry->getDescription(); |
| 259 | dump += StringPrintf(", seq=%" PRIu32 |
| 260 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 261 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 262 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 263 | if (entry.deliveryTime != 0) { |
| 264 | // This entry was delivered, so add information on how long we've been waiting |
| 265 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 266 | } |
| 267 | dump.append("\n"); |
| 268 | } |
| 269 | return dump; |
| 270 | } |
| 271 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 272 | /** |
| 273 | * Find the entry in std::unordered_map by key, and return it. |
| 274 | * If the entry is not found, return a default constructed entry. |
| 275 | * |
| 276 | * Useful when the entries are vectors, since an empty vector will be returned |
| 277 | * if the entry is not found. |
| 278 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 279 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 280 | template <typename K, typename V> |
| 281 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 282 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 283 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 284 | } |
| 285 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Find the entry in std::unordered_map by value, and remove it. |
| 288 | * If more than one entry has the same value, then all matching |
| 289 | * key-value pairs will be removed. |
| 290 | * |
| 291 | * Return true if at least one value has been removed. |
| 292 | */ |
| 293 | template <typename K, typename V> |
| 294 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { |
| 295 | bool removed = false; |
| 296 | for (auto it = map.begin(); it != map.end();) { |
| 297 | if (it->second == value) { |
| 298 | it = map.erase(it); |
| 299 | removed = true; |
| 300 | } else { |
| 301 | it++; |
| 302 | } |
| 303 | } |
| 304 | return removed; |
| 305 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 306 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 307 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 308 | if (first == second) { |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | if (first == nullptr || second == nullptr) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | return first->getToken() == second->getToken(); |
| 317 | } |
| 318 | |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 319 | static bool haveSameApplicationToken(const InputWindowInfo* first, const InputWindowInfo* second) { |
| 320 | if (first == nullptr || second == nullptr) { |
| 321 | return false; |
| 322 | } |
| 323 | return first->applicationInfo.token != nullptr && |
| 324 | first->applicationInfo.token == second->applicationInfo.token; |
| 325 | } |
| 326 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 327 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 328 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 329 | } |
| 330 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 331 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 332 | std::shared_ptr<EventEntry> eventEntry, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 333 | int32_t inputTargetFlags) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 334 | if (eventEntry->type == EventEntry::Type::MOTION) { |
| 335 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 336 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) == 0) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 337 | const ui::Transform identityTransform; |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 338 | // Use identity transform for events that are not pointer events because their axes |
| 339 | // values do not represent on-screen coordinates, so they should not have any window |
| 340 | // transformations applied to them. |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 341 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, |
| 342 | 1.0f /*globalScaleFactor*/); |
| 343 | } |
| 344 | } |
| 345 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 346 | if (inputTarget.useDefaultPointerTransform()) { |
| 347 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 348 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 349 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 353 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 354 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 355 | std::vector<PointerCoords> pointerCoords; |
| 356 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 357 | |
| 358 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 359 | // 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] | 360 | // uses the transform for the normalized pointer. |
| 361 | const ui::Transform& firstPointerTransform = |
| 362 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 363 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 364 | |
| 365 | // Iterate through all pointers in the event to normalize against the first. |
| 366 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 367 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 368 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 369 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 370 | |
| 371 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 372 | // First, apply the current pointer's transform to update the coordinates into |
| 373 | // window space. |
| 374 | pointerCoords[pointerIndex].transform(currTransform); |
| 375 | // Next, apply the inverse transform of the normalized coordinates so the |
| 376 | // current coordinates are transformed into the normalized coordinate space. |
| 377 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 380 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 381 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 382 | motionEntry.deviceId, motionEntry.source, |
| 383 | motionEntry.displayId, motionEntry.policyFlags, |
| 384 | motionEntry.action, motionEntry.actionButton, |
| 385 | motionEntry.flags, motionEntry.metaState, |
| 386 | motionEntry.buttonState, motionEntry.classification, |
| 387 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 388 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 389 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 390 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 391 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 392 | |
| 393 | if (motionEntry.injectionState) { |
| 394 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 395 | combinedMotionEntry->injectionState->refCount += 1; |
| 396 | } |
| 397 | |
| 398 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 399 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
| 400 | firstPointerTransform, inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 401 | return dispatchEntry; |
| 402 | } |
| 403 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 404 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 405 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 406 | float yOffset = 0) { |
| 407 | if (monitors.empty()) { |
| 408 | return; |
| 409 | } |
| 410 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 411 | for (const Monitor& monitor : monitors) { |
| 412 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 413 | } |
| 414 | } |
| 415 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 416 | static status_t openInputChannelPair(const std::string& name, |
| 417 | std::shared_ptr<InputChannel>& serverChannel, |
| 418 | std::unique_ptr<InputChannel>& clientChannel) { |
| 419 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 420 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 421 | |
| 422 | serverChannel = std::move(uniqueServerChannel); |
| 423 | return result; |
| 424 | } |
| 425 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 426 | template <typename T> |
| 427 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 428 | if (lhs == nullptr && rhs == nullptr) { |
| 429 | return true; |
| 430 | } |
| 431 | if (lhs == nullptr || rhs == nullptr) { |
| 432 | return false; |
| 433 | } |
| 434 | return *lhs == *rhs; |
| 435 | } |
| 436 | |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 437 | static sp<IPlatformCompatNative> getCompatService() { |
| 438 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 439 | if (service == nullptr) { |
| 440 | ALOGE("Failed to link to compat service"); |
| 441 | return nullptr; |
| 442 | } |
| 443 | return interface_cast<IPlatformCompatNative>(service); |
| 444 | } |
| 445 | |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 446 | static KeyEvent createKeyEvent(const KeyEntry& entry) { |
| 447 | KeyEvent event; |
| 448 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 449 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 450 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 451 | return event; |
| 452 | } |
| 453 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 454 | static std::optional<int32_t> findMonitorPidByToken( |
| 455 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 456 | const sp<IBinder>& token) { |
| 457 | for (const auto& it : monitorsByDisplay) { |
| 458 | const std::vector<Monitor>& monitors = it.second; |
| 459 | for (const Monitor& monitor : monitors) { |
| 460 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 461 | return monitor.pid; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return std::nullopt; |
| 466 | } |
| 467 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 468 | // --- InputDispatcher --- |
| 469 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 470 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 471 | : mPolicy(policy), |
| 472 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 473 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 474 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 475 | mAppSwitchSawKeyDown(false), |
| 476 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 477 | mNextUnblockedEvent(nullptr), |
| 478 | mDispatchEnabled(false), |
| 479 | mDispatchFrozen(false), |
| 480 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 481 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 482 | // To avoid leaking stack in case that call never comes, and for tests, |
| 483 | // initialize it here anyways. |
| 484 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 485 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 486 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 487 | mFocusedWindowRequestedPointerCapture(false), |
| 488 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 489 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 491 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 493 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 494 | |
| 495 | policy->getDispatcherConfiguration(&mConfig); |
| 496 | } |
| 497 | |
| 498 | InputDispatcher::~InputDispatcher() { |
| 499 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 500 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 501 | |
| 502 | resetKeyRepeatLocked(); |
| 503 | releasePendingEventLocked(); |
| 504 | drainInboundQueueLocked(); |
| 505 | } |
| 506 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 507 | while (!mConnectionsByFd.empty()) { |
| 508 | sp<Connection> connection = mConnectionsByFd.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 509 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 513 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 514 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 515 | return ALREADY_EXISTS; |
| 516 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 517 | mThread = std::make_unique<InputThread>( |
| 518 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 519 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 523 | if (mThread && mThread->isCallingThread()) { |
| 524 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 525 | return INVALID_OPERATION; |
| 526 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 527 | mThread.reset(); |
| 528 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | void InputDispatcher::dispatchOnce() { |
| 532 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 533 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 534 | std::scoped_lock _l(mLock); |
| 535 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 536 | |
| 537 | // Run a dispatch loop if there are no pending commands. |
| 538 | // The dispatch loop might enqueue commands to run afterwards. |
| 539 | if (!haveCommandsLocked()) { |
| 540 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 541 | } |
| 542 | |
| 543 | // Run all pending commands if there are any. |
| 544 | // If any commands were run then force the next poll to wake up immediately. |
| 545 | if (runCommandsLockedInterruptible()) { |
| 546 | nextWakeupTime = LONG_LONG_MIN; |
| 547 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 548 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 549 | // If we are still waiting for ack on some events, |
| 550 | // we might have to wake up earlier to check if an app is anr'ing. |
| 551 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 552 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 553 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 554 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 555 | // pending or queued events |
| 556 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 557 | mDispatcherEnteredIdle.notify_all(); |
| 558 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | } // release lock |
| 560 | |
| 561 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 562 | nsecs_t currentTime = now(); |
| 563 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 564 | mLooper->pollOnce(timeoutMillis); |
| 565 | } |
| 566 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 567 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 568 | * Raise ANR if there is no focused window. |
| 569 | * Before the ANR is raised, do a final state check: |
| 570 | * 1. The currently focused application must be the same one we are waiting for. |
| 571 | * 2. Ensure we still don't have a focused window. |
| 572 | */ |
| 573 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 574 | // Check if the application that we are waiting for is still focused. |
| 575 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 576 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 577 | if (focusedApplication == nullptr || |
| 578 | focusedApplication->getApplicationToken() != |
| 579 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 580 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 581 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 582 | focusedApplication->getName().c_str()); |
| 583 | return; // The focused application has changed. |
| 584 | } |
| 585 | |
| 586 | const sp<InputWindowHandle>& focusedWindowHandle = |
| 587 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 588 | if (focusedWindowHandle != nullptr) { |
| 589 | return; // We now have a focused window. No need for ANR. |
| 590 | } |
| 591 | onAnrLocked(mAwaitedFocusedApplication); |
| 592 | } |
| 593 | |
| 594 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 595 | * Check if any of the connections' wait queues have events that are too old. |
| 596 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 597 | * Return the time at which we should wake up next. |
| 598 | */ |
| 599 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 600 | const nsecs_t currentTime = now(); |
| 601 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 602 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 603 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 604 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 605 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 606 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 607 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 608 | return LONG_LONG_MIN; |
| 609 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 610 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 611 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Check if any connection ANRs are due |
| 616 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 617 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 618 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 619 | } |
| 620 | |
| 621 | // If we reached here, we have an unresponsive connection. |
| 622 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 623 | if (connection == nullptr) { |
| 624 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 625 | return nextAnrCheck; |
| 626 | } |
| 627 | connection->responsive = false; |
| 628 | // Stop waking up for this unresponsive connection |
| 629 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 630 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 631 | return LONG_LONG_MIN; |
| 632 | } |
| 633 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 634 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 635 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 636 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 637 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 638 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 639 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 642 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 643 | nsecs_t currentTime = now(); |
| 644 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 645 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 646 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 647 | // repeat if the device is just coming out of sleep. |
| 648 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 649 | resetKeyRepeatLocked(); |
| 650 | } |
| 651 | |
| 652 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 653 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 654 | if (DEBUG_FOCUS) { |
| 655 | ALOGD("Dispatch frozen. Waiting some more."); |
| 656 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | |
| 660 | // Optimize latency of app switches. |
| 661 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 662 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 663 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 664 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 665 | *nextWakeupTime = mAppSwitchDueTime; |
| 666 | } |
| 667 | |
| 668 | // Ready to start a new event. |
| 669 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 670 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 671 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 672 | if (isAppSwitchDue) { |
| 673 | // The inbound queue is empty so the app switch key we were waiting |
| 674 | // for will never arrive. Stop waiting for it. |
| 675 | resetPendingAppSwitchLocked(false); |
| 676 | isAppSwitchDue = false; |
| 677 | } |
| 678 | |
| 679 | // Synthesize a key repeat if appropriate. |
| 680 | if (mKeyRepeatState.lastKeyEntry) { |
| 681 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 682 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 683 | } else { |
| 684 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 685 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Nothing to do if there is no pending event. |
| 691 | if (!mPendingEvent) { |
| 692 | return; |
| 693 | } |
| 694 | } else { |
| 695 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 696 | mPendingEvent = mInboundQueue.front(); |
| 697 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 698 | traceInboundQueueLengthLocked(); |
| 699 | } |
| 700 | |
| 701 | // Poke user activity for this event. |
| 702 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 703 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 704 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | // Now we have an event to dispatch. |
| 708 | // 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] | 709 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 710 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 711 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 712 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 713 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 714 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 715 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 719 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 723 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 724 | const ConfigurationChangedEntry& typedEntry = |
| 725 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 726 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 727 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 728 | break; |
| 729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 730 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 731 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 732 | const DeviceResetEntry& typedEntry = |
| 733 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 734 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 735 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 736 | break; |
| 737 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 739 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 740 | std::shared_ptr<FocusEntry> typedEntry = |
| 741 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 742 | dispatchFocusLocked(currentTime, typedEntry); |
| 743 | done = true; |
| 744 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 745 | break; |
| 746 | } |
| 747 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 748 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 749 | const auto typedEntry = |
| 750 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 751 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 752 | done = true; |
| 753 | break; |
| 754 | } |
| 755 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 756 | case EventEntry::Type::DRAG: { |
| 757 | std::shared_ptr<DragEntry> typedEntry = |
| 758 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 759 | dispatchDragLocked(currentTime, typedEntry); |
| 760 | done = true; |
| 761 | break; |
| 762 | } |
| 763 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 764 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 765 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 766 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 767 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 768 | resetPendingAppSwitchLocked(true); |
| 769 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 770 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 771 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 774 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 775 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 776 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 777 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 778 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 779 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 780 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 784 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 785 | std::shared_ptr<MotionEntry> motionEntry = |
| 786 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 787 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 788 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 790 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 791 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 792 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 793 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 794 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 795 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 796 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 797 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 799 | |
| 800 | case EventEntry::Type::SENSOR: { |
| 801 | std::shared_ptr<SensorEntry> sensorEntry = |
| 802 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 803 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 804 | dropReason = DropReason::APP_SWITCH; |
| 805 | } |
| 806 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 807 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 808 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 809 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 810 | dropReason = DropReason::STALE; |
| 811 | } |
| 812 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 813 | done = true; |
| 814 | break; |
| 815 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 819 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 820 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 821 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 822 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | |
| 824 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 825 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 829 | /** |
| 830 | * Return true if the events preceding this incoming motion event should be dropped |
| 831 | * Return false otherwise (the default behaviour) |
| 832 | */ |
| 833 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 834 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 835 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 836 | |
| 837 | // Optimize case where the current application is unresponsive and the user |
| 838 | // decides to touch a window in a different application. |
| 839 | // If the application takes too long to catch up then we drop all events preceding |
| 840 | // the touch into the other window. |
| 841 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 842 | int32_t displayId = motionEntry.displayId; |
| 843 | int32_t x = static_cast<int32_t>( |
| 844 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 845 | int32_t y = static_cast<int32_t>( |
| 846 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 847 | sp<InputWindowHandle> touchedWindowHandle = |
| 848 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 849 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 850 | touchedWindowHandle->getApplicationToken() != |
| 851 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 852 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 853 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 854 | "for %s", |
| 855 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 856 | return true; |
| 857 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 858 | |
| 859 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 860 | std::vector<TouchedMonitor> gestureMonitors = |
| 861 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 862 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 863 | sp<Connection> connection = |
| 864 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 865 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 866 | // This monitor could take more input. Drop all events preceding this |
| 867 | // event, so that gesture monitor could get a chance to receive the stream |
| 868 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 869 | "responsive gesture monitor that may handle the event", |
| 870 | mAwaitedFocusedApplication->getName().c_str()); |
| 871 | return true; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 877 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 878 | // events to be processed before dispatching the key event. This is because these motion events |
| 879 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 880 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 881 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 882 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 883 | "just send the pending key event to the focused window."); |
| 884 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 885 | } |
| 886 | return false; |
| 887 | } |
| 888 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 889 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 890 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 891 | mInboundQueue.push_back(std::move(newEntry)); |
| 892 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 893 | traceInboundQueueLengthLocked(); |
| 894 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 895 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 896 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 897 | // Optimize app switch latency. |
| 898 | // If the application takes too long to catch up then we drop all events preceding |
| 899 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 900 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 901 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 902 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 903 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 904 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 905 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 907 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 909 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 910 | mAppSwitchSawKeyDown = false; |
| 911 | needWake = true; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | break; |
| 916 | } |
| 917 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 918 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 919 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 920 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 921 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 923 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 924 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 925 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 926 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 927 | break; |
| 928 | } |
| 929 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 930 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 931 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 932 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 933 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 934 | // nothing to do |
| 935 | break; |
| 936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | return needWake; |
| 940 | } |
| 941 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 942 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 943 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 944 | if (entry->type != EventEntry::Type::SENSOR) { |
| 945 | mRecentQueue.push_back(entry); |
| 946 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 947 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 948 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 952 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 953 | int32_t y, TouchState* touchState, |
| 954 | bool addOutsideTargets, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 955 | bool addPortalWindows, |
| 956 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 957 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 958 | LOG_ALWAYS_FATAL( |
| 959 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 960 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 961 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 962 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 963 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 964 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 965 | continue; |
| 966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 967 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 968 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 969 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 970 | |
| 971 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 972 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 973 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 974 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 975 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 976 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 977 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 978 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 979 | if (addPortalWindows) { |
| 980 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 981 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 982 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 983 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 984 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 985 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 | // Found window. |
| 987 | return windowHandle; |
| 988 | } |
| 989 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 990 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 991 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 992 | touchState->addOrUpdateWindow(windowHandle, |
| 993 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 994 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 995 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 996 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | } |
| 998 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 999 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1002 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1003 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1004 | std::vector<TouchedMonitor> touchedMonitors; |
| 1005 | |
| 1006 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 1007 | addGestureMonitors(monitors, touchedMonitors); |
| 1008 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 1009 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 1010 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1011 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 1012 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1013 | } |
| 1014 | return touchedMonitors; |
| 1015 | } |
| 1016 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1017 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | const char* reason; |
| 1019 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1020 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1021 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1022 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1023 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1024 | reason = "inbound event was dropped because the policy consumed it"; |
| 1025 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1026 | case DropReason::DISABLED: |
| 1027 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1028 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1029 | } |
| 1030 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1031 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1032 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1033 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1034 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1035 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1036 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1037 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1038 | "has started interacting with a different application."); |
| 1039 | reason = "inbound event was dropped because the current application is not responding " |
| 1040 | "and the user has started interacting with a different application"; |
| 1041 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1042 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1043 | ALOGI("Dropped event because it is stale."); |
| 1044 | reason = "inbound event was dropped because it is stale"; |
| 1045 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | case DropReason::NO_POINTER_CAPTURE: |
| 1047 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1048 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1049 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1050 | case DropReason::NOT_DROPPED: { |
| 1051 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1052 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1053 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1056 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1057 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1059 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1060 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1061 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1062 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1063 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1064 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1065 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1066 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1067 | } else { |
| 1068 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1069 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1070 | } |
| 1071 | break; |
| 1072 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1073 | case EventEntry::Type::SENSOR: { |
| 1074 | break; |
| 1075 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1076 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1077 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1078 | break; |
| 1079 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1080 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1081 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1082 | case EventEntry::Type::DEVICE_RESET: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1083 | 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] | 1084 | break; |
| 1085 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1089 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1090 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1091 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1094 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1095 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1096 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1097 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1101 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1102 | } |
| 1103 | |
| 1104 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1105 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1106 | |
| 1107 | #if DEBUG_APP_SWITCH |
| 1108 | if (handled) { |
| 1109 | ALOGD("App switch has arrived."); |
| 1110 | } else { |
| 1111 | ALOGD("App switch was abandoned."); |
| 1112 | } |
| 1113 | #endif |
| 1114 | } |
| 1115 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1116 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1117 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1121 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1126 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1127 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1128 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1129 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1130 | |
| 1131 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1132 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | return true; |
| 1134 | } |
| 1135 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1136 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1137 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1141 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1142 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1143 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1144 | releaseInboundEventLocked(entry); |
| 1145 | } |
| 1146 | traceInboundQueueLengthLocked(); |
| 1147 | } |
| 1148 | |
| 1149 | void InputDispatcher::releasePendingEventLocked() { |
| 1150 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1151 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1152 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1156 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1157 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1158 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | #if DEBUG_DISPATCH_CYCLE |
| 1160 | ALOGD("Injected inbound event was dropped."); |
| 1161 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1162 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | } |
| 1164 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1165 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1166 | } |
| 1167 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1171 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1172 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1176 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1177 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1179 | uint32_t policyFlags = entry->policyFlags & |
| 1180 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1182 | std::shared_ptr<KeyEntry> newEntry = |
| 1183 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1184 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1185 | entry->flags, entry->keyCode, entry->scanCode, |
| 1186 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1188 | newEntry->syntheticRepeat = true; |
| 1189 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1191 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1194 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1195 | const ConfigurationChangedEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1197 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | #endif |
| 1199 | |
| 1200 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1201 | resetKeyRepeatLocked(); |
| 1202 | |
| 1203 | // 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] | 1204 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1205 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1206 | commandEntry->eventTime = entry.eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1207 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | return true; |
| 1209 | } |
| 1210 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1211 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1212 | const DeviceResetEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1213 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1214 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1215 | entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | #endif |
| 1217 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1218 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1219 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1221 | return true; |
| 1222 | } |
| 1223 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1224 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1225 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1226 | if (mPendingEvent != nullptr) { |
| 1227 | // Move the pending event to the front of the queue. This will give the chance |
| 1228 | // for the pending event to get dispatched to the newly focused window |
| 1229 | mInboundQueue.push_front(mPendingEvent); |
| 1230 | mPendingEvent = nullptr; |
| 1231 | } |
| 1232 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1233 | std::unique_ptr<FocusEntry> focusEntry = |
| 1234 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1235 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1236 | |
| 1237 | // This event should go to the front of the queue, but behind all other focus events |
| 1238 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1239 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1240 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1241 | [](const std::shared_ptr<EventEntry>& event) { |
| 1242 | return event->type == EventEntry::Type::FOCUS; |
| 1243 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1244 | |
| 1245 | // 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] | 1246 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1247 | } |
| 1248 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1249 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1250 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1251 | if (channel == nullptr) { |
| 1252 | return; // Window has gone away |
| 1253 | } |
| 1254 | InputTarget target; |
| 1255 | target.inputChannel = channel; |
| 1256 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1257 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1258 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1259 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1260 | std::string reason = std::string("reason=").append(entry->reason); |
| 1261 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1262 | dispatchEventLocked(currentTime, entry, {target}); |
| 1263 | } |
| 1264 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1265 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1266 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1267 | DropReason& dropReason) { |
| 1268 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1269 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { |
| 1270 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); |
| 1271 | } |
| 1272 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1273 | // Pointer capture was already forcefully disabled because of focus change. |
| 1274 | dropReason = DropReason::NOT_DROPPED; |
| 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | // Set drop reason for early returns |
| 1279 | dropReason = DropReason::NO_POINTER_CAPTURE; |
| 1280 | |
| 1281 | sp<IBinder> token; |
| 1282 | if (entry->pointerCaptureEnabled) { |
| 1283 | // Enable Pointer Capture |
| 1284 | if (!mFocusedWindowRequestedPointerCapture) { |
| 1285 | // This can happen if a window requests capture and immediately releases capture. |
| 1286 | ALOGW("No window requested Pointer Capture."); |
| 1287 | return; |
| 1288 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1289 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1290 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1291 | mWindowTokenWithPointerCapture = token; |
| 1292 | } else { |
| 1293 | // Disable Pointer Capture |
| 1294 | token = mWindowTokenWithPointerCapture; |
| 1295 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1296 | if (mFocusedWindowRequestedPointerCapture) { |
| 1297 | mFocusedWindowRequestedPointerCapture = false; |
| 1298 | setPointerCaptureLocked(false); |
| 1299 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | auto channel = getInputChannelLocked(token); |
| 1303 | if (channel == nullptr) { |
| 1304 | // Window has gone away, clean up Pointer Capture state. |
| 1305 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1306 | if (mFocusedWindowRequestedPointerCapture) { |
| 1307 | mFocusedWindowRequestedPointerCapture = false; |
| 1308 | setPointerCaptureLocked(false); |
| 1309 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1310 | return; |
| 1311 | } |
| 1312 | InputTarget target; |
| 1313 | target.inputChannel = channel; |
| 1314 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1315 | entry->dispatchInProgress = true; |
| 1316 | dispatchEventLocked(currentTime, entry, {target}); |
| 1317 | |
| 1318 | dropReason = DropReason::NOT_DROPPED; |
| 1319 | } |
| 1320 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1321 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1322 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1323 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1324 | if (!entry->dispatchInProgress) { |
| 1325 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1326 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1327 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1328 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1329 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1330 | // We have seen two identical key downs in a row which indicates that the device |
| 1331 | // driver is automatically generating key repeats itself. We take note of the |
| 1332 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1333 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1334 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1335 | // Make sure we don't get key down from a different device. If a different |
| 1336 | // device Id has same key pressed down, the new device Id will replace the |
| 1337 | // current one to hold the key repeat with repeat count reset. |
| 1338 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1339 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1340 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1341 | resetKeyRepeatLocked(); |
| 1342 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1343 | } else { |
| 1344 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1345 | resetKeyRepeatLocked(); |
| 1346 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1347 | } |
| 1348 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1349 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1350 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1351 | // 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] | 1352 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1353 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1354 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1355 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | resetKeyRepeatLocked(); |
| 1357 | } |
| 1358 | |
| 1359 | if (entry->repeatCount == 1) { |
| 1360 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1361 | } else { |
| 1362 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1363 | } |
| 1364 | |
| 1365 | entry->dispatchInProgress = true; |
| 1366 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1367 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | // Handle case where the policy asked us to try again later last time. |
| 1371 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1372 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1373 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1374 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1375 | } |
| 1376 | return false; // wait until next wakeup |
| 1377 | } |
| 1378 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1379 | entry->interceptKeyWakeupTime = 0; |
| 1380 | } |
| 1381 | |
| 1382 | // Give the policy a chance to intercept the key. |
| 1383 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1384 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1385 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1386 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1387 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1388 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1389 | commandEntry->connectionToken = focusedWindowToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1390 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1391 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1392 | return false; // wait for the command to run |
| 1393 | } else { |
| 1394 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1395 | } |
| 1396 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1397 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1398 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1403 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1404 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1405 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1406 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1407 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1408 | return true; |
| 1409 | } |
| 1410 | |
| 1411 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1412 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1413 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1414 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1415 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1416 | return false; |
| 1417 | } |
| 1418 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1419 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1420 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1421 | return true; |
| 1422 | } |
| 1423 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1424 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1425 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1426 | |
| 1427 | // Dispatch the key. |
| 1428 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1429 | return true; |
| 1430 | } |
| 1431 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1432 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1433 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1434 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1435 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1436 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1437 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1438 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1439 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | #endif |
| 1441 | } |
| 1442 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1443 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { |
| 1444 | mLock.unlock(); |
| 1445 | |
| 1446 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; |
| 1447 | if (entry->accuracyChanged) { |
| 1448 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1449 | } |
| 1450 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1451 | entry->hwTimestamp, entry->values); |
| 1452 | mLock.lock(); |
| 1453 | } |
| 1454 | |
| 1455 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, |
| 1456 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 1457 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1458 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1459 | "source=0x%x, sensorType=%s", |
| 1460 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1461 | NamedEnum::string(entry->sensorType).c_str()); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1462 | #endif |
| 1463 | std::unique_ptr<CommandEntry> commandEntry = |
| 1464 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); |
| 1465 | commandEntry->sensorEntry = entry; |
| 1466 | postCommandLocked(std::move(commandEntry)); |
| 1467 | } |
| 1468 | |
| 1469 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
| 1470 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1471 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
| 1472 | NamedEnum::string(sensorType).c_str()); |
| 1473 | #endif |
| 1474 | { // acquire lock |
| 1475 | std::scoped_lock _l(mLock); |
| 1476 | |
| 1477 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1478 | std::shared_ptr<EventEntry> entry = *it; |
| 1479 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1480 | it = mInboundQueue.erase(it); |
| 1481 | releaseInboundEventLocked(entry); |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1488 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1489 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1490 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1491 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1492 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1493 | entry->dispatchInProgress = true; |
| 1494 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1495 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1499 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1500 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1501 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1502 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | return true; |
| 1504 | } |
| 1505 | |
| 1506 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1507 | |
| 1508 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1509 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1510 | |
| 1511 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1512 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | if (isPointerEvent) { |
| 1514 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1515 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1516 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1517 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | } else { |
| 1519 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1520 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1521 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1523 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1524 | return false; |
| 1525 | } |
| 1526 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1527 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1528 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1529 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1530 | return true; |
| 1531 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1532 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1533 | CancelationOptions::Mode mode(isPointerEvent |
| 1534 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1535 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1536 | CancelationOptions options(mode, "input event injection failed"); |
| 1537 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1538 | return true; |
| 1539 | } |
| 1540 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1541 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1542 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1544 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1545 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1546 | mTouchStatesByDisplay.find(entry->displayId); |
| 1547 | if (it != mTouchStatesByDisplay.end()) { |
| 1548 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1549 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1550 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1551 | // the corresponding displays as well. |
| 1552 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1553 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1554 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1555 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1561 | // Dispatch the motion. |
| 1562 | if (conflictingPointerActions) { |
| 1563 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1564 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1565 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1566 | } |
| 1567 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1568 | return true; |
| 1569 | } |
| 1570 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1571 | void InputDispatcher::enqueueDragEventLocked(const sp<InputWindowHandle>& windowHandle, |
| 1572 | bool isExiting, const MotionEntry& motionEntry) { |
| 1573 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1574 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1575 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1576 | PointerCoords pointerCoords; |
| 1577 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1578 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1579 | |
| 1580 | std::unique_ptr<DragEntry> dragEntry = |
| 1581 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1582 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1583 | pointerCoords.getY()); |
| 1584 | |
| 1585 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1586 | } |
| 1587 | |
| 1588 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1589 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1590 | if (channel == nullptr) { |
| 1591 | return; // Window has gone away |
| 1592 | } |
| 1593 | InputTarget target; |
| 1594 | target.inputChannel = channel; |
| 1595 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1596 | entry->dispatchInProgress = true; |
| 1597 | dispatchEventLocked(currentTime, entry, {target}); |
| 1598 | } |
| 1599 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1600 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1601 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1602 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1603 | ", policyFlags=0x%x, " |
| 1604 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1605 | "metaState=0x%x, buttonState=0x%x," |
| 1606 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1607 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1608 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1609 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1610 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1611 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1612 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1613 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1614 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1615 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1616 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1617 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1618 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1619 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1620 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1621 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1622 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1623 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1624 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1625 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1626 | } |
| 1627 | #endif |
| 1628 | } |
| 1629 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1630 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1631 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1632 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1633 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1634 | #if DEBUG_DISPATCH_CYCLE |
| 1635 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1636 | #endif |
| 1637 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1638 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1639 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1640 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1641 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1642 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1643 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1644 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1645 | sp<Connection> connection = |
| 1646 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1647 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1648 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1650 | if (DEBUG_FOCUS) { |
| 1651 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1652 | "is no longer registered with the input dispatcher.", |
| 1653 | inputTarget.inputChannel->getName().c_str()); |
| 1654 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1659 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1660 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1661 | // If the policy decides to close the app, we will get a channel removal event via |
| 1662 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1663 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1664 | // pile up. |
| 1665 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1666 | connection->inputChannel->getName().c_str()); |
| 1667 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1668 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1669 | "application not responding"); |
| 1670 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1674 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1675 | if (DEBUG_FOCUS) { |
| 1676 | ALOGD("Resetting ANR timeouts."); |
| 1677 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | |
| 1679 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1680 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1681 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1684 | /** |
| 1685 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1686 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1687 | * Focused display is the display that the user most recently interacted with. |
| 1688 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1689 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1690 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1691 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1692 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1693 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1694 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1695 | break; |
| 1696 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1697 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1698 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1699 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1700 | break; |
| 1701 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1702 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1703 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1704 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1705 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1706 | case EventEntry::Type::SENSOR: |
| 1707 | case EventEntry::Type::DRAG: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1708 | 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] | 1709 | return ADISPLAY_ID_NONE; |
| 1710 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1711 | } |
| 1712 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1713 | } |
| 1714 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1715 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1716 | const char* focusedWindowName) { |
| 1717 | if (mAnrTracker.empty()) { |
| 1718 | // already processed all events that we waited for |
| 1719 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1724 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1725 | // 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] | 1726 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1727 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1728 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1729 | return true; |
| 1730 | } |
| 1731 | |
| 1732 | // We still have pending events, and already started the timer |
| 1733 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1734 | return true; // Still waiting |
| 1735 | } |
| 1736 | |
| 1737 | // Waited too long, and some connection still hasn't processed all motions |
| 1738 | // Just send the key to the focused window |
| 1739 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1740 | focusedWindowName); |
| 1741 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1745 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1746 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1747 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1748 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1750 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1751 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1752 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1753 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1754 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1755 | // If there is no currently focused window and no focused application |
| 1756 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1757 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1758 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1759 | "display %" PRId32 ".", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1760 | NamedEnum::string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1761 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1764 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1765 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1766 | // start interacting with another application via touch (app switch). This code can be removed |
| 1767 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1768 | // an app is expected to have a focused window. |
| 1769 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1770 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1771 | // 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] | 1772 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1773 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1774 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1775 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1776 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1777 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1778 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1779 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1780 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1781 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1782 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1783 | // Already raised ANR. Drop the event |
| 1784 | ALOGE("Dropping %s event because there is no focused window", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1785 | NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1786 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1787 | } else { |
| 1788 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1789 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | // we have a valid, non-null focused window |
| 1794 | resetNoFocusedWindowTimeoutLocked(); |
| 1795 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1797 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1798 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1801 | if (focusedWindowHandle->getInfo()->paused) { |
| 1802 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1803 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | // If the event is a key event, then we must wait for all previous events to |
| 1807 | // complete before delivering it because previous events may have the |
| 1808 | // side-effect of transferring focus to a different window and we want to |
| 1809 | // ensure that the following keys are sent to the new window. |
| 1810 | // |
| 1811 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1812 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1813 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1814 | // often anticipate pending UI changes when typing on a keyboard. |
| 1815 | // To obtain this behavior, we must serialize key events with respect to all |
| 1816 | // prior input events. |
| 1817 | if (entry.type == EventEntry::Type::KEY) { |
| 1818 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1819 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1820 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1821 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1825 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1826 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1827 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | |
| 1829 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1830 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | } |
| 1832 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1833 | /** |
| 1834 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1835 | * that are currently unresponsive. |
| 1836 | */ |
| 1837 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1838 | const std::vector<TouchedMonitor>& monitors) const { |
| 1839 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1840 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1841 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1842 | sp<Connection> connection = getConnectionLocked( |
| 1843 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1844 | if (connection == nullptr) { |
| 1845 | ALOGE("Could not find connection for monitor %s", |
| 1846 | monitor.monitor.inputChannel->getName().c_str()); |
| 1847 | return false; |
| 1848 | } |
| 1849 | if (!connection->responsive) { |
| 1850 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1851 | connection->inputChannel->getName().c_str()); |
| 1852 | return false; |
| 1853 | } |
| 1854 | return true; |
| 1855 | }); |
| 1856 | return responsiveMonitors; |
| 1857 | } |
| 1858 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1859 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1860 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1861 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1862 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1863 | enum InjectionPermission { |
| 1864 | INJECTION_PERMISSION_UNKNOWN, |
| 1865 | INJECTION_PERMISSION_GRANTED, |
| 1866 | INJECTION_PERMISSION_DENIED |
| 1867 | }; |
| 1868 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1869 | // For security reasons, we defer updating the touch state until we are sure that |
| 1870 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1871 | int32_t displayId = entry.displayId; |
| 1872 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1873 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1874 | |
| 1875 | // 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] | 1876 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1877 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1878 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1879 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1881 | // Copy current touch state into tempTouchState. |
| 1882 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1883 | // 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] | 1884 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1885 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1886 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1887 | mTouchStatesByDisplay.find(displayId); |
| 1888 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1889 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1890 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1893 | bool isSplit = tempTouchState.split; |
| 1894 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1895 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1896 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1897 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1898 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1899 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1900 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1901 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1902 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | bool wrongDevice = false; |
| 1904 | if (newGesture) { |
| 1905 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1906 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1907 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1908 | "in display %" PRId32, |
| 1909 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1910 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1911 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1912 | switchedDevice = false; |
| 1913 | wrongDevice = true; |
| 1914 | goto Failed; |
| 1915 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1916 | tempTouchState.reset(); |
| 1917 | tempTouchState.down = down; |
| 1918 | tempTouchState.deviceId = entry.deviceId; |
| 1919 | tempTouchState.source = entry.source; |
| 1920 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1921 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1922 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1923 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1924 | "in display %" PRId32, |
| 1925 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1926 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1927 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1928 | switchedDevice = false; |
| 1929 | wrongDevice = true; |
| 1930 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1934 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1935 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1936 | int32_t x; |
| 1937 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1938 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1939 | // Always dispatch mouse events to cursor position. |
| 1940 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1941 | x = int32_t(entry.xCursorPosition); |
| 1942 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1943 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1944 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1945 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1946 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1947 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1948 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1949 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1950 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1951 | |
| 1952 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1953 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1954 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1955 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1956 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1957 | if (newTouchedWindowHandle != nullptr && |
| 1958 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1959 | // New window supports splitting, but we should never split mouse events. |
| 1960 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1961 | } else if (isSplit) { |
| 1962 | // New window does not support splitting but we have already split events. |
| 1963 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1964 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1968 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1969 | // 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] | 1970 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1973 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1974 | ALOGI("Not sending touch event to %s because it is paused", |
| 1975 | newTouchedWindowHandle->getName().c_str()); |
| 1976 | newTouchedWindowHandle = nullptr; |
| 1977 | } |
| 1978 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1979 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1980 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1981 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1982 | if (!isResponsive) { |
| 1983 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1984 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1985 | newTouchedWindowHandle = nullptr; |
| 1986 | } |
| 1987 | } |
| 1988 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1989 | // Drop events that can't be trusted due to occlusion |
| 1990 | if (newTouchedWindowHandle != nullptr && |
| 1991 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 1992 | TouchOcclusionInfo occlusionInfo = |
| 1993 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1994 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 1995 | if (DEBUG_TOUCH_OCCLUSION) { |
| 1996 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 1997 | for (const auto& log : occlusionInfo.debugInfo) { |
| 1998 | ALOGD("%s", log.c_str()); |
| 1999 | } |
| 2000 | } |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2001 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 2002 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2003 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2004 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2005 | newTouchedWindowHandle = nullptr; |
| 2006 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2010 | // Also don't send the new touch event to unresponsive gesture monitors |
| 2011 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 2012 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2013 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2014 | 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] | 2015 | "(%d, %d) in display %" PRId32 ".", |
| 2016 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2017 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2018 | goto Failed; |
| 2019 | } |
| 2020 | |
| 2021 | if (newTouchedWindowHandle != nullptr) { |
| 2022 | // Set target flags. |
| 2023 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2024 | if (isSplit) { |
| 2025 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2026 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2027 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2028 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2029 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2030 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2031 | } |
| 2032 | |
| 2033 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2034 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2035 | newHoverWindowHandle = nullptr; |
| 2036 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2037 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | // Update the temporary touch state. |
| 2041 | BitSet32 pointerIds; |
| 2042 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2043 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2044 | pointerIds.markBit(pointerId); |
| 2045 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2046 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2047 | } |
| 2048 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2049 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2050 | } else { |
| 2051 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2052 | |
| 2053 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2054 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2055 | if (DEBUG_FOCUS) { |
| 2056 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2057 | "dropped the pointer down event in display %" PRId32, |
| 2058 | displayId); |
| 2059 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2060 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | goto Failed; |
| 2062 | } |
| 2063 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2064 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2065 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2066 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2067 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2068 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2069 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2070 | 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] | 2071 | |
| 2072 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2073 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2074 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2075 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2076 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2077 | if (DEBUG_FOCUS) { |
| 2078 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2079 | oldTouchedWindowHandle->getName().c_str(), |
| 2080 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2081 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2082 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2083 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2084 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2085 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2086 | |
| 2087 | // Make a slippery entrance into the new window. |
| 2088 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2089 | isSplit = true; |
| 2090 | } |
| 2091 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2092 | int32_t targetFlags = |
| 2093 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2094 | if (isSplit) { |
| 2095 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2096 | } |
| 2097 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2098 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2099 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2100 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | BitSet32 pointerIds; |
| 2104 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2105 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2106 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2107 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2113 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2114 | // when dispatching it as is to newTouchedWindowHandle. |
| 2115 | if (mLastHoverWindowHandle != nullptr && |
| 2116 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2117 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2118 | #if DEBUG_HOVER |
| 2119 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2120 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2121 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2122 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2123 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | } |
| 2125 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2126 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2127 | // when dispatching it as is to newTouchedWindowHandle. |
| 2128 | if (newHoverWindowHandle != nullptr && |
| 2129 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2130 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2131 | #if DEBUG_HOVER |
| 2132 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2133 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2134 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2135 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2136 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2137 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | // Check permission to inject into all touched foreground windows and ensure there |
| 2142 | // is at least one touched foreground window. |
| 2143 | { |
| 2144 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2145 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2146 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2147 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2148 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2149 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2151 | goto Failed; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2155 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2156 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2157 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2158 | "%" PRId32 " or gesture monitor to receive it.", |
| 2159 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2160 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2161 | goto Failed; |
| 2162 | } |
| 2163 | |
| 2164 | // Permission granted to injection into all touched foreground windows. |
| 2165 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2166 | } |
| 2167 | |
| 2168 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2169 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2170 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2171 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2172 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2173 | if (foregroundWindowHandle) { |
| 2174 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2175 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2176 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2177 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 2178 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2179 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 2180 | InputTarget::FLAG_ZERO_COORDS, |
| 2181 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2182 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | } |
| 2187 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2189 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2190 | // of the touch gesture. |
| 2191 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2192 | // engine only supports touch events. We would need to add a mechanism similar |
| 2193 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2194 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2195 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2196 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2197 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2198 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2199 | getWindowHandlesLocked(displayId); |
| 2200 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2202 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2203 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2204 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2205 | .addOrUpdateWindow(windowHandle, |
| 2206 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2207 | InputTarget:: |
| 2208 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2209 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2210 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2217 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2218 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2219 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2220 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2221 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2224 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2225 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2226 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | // Drop the outside or hover touch windows since we will not care about them |
| 2230 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2231 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | |
| 2233 | Failed: |
| 2234 | // Check injection permission once and for all. |
| 2235 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2236 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2237 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2238 | } else { |
| 2239 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2240 | } |
| 2241 | } |
| 2242 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2243 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2244 | return injectionResult; |
| 2245 | } |
| 2246 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2247 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2248 | if (!wrongDevice) { |
| 2249 | if (switchedDevice) { |
| 2250 | if (DEBUG_FOCUS) { |
| 2251 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2252 | } |
| 2253 | *outConflictingPointerActions = true; |
| 2254 | } |
| 2255 | |
| 2256 | if (isHoverAction) { |
| 2257 | // Started hovering, therefore no longer down. |
| 2258 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2259 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2260 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2261 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2262 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2263 | *outConflictingPointerActions = true; |
| 2264 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2265 | tempTouchState.reset(); |
| 2266 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2267 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2268 | tempTouchState.deviceId = entry.deviceId; |
| 2269 | tempTouchState.source = entry.source; |
| 2270 | tempTouchState.displayId = displayId; |
| 2271 | } |
| 2272 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2273 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2274 | // All pointers up or canceled. |
| 2275 | tempTouchState.reset(); |
| 2276 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2277 | // First pointer went down. |
| 2278 | if (oldState && oldState->down) { |
| 2279 | if (DEBUG_FOCUS) { |
| 2280 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2281 | } |
| 2282 | *outConflictingPointerActions = true; |
| 2283 | } |
| 2284 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2285 | // One pointer went up. |
| 2286 | if (isSplit) { |
| 2287 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2288 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2289 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2290 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2291 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2292 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2293 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2294 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2295 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2296 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2297 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2298 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2299 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2300 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2301 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2302 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2303 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2304 | // Save changes unless the action was scroll in which case the temporary touch |
| 2305 | // state was only valid for this one action. |
| 2306 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2307 | if (tempTouchState.displayId >= 0) { |
| 2308 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2309 | } else { |
| 2310 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2311 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2312 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2314 | // Update hover state. |
| 2315 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 | } |
| 2317 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2318 | return injectionResult; |
| 2319 | } |
| 2320 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2321 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
| 2322 | const sp<InputWindowHandle> dropWindow = |
| 2323 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
| 2324 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2325 | true /*ignoreDragWindow*/); |
| 2326 | if (dropWindow) { |
| 2327 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
| 2328 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2329 | } else { |
| 2330 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2331 | } |
| 2332 | mDragState.reset(); |
| 2333 | } |
| 2334 | |
| 2335 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2336 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2337 | return; |
| 2338 | } |
| 2339 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2340 | if (!mDragState->isStartDrag) { |
| 2341 | mDragState->isStartDrag = true; |
| 2342 | mDragState->isStylusButtonDownAtStart = |
| 2343 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2344 | } |
| 2345 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2346 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2347 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2348 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2349 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2350 | // Handle the special case : stylus button no longer pressed. |
| 2351 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2352 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2353 | finishDragAndDrop(entry.displayId, x, y); |
| 2354 | return; |
| 2355 | } |
| 2356 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2357 | const sp<InputWindowHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2358 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2359 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2360 | true /*ignoreDragWindow*/); |
| 2361 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2362 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2363 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2364 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2365 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2366 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2367 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2368 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2369 | } |
| 2370 | // enqueue drag location if needed. |
| 2371 | if (hoverWindowHandle != nullptr) { |
| 2372 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2373 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2374 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2375 | finishDragAndDrop(entry.displayId, x, y); |
| 2376 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2377 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2378 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2379 | } |
| 2380 | } |
| 2381 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2382 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2383 | int32_t targetFlags, BitSet32 pointerIds, |
| 2384 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2385 | std::vector<InputTarget>::iterator it = |
| 2386 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2387 | [&windowHandle](const InputTarget& inputTarget) { |
| 2388 | return inputTarget.inputChannel->getConnectionToken() == |
| 2389 | windowHandle->getToken(); |
| 2390 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2391 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2392 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2393 | |
| 2394 | if (it == inputTargets.end()) { |
| 2395 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2396 | std::shared_ptr<InputChannel> inputChannel = |
| 2397 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2398 | if (inputChannel == nullptr) { |
| 2399 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2400 | return; |
| 2401 | } |
| 2402 | inputTarget.inputChannel = inputChannel; |
| 2403 | inputTarget.flags = targetFlags; |
| 2404 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
| 2405 | inputTargets.push_back(inputTarget); |
| 2406 | it = inputTargets.end() - 1; |
| 2407 | } |
| 2408 | |
| 2409 | ALOG_ASSERT(it->flags == targetFlags); |
| 2410 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2411 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2412 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2413 | } |
| 2414 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2415 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2416 | int32_t displayId, float xOffset, |
| 2417 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2418 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2419 | mGlobalMonitorsByDisplay.find(displayId); |
| 2420 | |
| 2421 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2422 | const std::vector<Monitor>& monitors = it->second; |
| 2423 | for (const Monitor& monitor : monitors) { |
| 2424 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2425 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2429 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2430 | float yOffset, |
| 2431 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2432 | InputTarget target; |
| 2433 | target.inputChannel = monitor.inputChannel; |
| 2434 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2435 | ui::Transform t; |
| 2436 | t.set(xOffset, yOffset); |
| 2437 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2438 | inputTargets.push_back(target); |
| 2439 | } |
| 2440 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2441 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2442 | const InjectionState* injectionState) { |
| 2443 | if (injectionState && |
| 2444 | (windowHandle == nullptr || |
| 2445 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2446 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2447 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2448 | 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] | 2449 | "owned by uid %d", |
| 2450 | injectionState->injectorPid, injectionState->injectorUid, |
| 2451 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2452 | } else { |
| 2453 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2454 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2455 | } |
| 2456 | return false; |
| 2457 | } |
| 2458 | return true; |
| 2459 | } |
| 2460 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2461 | /** |
| 2462 | * Indicate whether one window handle should be considered as obscuring |
| 2463 | * another window handle. We only check a few preconditions. Actually |
| 2464 | * checking the bounds is left to the caller. |
| 2465 | */ |
| 2466 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2467 | const sp<InputWindowHandle>& otherHandle) { |
| 2468 | // Compare by token so cloned layers aren't counted |
| 2469 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2470 | return false; |
| 2471 | } |
| 2472 | auto info = windowHandle->getInfo(); |
| 2473 | auto otherInfo = otherHandle->getInfo(); |
| 2474 | if (!otherInfo->visible) { |
| 2475 | return false; |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2476 | } else if (otherInfo->alpha == 0 && |
| 2477 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 2478 | // Those act as if they were invisible, so we don't need to flag them. |
| 2479 | // We do want to potentially flag touchable windows even if they have 0 |
| 2480 | // opacity, since they can consume touches and alter the effects of the |
| 2481 | // user interaction (eg. apps that rely on |
| 2482 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2483 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2484 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2485 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2486 | // If ownerUid is the same we don't generate occlusion events as there |
| 2487 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2488 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2489 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2490 | return false; |
| 2491 | } else if (otherInfo->displayId != info->displayId) { |
| 2492 | return false; |
| 2493 | } |
| 2494 | return true; |
| 2495 | } |
| 2496 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2497 | /** |
| 2498 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2499 | * untrusted, one should check: |
| 2500 | * |
| 2501 | * 1. If result.hasBlockingOcclusion is true. |
| 2502 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2503 | * BLOCK_UNTRUSTED. |
| 2504 | * |
| 2505 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2506 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2507 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2508 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2509 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2510 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2511 | * |
| 2512 | * If neither of those is true, then it means the touch can be allowed. |
| 2513 | */ |
| 2514 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
| 2515 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2516 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2517 | int32_t displayId = windowInfo->displayId; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2518 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2519 | TouchOcclusionInfo info; |
| 2520 | info.hasBlockingOcclusion = false; |
| 2521 | info.obscuringOpacity = 0; |
| 2522 | info.obscuringUid = -1; |
| 2523 | std::map<int32_t, float> opacityByUid; |
| 2524 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| 2525 | if (windowHandle == otherHandle) { |
| 2526 | break; // All future windows are below us. Exit early. |
| 2527 | } |
| 2528 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2529 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2530 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2531 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2532 | info.debugInfo.push_back( |
| 2533 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2534 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2535 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2536 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2537 | // window's touch occlusion mode |
| 2538 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2539 | info.hasBlockingOcclusion = true; |
| 2540 | info.obscuringUid = otherInfo->ownerUid; |
| 2541 | info.obscuringPackage = otherInfo->packageName; |
| 2542 | break; |
| 2543 | } |
| 2544 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2545 | uint32_t uid = otherInfo->ownerUid; |
| 2546 | float opacity = |
| 2547 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2548 | // Given windows A and B: |
| 2549 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2550 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2551 | opacityByUid[uid] = opacity; |
| 2552 | if (opacity > info.obscuringOpacity) { |
| 2553 | info.obscuringOpacity = opacity; |
| 2554 | info.obscuringUid = uid; |
| 2555 | info.obscuringPackage = otherInfo->packageName; |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2560 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2561 | info.debugInfo.push_back( |
| 2562 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2563 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2564 | return info; |
| 2565 | } |
| 2566 | |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2567 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, |
| 2568 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2569 | return StringPrintf(INDENT2 |
| 2570 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2571 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2572 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2573 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2574 | (isTouchedWindow) ? "[TOUCHED] " : "", |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2575 | NamedEnum::string(info->type, "%" PRId32).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2576 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2577 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2578 | info->frameTop, info->frameRight, info->frameBottom, |
| 2579 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2580 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2581 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2582 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2585 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2586 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2587 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2588 | occlusionInfo.obscuringUid); |
| 2589 | return false; |
| 2590 | } |
| 2591 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2592 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2593 | "%.2f, maximum allowed = %.2f)", |
| 2594 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2595 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2596 | return false; |
| 2597 | } |
| 2598 | return true; |
| 2599 | } |
| 2600 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2601 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2602 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2603 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2604 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
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 | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2608 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [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->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2612 | return true; |
| 2613 | } |
| 2614 | } |
| 2615 | return false; |
| 2616 | } |
| 2617 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2618 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2619 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2620 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2621 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2622 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2623 | if (windowHandle == otherHandle) { |
| 2624 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2625 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2626 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2627 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2628 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2629 | return true; |
| 2630 | } |
| 2631 | } |
| 2632 | return false; |
| 2633 | } |
| 2634 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2635 | std::string InputDispatcher::getApplicationWindowLabel( |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2636 | const InputApplicationHandle* applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2637 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2638 | if (applicationHandle != nullptr) { |
| 2639 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2640 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2641 | } else { |
| 2642 | return applicationHandle->getName(); |
| 2643 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2644 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2645 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2646 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2647 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2651 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2652 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2653 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2654 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2655 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2656 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2657 | return; |
| 2658 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2659 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2660 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2661 | if (focusedWindowHandle != nullptr) { |
| 2662 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2663 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2664 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2665 | 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] | 2666 | #endif |
| 2667 | return; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2672 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2673 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2674 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2675 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2676 | return; |
| 2677 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2678 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2679 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2680 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2681 | } |
| 2682 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2683 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2684 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2685 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2686 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2687 | return; |
| 2688 | } |
| 2689 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2690 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2691 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2692 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2693 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2694 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2695 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2696 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2697 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2698 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2699 | NamedEnum::string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2700 | break; |
| 2701 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2702 | } |
| 2703 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2704 | std::unique_ptr<CommandEntry> commandEntry = |
| 2705 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2706 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2707 | commandEntry->userActivityEventType = eventType; |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2708 | commandEntry->displayId = displayId; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2709 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2713 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2714 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2715 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2716 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2717 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2718 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2719 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2720 | ATRACE_NAME(message.c_str()); |
| 2721 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2722 | #if DEBUG_DISPATCH_CYCLE |
| 2723 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2724 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2725 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2726 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2727 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2728 | #endif |
| 2729 | |
| 2730 | // Skip this event if the connection status is not normal. |
| 2731 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2732 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2733 | #if DEBUG_DISPATCH_CYCLE |
| 2734 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2735 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2736 | #endif |
| 2737 | return; |
| 2738 | } |
| 2739 | |
| 2740 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2741 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2742 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2743 | "Entry type %s should not have FLAG_SPLIT", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2744 | NamedEnum::string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2745 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2746 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2747 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2748 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2749 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2750 | if (!splitMotionEntry) { |
| 2751 | return; // split event was dropped |
| 2752 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2753 | if (DEBUG_FOCUS) { |
| 2754 | ALOGD("channel '%s' ~ Split motion event.", |
| 2755 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2756 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2757 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2758 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2759 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2760 | return; |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2765 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2766 | } |
| 2767 | |
| 2768 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2769 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2770 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2771 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2772 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2773 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2774 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2775 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2776 | ATRACE_NAME(message.c_str()); |
| 2777 | } |
| 2778 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2779 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2780 | |
| 2781 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2782 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2783 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2784 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2785 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2786 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2787 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2788 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2789 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2790 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2791 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2792 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2793 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2794 | |
| 2795 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2796 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2797 | startDispatchCycleLocked(currentTime, connection); |
| 2798 | } |
| 2799 | } |
| 2800 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2801 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2802 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2803 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2804 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2805 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2806 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2807 | connection->getInputChannelName().c_str(), |
| 2808 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2809 | ATRACE_NAME(message.c_str()); |
| 2810 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2811 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2812 | if (!(inputTargetFlags & dispatchMode)) { |
| 2813 | return; |
| 2814 | } |
| 2815 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2816 | |
| 2817 | // This is a new event. |
| 2818 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2819 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2820 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2821 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2822 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2823 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2824 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2825 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2826 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2827 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2828 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2829 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2830 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2831 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2832 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2833 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2834 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2835 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2836 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2837 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2839 | return; // skip the inconsistent event |
| 2840 | } |
| 2841 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2842 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2843 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2844 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2845 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2846 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2847 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2848 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2849 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2850 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2851 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2852 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2853 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2854 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2855 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2856 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2857 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2858 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2859 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2860 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2861 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2862 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2863 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2864 | } |
| 2865 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2866 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2867 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2869 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2870 | "event", |
| 2871 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2872 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2873 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2874 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2876 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2877 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2878 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2879 | } |
| 2880 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2881 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2882 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2883 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2884 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2885 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2886 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2887 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2888 | "event", |
| 2889 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2890 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2891 | return; // skip the inconsistent event |
| 2892 | } |
| 2893 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2894 | dispatchEntry->resolvedEventId = |
| 2895 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2896 | ? mIdGenerator.nextId() |
| 2897 | : motionEntry.id; |
| 2898 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2899 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2900 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2901 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2902 | ATRACE_NAME(message.c_str()); |
| 2903 | } |
| 2904 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2905 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2906 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2907 | |
| 2908 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2909 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2910 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2911 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2912 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2913 | break; |
| 2914 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2915 | case EventEntry::Type::SENSOR: { |
| 2916 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 2917 | break; |
| 2918 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2919 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2920 | case EventEntry::Type::DEVICE_RESET: { |
| 2921 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2922 | NamedEnum::string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2923 | break; |
| 2924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | // Remember that we are waiting for this dispatch to complete. |
| 2928 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2929 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2933 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2934 | traceOutboundQueueLength(connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2937 | /** |
| 2938 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2939 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2940 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2941 | * We will see both launcher and wallpaper in that list. |
| 2942 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2943 | * until the set of interacted connections changes. |
| 2944 | * |
| 2945 | * The following items are skipped, to reduce the logspam: |
| 2946 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2947 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2948 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2949 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2950 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2951 | */ |
| 2952 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2953 | const std::vector<InputTarget>& targets) { |
| 2954 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2955 | if (entry.type == EventEntry::Type::KEY) { |
| 2956 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2957 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2958 | return; |
| 2959 | } |
| 2960 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2961 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2962 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2963 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2964 | return; |
| 2965 | } |
| 2966 | } else { |
| 2967 | return; // Not a key or a motion |
| 2968 | } |
| 2969 | |
| 2970 | std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; |
| 2971 | std::vector<sp<Connection>> newConnections; |
| 2972 | for (const InputTarget& target : targets) { |
| 2973 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2974 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2975 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2976 | } |
| 2977 | |
| 2978 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2979 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2980 | if (connection == nullptr) { |
| 2981 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2982 | } |
| 2983 | newConnectionTokens.insert(std::move(token)); |
| 2984 | newConnections.emplace_back(connection); |
| 2985 | } |
| 2986 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 2987 | return; // no change |
| 2988 | } |
| 2989 | mInteractionConnectionTokens = newConnectionTokens; |
| 2990 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2991 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2992 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2993 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2994 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2995 | std::string message = "Interaction with: " + targetList; |
| 2996 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2997 | message += "<none>"; |
| 2998 | } |
| 2999 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3000 | } |
| 3001 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3002 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3003 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3004 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3005 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3006 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3007 | return; |
| 3008 | } |
| 3009 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3010 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3011 | if (focusedToken == token) { |
| 3012 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3013 | return; |
| 3014 | } |
| 3015 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3016 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 3017 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3018 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3019 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3023 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3024 | if (ATRACE_ENABLED()) { |
| 3025 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3026 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3027 | ATRACE_NAME(message.c_str()); |
| 3028 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3029 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3030 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3031 | #endif |
| 3032 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3033 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3034 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3036 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3037 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3038 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3039 | |
| 3040 | // Publish the event. |
| 3041 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3042 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3043 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3044 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3045 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3046 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3047 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3048 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3049 | status = connection->inputPublisher |
| 3050 | .publishKeyEvent(dispatchEntry->seq, |
| 3051 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3052 | keyEntry.source, keyEntry.displayId, |
| 3053 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3054 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3055 | keyEntry.scanCode, keyEntry.metaState, |
| 3056 | keyEntry.repeatCount, keyEntry.downTime, |
| 3057 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3058 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3059 | } |
| 3060 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3061 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3062 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3063 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3064 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3065 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3066 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3067 | // 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] | 3068 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3069 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3070 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3071 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3072 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3073 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3074 | // Don't apply window scale here since we don't want scale to affect raw |
| 3075 | // coordinates. The scale will be sent back to the client and applied |
| 3076 | // later when requesting relative coordinates. |
| 3077 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3078 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3079 | } |
| 3080 | usingCoords = scaledCoords; |
| 3081 | } |
| 3082 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3083 | // We don't want the dispatch target to know. |
| 3084 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3085 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3086 | scaledCoords[i].clear(); |
| 3087 | } |
| 3088 | usingCoords = scaledCoords; |
| 3089 | } |
| 3090 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3091 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3092 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3093 | |
| 3094 | // Publish the motion event. |
| 3095 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3096 | .publishMotionEvent(dispatchEntry->seq, |
| 3097 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3098 | motionEntry.deviceId, motionEntry.source, |
| 3099 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3100 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3101 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3102 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3103 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3104 | motionEntry.buttonState, |
| 3105 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3106 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3107 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3108 | motionEntry.xCursorPosition, |
| 3109 | motionEntry.yCursorPosition, |
| 3110 | motionEntry.downTime, motionEntry.eventTime, |
| 3111 | motionEntry.pointerCount, |
| 3112 | motionEntry.pointerProperties, usingCoords); |
| 3113 | reportTouchEventForStatistics(motionEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3114 | break; |
| 3115 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3116 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3117 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3118 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3119 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3120 | focusEntry.id, |
| 3121 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3122 | mInTouchMode); |
| 3123 | break; |
| 3124 | } |
| 3125 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3126 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3127 | const auto& captureEntry = |
| 3128 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3129 | status = connection->inputPublisher |
| 3130 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
| 3131 | captureEntry.pointerCaptureEnabled); |
| 3132 | break; |
| 3133 | } |
| 3134 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3135 | case EventEntry::Type::DRAG: { |
| 3136 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3137 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3138 | dragEntry.id, dragEntry.x, |
| 3139 | dragEntry.y, |
| 3140 | dragEntry.isExiting); |
| 3141 | break; |
| 3142 | } |
| 3143 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3144 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3145 | case EventEntry::Type::DEVICE_RESET: |
| 3146 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3147 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3148 | NamedEnum::string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3149 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3150 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | // Check the result. |
| 3154 | if (status) { |
| 3155 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3156 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3157 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3158 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3159 | "should be empty and we shouldn't have any problems writing an " |
| 3160 | "event to it, status=%d", |
| 3161 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3162 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3163 | } else { |
| 3164 | // Pipe is full and we are waiting for the app to finish process some events |
| 3165 | // before sending more events to it. |
| 3166 | #if DEBUG_DISPATCH_CYCLE |
| 3167 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3168 | "waiting for the application to catch up", |
| 3169 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3170 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | } |
| 3172 | } else { |
| 3173 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3174 | "status=%d", |
| 3175 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3176 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3177 | } |
| 3178 | return; |
| 3179 | } |
| 3180 | |
| 3181 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3182 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3183 | connection->outboundQueue.end(), |
| 3184 | dispatchEntry)); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3185 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3186 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3187 | if (connection->responsive) { |
| 3188 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3189 | connection->inputChannel->getConnectionToken()); |
| 3190 | } |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3191 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3195 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3196 | size_t size; |
| 3197 | switch (event.type) { |
| 3198 | case VerifiedInputEvent::Type::KEY: { |
| 3199 | size = sizeof(VerifiedKeyEvent); |
| 3200 | break; |
| 3201 | } |
| 3202 | case VerifiedInputEvent::Type::MOTION: { |
| 3203 | size = sizeof(VerifiedMotionEvent); |
| 3204 | break; |
| 3205 | } |
| 3206 | } |
| 3207 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3208 | return mHmacKeyManager.sign(start, size); |
| 3209 | } |
| 3210 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3211 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3212 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3213 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3214 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3215 | // Only sign events up and down events as the purely move events |
| 3216 | // are tied to their up/down counterparts so signing would be redundant. |
| 3217 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3218 | verifiedEvent.actionMasked = actionMasked; |
| 3219 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3220 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3221 | } |
| 3222 | return INVALID_HMAC; |
| 3223 | } |
| 3224 | |
| 3225 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3226 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3227 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3228 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3229 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3230 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3231 | } |
| 3232 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3233 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3234 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3235 | bool handled, nsecs_t consumeTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3236 | #if DEBUG_DISPATCH_CYCLE |
| 3237 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3238 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3239 | #endif |
| 3240 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3241 | if (connection->status == Connection::STATUS_BROKEN || |
| 3242 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3243 | return; |
| 3244 | } |
| 3245 | |
| 3246 | // Notify other system components and prepare to start the next dispatch cycle. |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3247 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3251 | const sp<Connection>& connection, |
| 3252 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3253 | #if DEBUG_DISPATCH_CYCLE |
| 3254 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3255 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | #endif |
| 3257 | |
| 3258 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3259 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3260 | traceOutboundQueueLength(connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3261 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3262 | traceWaitQueueLength(connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3263 | |
| 3264 | // The connection appears to be unrecoverably broken. |
| 3265 | // Ignore already broken or zombie connections. |
| 3266 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3267 | connection->status = Connection::STATUS_BROKEN; |
| 3268 | |
| 3269 | if (notify) { |
| 3270 | // Notify other system components. |
| 3271 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3276 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3277 | while (!queue.empty()) { |
| 3278 | DispatchEntry* dispatchEntry = queue.front(); |
| 3279 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3280 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3284 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3285 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3286 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3287 | } |
| 3288 | delete dispatchEntry; |
| 3289 | } |
| 3290 | |
| 3291 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
| 3292 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 3293 | |
| 3294 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3295 | std::scoped_lock _l(d->mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3296 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3297 | if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | ALOGE("Received spurious receive callback for unknown input channel. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3299 | "fd=%d, events=0x%x", |
| 3300 | fd, events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3301 | return 0; // remove the callback |
| 3302 | } |
| 3303 | |
| 3304 | bool notify; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3305 | sp<Connection> connection = d->mConnectionsByFd[fd]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3306 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3307 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3308 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3309 | "events=0x%x", |
| 3310 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3311 | return 1; |
| 3312 | } |
| 3313 | |
| 3314 | nsecs_t currentTime = now(); |
| 3315 | bool gotOne = false; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 3316 | status_t status = OK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 | for (;;) { |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3318 | Result<InputPublisher::ConsumerResponse> result = |
| 3319 | connection->inputPublisher.receiveConsumerResponse(); |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 3320 | if (!result.ok()) { |
| 3321 | status = result.error().code(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3322 | break; |
| 3323 | } |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3324 | |
| 3325 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3326 | const InputPublisher::Finished& finish = |
| 3327 | std::get<InputPublisher::Finished>(*result); |
| 3328 | d->finishDispatchCycleLocked(currentTime, connection, finish.seq, |
| 3329 | finish.handled, finish.consumeTime); |
| 3330 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
| 3331 | // TODO(b/167947340): Report this data to LatencyTracker |
| 3332 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3333 | gotOne = true; |
| 3334 | } |
| 3335 | if (gotOne) { |
| 3336 | d->runCommandsLockedInterruptible(); |
| 3337 | if (status == WOULD_BLOCK) { |
| 3338 | return 1; |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3343 | if (notify) { |
| 3344 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3345 | connection->getInputChannelName().c_str(), status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3346 | } |
| 3347 | } else { |
| 3348 | // Monitor channels are never explicitly unregistered. |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3349 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 3350 | const bool stillHaveWindowHandle = |
| 3351 | d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != |
| 3352 | nullptr; |
| 3353 | notify = !connection->monitor && stillHaveWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3354 | if (notify) { |
| 3355 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3356 | "events=0x%x", |
| 3357 | connection->getInputChannelName().c_str(), events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } |
| 3359 | } |
| 3360 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3361 | // Remove the channel. |
| 3362 | d->removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3363 | return 0; // remove the callback |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3364 | } // release lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3365 | } |
| 3366 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3367 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3368 | const CancelationOptions& options) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3369 | for (const auto& [fd, connection] : mConnectionsByFd) { |
| 3370 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3371 | } |
| 3372 | } |
| 3373 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3374 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3375 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3376 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3377 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3378 | } |
| 3379 | |
| 3380 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3381 | const CancelationOptions& options, |
| 3382 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3383 | for (const auto& it : monitorsByDisplay) { |
| 3384 | const std::vector<Monitor>& monitors = it.second; |
| 3385 | for (const Monitor& monitor : monitors) { |
| 3386 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3387 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3391 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3392 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3393 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3394 | if (connection == nullptr) { |
| 3395 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3396 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3397 | |
| 3398 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3402 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3403 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3404 | return; |
| 3405 | } |
| 3406 | |
| 3407 | nsecs_t currentTime = now(); |
| 3408 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3409 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3410 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3411 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3412 | if (cancelationEvents.empty()) { |
| 3413 | return; |
| 3414 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3415 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3416 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3417 | "with reality: %s, mode=%d.", |
| 3418 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3419 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3420 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3421 | |
| 3422 | InputTarget target; |
| 3423 | sp<InputWindowHandle> windowHandle = |
| 3424 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3425 | if (windowHandle != nullptr) { |
| 3426 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3427 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3428 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3429 | } |
| 3430 | target.inputChannel = connection->inputChannel; |
| 3431 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3432 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3433 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3434 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3435 | switch (cancelationEventEntry->type) { |
| 3436 | case EventEntry::Type::KEY: { |
| 3437 | logOutboundKeyDetails("cancel - ", |
| 3438 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3439 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3440 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3441 | case EventEntry::Type::MOTION: { |
| 3442 | logOutboundMotionDetails("cancel - ", |
| 3443 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3444 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3445 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3446 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3447 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3448 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3449 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3450 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3451 | break; |
| 3452 | } |
| 3453 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3454 | case EventEntry::Type::DEVICE_RESET: |
| 3455 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3456 | 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] | 3457 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3458 | break; |
| 3459 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3460 | } |
| 3461 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3462 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3463 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3464 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3465 | |
| 3466 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3467 | } |
| 3468 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3469 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3470 | const sp<Connection>& connection) { |
| 3471 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3472 | return; |
| 3473 | } |
| 3474 | |
| 3475 | nsecs_t currentTime = now(); |
| 3476 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3477 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3478 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3479 | |
| 3480 | if (downEvents.empty()) { |
| 3481 | return; |
| 3482 | } |
| 3483 | |
| 3484 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3485 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3486 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3487 | #endif |
| 3488 | |
| 3489 | InputTarget target; |
| 3490 | sp<InputWindowHandle> windowHandle = |
| 3491 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3492 | if (windowHandle != nullptr) { |
| 3493 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3494 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3495 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3496 | } |
| 3497 | target.inputChannel = connection->inputChannel; |
| 3498 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3499 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3500 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3501 | switch (downEventEntry->type) { |
| 3502 | case EventEntry::Type::MOTION: { |
| 3503 | logOutboundMotionDetails("down - ", |
| 3504 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3505 | break; |
| 3506 | } |
| 3507 | |
| 3508 | case EventEntry::Type::KEY: |
| 3509 | case EventEntry::Type::FOCUS: |
| 3510 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3511 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3512 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3513 | case EventEntry::Type::SENSOR: |
| 3514 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3515 | 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] | 3516 | NamedEnum::string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3517 | break; |
| 3518 | } |
| 3519 | } |
| 3520 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3521 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3522 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | startDispatchCycleLocked(currentTime, connection); |
| 3526 | } |
| 3527 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3528 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3529 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | ALOG_ASSERT(pointerIds.value != 0); |
| 3531 | |
| 3532 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3533 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3534 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3535 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3536 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3537 | uint32_t splitPointerCount = 0; |
| 3538 | |
| 3539 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3540 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3541 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3542 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3543 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3544 | if (pointerIds.hasBit(pointerId)) { |
| 3545 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3546 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3547 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3548 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3549 | splitPointerCount += 1; |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | if (splitPointerCount != pointerIds.count()) { |
| 3554 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3555 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3556 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3557 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3558 | // in this way. |
| 3559 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3560 | "we expected there to be %d pointers. This probably means we received " |
| 3561 | "a broken sequence of pointer ids from the input device.", |
| 3562 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3563 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3564 | } |
| 3565 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3566 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3567 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3568 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3569 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3570 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3571 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3572 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3573 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3574 | if (pointerIds.hasBit(pointerId)) { |
| 3575 | if (pointerIds.count() == 1) { |
| 3576 | // The first/last pointer went down/up. |
| 3577 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3578 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3579 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3580 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3581 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3582 | } else { |
| 3583 | // A secondary pointer went down/up. |
| 3584 | uint32_t splitPointerIndex = 0; |
| 3585 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3586 | splitPointerIndex += 1; |
| 3587 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3588 | action = maskedAction | |
| 3589 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3590 | } |
| 3591 | } else { |
| 3592 | // An unrelated pointer changed. |
| 3593 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3594 | } |
| 3595 | } |
| 3596 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3597 | int32_t newId = mIdGenerator.nextId(); |
| 3598 | if (ATRACE_ENABLED()) { |
| 3599 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3600 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3601 | originalMotionEntry.id, newId); |
| 3602 | ATRACE_NAME(message.c_str()); |
| 3603 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3604 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3605 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3606 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3607 | originalMotionEntry.displayId, |
| 3608 | originalMotionEntry.policyFlags, action, |
| 3609 | originalMotionEntry.actionButton, |
| 3610 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3611 | originalMotionEntry.buttonState, |
| 3612 | originalMotionEntry.classification, |
| 3613 | originalMotionEntry.edgeFlags, |
| 3614 | originalMotionEntry.xPrecision, |
| 3615 | originalMotionEntry.yPrecision, |
| 3616 | originalMotionEntry.xCursorPosition, |
| 3617 | originalMotionEntry.yCursorPosition, |
| 3618 | originalMotionEntry.downTime, splitPointerCount, |
| 3619 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3621 | if (originalMotionEntry.injectionState) { |
| 3622 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3623 | splitMotionEntry->injectionState->refCount += 1; |
| 3624 | } |
| 3625 | |
| 3626 | return splitMotionEntry; |
| 3627 | } |
| 3628 | |
| 3629 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3630 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3631 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | #endif |
| 3633 | |
| 3634 | bool needWake; |
| 3635 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3636 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3638 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3639 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3640 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3641 | } // release lock |
| 3642 | |
| 3643 | if (needWake) { |
| 3644 | mLooper->wake(); |
| 3645 | } |
| 3646 | } |
| 3647 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3648 | /** |
| 3649 | * If one of the meta shortcuts is detected, process them here: |
| 3650 | * Meta + Backspace -> generate BACK |
| 3651 | * Meta + Enter -> generate HOME |
| 3652 | * This will potentially overwrite keyCode and metaState. |
| 3653 | */ |
| 3654 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3655 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3656 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3657 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3658 | if (keyCode == AKEYCODE_DEL) { |
| 3659 | newKeyCode = AKEYCODE_BACK; |
| 3660 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3661 | newKeyCode = AKEYCODE_HOME; |
| 3662 | } |
| 3663 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3664 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3665 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3666 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3667 | keyCode = newKeyCode; |
| 3668 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3669 | } |
| 3670 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3671 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3672 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3673 | // 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] | 3674 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3675 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3676 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3677 | if (replacementIt != mReplacedKeys.end()) { |
| 3678 | keyCode = replacementIt->second; |
| 3679 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3680 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3681 | } |
| 3682 | } |
| 3683 | } |
| 3684 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3685 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3686 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3687 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3688 | "policyFlags=0x%x, action=0x%x, " |
| 3689 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3690 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3691 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3692 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3693 | #endif |
| 3694 | if (!validateKeyEvent(args->action)) { |
| 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | uint32_t policyFlags = args->policyFlags; |
| 3699 | int32_t flags = args->flags; |
| 3700 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3701 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3702 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3703 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3705 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3706 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3707 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3709 | metaState |= AMETA_FUNCTION_ON; |
| 3710 | } |
| 3711 | |
| 3712 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3713 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3714 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3715 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3716 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3717 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3718 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3719 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3720 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3721 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3722 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3723 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3724 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3725 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3726 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3727 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3728 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3729 | bool needWake; |
| 3730 | { // acquire lock |
| 3731 | mLock.lock(); |
| 3732 | |
| 3733 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3734 | mLock.unlock(); |
| 3735 | |
| 3736 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3737 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3738 | return; // event was consumed by the filter |
| 3739 | } |
| 3740 | |
| 3741 | mLock.lock(); |
| 3742 | } |
| 3743 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3744 | std::unique_ptr<KeyEntry> newEntry = |
| 3745 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3746 | args->displayId, policyFlags, args->action, flags, |
| 3747 | keyCode, args->scanCode, metaState, repeatCount, |
| 3748 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3749 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3750 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3751 | mLock.unlock(); |
| 3752 | } // release lock |
| 3753 | |
| 3754 | if (needWake) { |
| 3755 | mLooper->wake(); |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3760 | return mInputFilterEnabled; |
| 3761 | } |
| 3762 | |
| 3763 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3764 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3765 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3766 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3767 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3768 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3769 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3770 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3771 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3772 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3773 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3775 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3776 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3777 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3778 | "orientation=%f", |
| 3779 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3780 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3781 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3782 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3783 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3784 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3785 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3786 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3787 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3788 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3789 | } |
| 3790 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3791 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3792 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | return; |
| 3794 | } |
| 3795 | |
| 3796 | uint32_t policyFlags = args->policyFlags; |
| 3797 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3798 | |
| 3799 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3800 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3801 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3802 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3803 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3804 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3805 | |
| 3806 | bool needWake; |
| 3807 | { // acquire lock |
| 3808 | mLock.lock(); |
| 3809 | |
| 3810 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3811 | mLock.unlock(); |
| 3812 | |
| 3813 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3814 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3815 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3816 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3817 | args->metaState, args->buttonState, args->classification, transform, |
| 3818 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
| 3819 | args->yCursorPosition, args->downTime, args->eventTime, |
| 3820 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3821 | |
| 3822 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3823 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3824 | return; // event was consumed by the filter |
| 3825 | } |
| 3826 | |
| 3827 | mLock.lock(); |
| 3828 | } |
| 3829 | |
| 3830 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3831 | std::unique_ptr<MotionEntry> newEntry = |
| 3832 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3833 | args->source, args->displayId, policyFlags, |
| 3834 | args->action, args->actionButton, args->flags, |
| 3835 | args->metaState, args->buttonState, |
| 3836 | args->classification, args->edgeFlags, |
| 3837 | args->xPrecision, args->yPrecision, |
| 3838 | args->xCursorPosition, args->yCursorPosition, |
| 3839 | args->downTime, args->pointerCount, |
| 3840 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3842 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3843 | mLock.unlock(); |
| 3844 | } // release lock |
| 3845 | |
| 3846 | if (needWake) { |
| 3847 | mLooper->wake(); |
| 3848 | } |
| 3849 | } |
| 3850 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3851 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
| 3852 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3853 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3854 | " sensorType=%s", |
| 3855 | args->id, args->eventTime, args->deviceId, args->source, |
| 3856 | NamedEnum::string(args->sensorType).c_str()); |
| 3857 | #endif |
| 3858 | |
| 3859 | bool needWake; |
| 3860 | { // acquire lock |
| 3861 | mLock.lock(); |
| 3862 | |
| 3863 | // Just enqueue a new sensor event. |
| 3864 | std::unique_ptr<SensorEntry> newEntry = |
| 3865 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 3866 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 3867 | args->sensorType, args->accuracy, |
| 3868 | args->accuracyChanged, args->values); |
| 3869 | |
| 3870 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 3871 | mLock.unlock(); |
| 3872 | } // release lock |
| 3873 | |
| 3874 | if (needWake) { |
| 3875 | mLooper->wake(); |
| 3876 | } |
| 3877 | } |
| 3878 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3879 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
| 3880 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3881 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 3882 | args->deviceId, args->isOn); |
| 3883 | #endif |
| 3884 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 3885 | } |
| 3886 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3887 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3888 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3892 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3893 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3894 | "switchMask=0x%08x", |
| 3895 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3896 | #endif |
| 3897 | |
| 3898 | uint32_t policyFlags = args->policyFlags; |
| 3899 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3900 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3904 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3905 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3906 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3907 | #endif |
| 3908 | |
| 3909 | bool needWake; |
| 3910 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3911 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3912 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3913 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 3914 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 3915 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3916 | } // release lock |
| 3917 | |
| 3918 | if (needWake) { |
| 3919 | mLooper->wake(); |
| 3920 | } |
| 3921 | } |
| 3922 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3923 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
| 3924 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3925 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
| 3926 | args->enabled ? "true" : "false"); |
| 3927 | #endif |
| 3928 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3929 | bool needWake; |
| 3930 | { // acquire lock |
| 3931 | std::scoped_lock _l(mLock); |
| 3932 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
| 3933 | args->enabled); |
| 3934 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 3935 | } // release lock |
| 3936 | |
| 3937 | if (needWake) { |
| 3938 | mLooper->wake(); |
| 3939 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3940 | } |
| 3941 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3942 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 3943 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 3944 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3945 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3946 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3947 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3948 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3949 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3950 | 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] | 3951 | |
| 3952 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3953 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3954 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3955 | } |
| 3956 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3957 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3958 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3959 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3960 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3961 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3962 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3963 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3964 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3965 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3966 | int32_t flags = incomingKey.getFlags(); |
| 3967 | int32_t keyCode = incomingKey.getKeyCode(); |
| 3968 | int32_t metaState = incomingKey.getMetaState(); |
| 3969 | accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3970 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3971 | KeyEvent keyEvent; |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3972 | keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3973 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 3974 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 3975 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3976 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3977 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3978 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3979 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3980 | |
| 3981 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3982 | android::base::Timer t; |
| 3983 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 3984 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3985 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 3986 | std::to_string(t.duration().count()).c_str()); |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3991 | std::unique_ptr<KeyEntry> injectedEntry = |
| 3992 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
| 3993 | VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), |
| 3994 | incomingKey.getDisplayId(), policyFlags, action, |
| 3995 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 3996 | incomingKey.getRepeatCount(), |
| 3997 | incomingKey.getDownTime()); |
| 3998 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3999 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4000 | } |
| 4001 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4002 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4003 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 4004 | int32_t action = motionEvent->getAction(); |
| 4005 | size_t pointerCount = motionEvent->getPointerCount(); |
| 4006 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 4007 | int32_t actionButton = motionEvent->getActionButton(); |
| 4008 | int32_t displayId = motionEvent->getDisplayId(); |
| 4009 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4010 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4014 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 4015 | android::base::Timer t; |
| 4016 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4017 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4018 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4019 | std::to_string(t.duration().count()).c_str()); |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | mLock.lock(); |
| 4024 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 4025 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4026 | std::unique_ptr<MotionEntry> injectedEntry = |
| 4027 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, |
| 4028 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
| 4029 | motionEvent->getDisplayId(), policyFlags, action, |
| 4030 | actionButton, motionEvent->getFlags(), |
| 4031 | motionEvent->getMetaState(), |
| 4032 | motionEvent->getButtonState(), |
| 4033 | motionEvent->getClassification(), |
| 4034 | motionEvent->getEdgeFlags(), |
| 4035 | motionEvent->getXPrecision(), |
| 4036 | motionEvent->getYPrecision(), |
| 4037 | motionEvent->getRawXCursorPosition(), |
| 4038 | motionEvent->getRawYCursorPosition(), |
| 4039 | motionEvent->getDownTime(), |
| 4040 | uint32_t(pointerCount), pointerProperties, |
| 4041 | samplePointerCoords, motionEvent->getXOffset(), |
| 4042 | motionEvent->getYOffset()); |
| 4043 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4044 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 4045 | sampleEventTimes += 1; |
| 4046 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4047 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
| 4048 | std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, |
| 4049 | VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), |
| 4050 | motionEvent->getDisplayId(), policyFlags, |
| 4051 | action, actionButton, motionEvent->getFlags(), |
| 4052 | motionEvent->getMetaState(), |
| 4053 | motionEvent->getButtonState(), |
| 4054 | motionEvent->getClassification(), |
| 4055 | motionEvent->getEdgeFlags(), |
| 4056 | motionEvent->getXPrecision(), |
| 4057 | motionEvent->getYPrecision(), |
| 4058 | motionEvent->getRawXCursorPosition(), |
| 4059 | motionEvent->getRawYCursorPosition(), |
| 4060 | motionEvent->getDownTime(), |
| 4061 | uint32_t(pointerCount), pointerProperties, |
| 4062 | samplePointerCoords, |
| 4063 | motionEvent->getXOffset(), |
| 4064 | motionEvent->getYOffset()); |
| 4065 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4066 | } |
| 4067 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4068 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4069 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4070 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4071 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4072 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4076 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4077 | injectionState->injectionIsAsync = true; |
| 4078 | } |
| 4079 | |
| 4080 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4081 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4082 | |
| 4083 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4084 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4085 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4086 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | mLock.unlock(); |
| 4090 | |
| 4091 | if (needWake) { |
| 4092 | mLooper->wake(); |
| 4093 | } |
| 4094 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4095 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4096 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4097 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4098 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4099 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4100 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4101 | } else { |
| 4102 | for (;;) { |
| 4103 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4104 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4105 | break; |
| 4106 | } |
| 4107 | |
| 4108 | nsecs_t remainingTimeout = endTime - now(); |
| 4109 | if (remainingTimeout <= 0) { |
| 4110 | #if DEBUG_INJECTION |
| 4111 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4112 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4114 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4115 | break; |
| 4116 | } |
| 4117 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4118 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | } |
| 4120 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4121 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4122 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | while (injectionState->pendingForegroundDispatches != 0) { |
| 4124 | #if DEBUG_INJECTION |
| 4125 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4126 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4127 | #endif |
| 4128 | nsecs_t remainingTimeout = endTime - now(); |
| 4129 | if (remainingTimeout <= 0) { |
| 4130 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4131 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4132 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4133 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4134 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | break; |
| 4136 | } |
| 4137 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4138 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4139 | } |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | injectionState->release(); |
| 4144 | } // release lock |
| 4145 | |
| 4146 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4147 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4148 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4149 | #endif |
| 4150 | |
| 4151 | return injectionResult; |
| 4152 | } |
| 4153 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4154 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4155 | std::array<uint8_t, 32> calculatedHmac; |
| 4156 | std::unique_ptr<VerifiedInputEvent> result; |
| 4157 | switch (event.getType()) { |
| 4158 | case AINPUT_EVENT_TYPE_KEY: { |
| 4159 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4160 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4161 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4162 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4163 | break; |
| 4164 | } |
| 4165 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4166 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4167 | VerifiedMotionEvent verifiedMotionEvent = |
| 4168 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4169 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4170 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4171 | break; |
| 4172 | } |
| 4173 | default: { |
| 4174 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4175 | return nullptr; |
| 4176 | } |
| 4177 | } |
| 4178 | if (calculatedHmac == INVALID_HMAC) { |
| 4179 | return nullptr; |
| 4180 | } |
| 4181 | if (calculatedHmac != event.getHmac()) { |
| 4182 | return nullptr; |
| 4183 | } |
| 4184 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4185 | } |
| 4186 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4188 | return injectorUid == 0 || |
| 4189 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4192 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4193 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4194 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | if (injectionState) { |
| 4196 | #if DEBUG_INJECTION |
| 4197 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4198 | "injectorPid=%d, injectorUid=%d", |
| 4199 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4200 | #endif |
| 4201 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4202 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4203 | // Log the outcome since the injector did not wait for the injection result. |
| 4204 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4205 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4206 | ALOGV("Asynchronous input event injection succeeded."); |
| 4207 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4208 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4209 | ALOGW("Asynchronous input event injection failed."); |
| 4210 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4211 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4212 | ALOGW("Asynchronous input event injection permission denied."); |
| 4213 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4214 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4215 | ALOGW("Asynchronous input event injection timed out."); |
| 4216 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4217 | case InputEventInjectionResult::PENDING: |
| 4218 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4219 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4224 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | } |
| 4226 | } |
| 4227 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4228 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4229 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4230 | if (injectionState) { |
| 4231 | injectionState->pendingForegroundDispatches += 1; |
| 4232 | } |
| 4233 | } |
| 4234 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4235 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4236 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4237 | if (injectionState) { |
| 4238 | injectionState->pendingForegroundDispatches -= 1; |
| 4239 | |
| 4240 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4241 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | } |
| 4243 | } |
| 4244 | } |
| 4245 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4246 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4247 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4248 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 4249 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4250 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4251 | } |
| 4252 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4253 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4254 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4255 | if (windowHandleToken == nullptr) { |
| 4256 | return nullptr; |
| 4257 | } |
| 4258 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4259 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4260 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4261 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4262 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4263 | return windowHandle; |
| 4264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | } |
| 4266 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4267 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4270 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4271 | int displayId) const { |
| 4272 | if (windowHandleToken == nullptr) { |
| 4273 | return nullptr; |
| 4274 | } |
| 4275 | |
| 4276 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 4277 | if (windowHandle->getToken() == windowHandleToken) { |
| 4278 | return windowHandle; |
| 4279 | } |
| 4280 | } |
| 4281 | return nullptr; |
| 4282 | } |
| 4283 | |
| 4284 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4285 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4286 | return getWindowHandleLocked(focusedToken, displayId); |
| 4287 | } |
| 4288 | |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4289 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 4290 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4291 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4292 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4293 | if (handle->getId() == windowHandle->getId() && |
| 4294 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4295 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4296 | ALOGE("Found window %s in display %" PRId32 |
| 4297 | ", but it should belong to display %" PRId32, |
| 4298 | windowHandle->getName().c_str(), it.first, |
| 4299 | windowHandle->getInfo()->displayId); |
| 4300 | } |
| 4301 | return true; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4302 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | return false; |
| 4306 | } |
| 4307 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4308 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 4309 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4310 | const bool noInputChannel = |
| 4311 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4312 | if (connection != nullptr && noInputChannel) { |
| 4313 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4314 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4315 | return false; |
| 4316 | } |
| 4317 | |
| 4318 | if (connection == nullptr) { |
| 4319 | if (!noInputChannel) { |
| 4320 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4321 | } |
| 4322 | return false; |
| 4323 | } |
| 4324 | if (!connection->responsive) { |
| 4325 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4326 | return false; |
| 4327 | } |
| 4328 | return true; |
| 4329 | } |
| 4330 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4331 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4332 | const sp<IBinder>& token) const { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4333 | size_t count = mInputChannelsByToken.count(token); |
| 4334 | if (count == 0) { |
| 4335 | return nullptr; |
| 4336 | } |
| 4337 | return mInputChannelsByToken.at(token); |
| 4338 | } |
| 4339 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4340 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 4341 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 4342 | if (inputWindowHandles.empty()) { |
| 4343 | // Remove all handles on a display if there are no windows left. |
| 4344 | mWindowHandlesByDisplay.erase(displayId); |
| 4345 | return; |
| 4346 | } |
| 4347 | |
| 4348 | // Since we compare the pointer of input window handles across window updates, we need |
| 4349 | // to make sure the handle object for the same window stays unchanged across updates. |
| 4350 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4351 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4352 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4353 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | std::vector<sp<InputWindowHandle>> newHandles; |
| 4357 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 4358 | if (!handle->updateInfo()) { |
| 4359 | // handle no longer valid |
| 4360 | continue; |
| 4361 | } |
| 4362 | |
| 4363 | const InputWindowInfo* info = handle->getInfo(); |
| 4364 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 4365 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 4366 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4367 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4368 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 4369 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4370 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4371 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4372 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4373 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4374 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | if (info->displayId != displayId) { |
| 4378 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4379 | handle->getName().c_str(), displayId, info->displayId); |
| 4380 | continue; |
| 4381 | } |
| 4382 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4383 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4384 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4385 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4386 | oldHandle->updateFrom(handle); |
| 4387 | newHandles.push_back(oldHandle); |
| 4388 | } else { |
| 4389 | newHandles.push_back(handle); |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | // Insert or replace |
| 4394 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4395 | } |
| 4396 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4397 | void InputDispatcher::setInputWindows( |
| 4398 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 4399 | { // acquire lock |
| 4400 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4401 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4402 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4406 | mLooper->wake(); |
| 4407 | } |
| 4408 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4409 | /** |
| 4410 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4411 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4412 | * If set an empty list, remove all handles from the specific display. |
| 4413 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4414 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4415 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4416 | void InputDispatcher::setInputWindowsLocked( |
| 4417 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4418 | if (DEBUG_FOCUS) { |
| 4419 | std::string windowList; |
| 4420 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 4421 | windowList += iwh->getName() + " "; |
| 4422 | } |
| 4423 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4424 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4425 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4426 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4427 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 4428 | const bool noInputWindow = |
| 4429 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4430 | if (noInputWindow && window->getToken() != nullptr) { |
| 4431 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4432 | window->getName().c_str()); |
| 4433 | window->releaseChannel(); |
| 4434 | } |
| 4435 | } |
| 4436 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4437 | // Copy old handles for release if they are no longer present. |
| 4438 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4439 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4440 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4441 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4442 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 4443 | if (mLastHoverWindowHandle && |
| 4444 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4445 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4446 | mLastHoverWindowHandle = nullptr; |
| 4447 | } |
| 4448 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4449 | std::optional<FocusResolver::FocusChanges> changes = |
| 4450 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4451 | if (changes) { |
| 4452 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4453 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4454 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4455 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4456 | mTouchStatesByDisplay.find(displayId); |
| 4457 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4458 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4459 | for (size_t i = 0; i < state.windows.size();) { |
| 4460 | TouchedWindow& touchedWindow = state.windows[i]; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4461 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4462 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4463 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4464 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4465 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4466 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4467 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4468 | if (touchedInputChannel != nullptr) { |
| 4469 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4470 | "touched window was removed"); |
| 4471 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4472 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4473 | state.windows.erase(state.windows.begin() + i); |
| 4474 | } else { |
| 4475 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4476 | } |
| 4477 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4478 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4479 | // 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] | 4480 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4481 | if (mDragState && |
| 4482 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4483 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4484 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4485 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4486 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4487 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4488 | // Release information for windows that are no longer present. |
| 4489 | // This ensures that unused input channels are released promptly. |
| 4490 | // Otherwise, they might stick around until the window handle is destroyed |
| 4491 | // which might not happen until the next GC. |
| 4492 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4493 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4494 | if (DEBUG_FOCUS) { |
| 4495 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4496 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4497 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4498 | // To avoid making too many calls into the compat framework, only |
| 4499 | // check for window flags when windows are going away. |
| 4500 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4501 | // in order to gather some data about the flag usage |
| 4502 | if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { |
| 4503 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4504 | oldWindowHandle->getName().c_str()); |
| 4505 | if (mCompatService != nullptr) { |
| 4506 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4507 | oldWindowHandle->getInfo()->ownerUid); |
| 4508 | } |
| 4509 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4510 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4511 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4515 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4516 | if (DEBUG_FOCUS) { |
| 4517 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4518 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4519 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4520 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4521 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4522 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4523 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4524 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4525 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4526 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4527 | 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] | 4528 | } |
| 4529 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4530 | // Set the new application handle. |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4531 | if (inputApplicationHandle != nullptr) { |
| 4532 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4533 | } else { |
| 4534 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4535 | } |
| 4536 | |
| 4537 | // No matter what the old focused application was, stop waiting on it because it is |
| 4538 | // no longer focused. |
| 4539 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4540 | } // release lock |
| 4541 | |
| 4542 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4543 | mLooper->wake(); |
| 4544 | } |
| 4545 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4546 | /** |
| 4547 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4548 | * the display not specified. |
| 4549 | * |
| 4550 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4551 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4552 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4553 | * display. The display-specified events won't be affected. |
| 4554 | */ |
| 4555 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4556 | if (DEBUG_FOCUS) { |
| 4557 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4558 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4559 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4560 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4561 | |
| 4562 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4563 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4564 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4565 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4566 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4567 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4568 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4569 | CancelationOptions |
| 4570 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4571 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4572 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4573 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4574 | } |
| 4575 | } |
| 4576 | mFocusedDisplayId = displayId; |
| 4577 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4578 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4579 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4580 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4581 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4582 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4583 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4584 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4585 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4586 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4591 | if (DEBUG_FOCUS) { |
| 4592 | logDispatchStateLocked(); |
| 4593 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4594 | } // release lock |
| 4595 | |
| 4596 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4597 | mLooper->wake(); |
| 4598 | } |
| 4599 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4600 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4601 | if (DEBUG_FOCUS) { |
| 4602 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4603 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4604 | |
| 4605 | bool changed; |
| 4606 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4607 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4608 | |
| 4609 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4610 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4611 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | if (mDispatchEnabled && !enabled) { |
| 4615 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4616 | } |
| 4617 | |
| 4618 | mDispatchEnabled = enabled; |
| 4619 | mDispatchFrozen = frozen; |
| 4620 | changed = true; |
| 4621 | } else { |
| 4622 | changed = false; |
| 4623 | } |
| 4624 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4625 | if (DEBUG_FOCUS) { |
| 4626 | logDispatchStateLocked(); |
| 4627 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4628 | } // release lock |
| 4629 | |
| 4630 | if (changed) { |
| 4631 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4632 | mLooper->wake(); |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4637 | if (DEBUG_FOCUS) { |
| 4638 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4639 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4640 | |
| 4641 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4642 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | |
| 4644 | if (mInputFilterEnabled == enabled) { |
| 4645 | return; |
| 4646 | } |
| 4647 | |
| 4648 | mInputFilterEnabled = enabled; |
| 4649 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4650 | } // release lock |
| 4651 | |
| 4652 | // Wake up poll loop since there might be work to do to drop everything. |
| 4653 | mLooper->wake(); |
| 4654 | } |
| 4655 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4656 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4657 | std::scoped_lock lock(mLock); |
| 4658 | mInTouchMode = inTouchMode; |
| 4659 | } |
| 4660 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4661 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4662 | if (opacity < 0 || opacity > 1) { |
| 4663 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4664 | return; |
| 4665 | } |
| 4666 | |
| 4667 | std::scoped_lock lock(mLock); |
| 4668 | mMaximumObscuringOpacityForTouch = opacity; |
| 4669 | } |
| 4670 | |
| 4671 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4672 | std::scoped_lock lock(mLock); |
| 4673 | mBlockUntrustedTouchesMode = mode; |
| 4674 | } |
| 4675 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4676 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4677 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4678 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4679 | if (DEBUG_FOCUS) { |
| 4680 | ALOGD("Trivial transfer to same window."); |
| 4681 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4682 | return true; |
| 4683 | } |
| 4684 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4685 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4686 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4687 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4688 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4689 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4690 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4691 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4692 | return false; |
| 4693 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4694 | if (DEBUG_FOCUS) { |
| 4695 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4696 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4697 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4698 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4699 | if (DEBUG_FOCUS) { |
| 4700 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4701 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4702 | return false; |
| 4703 | } |
| 4704 | |
| 4705 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4706 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4707 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4708 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4709 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4710 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4711 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4712 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4713 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4714 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4715 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4716 | int32_t newTargetFlags = oldTargetFlags & |
| 4717 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4718 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4719 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4720 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4721 | // Store the dragging window. |
| 4722 | if (isDragDrop) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4723 | mDragState = std::make_unique<DragState>(toWindowHandle); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4724 | } |
| 4725 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4726 | found = true; |
| 4727 | goto Found; |
| 4728 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4729 | } |
| 4730 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4731 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4732 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4733 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4734 | if (DEBUG_FOCUS) { |
| 4735 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4736 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4737 | return false; |
| 4738 | } |
| 4739 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4740 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4741 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4742 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4743 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4744 | CancelationOptions |
| 4745 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4746 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4747 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4748 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4749 | } |
| 4750 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4751 | if (DEBUG_FOCUS) { |
| 4752 | logDispatchStateLocked(); |
| 4753 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4754 | } // release lock |
| 4755 | |
| 4756 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4757 | mLooper->wake(); |
| 4758 | return true; |
| 4759 | } |
| 4760 | |
| 4761 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4762 | if (DEBUG_FOCUS) { |
| 4763 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4764 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4765 | |
| 4766 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4767 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4768 | |
| 4769 | resetKeyRepeatLocked(); |
| 4770 | releasePendingEventLocked(); |
| 4771 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4772 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4774 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4775 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4776 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4777 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4781 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4782 | dumpDispatchStateLocked(dump); |
| 4783 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4784 | std::istringstream stream(dump); |
| 4785 | std::string line; |
| 4786 | |
| 4787 | while (std::getline(stream, line, '\n')) { |
| 4788 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4789 | } |
| 4790 | } |
| 4791 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4792 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 4793 | std::string dump; |
| 4794 | |
| 4795 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", |
| 4796 | toString(mFocusedWindowRequestedPointerCapture)); |
| 4797 | |
| 4798 | std::string windowName = "None"; |
| 4799 | if (mWindowTokenWithPointerCapture) { |
| 4800 | const sp<InputWindowHandle> captureWindowHandle = |
| 4801 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 4802 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 4803 | : "token has capture without window"; |
| 4804 | } |
| 4805 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); |
| 4806 | |
| 4807 | return dump; |
| 4808 | } |
| 4809 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4810 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4811 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4812 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4813 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4814 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4815 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4816 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4817 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4818 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4819 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4820 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4821 | const std::chrono::duration timeout = |
| 4822 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4823 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4824 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4825 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4828 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4829 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4830 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4831 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4832 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4833 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4834 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4835 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4836 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4837 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4838 | 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] | 4839 | state.displayId, toString(state.down), toString(state.split), |
| 4840 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4841 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4842 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4843 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4844 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4845 | dump += StringPrintf(INDENT4 |
| 4846 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4847 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4848 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4849 | } |
| 4850 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4851 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4852 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4853 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4854 | dump += INDENT3 "Portal windows:\n"; |
| 4855 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4856 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4857 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 4858 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4859 | } |
| 4860 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4861 | } |
| 4862 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4863 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4864 | } |
| 4865 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4866 | if (mDragState) { |
| 4867 | dump += StringPrintf(INDENT "DragState:\n"); |
| 4868 | mDragState->dump(dump, INDENT2); |
| 4869 | } |
| 4870 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4871 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4872 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4873 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4874 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4875 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4876 | dump += INDENT2 "Windows:\n"; |
| 4877 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4878 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4879 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4880 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4881 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4882 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4883 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4884 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4885 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4886 | "applicationInfo.name=%s, " |
| 4887 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4888 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4889 | i, windowInfo->name.c_str(), windowInfo->id, |
| 4890 | windowInfo->displayId, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4891 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4892 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4893 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4894 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4895 | windowInfo->flags.string().c_str(), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4896 | NamedEnum::string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4897 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4898 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4899 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4900 | windowInfo->applicationInfo.name.c_str(), |
| 4901 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4902 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4903 | dump += StringPrintf(", inputFeatures=%s", |
| 4904 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4905 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4906 | "ms, trustedOverlay=%s, hasToken=%s, " |
| 4907 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4908 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4909 | millis(windowInfo->dispatchingTimeout), |
| 4910 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4911 | toString(windowInfo->token != nullptr), |
| 4912 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4913 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4914 | } |
| 4915 | } else { |
| 4916 | dump += INDENT2 "Windows: <none>\n"; |
| 4917 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4918 | } |
| 4919 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4920 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4921 | } |
| 4922 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4923 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4924 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4925 | const std::vector<Monitor>& monitors = it.second; |
| 4926 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 4927 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4928 | } |
| 4929 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4930 | const std::vector<Monitor>& monitors = it.second; |
| 4931 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 4932 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4933 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4934 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4935 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | nsecs_t currentTime = now(); |
| 4939 | |
| 4940 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4941 | if (!mRecentQueue.empty()) { |
| 4942 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4943 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4944 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4945 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4946 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4947 | } |
| 4948 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4949 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | // Dump event currently being dispatched. |
| 4953 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4954 | dump += INDENT "PendingEvent:\n"; |
| 4955 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4956 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4957 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 4958 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4960 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4964 | if (!mInboundQueue.empty()) { |
| 4965 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4966 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4967 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4968 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4969 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4970 | } |
| 4971 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4972 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4973 | } |
| 4974 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4975 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4976 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4977 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 4978 | const KeyReplacement& replacement = pair.first; |
| 4979 | int32_t newKeyCode = pair.second; |
| 4980 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4981 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4982 | } |
| 4983 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4984 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4985 | } |
| 4986 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4987 | if (!mConnectionsByFd.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4988 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4989 | for (const auto& pair : mConnectionsByFd) { |
| 4990 | const sp<Connection>& connection = pair.second; |
| 4991 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4992 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4993 | pair.first, connection->getInputChannelName().c_str(), |
| 4994 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4995 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4996 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4997 | if (!connection->outboundQueue.empty()) { |
| 4998 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 4999 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5000 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5001 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5002 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5003 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5004 | } |
| 5005 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5006 | if (!connection->waitQueue.empty()) { |
| 5007 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5008 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5009 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5010 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5011 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5012 | } |
| 5013 | } |
| 5014 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5015 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5019 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5020 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5021 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5022 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5023 | } |
| 5024 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5025 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5026 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5027 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5028 | ns2ms(mConfig.keyRepeatTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5029 | } |
| 5030 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5031 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5032 | const size_t numMonitors = monitors.size(); |
| 5033 | for (size_t i = 0; i < numMonitors; i++) { |
| 5034 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5035 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5036 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5037 | dump += "\n"; |
| 5038 | } |
| 5039 | } |
| 5040 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5041 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5042 | #if DEBUG_CHANNEL_CREATION |
| 5043 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5044 | #endif |
| 5045 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5046 | std::shared_ptr<InputChannel> serverChannel; |
| 5047 | std::unique_ptr<InputChannel> clientChannel; |
| 5048 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5049 | |
| 5050 | if (result) { |
| 5051 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5052 | } |
| 5053 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5054 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5055 | std::scoped_lock _l(mLock); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5056 | sp<Connection> connection = new Connection(serverChannel, false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5057 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5058 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5059 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5060 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5061 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5062 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
| 5063 | } // release lock |
| 5064 | |
| 5065 | // Wake the looper because some connections have changed. |
| 5066 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5067 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5068 | } |
| 5069 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5070 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5071 | bool isGestureMonitor, |
| 5072 | const std::string& name, |
| 5073 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5074 | std::shared_ptr<InputChannel> serverChannel; |
| 5075 | std::unique_ptr<InputChannel> clientChannel; |
| 5076 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5077 | if (result) { |
| 5078 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5079 | } |
| 5080 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5081 | { // acquire lock |
| 5082 | std::scoped_lock _l(mLock); |
| 5083 | |
| 5084 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5085 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5086 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5089 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5090 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5091 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5092 | mConnectionsByFd[fd] = connection; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5093 | mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5094 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5095 | auto& monitorsByDisplay = |
| 5096 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5097 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5098 | |
| 5099 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5100 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5101 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 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 | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5107 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5108 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5109 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5110 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5111 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5112 | if (status) { |
| 5113 | return status; |
| 5114 | } |
| 5115 | } // release lock |
| 5116 | |
| 5117 | // Wake the poll loop because removing the connection may have changed the current |
| 5118 | // synchronization state. |
| 5119 | mLooper->wake(); |
| 5120 | return OK; |
| 5121 | } |
| 5122 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5123 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5124 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5125 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5126 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5127 | // 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] | 5128 | return BAD_VALUE; |
| 5129 | } |
| 5130 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5131 | removeConnectionLocked(connection); |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5132 | mInputChannelsByToken.erase(connectionToken); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5133 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5134 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5135 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5136 | } |
| 5137 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5138 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5139 | |
| 5140 | nsecs_t currentTime = now(); |
| 5141 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5142 | |
| 5143 | connection->status = Connection::STATUS_ZOMBIE; |
| 5144 | return OK; |
| 5145 | } |
| 5146 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5147 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5148 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5149 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5152 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5153 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5154 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5155 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5156 | std::vector<Monitor>& monitors = it->second; |
| 5157 | const size_t numMonitors = monitors.size(); |
| 5158 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5159 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5160 | monitors.erase(monitors.begin() + i); |
| 5161 | break; |
| 5162 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5163 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5164 | if (monitors.empty()) { |
| 5165 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5166 | } else { |
| 5167 | ++it; |
| 5168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | } |
| 5170 | } |
| 5171 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5172 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5173 | { // acquire lock |
| 5174 | std::scoped_lock _l(mLock); |
| 5175 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5176 | |
| 5177 | if (!foundDisplayId) { |
| 5178 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5179 | return BAD_VALUE; |
| 5180 | } |
| 5181 | int32_t displayId = foundDisplayId.value(); |
| 5182 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5183 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5184 | mTouchStatesByDisplay.find(displayId); |
| 5185 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5186 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5187 | return BAD_VALUE; |
| 5188 | } |
| 5189 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5190 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5191 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5192 | std::optional<int32_t> foundDeviceId; |
| 5193 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5194 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5195 | requestingChannel = touchedMonitor.monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5196 | foundDeviceId = state.deviceId; |
| 5197 | } |
| 5198 | } |
| 5199 | if (!foundDeviceId || !state.down) { |
| 5200 | 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] | 5201 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5202 | return BAD_VALUE; |
| 5203 | } |
| 5204 | int32_t deviceId = foundDeviceId.value(); |
| 5205 | |
| 5206 | // Send cancel events to all the input channels we're stealing from. |
| 5207 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5208 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5209 | options.deviceId = deviceId; |
| 5210 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5211 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5212 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5213 | std::shared_ptr<InputChannel> channel = |
| 5214 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5215 | if (channel != nullptr) { |
| 5216 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5217 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5218 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5219 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5220 | canceledWindows += "]"; |
| 5221 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5222 | canceledWindows.c_str()); |
| 5223 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5224 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5225 | state.filterNonMonitors(); |
| 5226 | } |
| 5227 | return OK; |
| 5228 | } |
| 5229 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5230 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5231 | { // acquire lock |
| 5232 | std::scoped_lock _l(mLock); |
| 5233 | if (DEBUG_FOCUS) { |
| 5234 | const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); |
| 5235 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5236 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5237 | : "token without window"); |
| 5238 | } |
| 5239 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5240 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5241 | if (focusedToken != windowToken) { |
| 5242 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5243 | enabled ? "enable" : "disable"); |
| 5244 | return; |
| 5245 | } |
| 5246 | |
| 5247 | if (enabled == mFocusedWindowRequestedPointerCapture) { |
| 5248 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5249 | "window has %s requested pointer capture.", |
| 5250 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5251 | return; |
| 5252 | } |
| 5253 | |
| 5254 | mFocusedWindowRequestedPointerCapture = enabled; |
| 5255 | setPointerCaptureLocked(enabled); |
| 5256 | } // release lock |
| 5257 | |
| 5258 | // Wake the thread to process command entries. |
| 5259 | mLooper->wake(); |
| 5260 | } |
| 5261 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5262 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5263 | const sp<IBinder>& token) { |
| 5264 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5265 | const std::vector<Monitor>& monitors = it.second; |
| 5266 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5267 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5268 | return it.first; |
| 5269 | } |
| 5270 | } |
| 5271 | } |
| 5272 | return std::nullopt; |
| 5273 | } |
| 5274 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5275 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5276 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5277 | if (gesturePid.has_value()) { |
| 5278 | return gesturePid; |
| 5279 | } |
| 5280 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5281 | } |
| 5282 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5283 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5284 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5285 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5286 | } |
| 5287 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5288 | for (const auto& pair : mConnectionsByFd) { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5289 | const sp<Connection>& connection = pair.second; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5290 | if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5291 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5292 | } |
| 5293 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5294 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5295 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5296 | } |
| 5297 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5298 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5299 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5300 | if (connection == nullptr) { |
| 5301 | return "<nullptr>"; |
| 5302 | } |
| 5303 | return connection->getInputChannelName(); |
| 5304 | } |
| 5305 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5306 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5307 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5308 | removeByValue(mConnectionsByFd, connection); |
| 5309 | } |
| 5310 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5311 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 5312 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5313 | bool handled, nsecs_t consumeTime) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5314 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5315 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5316 | commandEntry->connection = connection; |
| 5317 | commandEntry->eventTime = currentTime; |
| 5318 | commandEntry->seq = seq; |
| 5319 | commandEntry->handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5320 | commandEntry->consumeTime = consumeTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5321 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5322 | } |
| 5323 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5324 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 5325 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5326 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5327 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5328 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5329 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5330 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5331 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5332 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | } |
| 5334 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5335 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 5336 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5337 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5338 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5339 | commandEntry->oldToken = oldToken; |
| 5340 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5341 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5342 | } |
| 5343 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5344 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { |
| 5345 | std::unique_ptr<CommandEntry> commandEntry = |
| 5346 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); |
| 5347 | commandEntry->newToken = token; |
| 5348 | commandEntry->x = x; |
| 5349 | commandEntry->y = y; |
| 5350 | postCommandLocked(std::move(commandEntry)); |
| 5351 | } |
| 5352 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5353 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5354 | if (connection == nullptr) { |
| 5355 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5356 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5357 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5358 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5359 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5360 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5361 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5362 | return; |
| 5363 | } |
| 5364 | /** |
| 5365 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5366 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5367 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5368 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5369 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5370 | * most useful. |
| 5371 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5372 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5373 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5374 | std::string reason = |
| 5375 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5376 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5377 | ns2ms(currentWait), |
| 5378 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5379 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5380 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5381 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5382 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5383 | |
| 5384 | // Stop waking up for events on this connection, it is already unresponsive |
| 5385 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5386 | } |
| 5387 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5388 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5389 | std::string reason = |
| 5390 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5391 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5392 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5393 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5394 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); |
| 5395 | commandEntry->inputApplicationHandle = std::move(application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5396 | postCommandLocked(std::move(commandEntry)); |
| 5397 | } |
| 5398 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5399 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 5400 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5401 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 5402 | commandEntry->obscuringPackage = obscuringPackage; |
| 5403 | postCommandLocked(std::move(commandEntry)); |
| 5404 | } |
| 5405 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5406 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 5407 | const std::string& reason) { |
| 5408 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5409 | updateLastAnrStateLocked(windowLabel, reason); |
| 5410 | } |
| 5411 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5412 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5413 | const std::string& reason) { |
| 5414 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5415 | updateLastAnrStateLocked(windowLabel, reason); |
| 5416 | } |
| 5417 | |
| 5418 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5419 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5420 | // 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] | 5421 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5422 | struct tm tm; |
| 5423 | localtime_r(&t, &tm); |
| 5424 | char timestr[64]; |
| 5425 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5426 | mLastAnrState.clear(); |
| 5427 | mLastAnrState += INDENT "ANR:\n"; |
| 5428 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5429 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5430 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5431 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5432 | } |
| 5433 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5434 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5435 | mLock.unlock(); |
| 5436 | |
| 5437 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 5438 | |
| 5439 | mLock.lock(); |
| 5440 | } |
| 5441 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5442 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5443 | sp<Connection> connection = commandEntry->connection; |
| 5444 | |
| 5445 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 5446 | mLock.unlock(); |
| 5447 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5448 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5449 | |
| 5450 | mLock.lock(); |
| 5451 | } |
| 5452 | } |
| 5453 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5454 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5455 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 5456 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5457 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5458 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5459 | mLock.lock(); |
| 5460 | } |
| 5461 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5462 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { |
| 5463 | sp<IBinder> newToken = commandEntry->newToken; |
| 5464 | mLock.unlock(); |
| 5465 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); |
| 5466 | mLock.lock(); |
| 5467 | } |
| 5468 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5469 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5470 | mLock.unlock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5471 | |
| 5472 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); |
| 5473 | |
| 5474 | mLock.lock(); |
| 5475 | } |
| 5476 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5477 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5478 | mLock.unlock(); |
| 5479 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5480 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5481 | |
| 5482 | mLock.lock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5483 | } |
| 5484 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5485 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5486 | mLock.unlock(); |
| 5487 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5488 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); |
| 5489 | |
| 5490 | mLock.lock(); |
| 5491 | } |
| 5492 | |
| 5493 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5494 | mLock.unlock(); |
| 5495 | |
| 5496 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); |
| 5497 | |
| 5498 | mLock.lock(); |
| 5499 | } |
| 5500 | |
| 5501 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5502 | mLock.unlock(); |
| 5503 | |
| 5504 | mPolicy->notifyMonitorResponsive(commandEntry->pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5505 | |
| 5506 | mLock.lock(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5507 | } |
| 5508 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5509 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 5510 | mLock.unlock(); |
| 5511 | |
| 5512 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 5513 | |
| 5514 | mLock.lock(); |
| 5515 | } |
| 5516 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5517 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5518 | CommandEntry* commandEntry) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5519 | KeyEntry& entry = *(commandEntry->keyEntry); |
| 5520 | KeyEvent event = createKeyEvent(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5521 | |
| 5522 | mLock.unlock(); |
| 5523 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5524 | android::base::Timer t; |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5525 | const sp<IBinder>& token = commandEntry->connectionToken; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5526 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5527 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5528 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5529 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5530 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5531 | |
| 5532 | mLock.lock(); |
| 5533 | |
| 5534 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5535 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5536 | } else if (!delay) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5537 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5538 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5539 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5540 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5542 | } |
| 5543 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5544 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5545 | mLock.unlock(); |
| 5546 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5547 | mLock.lock(); |
| 5548 | } |
| 5549 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5550 | /** |
| 5551 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5552 | * current time. |
| 5553 | */ |
| 5554 | static bool isConnectionResponsive(const Connection& connection) { |
| 5555 | const nsecs_t currentTime = now(); |
| 5556 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5557 | if (entry->timeoutTime < currentTime) { |
| 5558 | return false; |
| 5559 | } |
| 5560 | } |
| 5561 | return true; |
| 5562 | } |
| 5563 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5564 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5565 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5566 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5567 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5568 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5569 | |
| 5570 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5571 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5572 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5573 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5574 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5575 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5576 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5577 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5578 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5579 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5580 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5581 | reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5582 | |
| 5583 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5584 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5585 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5586 | restartEvent = |
| 5587 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5588 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5589 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5590 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5591 | handled); |
| 5592 | } else { |
| 5593 | restartEvent = false; |
| 5594 | } |
| 5595 | |
| 5596 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5597 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5598 | // contents of the wait queue to have been drained, so we need to double-check |
| 5599 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5600 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5601 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5602 | dispatchEntry = *dispatchEntryIt; |
| 5603 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5604 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5605 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5606 | if (!connection->responsive) { |
| 5607 | connection->responsive = isConnectionResponsive(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5608 | if (connection->responsive) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5609 | // The connection was unresponsive, and now it's responsive. |
| 5610 | processConnectionResponsiveLocked(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5611 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5612 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5613 | traceWaitQueueLength(connection); |
| 5614 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5615 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5616 | traceOutboundQueueLength(connection); |
| 5617 | } else { |
| 5618 | releaseDispatchEntry(dispatchEntry); |
| 5619 | } |
| 5620 | } |
| 5621 | |
| 5622 | // Start the next dispatch cycle for this connection. |
| 5623 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5624 | } |
| 5625 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5626 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
| 5627 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5628 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); |
| 5629 | monitorUnresponsiveCommand->pid = pid; |
| 5630 | monitorUnresponsiveCommand->reason = std::move(reason); |
| 5631 | postCommandLocked(std::move(monitorUnresponsiveCommand)); |
| 5632 | } |
| 5633 | |
| 5634 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, |
| 5635 | std::string reason) { |
| 5636 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5637 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); |
| 5638 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); |
| 5639 | windowUnresponsiveCommand->reason = std::move(reason); |
| 5640 | postCommandLocked(std::move(windowUnresponsiveCommand)); |
| 5641 | } |
| 5642 | |
| 5643 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
| 5644 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( |
| 5645 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); |
| 5646 | monitorResponsiveCommand->pid = pid; |
| 5647 | postCommandLocked(std::move(monitorResponsiveCommand)); |
| 5648 | } |
| 5649 | |
| 5650 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { |
| 5651 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( |
| 5652 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); |
| 5653 | windowResponsiveCommand->connectionToken = std::move(connectionToken); |
| 5654 | postCommandLocked(std::move(windowResponsiveCommand)); |
| 5655 | } |
| 5656 | |
| 5657 | /** |
| 5658 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5659 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5660 | * command entry to the command queue. |
| 5661 | */ |
| 5662 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5663 | std::string reason) { |
| 5664 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5665 | if (connection.monitor) { |
| 5666 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5667 | reason.c_str()); |
| 5668 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5669 | if (!pid.has_value()) { |
| 5670 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5671 | connection.inputChannel->getName().c_str()); |
| 5672 | return; |
| 5673 | } |
| 5674 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5675 | return; |
| 5676 | } |
| 5677 | // If not a monitor, must be a window |
| 5678 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5679 | reason.c_str()); |
| 5680 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5681 | } |
| 5682 | |
| 5683 | /** |
| 5684 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5685 | */ |
| 5686 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5687 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5688 | if (connection.monitor) { |
| 5689 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5690 | if (!pid.has_value()) { |
| 5691 | ALOGE("Could not find responsive monitor for connection %s", |
| 5692 | connection.inputChannel->getName().c_str()); |
| 5693 | return; |
| 5694 | } |
| 5695 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5696 | return; |
| 5697 | } |
| 5698 | // If not a monitor, must be a window |
| 5699 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5700 | } |
| 5701 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5702 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5703 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5704 | KeyEntry& keyEntry, bool handled) { |
| 5705 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5706 | if (!handled) { |
| 5707 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5708 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5709 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5710 | return false; |
| 5711 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5712 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5713 | // Get the fallback key state. |
| 5714 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5715 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5716 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5717 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5718 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5719 | } |
| 5720 | |
| 5721 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5722 | // If the application handles the original key for which we previously |
| 5723 | // generated a fallback or if the window is not a foreground window, |
| 5724 | // then cancel the associated fallback key, if any. |
| 5725 | if (fallbackKeyCode != -1) { |
| 5726 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5727 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5728 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5729 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5730 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5731 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5732 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5733 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5734 | |
| 5735 | mLock.unlock(); |
| 5736 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5737 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5738 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5739 | |
| 5740 | mLock.lock(); |
| 5741 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5742 | // Cancel the fallback key. |
| 5743 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5744 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5745 | "application handled the original non-fallback key " |
| 5746 | "or is no longer a foreground target, " |
| 5747 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5748 | options.keyCode = fallbackKeyCode; |
| 5749 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5750 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5751 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5752 | } |
| 5753 | } else { |
| 5754 | // If the application did not handle a non-fallback key, first check |
| 5755 | // that we are in a good state to perform unhandled key event processing |
| 5756 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5757 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5758 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5759 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5760 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5761 | "since this is not an initial down. " |
| 5762 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5763 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5764 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5765 | return false; |
| 5766 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5767 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5768 | // Dispatch the unhandled key to the policy. |
| 5769 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5770 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5771 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5772 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5773 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5774 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5775 | |
| 5776 | mLock.unlock(); |
| 5777 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5778 | bool fallback = |
| 5779 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5780 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5781 | |
| 5782 | mLock.lock(); |
| 5783 | |
| 5784 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5785 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5786 | return false; |
| 5787 | } |
| 5788 | |
| 5789 | // Latch the fallback keycode for this key on an initial down. |
| 5790 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5791 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5793 | fallbackKeyCode = event.getKeyCode(); |
| 5794 | } else { |
| 5795 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5796 | } |
| 5797 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5798 | } |
| 5799 | |
| 5800 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5801 | |
| 5802 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5803 | // We will continue to dispatch the key to the policy but we will no |
| 5804 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5805 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5806 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5807 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5808 | if (fallback) { |
| 5809 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5810 | "as a fallback for %d, but on the DOWN it had requested " |
| 5811 | "to send %d instead. Fallback canceled.", |
| 5812 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5813 | } else { |
| 5814 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5815 | "but on the DOWN it had requested to send %d. " |
| 5816 | "Fallback canceled.", |
| 5817 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5818 | } |
| 5819 | #endif |
| 5820 | |
| 5821 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5822 | "canceling fallback, policy no longer desires it"); |
| 5823 | options.keyCode = fallbackKeyCode; |
| 5824 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5825 | |
| 5826 | fallback = false; |
| 5827 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5828 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5829 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5830 | } |
| 5831 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5832 | |
| 5833 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5834 | { |
| 5835 | std::string msg; |
| 5836 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5837 | connection->inputState.getFallbackKeys(); |
| 5838 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5839 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5840 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5841 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5842 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5843 | } |
| 5844 | #endif |
| 5845 | |
| 5846 | if (fallback) { |
| 5847 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5848 | keyEntry.eventTime = event.getEventTime(); |
| 5849 | keyEntry.deviceId = event.getDeviceId(); |
| 5850 | keyEntry.source = event.getSource(); |
| 5851 | keyEntry.displayId = event.getDisplayId(); |
| 5852 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5853 | keyEntry.keyCode = fallbackKeyCode; |
| 5854 | keyEntry.scanCode = event.getScanCode(); |
| 5855 | keyEntry.metaState = event.getMetaState(); |
| 5856 | keyEntry.repeatCount = event.getRepeatCount(); |
| 5857 | keyEntry.downTime = event.getDownTime(); |
| 5858 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5859 | |
| 5860 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5861 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5862 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5863 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5864 | #endif |
| 5865 | return true; // restart the event |
| 5866 | } else { |
| 5867 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5868 | ALOGD("Unhandled key event: No fallback key."); |
| 5869 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5870 | |
| 5871 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5872 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5873 | } |
| 5874 | } |
| 5875 | return false; |
| 5876 | } |
| 5877 | |
| 5878 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5879 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5880 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5881 | return false; |
| 5882 | } |
| 5883 | |
| 5884 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 5885 | mLock.unlock(); |
| 5886 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 5887 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, |
| 5888 | commandEntry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | |
| 5890 | mLock.lock(); |
| 5891 | } |
| 5892 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5893 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, |
| 5894 | const Connection& connection, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5895 | // TODO Write some statistics about how long we spend waiting. |
| 5896 | } |
| 5897 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5898 | /** |
| 5899 | * Report the touch event latency to the statsd server. |
| 5900 | * Input events are reported for statistics if: |
| 5901 | * - This is a touchscreen event |
| 5902 | * - InputFilter is not enabled |
| 5903 | * - Event is not injected or synthesized |
| 5904 | * |
| 5905 | * Statistics should be reported before calling addValue, to prevent a fresh new sample |
| 5906 | * from getting aggregated with the "old" data. |
| 5907 | */ |
| 5908 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) |
| 5909 | REQUIRES(mLock) { |
| 5910 | const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && |
| 5911 | !(motionEntry.isSynthesized()) && !mInputFilterEnabled; |
| 5912 | if (!reportForStatistics) { |
| 5913 | return; |
| 5914 | } |
| 5915 | |
| 5916 | if (mTouchStatistics.shouldReport()) { |
| 5917 | android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), |
| 5918 | mTouchStatistics.getMax(), mTouchStatistics.getMean(), |
| 5919 | mTouchStatistics.getStDev(), mTouchStatistics.getCount()); |
| 5920 | mTouchStatistics.reset(); |
| 5921 | } |
| 5922 | const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); |
| 5923 | mTouchStatistics.addValue(latencyMicros); |
| 5924 | } |
| 5925 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5926 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 5927 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5928 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5929 | } |
| 5930 | } |
| 5931 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5932 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5933 | if (ATRACE_ENABLED()) { |
| 5934 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5935 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5936 | ATRACE_INT(counterName, connection->outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5937 | } |
| 5938 | } |
| 5939 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5940 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5941 | if (ATRACE_ENABLED()) { |
| 5942 | char counterName[40]; |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5943 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5944 | ATRACE_INT(counterName, connection->waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5945 | } |
| 5946 | } |
| 5947 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5948 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5949 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5950 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5951 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5952 | dumpDispatchStateLocked(dump); |
| 5953 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5954 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5955 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5956 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | void InputDispatcher::monitor() { |
| 5961 | // 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] | 5962 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5963 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5964 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5965 | } |
| 5966 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5967 | /** |
| 5968 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 5969 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 5970 | * this method can be safely called from any thread, as long as you've ensured that |
| 5971 | * the work you are interested in completing has already been queued. |
| 5972 | */ |
| 5973 | bool InputDispatcher::waitForIdle() { |
| 5974 | /** |
| 5975 | * Timeout should represent the longest possible time that a device might spend processing |
| 5976 | * events and commands. |
| 5977 | */ |
| 5978 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 5979 | std::unique_lock lock(mLock); |
| 5980 | mLooper->wake(); |
| 5981 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 5982 | return result == std::cv_status::no_timeout; |
| 5983 | } |
| 5984 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5985 | /** |
| 5986 | * Sets focus to the window identified by the token. This must be called |
| 5987 | * after updating any input window handles. |
| 5988 | * |
| 5989 | * Params: |
| 5990 | * request.token - input channel token used to identify the window that should gain focus. |
| 5991 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 5992 | * specified token does not match the currently focused window, this request will be dropped. |
| 5993 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 5994 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 5995 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 5996 | * when requesting the focus change. This determines which request gets |
| 5997 | * precedence if there is a focus change request from another source such as pointer down. |
| 5998 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5999 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6000 | { // acquire lock |
| 6001 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6002 | std::optional<FocusResolver::FocusChanges> changes = |
| 6003 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6004 | if (changes) { |
| 6005 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6006 | } |
| 6007 | } // release lock |
| 6008 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6009 | mLooper->wake(); |
| 6010 | } |
| 6011 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6012 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6013 | if (changes.oldFocus) { |
| 6014 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6015 | if (focusedInputChannel) { |
| 6016 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6017 | "focus left window"); |
| 6018 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6019 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6020 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6021 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6022 | if (changes.newFocus) { |
| 6023 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6024 | } |
| 6025 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6026 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6027 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6028 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6029 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6030 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6031 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6032 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6033 | // front. |
| 6034 | disablePointerCaptureForcedLocked(); |
| 6035 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6036 | if (mFocusedDisplayId == changes.displayId) { |
| 6037 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6038 | } |
| 6039 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6040 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6041 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
| 6042 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { |
| 6043 | return; |
| 6044 | } |
| 6045 | |
| 6046 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6047 | |
| 6048 | if (mFocusedWindowRequestedPointerCapture) { |
| 6049 | mFocusedWindowRequestedPointerCapture = false; |
| 6050 | setPointerCaptureLocked(false); |
| 6051 | } |
| 6052 | |
| 6053 | if (!mWindowTokenWithPointerCapture) { |
| 6054 | // No need to send capture changes because no window has capture. |
| 6055 | return; |
| 6056 | } |
| 6057 | |
| 6058 | if (mPendingEvent != nullptr) { |
| 6059 | // Move the pending event to the front of the queue. This will give the chance |
| 6060 | // for the pending event to be dropped if it is a captured event. |
| 6061 | mInboundQueue.push_front(mPendingEvent); |
| 6062 | mPendingEvent = nullptr; |
| 6063 | } |
| 6064 | |
| 6065 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
| 6066 | false /* hasCapture */); |
| 6067 | mInboundQueue.push_front(std::move(entry)); |
| 6068 | } |
| 6069 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6070 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { |
| 6071 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 6072 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); |
| 6073 | commandEntry->enabled = enabled; |
| 6074 | postCommandLocked(std::move(commandEntry)); |
| 6075 | } |
| 6076 | |
| 6077 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( |
| 6078 | android::inputdispatcher::CommandEntry* commandEntry) { |
| 6079 | mLock.unlock(); |
| 6080 | |
| 6081 | mPolicy->setPointerCapture(commandEntry->enabled); |
| 6082 | |
| 6083 | mLock.lock(); |
| 6084 | } |
| 6085 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6086 | } // namespace android::inputdispatcher |