Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
| 22 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 23 | #define DEBUG_INBOUND_EVENT_DETAILS 0 |
| 24 | |
| 25 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 26 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 |
| 27 | |
| 28 | // Log debug messages about the dispatch cycle. |
| 29 | #define DEBUG_DISPATCH_CYCLE 0 |
| 30 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 31 | // Log debug messages about channel creation |
| 32 | #define DEBUG_CHANNEL_CREATION 0 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | |
| 34 | // Log debug messages about input event injection. |
| 35 | #define DEBUG_INJECTION 0 |
| 36 | |
| 37 | // Log debug messages about input focus tracking. |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 38 | static constexpr bool DEBUG_FOCUS = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 | |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 40 | // Log debug messages about touch occlusion |
| 41 | // STOPSHIP(b/169067926): Set to false |
| 42 | static constexpr bool DEBUG_TOUCH_OCCLUSION = true; |
| 43 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | // Log debug messages about the app switch latency optimization. |
| 45 | #define DEBUG_APP_SWITCH 0 |
| 46 | |
| 47 | // Log debug messages about hover events. |
| 48 | #define DEBUG_HOVER 0 |
| 49 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 50 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 51 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 52 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 53 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 54 | #include <binder/Binder.h> |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 55 | #include <binder/IServiceManager.h> |
| 56 | #include <com/android/internal/compat/IPlatformCompatNative.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 57 | #include <input/InputDevice.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 58 | #include <input/InputWindow.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 60 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 61 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 62 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 63 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 65 | #include <cerrno> |
| 66 | #include <cinttypes> |
| 67 | #include <climits> |
| 68 | #include <cstddef> |
| 69 | #include <ctime> |
| 70 | #include <queue> |
| 71 | #include <sstream> |
| 72 | |
| 73 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 74 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 75 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | #define INDENT " " |
| 77 | #define INDENT2 " " |
| 78 | #define INDENT3 " " |
| 79 | #define INDENT4 " " |
| 80 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 81 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 82 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 83 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 84 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 85 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 86 | using android::os::InputEventInjectionResult; |
| 87 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 88 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 89 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 90 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 92 | // When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display |
| 93 | // coordinates and SurfaceFlinger includes the display rotation in the input window transforms. |
| 94 | static bool isPerWindowInputRotationEnabled() { |
| 95 | static const bool PER_WINDOW_INPUT_ROTATION = |
| 96 | base::GetBoolProperty("persist.debug.per_window_input_rotation", false); |
| 97 | return PER_WINDOW_INPUT_ROTATION; |
| 98 | } |
| 99 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 100 | // Default input dispatching timeout if there is no focused application or paused window |
| 101 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 102 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 103 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 104 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | |
| 106 | // Amount of time to allow for all pending events to be processed when an app switch |
| 107 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 108 | // 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] | 109 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | |
| 111 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 112 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 113 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | // 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] | 116 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 117 | |
| 118 | // Log a warning when an interception call takes longer than this to process. |
| 119 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 121 | // Additional key latency in case a connection is still processing some motion events. |
| 122 | // This will help with the case when a user touched a button that opens a new window, |
| 123 | // and gives us the chance to dispatch the key to this new window. |
| 124 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 125 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 127 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 128 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 129 | // Event log tags. See EventLogTags.logtags for reference |
| 130 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 131 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 132 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | static inline nsecs_t now() { |
| 134 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 135 | } |
| 136 | |
| 137 | static inline const char* toString(bool value) { |
| 138 | return value ? "true" : "false"; |
| 139 | } |
| 140 | |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 141 | static inline const std::string toString(sp<IBinder> binder) { |
| 142 | if (binder == nullptr) { |
| 143 | return "<null>"; |
| 144 | } |
| 145 | return StringPrintf("%p", binder.get()); |
| 146 | } |
| 147 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 148 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 149 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 150 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static bool isValidKeyAction(int32_t action) { |
| 154 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 155 | case AKEY_EVENT_ACTION_DOWN: |
| 156 | case AKEY_EVENT_ACTION_UP: |
| 157 | return true; |
| 158 | default: |
| 159 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 164 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | ALOGE("Key event has invalid action code 0x%x", action); |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 171 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 173 | case AMOTION_EVENT_ACTION_DOWN: |
| 174 | case AMOTION_EVENT_ACTION_UP: |
| 175 | case AMOTION_EVENT_ACTION_CANCEL: |
| 176 | case AMOTION_EVENT_ACTION_MOVE: |
| 177 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 178 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 179 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 180 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 181 | case AMOTION_EVENT_ACTION_SCROLL: |
| 182 | return true; |
| 183 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 184 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 185 | int32_t index = getMotionEventActionPointerIndex(action); |
| 186 | return index >= 0 && index < pointerCount; |
| 187 | } |
| 188 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 189 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 190 | return actionButton != 0; |
| 191 | default: |
| 192 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 196 | static int64_t millis(std::chrono::nanoseconds t) { |
| 197 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 198 | } |
| 199 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 200 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 201 | const PointerProperties* pointerProperties) { |
| 202 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 204 | return false; |
| 205 | } |
| 206 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 207 | 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] | 208 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | BitSet32 pointerIdBits; |
| 212 | for (size_t i = 0; i < pointerCount; i++) { |
| 213 | int32_t id = pointerProperties[i].id; |
| 214 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 215 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 216 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | if (pointerIdBits.hasBit(id)) { |
| 220 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 221 | return false; |
| 222 | } |
| 223 | pointerIdBits.markBit(id); |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 228 | static std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 230 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 233 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | bool first = true; |
| 235 | Region::const_iterator cur = region.begin(); |
| 236 | Region::const_iterator const tail = region.end(); |
| 237 | while (cur != tail) { |
| 238 | if (first) { |
| 239 | first = false; |
| 240 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 241 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 242 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 243 | 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] | 244 | cur++; |
| 245 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 246 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 249 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
| 250 | constexpr size_t maxEntries = 50; // max events to print |
| 251 | constexpr size_t skipBegin = maxEntries / 2; |
| 252 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 253 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 254 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 255 | |
| 256 | std::string dump; |
| 257 | for (size_t i = 0; i < queue.size(); i++) { |
| 258 | const DispatchEntry& entry = *queue[i]; |
| 259 | if (i >= skipBegin && i < skipEnd) { |
| 260 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 261 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 262 | continue; |
| 263 | } |
| 264 | dump.append(INDENT4); |
| 265 | dump += entry.eventEntry->getDescription(); |
| 266 | dump += StringPrintf(", seq=%" PRIu32 |
| 267 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 268 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 269 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 270 | if (entry.deliveryTime != 0) { |
| 271 | // This entry was delivered, so add information on how long we've been waiting |
| 272 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 273 | } |
| 274 | dump.append("\n"); |
| 275 | } |
| 276 | return dump; |
| 277 | } |
| 278 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Find the entry in std::unordered_map by key, and return it. |
| 281 | * If the entry is not found, return a default constructed entry. |
| 282 | * |
| 283 | * Useful when the entries are vectors, since an empty vector will be returned |
| 284 | * if the entry is not found. |
| 285 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 286 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 287 | template <typename K, typename V> |
| 288 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 289 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 290 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 291 | } |
| 292 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 293 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 294 | if (first == second) { |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | if (first == nullptr || second == nullptr) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | return first->getToken() == second->getToken(); |
| 303 | } |
| 304 | |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 305 | static bool haveSameApplicationToken(const InputWindowInfo* first, const InputWindowInfo* second) { |
| 306 | if (first == nullptr || second == nullptr) { |
| 307 | return false; |
| 308 | } |
| 309 | return first->applicationInfo.token != nullptr && |
| 310 | first->applicationInfo.token == second->applicationInfo.token; |
| 311 | } |
| 312 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 313 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 314 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 315 | } |
| 316 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 317 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 318 | std::shared_ptr<EventEntry> eventEntry, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 319 | int32_t inputTargetFlags) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 320 | if (eventEntry->type == EventEntry::Type::MOTION) { |
| 321 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 322 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) == 0) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 323 | const ui::Transform identityTransform; |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 324 | // Use identity transform for events that are not pointer events because their axes |
| 325 | // values do not represent on-screen coordinates, so they should not have any window |
| 326 | // transformations applied to them. |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 327 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 328 | 1.0f /*globalScaleFactor*/, |
| 329 | inputTarget.displaySize); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 333 | if (inputTarget.useDefaultPointerTransform()) { |
| 334 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 335 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 336 | inputTarget.globalScaleFactor, |
| 337 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 341 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 342 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 343 | std::vector<PointerCoords> pointerCoords; |
| 344 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 345 | |
| 346 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 347 | // 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] | 348 | // uses the transform for the normalized pointer. |
| 349 | const ui::Transform& firstPointerTransform = |
| 350 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 351 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 352 | |
| 353 | // Iterate through all pointers in the event to normalize against the first. |
| 354 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 355 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 356 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 357 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 358 | |
| 359 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 360 | // First, apply the current pointer's transform to update the coordinates into |
| 361 | // window space. |
| 362 | pointerCoords[pointerIndex].transform(currTransform); |
| 363 | // Next, apply the inverse transform of the normalized coordinates so the |
| 364 | // current coordinates are transformed into the normalized coordinate space. |
| 365 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 368 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 369 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 370 | motionEntry.deviceId, motionEntry.source, |
| 371 | motionEntry.displayId, motionEntry.policyFlags, |
| 372 | motionEntry.action, motionEntry.actionButton, |
| 373 | motionEntry.flags, motionEntry.metaState, |
| 374 | motionEntry.buttonState, motionEntry.classification, |
| 375 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 376 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 377 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 378 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 379 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 380 | |
| 381 | if (motionEntry.injectionState) { |
| 382 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 383 | combinedMotionEntry->injectionState->refCount += 1; |
| 384 | } |
| 385 | |
| 386 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 387 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 388 | firstPointerTransform, inputTarget.globalScaleFactor, |
| 389 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 390 | return dispatchEntry; |
| 391 | } |
| 392 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 393 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 394 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 395 | float yOffset = 0) { |
| 396 | if (monitors.empty()) { |
| 397 | return; |
| 398 | } |
| 399 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 400 | for (const Monitor& monitor : monitors) { |
| 401 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 402 | } |
| 403 | } |
| 404 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 405 | static status_t openInputChannelPair(const std::string& name, |
| 406 | std::shared_ptr<InputChannel>& serverChannel, |
| 407 | std::unique_ptr<InputChannel>& clientChannel) { |
| 408 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 409 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 410 | |
| 411 | serverChannel = std::move(uniqueServerChannel); |
| 412 | return result; |
| 413 | } |
| 414 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 415 | template <typename T> |
| 416 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 417 | if (lhs == nullptr && rhs == nullptr) { |
| 418 | return true; |
| 419 | } |
| 420 | if (lhs == nullptr || rhs == nullptr) { |
| 421 | return false; |
| 422 | } |
| 423 | return *lhs == *rhs; |
| 424 | } |
| 425 | |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 426 | static sp<IPlatformCompatNative> getCompatService() { |
| 427 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 428 | if (service == nullptr) { |
| 429 | ALOGE("Failed to link to compat service"); |
| 430 | return nullptr; |
| 431 | } |
| 432 | return interface_cast<IPlatformCompatNative>(service); |
| 433 | } |
| 434 | |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 435 | static KeyEvent createKeyEvent(const KeyEntry& entry) { |
| 436 | KeyEvent event; |
| 437 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 438 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 439 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 440 | return event; |
| 441 | } |
| 442 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 443 | static std::optional<int32_t> findMonitorPidByToken( |
| 444 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 445 | const sp<IBinder>& token) { |
| 446 | for (const auto& it : monitorsByDisplay) { |
| 447 | const std::vector<Monitor>& monitors = it.second; |
| 448 | for (const Monitor& monitor : monitors) { |
| 449 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 450 | return monitor.pid; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | return std::nullopt; |
| 455 | } |
| 456 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 457 | static bool shouldReportMetricsForConnection(const Connection& connection) { |
| 458 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 459 | // affect the statistics. |
| 460 | if (connection.monitor) { |
| 461 | return false; |
| 462 | } |
| 463 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 464 | if (!connection.responsive) { |
| 465 | return false; |
| 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | static bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, |
| 471 | const Connection& connection) { |
| 472 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 473 | const int32_t& inputEventId = eventEntry.id; |
| 474 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 475 | // Event was transmuted |
| 476 | return false; |
| 477 | } |
| 478 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 479 | return false; |
| 480 | } |
| 481 | // Only track latency for events that originated from hardware |
| 482 | if (eventEntry.isSynthesized()) { |
| 483 | return false; |
| 484 | } |
| 485 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 486 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 487 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 488 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 489 | return false; |
| 490 | } |
| 491 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 492 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 493 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 494 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 495 | return false; |
| 496 | } |
| 497 | } else { |
| 498 | // Not a key or a motion |
| 499 | return false; |
| 500 | } |
| 501 | if (!shouldReportMetricsForConnection(connection)) { |
| 502 | return false; |
| 503 | } |
| 504 | return true; |
| 505 | } |
| 506 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | // --- InputDispatcher --- |
| 508 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 509 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 510 | : mPolicy(policy), |
| 511 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 512 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 513 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 514 | mAppSwitchSawKeyDown(false), |
| 515 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 516 | mNextUnblockedEvent(nullptr), |
| 517 | mDispatchEnabled(false), |
| 518 | mDispatchFrozen(false), |
| 519 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 520 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 521 | // To avoid leaking stack in case that call never comes, and for tests, |
| 522 | // initialize it here anyways. |
| 523 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 524 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 525 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 526 | mFocusedWindowRequestedPointerCapture(false), |
| 527 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 528 | mLatencyAggregator(), |
| 529 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 530 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 532 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 534 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 535 | |
| 536 | policy->getDispatcherConfiguration(&mConfig); |
| 537 | } |
| 538 | |
| 539 | InputDispatcher::~InputDispatcher() { |
| 540 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 541 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | |
| 543 | resetKeyRepeatLocked(); |
| 544 | releasePendingEventLocked(); |
| 545 | drainInboundQueueLocked(); |
| 546 | } |
| 547 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 548 | while (!mConnectionsByToken.empty()) { |
| 549 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 550 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 554 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 555 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 556 | return ALREADY_EXISTS; |
| 557 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 558 | mThread = std::make_unique<InputThread>( |
| 559 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 560 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 564 | if (mThread && mThread->isCallingThread()) { |
| 565 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 566 | return INVALID_OPERATION; |
| 567 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 568 | mThread.reset(); |
| 569 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | void InputDispatcher::dispatchOnce() { |
| 573 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 574 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 575 | std::scoped_lock _l(mLock); |
| 576 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | |
| 578 | // Run a dispatch loop if there are no pending commands. |
| 579 | // The dispatch loop might enqueue commands to run afterwards. |
| 580 | if (!haveCommandsLocked()) { |
| 581 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 582 | } |
| 583 | |
| 584 | // Run all pending commands if there are any. |
| 585 | // If any commands were run then force the next poll to wake up immediately. |
| 586 | if (runCommandsLockedInterruptible()) { |
| 587 | nextWakeupTime = LONG_LONG_MIN; |
| 588 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 589 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 590 | // If we are still waiting for ack on some events, |
| 591 | // we might have to wake up earlier to check if an app is anr'ing. |
| 592 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 593 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 594 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 595 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 596 | // pending or queued events |
| 597 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 598 | mDispatcherEnteredIdle.notify_all(); |
| 599 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | } // release lock |
| 601 | |
| 602 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 603 | nsecs_t currentTime = now(); |
| 604 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 605 | mLooper->pollOnce(timeoutMillis); |
| 606 | } |
| 607 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 608 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 609 | * Raise ANR if there is no focused window. |
| 610 | * Before the ANR is raised, do a final state check: |
| 611 | * 1. The currently focused application must be the same one we are waiting for. |
| 612 | * 2. Ensure we still don't have a focused window. |
| 613 | */ |
| 614 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 615 | // Check if the application that we are waiting for is still focused. |
| 616 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 617 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 618 | if (focusedApplication == nullptr || |
| 619 | focusedApplication->getApplicationToken() != |
| 620 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 621 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 622 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 623 | focusedApplication->getName().c_str()); |
| 624 | return; // The focused application has changed. |
| 625 | } |
| 626 | |
| 627 | const sp<InputWindowHandle>& focusedWindowHandle = |
| 628 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 629 | if (focusedWindowHandle != nullptr) { |
| 630 | return; // We now have a focused window. No need for ANR. |
| 631 | } |
| 632 | onAnrLocked(mAwaitedFocusedApplication); |
| 633 | } |
| 634 | |
| 635 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 636 | * Check if any of the connections' wait queues have events that are too old. |
| 637 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 638 | * Return the time at which we should wake up next. |
| 639 | */ |
| 640 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 641 | const nsecs_t currentTime = now(); |
| 642 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 643 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 644 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 645 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 646 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 647 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 648 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 649 | return LONG_LONG_MIN; |
| 650 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 651 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 652 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | // Check if any connection ANRs are due |
| 657 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 658 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 659 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 660 | } |
| 661 | |
| 662 | // If we reached here, we have an unresponsive connection. |
| 663 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 664 | if (connection == nullptr) { |
| 665 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 666 | return nextAnrCheck; |
| 667 | } |
| 668 | connection->responsive = false; |
| 669 | // Stop waking up for this unresponsive connection |
| 670 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 671 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 672 | return LONG_LONG_MIN; |
| 673 | } |
| 674 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 675 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 676 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 677 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 678 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 679 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 680 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 684 | nsecs_t currentTime = now(); |
| 685 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 686 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 687 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 688 | // repeat if the device is just coming out of sleep. |
| 689 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 690 | resetKeyRepeatLocked(); |
| 691 | } |
| 692 | |
| 693 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 694 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 695 | if (DEBUG_FOCUS) { |
| 696 | ALOGD("Dispatch frozen. Waiting some more."); |
| 697 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | |
| 701 | // Optimize latency of app switches. |
| 702 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 703 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 704 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 705 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 706 | *nextWakeupTime = mAppSwitchDueTime; |
| 707 | } |
| 708 | |
| 709 | // Ready to start a new event. |
| 710 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 711 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 712 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | if (isAppSwitchDue) { |
| 714 | // The inbound queue is empty so the app switch key we were waiting |
| 715 | // for will never arrive. Stop waiting for it. |
| 716 | resetPendingAppSwitchLocked(false); |
| 717 | isAppSwitchDue = false; |
| 718 | } |
| 719 | |
| 720 | // Synthesize a key repeat if appropriate. |
| 721 | if (mKeyRepeatState.lastKeyEntry) { |
| 722 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 723 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 724 | } else { |
| 725 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 726 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Nothing to do if there is no pending event. |
| 732 | if (!mPendingEvent) { |
| 733 | return; |
| 734 | } |
| 735 | } else { |
| 736 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 737 | mPendingEvent = mInboundQueue.front(); |
| 738 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | traceInboundQueueLengthLocked(); |
| 740 | } |
| 741 | |
| 742 | // Poke user activity for this event. |
| 743 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 744 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 745 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // Now we have an event to dispatch. |
| 749 | // 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] | 750 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 751 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 752 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 754 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 756 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 760 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 764 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 765 | const ConfigurationChangedEntry& typedEntry = |
| 766 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 767 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 768 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 769 | break; |
| 770 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 772 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 773 | const DeviceResetEntry& typedEntry = |
| 774 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 775 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 776 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 777 | break; |
| 778 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 780 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 781 | std::shared_ptr<FocusEntry> typedEntry = |
| 782 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 783 | dispatchFocusLocked(currentTime, typedEntry); |
| 784 | done = true; |
| 785 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 786 | break; |
| 787 | } |
| 788 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 789 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 790 | const auto typedEntry = |
| 791 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 792 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 793 | done = true; |
| 794 | break; |
| 795 | } |
| 796 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 797 | case EventEntry::Type::DRAG: { |
| 798 | std::shared_ptr<DragEntry> typedEntry = |
| 799 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 800 | dispatchDragLocked(currentTime, typedEntry); |
| 801 | done = true; |
| 802 | break; |
| 803 | } |
| 804 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 805 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 806 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 807 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 808 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | resetPendingAppSwitchLocked(true); |
| 810 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 811 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 812 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 813 | } |
| 814 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 815 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 816 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 817 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 818 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 819 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 820 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 821 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 825 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 826 | std::shared_ptr<MotionEntry> motionEntry = |
| 827 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 828 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 829 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 830 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 831 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 832 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 833 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 834 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 835 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 836 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 837 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 838 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 839 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 840 | |
| 841 | case EventEntry::Type::SENSOR: { |
| 842 | std::shared_ptr<SensorEntry> sensorEntry = |
| 843 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 844 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 845 | dropReason = DropReason::APP_SWITCH; |
| 846 | } |
| 847 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 848 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 849 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 850 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 851 | dropReason = DropReason::STALE; |
| 852 | } |
| 853 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 854 | done = true; |
| 855 | break; |
| 856 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 860 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 861 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 862 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 863 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 864 | |
| 865 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 866 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 870 | /** |
| 871 | * Return true if the events preceding this incoming motion event should be dropped |
| 872 | * Return false otherwise (the default behaviour) |
| 873 | */ |
| 874 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 875 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 876 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 877 | |
| 878 | // Optimize case where the current application is unresponsive and the user |
| 879 | // decides to touch a window in a different application. |
| 880 | // If the application takes too long to catch up then we drop all events preceding |
| 881 | // the touch into the other window. |
| 882 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 883 | int32_t displayId = motionEntry.displayId; |
| 884 | int32_t x = static_cast<int32_t>( |
| 885 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 886 | int32_t y = static_cast<int32_t>( |
| 887 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 888 | sp<InputWindowHandle> touchedWindowHandle = |
| 889 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 890 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 891 | touchedWindowHandle->getApplicationToken() != |
| 892 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 893 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 894 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 895 | "for %s", |
| 896 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 897 | return true; |
| 898 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 899 | |
| 900 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 901 | std::vector<TouchedMonitor> gestureMonitors = |
| 902 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 903 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 904 | sp<Connection> connection = |
| 905 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 906 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 907 | // This monitor could take more input. Drop all events preceding this |
| 908 | // event, so that gesture monitor could get a chance to receive the stream |
| 909 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 910 | "responsive gesture monitor that may handle the event", |
| 911 | mAwaitedFocusedApplication->getName().c_str()); |
| 912 | return true; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 918 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 919 | // events to be processed before dispatching the key event. This is because these motion events |
| 920 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 921 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 922 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 923 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 924 | "just send the pending key event to the focused window."); |
| 925 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 926 | } |
| 927 | return false; |
| 928 | } |
| 929 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 930 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 931 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 932 | mInboundQueue.push_back(std::move(newEntry)); |
| 933 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 934 | traceInboundQueueLengthLocked(); |
| 935 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 936 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 937 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 938 | // Optimize app switch latency. |
| 939 | // If the application takes too long to catch up then we drop all events preceding |
| 940 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 941 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 942 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 943 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 944 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 945 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 946 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 947 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 948 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 950 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 951 | mAppSwitchSawKeyDown = false; |
| 952 | needWake = true; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | break; |
| 957 | } |
| 958 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 959 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 960 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 961 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 962 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 963 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 964 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 965 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 966 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 967 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 968 | break; |
| 969 | } |
| 970 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 971 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 972 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 973 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 974 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 975 | // nothing to do |
| 976 | break; |
| 977 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | return needWake; |
| 981 | } |
| 982 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 983 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 984 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 985 | if (entry->type != EventEntry::Type::SENSOR) { |
| 986 | mRecentQueue.push_back(entry); |
| 987 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 988 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 989 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 994 | int32_t y, TouchState* touchState, |
| 995 | bool addOutsideTargets, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 996 | bool addPortalWindows, |
| 997 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 998 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 999 | LOG_ALWAYS_FATAL( |
| 1000 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 1001 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1002 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1003 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1004 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1005 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1006 | continue; |
| 1007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1008 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 1009 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1010 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | |
| 1012 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1013 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1014 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 1015 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1016 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1017 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1018 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 1019 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1020 | if (addPortalWindows) { |
| 1021 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1022 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1023 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1024 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1025 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1027 | // Found window. |
| 1028 | return windowHandle; |
| 1029 | } |
| 1030 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1031 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1032 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1033 | touchState->addOrUpdateWindow(windowHandle, |
| 1034 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1035 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1036 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1037 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1040 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1043 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1044 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1045 | std::vector<TouchedMonitor> touchedMonitors; |
| 1046 | |
| 1047 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 1048 | addGestureMonitors(monitors, touchedMonitors); |
| 1049 | for (const sp<InputWindowHandle>& portalWindow : portalWindows) { |
| 1050 | const InputWindowInfo* windowInfo = portalWindow->getInfo(); |
| 1051 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1052 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 1053 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1054 | } |
| 1055 | return touchedMonitors; |
| 1056 | } |
| 1057 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1058 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1059 | const char* reason; |
| 1060 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1061 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1063 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1065 | reason = "inbound event was dropped because the policy consumed it"; |
| 1066 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1067 | case DropReason::DISABLED: |
| 1068 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1069 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1070 | } |
| 1071 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1072 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1073 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1074 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1075 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1076 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1077 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1078 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1079 | "has started interacting with a different application."); |
| 1080 | reason = "inbound event was dropped because the current application is not responding " |
| 1081 | "and the user has started interacting with a different application"; |
| 1082 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1083 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1084 | ALOGI("Dropped event because it is stale."); |
| 1085 | reason = "inbound event was dropped because it is stale"; |
| 1086 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1087 | case DropReason::NO_POINTER_CAPTURE: |
| 1088 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1089 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1090 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1091 | case DropReason::NOT_DROPPED: { |
| 1092 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1093 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1094 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1097 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1098 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1099 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1100 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1101 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1102 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1103 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1104 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1105 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1106 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1107 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1108 | } else { |
| 1109 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1110 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1111 | } |
| 1112 | break; |
| 1113 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1114 | case EventEntry::Type::SENSOR: { |
| 1115 | break; |
| 1116 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1117 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1118 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1119 | break; |
| 1120 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1121 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1122 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1123 | case EventEntry::Type::DEVICE_RESET: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1124 | 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] | 1125 | break; |
| 1126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1130 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1131 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1132 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1135 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1136 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1137 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1138 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1142 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1143 | } |
| 1144 | |
| 1145 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1146 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1147 | |
| 1148 | #if DEBUG_APP_SWITCH |
| 1149 | if (handled) { |
| 1150 | ALOGD("App switch has arrived."); |
| 1151 | } else { |
| 1152 | ALOGD("App switch was abandoned."); |
| 1153 | } |
| 1154 | #endif |
| 1155 | } |
| 1156 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1157 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1158 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1162 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
| 1165 | |
| 1166 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1167 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1168 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1170 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | |
| 1172 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1173 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | return true; |
| 1175 | } |
| 1176 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1177 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1178 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1182 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1183 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1184 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1185 | releaseInboundEventLocked(entry); |
| 1186 | } |
| 1187 | traceInboundQueueLengthLocked(); |
| 1188 | } |
| 1189 | |
| 1190 | void InputDispatcher::releasePendingEventLocked() { |
| 1191 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1193 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1197 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1199 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | #if DEBUG_DISPATCH_CYCLE |
| 1201 | ALOGD("Injected inbound event was dropped."); |
| 1202 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1203 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | } |
| 1205 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1206 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | } |
| 1208 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1212 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1213 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1217 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1218 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1220 | uint32_t policyFlags = entry->policyFlags & |
| 1221 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1223 | std::shared_ptr<KeyEntry> newEntry = |
| 1224 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1225 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1226 | entry->flags, entry->keyCode, entry->scanCode, |
| 1227 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1229 | newEntry->syntheticRepeat = true; |
| 1230 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1231 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1232 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1235 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1236 | const ConfigurationChangedEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1237 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1238 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | #endif |
| 1240 | |
| 1241 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1242 | resetKeyRepeatLocked(); |
| 1243 | |
| 1244 | // 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] | 1245 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1246 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1247 | commandEntry->eventTime = entry.eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1248 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | return true; |
| 1250 | } |
| 1251 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1252 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1253 | const DeviceResetEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1256 | entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | #endif |
| 1258 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1259 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1260 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1262 | return true; |
| 1263 | } |
| 1264 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1265 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1266 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1267 | if (mPendingEvent != nullptr) { |
| 1268 | // Move the pending event to the front of the queue. This will give the chance |
| 1269 | // for the pending event to get dispatched to the newly focused window |
| 1270 | mInboundQueue.push_front(mPendingEvent); |
| 1271 | mPendingEvent = nullptr; |
| 1272 | } |
| 1273 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1274 | std::unique_ptr<FocusEntry> focusEntry = |
| 1275 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1276 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1277 | |
| 1278 | // This event should go to the front of the queue, but behind all other focus events |
| 1279 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1280 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1281 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1282 | [](const std::shared_ptr<EventEntry>& event) { |
| 1283 | return event->type == EventEntry::Type::FOCUS; |
| 1284 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1285 | |
| 1286 | // 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] | 1287 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1291 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1292 | if (channel == nullptr) { |
| 1293 | return; // Window has gone away |
| 1294 | } |
| 1295 | InputTarget target; |
| 1296 | target.inputChannel = channel; |
| 1297 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1298 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1299 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1300 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1301 | std::string reason = std::string("reason=").append(entry->reason); |
| 1302 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1303 | dispatchEventLocked(currentTime, entry, {target}); |
| 1304 | } |
| 1305 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1306 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1307 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1308 | DropReason& dropReason) { |
| 1309 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1310 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { |
| 1311 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); |
| 1312 | } |
| 1313 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1314 | // Pointer capture was already forcefully disabled because of focus change. |
| 1315 | dropReason = DropReason::NOT_DROPPED; |
| 1316 | return; |
| 1317 | } |
| 1318 | |
| 1319 | // Set drop reason for early returns |
| 1320 | dropReason = DropReason::NO_POINTER_CAPTURE; |
| 1321 | |
| 1322 | sp<IBinder> token; |
| 1323 | if (entry->pointerCaptureEnabled) { |
| 1324 | // Enable Pointer Capture |
| 1325 | if (!mFocusedWindowRequestedPointerCapture) { |
| 1326 | // This can happen if a window requests capture and immediately releases capture. |
| 1327 | ALOGW("No window requested Pointer Capture."); |
| 1328 | return; |
| 1329 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1330 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1331 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1332 | mWindowTokenWithPointerCapture = token; |
| 1333 | } else { |
| 1334 | // Disable Pointer Capture |
| 1335 | token = mWindowTokenWithPointerCapture; |
| 1336 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1337 | if (mFocusedWindowRequestedPointerCapture) { |
| 1338 | mFocusedWindowRequestedPointerCapture = false; |
| 1339 | setPointerCaptureLocked(false); |
| 1340 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | auto channel = getInputChannelLocked(token); |
| 1344 | if (channel == nullptr) { |
| 1345 | // Window has gone away, clean up Pointer Capture state. |
| 1346 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1347 | if (mFocusedWindowRequestedPointerCapture) { |
| 1348 | mFocusedWindowRequestedPointerCapture = false; |
| 1349 | setPointerCaptureLocked(false); |
| 1350 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1351 | return; |
| 1352 | } |
| 1353 | InputTarget target; |
| 1354 | target.inputChannel = channel; |
| 1355 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1356 | entry->dispatchInProgress = true; |
| 1357 | dispatchEventLocked(currentTime, entry, {target}); |
| 1358 | |
| 1359 | dropReason = DropReason::NOT_DROPPED; |
| 1360 | } |
| 1361 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1362 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1363 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1365 | if (!entry->dispatchInProgress) { |
| 1366 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1367 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1368 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1369 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1370 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1371 | // We have seen two identical key downs in a row which indicates that the device |
| 1372 | // driver is automatically generating key repeats itself. We take note of the |
| 1373 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1374 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1375 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1376 | // Make sure we don't get key down from a different device. If a different |
| 1377 | // device Id has same key pressed down, the new device Id will replace the |
| 1378 | // current one to hold the key repeat with repeat count reset. |
| 1379 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1380 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1382 | resetKeyRepeatLocked(); |
| 1383 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1384 | } else { |
| 1385 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1386 | resetKeyRepeatLocked(); |
| 1387 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1388 | } |
| 1389 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1390 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1391 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1392 | // 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] | 1393 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1394 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1395 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1396 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | resetKeyRepeatLocked(); |
| 1398 | } |
| 1399 | |
| 1400 | if (entry->repeatCount == 1) { |
| 1401 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1402 | } else { |
| 1403 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1404 | } |
| 1405 | |
| 1406 | entry->dispatchInProgress = true; |
| 1407 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1408 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | // Handle case where the policy asked us to try again later last time. |
| 1412 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1413 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1414 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1415 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1416 | } |
| 1417 | return false; // wait until next wakeup |
| 1418 | } |
| 1419 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1420 | entry->interceptKeyWakeupTime = 0; |
| 1421 | } |
| 1422 | |
| 1423 | // Give the policy a chance to intercept the key. |
| 1424 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1425 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1426 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1427 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1428 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1429 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1430 | commandEntry->connectionToken = focusedWindowToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1431 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1432 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1433 | return false; // wait for the command to run |
| 1434 | } else { |
| 1435 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1436 | } |
| 1437 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1438 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1439 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1444 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1445 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1446 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1447 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1448 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1449 | return true; |
| 1450 | } |
| 1451 | |
| 1452 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1453 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1454 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1455 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1456 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1457 | return false; |
| 1458 | } |
| 1459 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1460 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1461 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1462 | return true; |
| 1463 | } |
| 1464 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1465 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1466 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1467 | |
| 1468 | // Dispatch the key. |
| 1469 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1470 | return true; |
| 1471 | } |
| 1472 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1473 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1475 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1476 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1477 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1478 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1479 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1480 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | #endif |
| 1482 | } |
| 1483 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1484 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { |
| 1485 | mLock.unlock(); |
| 1486 | |
| 1487 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; |
| 1488 | if (entry->accuracyChanged) { |
| 1489 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1490 | } |
| 1491 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1492 | entry->hwTimestamp, entry->values); |
| 1493 | mLock.lock(); |
| 1494 | } |
| 1495 | |
| 1496 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, |
| 1497 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 1498 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1499 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1500 | "source=0x%x, sensorType=%s", |
| 1501 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1502 | NamedEnum::string(entry->sensorType).c_str()); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1503 | #endif |
| 1504 | std::unique_ptr<CommandEntry> commandEntry = |
| 1505 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); |
| 1506 | commandEntry->sensorEntry = entry; |
| 1507 | postCommandLocked(std::move(commandEntry)); |
| 1508 | } |
| 1509 | |
| 1510 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
| 1511 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1512 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
| 1513 | NamedEnum::string(sensorType).c_str()); |
| 1514 | #endif |
| 1515 | { // acquire lock |
| 1516 | std::scoped_lock _l(mLock); |
| 1517 | |
| 1518 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1519 | std::shared_ptr<EventEntry> entry = *it; |
| 1520 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1521 | it = mInboundQueue.erase(it); |
| 1522 | releaseInboundEventLocked(entry); |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | return true; |
| 1527 | } |
| 1528 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1529 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1530 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1531 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1533 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1534 | entry->dispatchInProgress = true; |
| 1535 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1536 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1540 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1541 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1542 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1543 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1544 | return true; |
| 1545 | } |
| 1546 | |
| 1547 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1548 | |
| 1549 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1550 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1551 | |
| 1552 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1553 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | if (isPointerEvent) { |
| 1555 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1556 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1557 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1558 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1559 | } else { |
| 1560 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1561 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1562 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1564 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1565 | return false; |
| 1566 | } |
| 1567 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1568 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1569 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1570 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1571 | return true; |
| 1572 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1573 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1574 | CancelationOptions::Mode mode(isPointerEvent |
| 1575 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1576 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1577 | CancelationOptions options(mode, "input event injection failed"); |
| 1578 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | return true; |
| 1580 | } |
| 1581 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1582 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1583 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1584 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1585 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1586 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1587 | mTouchStatesByDisplay.find(entry->displayId); |
| 1588 | if (it != mTouchStatesByDisplay.end()) { |
| 1589 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1590 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1591 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1592 | // the corresponding displays as well. |
| 1593 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1594 | const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1595 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1596 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1602 | // Dispatch the motion. |
| 1603 | if (conflictingPointerActions) { |
| 1604 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1605 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1606 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1607 | } |
| 1608 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1612 | void InputDispatcher::enqueueDragEventLocked(const sp<InputWindowHandle>& windowHandle, |
| 1613 | bool isExiting, const MotionEntry& motionEntry) { |
| 1614 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1615 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1616 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1617 | PointerCoords pointerCoords; |
| 1618 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1619 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1620 | |
| 1621 | std::unique_ptr<DragEntry> dragEntry = |
| 1622 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1623 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1624 | pointerCoords.getY()); |
| 1625 | |
| 1626 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1627 | } |
| 1628 | |
| 1629 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1630 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1631 | if (channel == nullptr) { |
| 1632 | return; // Window has gone away |
| 1633 | } |
| 1634 | InputTarget target; |
| 1635 | target.inputChannel = channel; |
| 1636 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1637 | entry->dispatchInProgress = true; |
| 1638 | dispatchEventLocked(currentTime, entry, {target}); |
| 1639 | } |
| 1640 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1641 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1643 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1644 | ", policyFlags=0x%x, " |
| 1645 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1646 | "metaState=0x%x, buttonState=0x%x," |
| 1647 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1648 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1649 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1650 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1651 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1652 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1653 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1654 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1655 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1656 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1657 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1658 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1659 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1660 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1661 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1662 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1663 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1664 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1665 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1666 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1667 | } |
| 1668 | #endif |
| 1669 | } |
| 1670 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1671 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1672 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1673 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1674 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | #if DEBUG_DISPATCH_CYCLE |
| 1676 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1677 | #endif |
| 1678 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1679 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1680 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1681 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1682 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1683 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1684 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1685 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1686 | sp<Connection> connection = |
| 1687 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1688 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1689 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1690 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1691 | if (DEBUG_FOCUS) { |
| 1692 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1693 | "is no longer registered with the input dispatcher.", |
| 1694 | inputTarget.inputChannel->getName().c_str()); |
| 1695 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1700 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1701 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1702 | // If the policy decides to close the app, we will get a channel removal event via |
| 1703 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1704 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1705 | // pile up. |
| 1706 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1707 | connection->inputChannel->getName().c_str()); |
| 1708 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1709 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1710 | "application not responding"); |
| 1711 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1715 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1716 | if (DEBUG_FOCUS) { |
| 1717 | ALOGD("Resetting ANR timeouts."); |
| 1718 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1719 | |
| 1720 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1721 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1722 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1723 | } |
| 1724 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1725 | /** |
| 1726 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1727 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1728 | * Focused display is the display that the user most recently interacted with. |
| 1729 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1730 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1731 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1732 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1733 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1734 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1735 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1736 | break; |
| 1737 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1738 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1739 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1740 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1741 | break; |
| 1742 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1743 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1744 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1745 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1746 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1747 | case EventEntry::Type::SENSOR: |
| 1748 | case EventEntry::Type::DRAG: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1749 | 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] | 1750 | return ADISPLAY_ID_NONE; |
| 1751 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1752 | } |
| 1753 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1754 | } |
| 1755 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1756 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1757 | const char* focusedWindowName) { |
| 1758 | if (mAnrTracker.empty()) { |
| 1759 | // already processed all events that we waited for |
| 1760 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1761 | return false; |
| 1762 | } |
| 1763 | |
| 1764 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1765 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1766 | // 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] | 1767 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1768 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1769 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1770 | return true; |
| 1771 | } |
| 1772 | |
| 1773 | // We still have pending events, and already started the timer |
| 1774 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1775 | return true; // Still waiting |
| 1776 | } |
| 1777 | |
| 1778 | // Waited too long, and some connection still hasn't processed all motions |
| 1779 | // Just send the key to the focused window |
| 1780 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1781 | focusedWindowName); |
| 1782 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1783 | return false; |
| 1784 | } |
| 1785 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1786 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1787 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1788 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1789 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1791 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1792 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1793 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1794 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1795 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | // If there is no currently focused window and no focused application |
| 1797 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1798 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1799 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1800 | "display %" PRId32 ".", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1801 | NamedEnum::string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1802 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
Vishnu Nair | 212dcf4 | 2022-01-27 22:44:01 +0000 | [diff] [blame^] | 1805 | // Drop key events if requested by input feature |
| 1806 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1807 | return InputEventInjectionResult::FAILED; |
| 1808 | } |
| 1809 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1810 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1811 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1812 | // start interacting with another application via touch (app switch). This code can be removed |
| 1813 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1814 | // an app is expected to have a focused window. |
| 1815 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1816 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1817 | // 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] | 1818 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1819 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1820 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1821 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1822 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1823 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1824 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1825 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1826 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1827 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1828 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1829 | // Already raised ANR. Drop the event |
| 1830 | ALOGE("Dropping %s event because there is no focused window", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1831 | NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1832 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1833 | } else { |
| 1834 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1835 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | // we have a valid, non-null focused window |
| 1840 | resetNoFocusedWindowTimeoutLocked(); |
| 1841 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1842 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1843 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1844 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1847 | if (focusedWindowHandle->getInfo()->paused) { |
| 1848 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1849 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | // If the event is a key event, then we must wait for all previous events to |
| 1853 | // complete before delivering it because previous events may have the |
| 1854 | // side-effect of transferring focus to a different window and we want to |
| 1855 | // ensure that the following keys are sent to the new window. |
| 1856 | // |
| 1857 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1858 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1859 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1860 | // often anticipate pending UI changes when typing on a keyboard. |
| 1861 | // To obtain this behavior, we must serialize key events with respect to all |
| 1862 | // prior input events. |
| 1863 | if (entry.type == EventEntry::Type::KEY) { |
| 1864 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1865 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1866 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1867 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1871 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1872 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1873 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1874 | |
| 1875 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1876 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1879 | /** |
| 1880 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1881 | * that are currently unresponsive. |
| 1882 | */ |
| 1883 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1884 | const std::vector<TouchedMonitor>& monitors) const { |
| 1885 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1886 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1887 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1888 | sp<Connection> connection = getConnectionLocked( |
| 1889 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1890 | if (connection == nullptr) { |
| 1891 | ALOGE("Could not find connection for monitor %s", |
| 1892 | monitor.monitor.inputChannel->getName().c_str()); |
| 1893 | return false; |
| 1894 | } |
| 1895 | if (!connection->responsive) { |
| 1896 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1897 | connection->inputChannel->getName().c_str()); |
| 1898 | return false; |
| 1899 | } |
| 1900 | return true; |
| 1901 | }); |
| 1902 | return responsiveMonitors; |
| 1903 | } |
| 1904 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1905 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1906 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1907 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1908 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1909 | enum InjectionPermission { |
| 1910 | INJECTION_PERMISSION_UNKNOWN, |
| 1911 | INJECTION_PERMISSION_GRANTED, |
| 1912 | INJECTION_PERMISSION_DENIED |
| 1913 | }; |
| 1914 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1915 | // For security reasons, we defer updating the touch state until we are sure that |
| 1916 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1917 | int32_t displayId = entry.displayId; |
| 1918 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1920 | |
| 1921 | // 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] | 1922 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1923 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1924 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1925 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1927 | // Copy current touch state into tempTouchState. |
| 1928 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1929 | // 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] | 1930 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1931 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1932 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1933 | mTouchStatesByDisplay.find(displayId); |
| 1934 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1935 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1936 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1939 | bool isSplit = tempTouchState.split; |
| 1940 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1941 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1942 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1943 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1944 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1945 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1946 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1947 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1948 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1949 | bool wrongDevice = false; |
| 1950 | if (newGesture) { |
| 1951 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1952 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1953 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1954 | "in display %" PRId32, |
| 1955 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1956 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1957 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1958 | switchedDevice = false; |
| 1959 | wrongDevice = true; |
| 1960 | goto Failed; |
| 1961 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1962 | tempTouchState.reset(); |
| 1963 | tempTouchState.down = down; |
| 1964 | tempTouchState.deviceId = entry.deviceId; |
| 1965 | tempTouchState.source = entry.source; |
| 1966 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1967 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1968 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1969 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1970 | "in display %" PRId32, |
| 1971 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1972 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1973 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1974 | switchedDevice = false; |
| 1975 | wrongDevice = true; |
| 1976 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1980 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1981 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1982 | int32_t x; |
| 1983 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1984 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1985 | // Always dispatch mouse events to cursor position. |
| 1986 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1987 | x = int32_t(entry.xCursorPosition); |
| 1988 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1989 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1990 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1991 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1992 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1993 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1994 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1995 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1996 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1997 | |
| 1998 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1999 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2000 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2002 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2003 | if (newTouchedWindowHandle != nullptr && |
| 2004 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2005 | // New window supports splitting, but we should never split mouse events. |
| 2006 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | } else if (isSplit) { |
| 2008 | // New window does not support splitting but we have already split events. |
| 2009 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2010 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2014 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2015 | // 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] | 2016 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2019 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2020 | ALOGI("Not sending touch event to %s because it is paused", |
| 2021 | newTouchedWindowHandle->getName().c_str()); |
| 2022 | newTouchedWindowHandle = nullptr; |
| 2023 | } |
| 2024 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2025 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2026 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2027 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2028 | if (!isResponsive) { |
| 2029 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2030 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2031 | newTouchedWindowHandle = nullptr; |
| 2032 | } |
| 2033 | } |
| 2034 | |
Vishnu Nair | 212dcf4 | 2022-01-27 22:44:01 +0000 | [diff] [blame^] | 2035 | // Drop touch events if requested by input feature |
| 2036 | if (newTouchedWindowHandle != nullptr && shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2037 | newTouchedWindowHandle = nullptr; |
| 2038 | } |
| 2039 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2040 | // Drop events that can't be trusted due to occlusion |
| 2041 | if (newTouchedWindowHandle != nullptr && |
| 2042 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2043 | TouchOcclusionInfo occlusionInfo = |
| 2044 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2045 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2046 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2047 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2048 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2049 | ALOGD("%s", log.c_str()); |
| 2050 | } |
| 2051 | } |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2052 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 2053 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2054 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2055 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2056 | newTouchedWindowHandle = nullptr; |
| 2057 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2061 | // Also don't send the new touch event to unresponsive gesture monitors |
| 2062 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 2063 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2064 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2065 | 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] | 2066 | "(%d, %d) in display %" PRId32 ".", |
| 2067 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2068 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2069 | goto Failed; |
| 2070 | } |
| 2071 | |
| 2072 | if (newTouchedWindowHandle != nullptr) { |
| 2073 | // Set target flags. |
| 2074 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2075 | if (isSplit) { |
| 2076 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2078 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2079 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2080 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2081 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2082 | } |
| 2083 | |
| 2084 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2085 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2086 | newHoverWindowHandle = nullptr; |
| 2087 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2088 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | // Update the temporary touch state. |
| 2092 | BitSet32 pointerIds; |
| 2093 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2094 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2095 | pointerIds.markBit(pointerId); |
| 2096 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2097 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2100 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 | } else { |
| 2102 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2103 | |
| 2104 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2105 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2106 | if (DEBUG_FOCUS) { |
| 2107 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2108 | "dropped the pointer down event in display %" PRId32, |
| 2109 | displayId); |
| 2110 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2111 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 | goto Failed; |
| 2113 | } |
| 2114 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2115 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2116 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2117 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2118 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2119 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2120 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2121 | 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] | 2122 | |
| 2123 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2124 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2125 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Vishnu Nair | 212dcf4 | 2022-01-27 22:44:01 +0000 | [diff] [blame^] | 2126 | |
| 2127 | // Drop touch events if requested by input feature |
| 2128 | if (newTouchedWindowHandle != nullptr && |
| 2129 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2130 | newTouchedWindowHandle = nullptr; |
| 2131 | } |
| 2132 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2133 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2134 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2135 | if (DEBUG_FOCUS) { |
| 2136 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2137 | oldTouchedWindowHandle->getName().c_str(), |
| 2138 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2139 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2140 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2141 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2142 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2143 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2144 | |
| 2145 | // Make a slippery entrance into the new window. |
| 2146 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2147 | isSplit = true; |
| 2148 | } |
| 2149 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2150 | int32_t targetFlags = |
| 2151 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 | if (isSplit) { |
| 2153 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2154 | } |
| 2155 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2156 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2157 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2158 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | BitSet32 pointerIds; |
| 2162 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2163 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2164 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2165 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2171 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2172 | // when dispatching it as is to newTouchedWindowHandle. |
| 2173 | if (mLastHoverWindowHandle != nullptr && |
| 2174 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2175 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2176 | #if DEBUG_HOVER |
| 2177 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2178 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2179 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2180 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2181 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2182 | } |
| 2183 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2184 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2185 | // when dispatching it as is to newTouchedWindowHandle. |
| 2186 | if (newHoverWindowHandle != nullptr && |
| 2187 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2188 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2189 | #if DEBUG_HOVER |
| 2190 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2191 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2192 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2193 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2194 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2195 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // Check permission to inject into all touched foreground windows and ensure there |
| 2200 | // is at least one touched foreground window. |
| 2201 | { |
| 2202 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2203 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2204 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2205 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2206 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2207 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2209 | goto Failed; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2213 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2214 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2215 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2216 | "%" PRId32 " or gesture monitor to receive it.", |
| 2217 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2218 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | goto Failed; |
| 2220 | } |
| 2221 | |
| 2222 | // Permission granted to injection into all touched foreground windows. |
| 2223 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2224 | } |
| 2225 | |
| 2226 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2227 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2228 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2229 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2230 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2231 | if (foregroundWindowHandle) { |
| 2232 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2233 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2234 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2235 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 2236 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2237 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 2238 | InputTarget::FLAG_ZERO_COORDS, |
| 2239 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2240 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | } |
| 2244 | } |
| 2245 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2246 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2247 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2248 | // of the touch gesture. |
| 2249 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2250 | // engine only supports touch events. We would need to add a mechanism similar |
| 2251 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2252 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2253 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2254 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2255 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2256 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2257 | getWindowHandlesLocked(displayId); |
| 2258 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2259 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2260 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2261 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2262 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2263 | .addOrUpdateWindow(windowHandle, |
| 2264 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2265 | InputTarget:: |
| 2266 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2267 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2268 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2275 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2277 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2278 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2279 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2282 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2283 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2284 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2287 | // Drop the outside or hover touch windows since we will not care about them |
| 2288 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2289 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 | |
| 2291 | Failed: |
| 2292 | // Check injection permission once and for all. |
| 2293 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2294 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2295 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2296 | } else { |
| 2297 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2298 | } |
| 2299 | } |
| 2300 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2301 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2302 | return injectionResult; |
| 2303 | } |
| 2304 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2305 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2306 | if (!wrongDevice) { |
| 2307 | if (switchedDevice) { |
| 2308 | if (DEBUG_FOCUS) { |
| 2309 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2310 | } |
| 2311 | *outConflictingPointerActions = true; |
| 2312 | } |
| 2313 | |
| 2314 | if (isHoverAction) { |
| 2315 | // Started hovering, therefore no longer down. |
| 2316 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2317 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2318 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2319 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2320 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2321 | *outConflictingPointerActions = true; |
| 2322 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2323 | tempTouchState.reset(); |
| 2324 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2325 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2326 | tempTouchState.deviceId = entry.deviceId; |
| 2327 | tempTouchState.source = entry.source; |
| 2328 | tempTouchState.displayId = displayId; |
| 2329 | } |
| 2330 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2331 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2332 | // All pointers up or canceled. |
| 2333 | tempTouchState.reset(); |
| 2334 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2335 | // First pointer went down. |
| 2336 | if (oldState && oldState->down) { |
| 2337 | if (DEBUG_FOCUS) { |
| 2338 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2339 | } |
| 2340 | *outConflictingPointerActions = true; |
| 2341 | } |
| 2342 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2343 | // One pointer went up. |
| 2344 | if (isSplit) { |
| 2345 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2346 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2348 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2349 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2350 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2351 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2352 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2353 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2354 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2356 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2357 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2359 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2360 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2361 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2362 | // Save changes unless the action was scroll in which case the temporary touch |
| 2363 | // state was only valid for this one action. |
| 2364 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2365 | if (tempTouchState.displayId >= 0) { |
| 2366 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2367 | } else { |
| 2368 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2369 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2370 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2371 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2372 | // Update hover state. |
| 2373 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2376 | return injectionResult; |
| 2377 | } |
| 2378 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2379 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
| 2380 | const sp<InputWindowHandle> dropWindow = |
| 2381 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
| 2382 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2383 | true /*ignoreDragWindow*/); |
| 2384 | if (dropWindow) { |
| 2385 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
| 2386 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2387 | } else { |
| 2388 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2389 | } |
| 2390 | mDragState.reset(); |
| 2391 | } |
| 2392 | |
| 2393 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2394 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2395 | return; |
| 2396 | } |
| 2397 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2398 | if (!mDragState->isStartDrag) { |
| 2399 | mDragState->isStartDrag = true; |
| 2400 | mDragState->isStylusButtonDownAtStart = |
| 2401 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2402 | } |
| 2403 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2404 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2405 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2406 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2407 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2408 | // Handle the special case : stylus button no longer pressed. |
| 2409 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2410 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2411 | finishDragAndDrop(entry.displayId, x, y); |
| 2412 | return; |
| 2413 | } |
| 2414 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2415 | const sp<InputWindowHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2416 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2417 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2418 | true /*ignoreDragWindow*/); |
| 2419 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2420 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2421 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2422 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2423 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2424 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2425 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2426 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2427 | } |
| 2428 | // enqueue drag location if needed. |
| 2429 | if (hoverWindowHandle != nullptr) { |
| 2430 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2431 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2432 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2433 | finishDragAndDrop(entry.displayId, x, y); |
| 2434 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2435 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2436 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2440 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2441 | int32_t targetFlags, BitSet32 pointerIds, |
| 2442 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2443 | std::vector<InputTarget>::iterator it = |
| 2444 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2445 | [&windowHandle](const InputTarget& inputTarget) { |
| 2446 | return inputTarget.inputChannel->getConnectionToken() == |
| 2447 | windowHandle->getToken(); |
| 2448 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2449 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2450 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2451 | |
| 2452 | if (it == inputTargets.end()) { |
| 2453 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2454 | std::shared_ptr<InputChannel> inputChannel = |
| 2455 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2456 | if (inputChannel == nullptr) { |
| 2457 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2458 | return; |
| 2459 | } |
| 2460 | inputTarget.inputChannel = inputChannel; |
| 2461 | inputTarget.flags = targetFlags; |
| 2462 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2463 | inputTarget.displaySize = |
Evan Rosky | 44edce9 | 2021-05-14 18:09:55 -0700 | [diff] [blame] | 2464 | int2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2465 | inputTargets.push_back(inputTarget); |
| 2466 | it = inputTargets.end() - 1; |
| 2467 | } |
| 2468 | |
| 2469 | ALOG_ASSERT(it->flags == targetFlags); |
| 2470 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2471 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2472 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2473 | } |
| 2474 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2475 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2476 | int32_t displayId, float xOffset, |
| 2477 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2478 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2479 | mGlobalMonitorsByDisplay.find(displayId); |
| 2480 | |
| 2481 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2482 | const std::vector<Monitor>& monitors = it->second; |
| 2483 | for (const Monitor& monitor : monitors) { |
| 2484 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2485 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2489 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2490 | float yOffset, |
| 2491 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2492 | InputTarget target; |
| 2493 | target.inputChannel = monitor.inputChannel; |
| 2494 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2495 | ui::Transform t; |
| 2496 | t.set(xOffset, yOffset); |
| 2497 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2498 | inputTargets.push_back(target); |
| 2499 | } |
| 2500 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2501 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2502 | const InjectionState* injectionState) { |
| 2503 | if (injectionState && |
| 2504 | (windowHandle == nullptr || |
| 2505 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2506 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2507 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2508 | 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] | 2509 | "owned by uid %d", |
| 2510 | injectionState->injectorPid, injectionState->injectorUid, |
| 2511 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2512 | } else { |
| 2513 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2514 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2515 | } |
| 2516 | return false; |
| 2517 | } |
| 2518 | return true; |
| 2519 | } |
| 2520 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2521 | /** |
| 2522 | * Indicate whether one window handle should be considered as obscuring |
| 2523 | * another window handle. We only check a few preconditions. Actually |
| 2524 | * checking the bounds is left to the caller. |
| 2525 | */ |
| 2526 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2527 | const sp<InputWindowHandle>& otherHandle) { |
| 2528 | // Compare by token so cloned layers aren't counted |
| 2529 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2530 | return false; |
| 2531 | } |
| 2532 | auto info = windowHandle->getInfo(); |
| 2533 | auto otherInfo = otherHandle->getInfo(); |
| 2534 | if (!otherInfo->visible) { |
| 2535 | return false; |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2536 | } else if (otherInfo->alpha == 0 && |
| 2537 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 2538 | // Those act as if they were invisible, so we don't need to flag them. |
| 2539 | // We do want to potentially flag touchable windows even if they have 0 |
| 2540 | // opacity, since they can consume touches and alter the effects of the |
| 2541 | // user interaction (eg. apps that rely on |
| 2542 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2543 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2544 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2545 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2546 | // If ownerUid is the same we don't generate occlusion events as there |
| 2547 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2548 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2549 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2550 | return false; |
| 2551 | } else if (otherInfo->displayId != info->displayId) { |
| 2552 | return false; |
| 2553 | } |
| 2554 | return true; |
| 2555 | } |
| 2556 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2557 | /** |
| 2558 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2559 | * untrusted, one should check: |
| 2560 | * |
| 2561 | * 1. If result.hasBlockingOcclusion is true. |
| 2562 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2563 | * BLOCK_UNTRUSTED. |
| 2564 | * |
| 2565 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2566 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2567 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2568 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2569 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2570 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2571 | * |
| 2572 | * If neither of those is true, then it means the touch can be allowed. |
| 2573 | */ |
| 2574 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
| 2575 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2576 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2577 | int32_t displayId = windowInfo->displayId; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2578 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2579 | TouchOcclusionInfo info; |
| 2580 | info.hasBlockingOcclusion = false; |
| 2581 | info.obscuringOpacity = 0; |
| 2582 | info.obscuringUid = -1; |
| 2583 | std::map<int32_t, float> opacityByUid; |
| 2584 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| 2585 | if (windowHandle == otherHandle) { |
| 2586 | break; // All future windows are below us. Exit early. |
| 2587 | } |
| 2588 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2589 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2590 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2591 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2592 | info.debugInfo.push_back( |
| 2593 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2594 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2595 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2596 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2597 | // window's touch occlusion mode |
| 2598 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2599 | info.hasBlockingOcclusion = true; |
| 2600 | info.obscuringUid = otherInfo->ownerUid; |
| 2601 | info.obscuringPackage = otherInfo->packageName; |
| 2602 | break; |
| 2603 | } |
| 2604 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2605 | uint32_t uid = otherInfo->ownerUid; |
| 2606 | float opacity = |
| 2607 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2608 | // Given windows A and B: |
| 2609 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2610 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2611 | opacityByUid[uid] = opacity; |
| 2612 | if (opacity > info.obscuringOpacity) { |
| 2613 | info.obscuringOpacity = opacity; |
| 2614 | info.obscuringUid = uid; |
| 2615 | info.obscuringPackage = otherInfo->packageName; |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2620 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2621 | info.debugInfo.push_back( |
| 2622 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2623 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2624 | return info; |
| 2625 | } |
| 2626 | |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2627 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, |
| 2628 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2629 | return StringPrintf(INDENT2 |
| 2630 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2631 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2632 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2633 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2634 | (isTouchedWindow) ? "[TOUCHED] " : "", |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2635 | NamedEnum::string(info->type, "%" PRId32).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2636 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2637 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2638 | info->frameTop, info->frameRight, info->frameBottom, |
| 2639 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2640 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2641 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2642 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2645 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2646 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2647 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2648 | occlusionInfo.obscuringUid); |
| 2649 | return false; |
| 2650 | } |
| 2651 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2652 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2653 | "%.2f, maximum allowed = %.2f)", |
| 2654 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2655 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2656 | return false; |
| 2657 | } |
| 2658 | return true; |
| 2659 | } |
| 2660 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2661 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2662 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2663 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2664 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2665 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2666 | if (windowHandle == otherHandle) { |
| 2667 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2668 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2669 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2670 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2671 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2672 | return true; |
| 2673 | } |
| 2674 | } |
| 2675 | return false; |
| 2676 | } |
| 2677 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2678 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2679 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2680 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2681 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2682 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2683 | if (windowHandle == otherHandle) { |
| 2684 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2685 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2686 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2687 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2688 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2689 | return true; |
| 2690 | } |
| 2691 | } |
| 2692 | return false; |
| 2693 | } |
| 2694 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2695 | std::string InputDispatcher::getApplicationWindowLabel( |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2696 | const InputApplicationHandle* applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2697 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2698 | if (applicationHandle != nullptr) { |
| 2699 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2700 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2701 | } else { |
| 2702 | return applicationHandle->getName(); |
| 2703 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2704 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2705 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2706 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2707 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2711 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2712 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2713 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2714 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2715 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2716 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2717 | return; |
| 2718 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2719 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2720 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2721 | if (focusedWindowHandle != nullptr) { |
| 2722 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2723 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2724 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2725 | 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] | 2726 | #endif |
| 2727 | return; |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2732 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2733 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2734 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2735 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2736 | return; |
| 2737 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2738 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2739 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2740 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2741 | } |
| 2742 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2743 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2744 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2745 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2746 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2747 | return; |
| 2748 | } |
| 2749 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2750 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2751 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2752 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2753 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2754 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2755 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2756 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2757 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2758 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2759 | NamedEnum::string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2760 | break; |
| 2761 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2764 | std::unique_ptr<CommandEntry> commandEntry = |
| 2765 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2766 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2767 | commandEntry->userActivityEventType = eventType; |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2768 | commandEntry->displayId = displayId; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2769 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2773 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2774 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2775 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2776 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2777 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2778 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2779 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2780 | ATRACE_NAME(message.c_str()); |
| 2781 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2782 | #if DEBUG_DISPATCH_CYCLE |
| 2783 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2784 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2785 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2786 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2787 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2788 | #endif |
| 2789 | |
| 2790 | // Skip this event if the connection status is not normal. |
| 2791 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2792 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2793 | #if DEBUG_DISPATCH_CYCLE |
| 2794 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2795 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2796 | #endif |
| 2797 | return; |
| 2798 | } |
| 2799 | |
| 2800 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2801 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2802 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2803 | "Entry type %s should not have FLAG_SPLIT", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2804 | NamedEnum::string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2805 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2806 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2807 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2808 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2809 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 | if (!splitMotionEntry) { |
| 2811 | return; // split event was dropped |
| 2812 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2813 | if (DEBUG_FOCUS) { |
| 2814 | ALOGD("channel '%s' ~ Split motion event.", |
| 2815 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2816 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2817 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2818 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2819 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2820 | return; |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2825 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2826 | } |
| 2827 | |
| 2828 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2829 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2830 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2831 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2832 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2833 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2834 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2835 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2836 | ATRACE_NAME(message.c_str()); |
| 2837 | } |
| 2838 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2839 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2840 | |
| 2841 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2842 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2843 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2844 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2845 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2846 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2847 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2848 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2849 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2850 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2851 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2852 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2853 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 | |
| 2855 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2856 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2857 | startDispatchCycleLocked(currentTime, connection); |
| 2858 | } |
| 2859 | } |
| 2860 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2861 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2862 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2863 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2864 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2865 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2866 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2867 | connection->getInputChannelName().c_str(), |
| 2868 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2869 | ATRACE_NAME(message.c_str()); |
| 2870 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2871 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2872 | if (!(inputTargetFlags & dispatchMode)) { |
| 2873 | return; |
| 2874 | } |
| 2875 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2876 | |
| 2877 | // This is a new event. |
| 2878 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2879 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2880 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2881 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2882 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2883 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2884 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2885 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2886 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2887 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2888 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2889 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2890 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2891 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2893 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2894 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2895 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2896 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2897 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2898 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | return; // skip the inconsistent event |
| 2900 | } |
| 2901 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2902 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2903 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2904 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2905 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2906 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2907 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2908 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2909 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2910 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2911 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2912 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2913 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2914 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2915 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2916 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2917 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2918 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2919 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2920 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2921 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2922 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2923 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2924 | } |
| 2925 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2926 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2927 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2929 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2930 | "event", |
| 2931 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2932 | #endif |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2933 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2934 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2935 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2937 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2938 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2939 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2940 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2941 | } |
| 2942 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2943 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2944 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2945 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2946 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2947 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2948 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2949 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2950 | "event", |
| 2951 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2952 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2953 | return; // skip the inconsistent event |
| 2954 | } |
| 2955 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2956 | dispatchEntry->resolvedEventId = |
| 2957 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2958 | ? mIdGenerator.nextId() |
| 2959 | : motionEntry.id; |
| 2960 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2961 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2962 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2963 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2964 | ATRACE_NAME(message.c_str()); |
| 2965 | } |
| 2966 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2967 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 2968 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 2969 | // Skip reporting pointer down outside focus to the policy. |
| 2970 | break; |
| 2971 | } |
| 2972 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2973 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2974 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2975 | |
| 2976 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2977 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2978 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2979 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2980 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2981 | break; |
| 2982 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2983 | case EventEntry::Type::SENSOR: { |
| 2984 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 2985 | break; |
| 2986 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2987 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2988 | case EventEntry::Type::DEVICE_RESET: { |
| 2989 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2990 | NamedEnum::string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2991 | break; |
| 2992 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | // Remember that we are waiting for this dispatch to complete. |
| 2996 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2997 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3001 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3002 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3003 | } |
| 3004 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3005 | /** |
| 3006 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3007 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3008 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3009 | * We will see both launcher and wallpaper in that list. |
| 3010 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3011 | * until the set of interacted connections changes. |
| 3012 | * |
| 3013 | * The following items are skipped, to reduce the logspam: |
| 3014 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3015 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3016 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3017 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3018 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3019 | */ |
| 3020 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3021 | const std::vector<InputTarget>& targets) { |
| 3022 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3023 | if (entry.type == EventEntry::Type::KEY) { |
| 3024 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3025 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3026 | return; |
| 3027 | } |
| 3028 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3029 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3030 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3031 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3032 | return; |
| 3033 | } |
| 3034 | } else { |
| 3035 | return; // Not a key or a motion |
| 3036 | } |
| 3037 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3038 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3039 | std::vector<sp<Connection>> newConnections; |
| 3040 | for (const InputTarget& target : targets) { |
| 3041 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3042 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3043 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3044 | } |
| 3045 | |
| 3046 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3047 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3048 | if (connection == nullptr) { |
| 3049 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3050 | } |
| 3051 | newConnectionTokens.insert(std::move(token)); |
| 3052 | newConnections.emplace_back(connection); |
| 3053 | } |
| 3054 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3055 | return; // no change |
| 3056 | } |
| 3057 | mInteractionConnectionTokens = newConnectionTokens; |
| 3058 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3059 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3060 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3061 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3062 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3063 | std::string message = "Interaction with: " + targetList; |
| 3064 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3065 | message += "<none>"; |
| 3066 | } |
| 3067 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3068 | } |
| 3069 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3070 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3071 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3072 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3073 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3074 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3075 | return; |
| 3076 | } |
| 3077 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3078 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3079 | if (focusedToken == token) { |
| 3080 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3084 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 3085 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3086 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3087 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3091 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3092 | if (ATRACE_ENABLED()) { |
| 3093 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3094 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3095 | ATRACE_NAME(message.c_str()); |
| 3096 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3097 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3098 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3099 | #endif |
| 3100 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3101 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3102 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3103 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3104 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3105 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3106 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3107 | |
| 3108 | // Publish the event. |
| 3109 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3110 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3111 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3112 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3113 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3114 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3115 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3116 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3117 | status = connection->inputPublisher |
| 3118 | .publishKeyEvent(dispatchEntry->seq, |
| 3119 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3120 | keyEntry.source, keyEntry.displayId, |
| 3121 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3122 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3123 | keyEntry.scanCode, keyEntry.metaState, |
| 3124 | keyEntry.repeatCount, keyEntry.downTime, |
| 3125 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3126 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3127 | } |
| 3128 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3129 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3130 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3131 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3132 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3133 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3134 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3135 | // 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] | 3136 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3137 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3138 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3139 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3140 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3141 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3142 | // Don't apply window scale here since we don't want scale to affect raw |
| 3143 | // coordinates. The scale will be sent back to the client and applied |
| 3144 | // later when requesting relative coordinates. |
| 3145 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3146 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3147 | } |
| 3148 | usingCoords = scaledCoords; |
| 3149 | } |
| 3150 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3151 | // We don't want the dispatch target to know. |
| 3152 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3153 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3154 | scaledCoords[i].clear(); |
| 3155 | } |
| 3156 | usingCoords = scaledCoords; |
| 3157 | } |
| 3158 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3159 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3160 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3161 | |
| 3162 | // Publish the motion event. |
| 3163 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3164 | .publishMotionEvent(dispatchEntry->seq, |
| 3165 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3166 | motionEntry.deviceId, motionEntry.source, |
| 3167 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3168 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3169 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3170 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3171 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3172 | motionEntry.buttonState, |
| 3173 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3174 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3175 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3176 | motionEntry.xCursorPosition, |
| 3177 | motionEntry.yCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3178 | dispatchEntry->displaySize.x, |
| 3179 | dispatchEntry->displaySize.y, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3180 | motionEntry.downTime, motionEntry.eventTime, |
| 3181 | motionEntry.pointerCount, |
| 3182 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3183 | break; |
| 3184 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3185 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3186 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3187 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3188 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3189 | focusEntry.id, |
| 3190 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3191 | mInTouchMode); |
| 3192 | break; |
| 3193 | } |
| 3194 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3195 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3196 | const auto& captureEntry = |
| 3197 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3198 | status = connection->inputPublisher |
| 3199 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
| 3200 | captureEntry.pointerCaptureEnabled); |
| 3201 | break; |
| 3202 | } |
| 3203 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3204 | case EventEntry::Type::DRAG: { |
| 3205 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3206 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3207 | dragEntry.id, dragEntry.x, |
| 3208 | dragEntry.y, |
| 3209 | dragEntry.isExiting); |
| 3210 | break; |
| 3211 | } |
| 3212 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3213 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3214 | case EventEntry::Type::DEVICE_RESET: |
| 3215 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3216 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3217 | NamedEnum::string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3218 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3219 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | // Check the result. |
| 3223 | if (status) { |
| 3224 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3225 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3226 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3227 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3228 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3229 | "event to it, status=%s(%d)", |
| 3230 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3231 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3232 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3233 | } else { |
| 3234 | // Pipe is full and we are waiting for the app to finish process some events |
| 3235 | // before sending more events to it. |
| 3236 | #if DEBUG_DISPATCH_CYCLE |
| 3237 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3238 | "waiting for the application to catch up", |
| 3239 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3240 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3241 | } |
| 3242 | } else { |
| 3243 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3244 | "status=%s(%d)", |
| 3245 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3246 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3247 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3248 | } |
| 3249 | return; |
| 3250 | } |
| 3251 | |
| 3252 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3253 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3254 | connection->outboundQueue.end(), |
| 3255 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3256 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3257 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3258 | if (connection->responsive) { |
| 3259 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3260 | connection->inputChannel->getConnectionToken()); |
| 3261 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3262 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3266 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3267 | size_t size; |
| 3268 | switch (event.type) { |
| 3269 | case VerifiedInputEvent::Type::KEY: { |
| 3270 | size = sizeof(VerifiedKeyEvent); |
| 3271 | break; |
| 3272 | } |
| 3273 | case VerifiedInputEvent::Type::MOTION: { |
| 3274 | size = sizeof(VerifiedMotionEvent); |
| 3275 | break; |
| 3276 | } |
| 3277 | } |
| 3278 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3279 | return mHmacKeyManager.sign(start, size); |
| 3280 | } |
| 3281 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3282 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3283 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3284 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3285 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3286 | // Only sign events up and down events as the purely move events |
| 3287 | // are tied to their up/down counterparts so signing would be redundant. |
| 3288 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3289 | verifiedEvent.actionMasked = actionMasked; |
| 3290 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3291 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3292 | } |
| 3293 | return INVALID_HMAC; |
| 3294 | } |
| 3295 | |
| 3296 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3297 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3298 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3299 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3300 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3301 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3304 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3305 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3306 | bool handled, nsecs_t consumeTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3307 | #if DEBUG_DISPATCH_CYCLE |
| 3308 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3309 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3310 | #endif |
| 3311 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3312 | if (connection->status == Connection::STATUS_BROKEN || |
| 3313 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3314 | return; |
| 3315 | } |
| 3316 | |
| 3317 | // Notify other system components and prepare to start the next dispatch cycle. |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3318 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3322 | const sp<Connection>& connection, |
| 3323 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3324 | #if DEBUG_DISPATCH_CYCLE |
| 3325 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3326 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | #endif |
| 3328 | |
| 3329 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3330 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3331 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3332 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3333 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3334 | |
| 3335 | // The connection appears to be unrecoverably broken. |
| 3336 | // Ignore already broken or zombie connections. |
| 3337 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3338 | connection->status = Connection::STATUS_BROKEN; |
| 3339 | |
| 3340 | if (notify) { |
| 3341 | // Notify other system components. |
| 3342 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 3343 | } |
| 3344 | } |
| 3345 | } |
| 3346 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3347 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3348 | while (!queue.empty()) { |
| 3349 | DispatchEntry* dispatchEntry = queue.front(); |
| 3350 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3351 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3355 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3356 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3357 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } |
| 3359 | delete dispatchEntry; |
| 3360 | } |
| 3361 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3362 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3363 | std::scoped_lock _l(mLock); |
| 3364 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3365 | if (connection == nullptr) { |
| 3366 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3367 | connectionToken.get(), events); |
| 3368 | return 0; // remove the callback |
| 3369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3371 | bool notify; |
| 3372 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3373 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3374 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3375 | "events=0x%x", |
| 3376 | connection->getInputChannelName().c_str(), events); |
| 3377 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3378 | } |
| 3379 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3380 | nsecs_t currentTime = now(); |
| 3381 | bool gotOne = false; |
| 3382 | status_t status = OK; |
| 3383 | for (;;) { |
| 3384 | Result<InputPublisher::ConsumerResponse> result = |
| 3385 | connection->inputPublisher.receiveConsumerResponse(); |
| 3386 | if (!result.ok()) { |
| 3387 | status = result.error().code(); |
| 3388 | break; |
| 3389 | } |
| 3390 | |
| 3391 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3392 | const InputPublisher::Finished& finish = |
| 3393 | std::get<InputPublisher::Finished>(*result); |
| 3394 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3395 | finish.consumeTime); |
| 3396 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3397 | if (shouldReportMetricsForConnection(*connection)) { |
| 3398 | const InputPublisher::Timeline& timeline = |
| 3399 | std::get<InputPublisher::Timeline>(*result); |
| 3400 | mLatencyTracker |
| 3401 | .trackGraphicsLatency(timeline.inputEventId, |
| 3402 | connection->inputChannel->getConnectionToken(), |
| 3403 | std::move(timeline.graphicsTimeline)); |
| 3404 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3405 | } |
| 3406 | gotOne = true; |
| 3407 | } |
| 3408 | if (gotOne) { |
| 3409 | runCommandsLockedInterruptible(); |
| 3410 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3411 | return 1; |
| 3412 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | } |
| 3414 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3415 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3416 | if (notify) { |
| 3417 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3418 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3419 | status); |
| 3420 | } |
| 3421 | } else { |
| 3422 | // Monitor channels are never explicitly unregistered. |
| 3423 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3424 | const bool stillHaveWindowHandle = |
| 3425 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3426 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3427 | if (notify) { |
| 3428 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3429 | connection->getInputChannelName().c_str(), events); |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | // Remove the channel. |
| 3434 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3435 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3436 | } |
| 3437 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3438 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3439 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3440 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3441 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3442 | } |
| 3443 | } |
| 3444 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3445 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3446 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3447 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3448 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3449 | } |
| 3450 | |
| 3451 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3452 | const CancelationOptions& options, |
| 3453 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3454 | for (const auto& it : monitorsByDisplay) { |
| 3455 | const std::vector<Monitor>& monitors = it.second; |
| 3456 | for (const Monitor& monitor : monitors) { |
| 3457 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3458 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3463 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3464 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3465 | if (connection == nullptr) { |
| 3466 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3467 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3468 | |
| 3469 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3473 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3474 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3475 | return; |
| 3476 | } |
| 3477 | |
| 3478 | nsecs_t currentTime = now(); |
| 3479 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3480 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3481 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3483 | if (cancelationEvents.empty()) { |
| 3484 | return; |
| 3485 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3486 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3487 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3488 | "with reality: %s, mode=%d.", |
| 3489 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3490 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3492 | |
| 3493 | InputTarget target; |
| 3494 | sp<InputWindowHandle> windowHandle = |
| 3495 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3496 | if (windowHandle != nullptr) { |
| 3497 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3498 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3499 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3500 | } |
| 3501 | target.inputChannel = connection->inputChannel; |
| 3502 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3503 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3504 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3505 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3506 | switch (cancelationEventEntry->type) { |
| 3507 | case EventEntry::Type::KEY: { |
| 3508 | logOutboundKeyDetails("cancel - ", |
| 3509 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3510 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3511 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3512 | case EventEntry::Type::MOTION: { |
| 3513 | logOutboundMotionDetails("cancel - ", |
| 3514 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3515 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3516 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3517 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3518 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3519 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3520 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3521 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3522 | break; |
| 3523 | } |
| 3524 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3525 | case EventEntry::Type::DEVICE_RESET: |
| 3526 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3527 | 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] | 3528 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3529 | break; |
| 3530 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3531 | } |
| 3532 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3533 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3534 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3535 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3536 | |
| 3537 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3538 | } |
| 3539 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3540 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3541 | const sp<Connection>& connection) { |
| 3542 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3543 | return; |
| 3544 | } |
| 3545 | |
| 3546 | nsecs_t currentTime = now(); |
| 3547 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3548 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3549 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3550 | |
| 3551 | if (downEvents.empty()) { |
| 3552 | return; |
| 3553 | } |
| 3554 | |
| 3555 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3556 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3557 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3558 | #endif |
| 3559 | |
| 3560 | InputTarget target; |
| 3561 | sp<InputWindowHandle> windowHandle = |
| 3562 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3563 | if (windowHandle != nullptr) { |
| 3564 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3565 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3566 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3567 | } |
| 3568 | target.inputChannel = connection->inputChannel; |
| 3569 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3570 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3571 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3572 | switch (downEventEntry->type) { |
| 3573 | case EventEntry::Type::MOTION: { |
| 3574 | logOutboundMotionDetails("down - ", |
| 3575 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3576 | break; |
| 3577 | } |
| 3578 | |
| 3579 | case EventEntry::Type::KEY: |
| 3580 | case EventEntry::Type::FOCUS: |
| 3581 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3582 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3583 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3584 | case EventEntry::Type::SENSOR: |
| 3585 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3586 | 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] | 3587 | NamedEnum::string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3588 | break; |
| 3589 | } |
| 3590 | } |
| 3591 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3592 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3593 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | startDispatchCycleLocked(currentTime, connection); |
| 3597 | } |
| 3598 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3599 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3600 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3601 | ALOG_ASSERT(pointerIds.value != 0); |
| 3602 | |
| 3603 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3604 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3605 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3606 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3607 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3608 | uint32_t splitPointerCount = 0; |
| 3609 | |
| 3610 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3611 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3612 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3613 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3614 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3615 | if (pointerIds.hasBit(pointerId)) { |
| 3616 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3617 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3618 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3619 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | splitPointerCount += 1; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | if (splitPointerCount != pointerIds.count()) { |
| 3625 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3626 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3627 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3628 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3629 | // in this way. |
| 3630 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3631 | "we expected there to be %d pointers. This probably means we received " |
| 3632 | "a broken sequence of pointer ids from the input device.", |
| 3633 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3634 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | } |
| 3636 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3637 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3639 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3640 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3641 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3642 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3643 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3644 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3645 | if (pointerIds.hasBit(pointerId)) { |
| 3646 | if (pointerIds.count() == 1) { |
| 3647 | // The first/last pointer went down/up. |
| 3648 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3649 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3650 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3651 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3652 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3653 | } else { |
| 3654 | // A secondary pointer went down/up. |
| 3655 | uint32_t splitPointerIndex = 0; |
| 3656 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3657 | splitPointerIndex += 1; |
| 3658 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3659 | action = maskedAction | |
| 3660 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3661 | } |
| 3662 | } else { |
| 3663 | // An unrelated pointer changed. |
| 3664 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3665 | } |
| 3666 | } |
| 3667 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3668 | int32_t newId = mIdGenerator.nextId(); |
| 3669 | if (ATRACE_ENABLED()) { |
| 3670 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3671 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3672 | originalMotionEntry.id, newId); |
| 3673 | ATRACE_NAME(message.c_str()); |
| 3674 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3675 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3676 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3677 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3678 | originalMotionEntry.displayId, |
| 3679 | originalMotionEntry.policyFlags, action, |
| 3680 | originalMotionEntry.actionButton, |
| 3681 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3682 | originalMotionEntry.buttonState, |
| 3683 | originalMotionEntry.classification, |
| 3684 | originalMotionEntry.edgeFlags, |
| 3685 | originalMotionEntry.xPrecision, |
| 3686 | originalMotionEntry.yPrecision, |
| 3687 | originalMotionEntry.xCursorPosition, |
| 3688 | originalMotionEntry.yCursorPosition, |
| 3689 | originalMotionEntry.downTime, splitPointerCount, |
| 3690 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3691 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3692 | if (originalMotionEntry.injectionState) { |
| 3693 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3694 | splitMotionEntry->injectionState->refCount += 1; |
| 3695 | } |
| 3696 | |
| 3697 | return splitMotionEntry; |
| 3698 | } |
| 3699 | |
| 3700 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3701 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3702 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3703 | #endif |
| 3704 | |
| 3705 | bool needWake; |
| 3706 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3707 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3709 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3710 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3711 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3712 | } // release lock |
| 3713 | |
| 3714 | if (needWake) { |
| 3715 | mLooper->wake(); |
| 3716 | } |
| 3717 | } |
| 3718 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3719 | /** |
| 3720 | * If one of the meta shortcuts is detected, process them here: |
| 3721 | * Meta + Backspace -> generate BACK |
| 3722 | * Meta + Enter -> generate HOME |
| 3723 | * This will potentially overwrite keyCode and metaState. |
| 3724 | */ |
| 3725 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3726 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3727 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3728 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3729 | if (keyCode == AKEYCODE_DEL) { |
| 3730 | newKeyCode = AKEYCODE_BACK; |
| 3731 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3732 | newKeyCode = AKEYCODE_HOME; |
| 3733 | } |
| 3734 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3735 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3736 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3737 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3738 | keyCode = newKeyCode; |
| 3739 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3740 | } |
| 3741 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3742 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3743 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3744 | // 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] | 3745 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3746 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3747 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3748 | if (replacementIt != mReplacedKeys.end()) { |
| 3749 | keyCode = replacementIt->second; |
| 3750 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3751 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3752 | } |
| 3753 | } |
| 3754 | } |
| 3755 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3756 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3757 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3758 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3759 | "policyFlags=0x%x, action=0x%x, " |
| 3760 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3761 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3762 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3763 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3764 | #endif |
| 3765 | if (!validateKeyEvent(args->action)) { |
| 3766 | return; |
| 3767 | } |
| 3768 | |
| 3769 | uint32_t policyFlags = args->policyFlags; |
| 3770 | int32_t flags = args->flags; |
| 3771 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3772 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3773 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3774 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3775 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3776 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3777 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3778 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3779 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3780 | metaState |= AMETA_FUNCTION_ON; |
| 3781 | } |
| 3782 | |
| 3783 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3784 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3785 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3786 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3787 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3788 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3789 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3790 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3791 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3793 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3794 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3795 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3796 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3797 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3798 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3799 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3800 | bool needWake; |
| 3801 | { // acquire lock |
| 3802 | mLock.lock(); |
| 3803 | |
| 3804 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3805 | mLock.unlock(); |
| 3806 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3807 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3808 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3809 | return; // event was consumed by the filter |
| 3810 | } |
| 3811 | |
| 3812 | mLock.lock(); |
| 3813 | } |
| 3814 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3815 | std::unique_ptr<KeyEntry> newEntry = |
| 3816 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3817 | args->displayId, policyFlags, args->action, flags, |
| 3818 | keyCode, args->scanCode, metaState, repeatCount, |
| 3819 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3820 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3821 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3822 | mLock.unlock(); |
| 3823 | } // release lock |
| 3824 | |
| 3825 | if (needWake) { |
| 3826 | mLooper->wake(); |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3831 | return mInputFilterEnabled; |
| 3832 | } |
| 3833 | |
| 3834 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3835 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3836 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3837 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3838 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3839 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3840 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3841 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3842 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3843 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3844 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3845 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3846 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3847 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3848 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3849 | "orientation=%f", |
| 3850 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3851 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3852 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3853 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3854 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3855 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3856 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3857 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3858 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3859 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3860 | } |
| 3861 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3862 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3863 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | return; |
| 3865 | } |
| 3866 | |
| 3867 | uint32_t policyFlags = args->policyFlags; |
| 3868 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3869 | |
| 3870 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3871 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3872 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3873 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3874 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3875 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3876 | |
| 3877 | bool needWake; |
| 3878 | { // acquire lock |
| 3879 | mLock.lock(); |
| 3880 | |
| 3881 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3882 | mLock.unlock(); |
| 3883 | |
| 3884 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3885 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3886 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3887 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3888 | args->metaState, args->buttonState, args->classification, transform, |
| 3889 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3890 | args->yCursorPosition, AMOTION_EVENT_INVALID_DISPLAY_SIZE, |
| 3891 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3892 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | |
| 3894 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3895 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3896 | return; // event was consumed by the filter |
| 3897 | } |
| 3898 | |
| 3899 | mLock.lock(); |
| 3900 | } |
| 3901 | |
| 3902 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3903 | std::unique_ptr<MotionEntry> newEntry = |
| 3904 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3905 | args->source, args->displayId, policyFlags, |
| 3906 | args->action, args->actionButton, args->flags, |
| 3907 | args->metaState, args->buttonState, |
| 3908 | args->classification, args->edgeFlags, |
| 3909 | args->xPrecision, args->yPrecision, |
| 3910 | args->xCursorPosition, args->yCursorPosition, |
| 3911 | args->downTime, args->pointerCount, |
| 3912 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3913 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3914 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3915 | mLock.unlock(); |
| 3916 | } // release lock |
| 3917 | |
| 3918 | if (needWake) { |
| 3919 | mLooper->wake(); |
| 3920 | } |
| 3921 | } |
| 3922 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3923 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
| 3924 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3925 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3926 | " sensorType=%s", |
| 3927 | args->id, args->eventTime, args->deviceId, args->source, |
| 3928 | NamedEnum::string(args->sensorType).c_str()); |
| 3929 | #endif |
| 3930 | |
| 3931 | bool needWake; |
| 3932 | { // acquire lock |
| 3933 | mLock.lock(); |
| 3934 | |
| 3935 | // Just enqueue a new sensor event. |
| 3936 | std::unique_ptr<SensorEntry> newEntry = |
| 3937 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 3938 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 3939 | args->sensorType, args->accuracy, |
| 3940 | args->accuracyChanged, args->values); |
| 3941 | |
| 3942 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 3943 | mLock.unlock(); |
| 3944 | } // release lock |
| 3945 | |
| 3946 | if (needWake) { |
| 3947 | mLooper->wake(); |
| 3948 | } |
| 3949 | } |
| 3950 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3951 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
| 3952 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3953 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 3954 | args->deviceId, args->isOn); |
| 3955 | #endif |
| 3956 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 3957 | } |
| 3958 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3960 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3964 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3965 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3966 | "switchMask=0x%08x", |
| 3967 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | #endif |
| 3969 | |
| 3970 | uint32_t policyFlags = args->policyFlags; |
| 3971 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3972 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3976 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3977 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3978 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3979 | #endif |
| 3980 | |
| 3981 | bool needWake; |
| 3982 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3983 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3984 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3985 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 3986 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 3987 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | } // release lock |
| 3989 | |
| 3990 | if (needWake) { |
| 3991 | mLooper->wake(); |
| 3992 | } |
| 3993 | } |
| 3994 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3995 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
| 3996 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3997 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
| 3998 | args->enabled ? "true" : "false"); |
| 3999 | #endif |
| 4000 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4001 | bool needWake; |
| 4002 | { // acquire lock |
| 4003 | std::scoped_lock _l(mLock); |
| 4004 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
| 4005 | args->enabled); |
| 4006 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4007 | } // release lock |
| 4008 | |
| 4009 | if (needWake) { |
| 4010 | mLooper->wake(); |
| 4011 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4014 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4015 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4016 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4017 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 4018 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4019 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4020 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4021 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4022 | 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] | 4023 | |
| 4024 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4025 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4026 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4027 | } |
| 4028 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4029 | // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4030 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4031 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4032 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4033 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4034 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4035 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4036 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4037 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4040 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4041 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4042 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4043 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4044 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4045 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4046 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4047 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4048 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4049 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4050 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4051 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4052 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4053 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4054 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4055 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4056 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4057 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4058 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4059 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4060 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4061 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4062 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4063 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4064 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4065 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4066 | |
| 4067 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4068 | android::base::Timer t; |
| 4069 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4070 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4071 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4072 | std::to_string(t.duration().count()).c_str()); |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4077 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4078 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4079 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4080 | incomingKey.getDisplayId(), policyFlags, action, |
| 4081 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4082 | incomingKey.getRepeatCount(), |
| 4083 | incomingKey.getDownTime()); |
| 4084 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4085 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4086 | } |
| 4087 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4088 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4089 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 4090 | int32_t action = motionEvent.getAction(); |
| 4091 | size_t pointerCount = motionEvent.getPointerCount(); |
| 4092 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
| 4093 | int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4094 | int32_t flags = motionEvent.getFlags(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4095 | int32_t displayId = motionEvent.getDisplayId(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4096 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4097 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4101 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4102 | android::base::Timer t; |
| 4103 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4104 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4105 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4106 | std::to_string(t.duration().count()).c_str()); |
| 4107 | } |
| 4108 | } |
| 4109 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4110 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4111 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4112 | } |
| 4113 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4114 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4115 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4116 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4117 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4118 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4119 | resolvedDeviceId, motionEvent.getSource(), |
| 4120 | motionEvent.getDisplayId(), policyFlags, action, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4121 | actionButton, flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4122 | motionEvent.getButtonState(), |
| 4123 | motionEvent.getClassification(), |
| 4124 | motionEvent.getEdgeFlags(), |
| 4125 | motionEvent.getXPrecision(), |
| 4126 | motionEvent.getYPrecision(), |
| 4127 | motionEvent.getRawXCursorPosition(), |
| 4128 | motionEvent.getRawYCursorPosition(), |
| 4129 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4130 | pointerProperties, samplePointerCoords, |
| 4131 | motionEvent.getXOffset(), |
| 4132 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4133 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4134 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4135 | sampleEventTimes += 1; |
| 4136 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4137 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4138 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4139 | resolvedDeviceId, motionEvent.getSource(), |
| 4140 | motionEvent.getDisplayId(), policyFlags, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4141 | action, actionButton, flags, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4142 | motionEvent.getMetaState(), |
| 4143 | motionEvent.getButtonState(), |
| 4144 | motionEvent.getClassification(), |
| 4145 | motionEvent.getEdgeFlags(), |
| 4146 | motionEvent.getXPrecision(), |
| 4147 | motionEvent.getYPrecision(), |
| 4148 | motionEvent.getRawXCursorPosition(), |
| 4149 | motionEvent.getRawYCursorPosition(), |
| 4150 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4151 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4152 | samplePointerCoords, motionEvent.getXOffset(), |
| 4153 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4154 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4155 | } |
| 4156 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4158 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4159 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4160 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4161 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4165 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 | injectionState->injectionIsAsync = true; |
| 4167 | } |
| 4168 | |
| 4169 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4170 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4171 | |
| 4172 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4173 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4174 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4175 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | mLock.unlock(); |
| 4179 | |
| 4180 | if (needWake) { |
| 4181 | mLooper->wake(); |
| 4182 | } |
| 4183 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4184 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4185 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4186 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4188 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4189 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4190 | } else { |
| 4191 | for (;;) { |
| 4192 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4193 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | break; |
| 4195 | } |
| 4196 | |
| 4197 | nsecs_t remainingTimeout = endTime - now(); |
| 4198 | if (remainingTimeout <= 0) { |
| 4199 | #if DEBUG_INJECTION |
| 4200 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4201 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4203 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4204 | break; |
| 4205 | } |
| 4206 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4207 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4210 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4211 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | while (injectionState->pendingForegroundDispatches != 0) { |
| 4213 | #if DEBUG_INJECTION |
| 4214 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4215 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | #endif |
| 4217 | nsecs_t remainingTimeout = endTime - now(); |
| 4218 | if (remainingTimeout <= 0) { |
| 4219 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4220 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4221 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4222 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4223 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4224 | break; |
| 4225 | } |
| 4226 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4227 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4228 | } |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | injectionState->release(); |
| 4233 | } // release lock |
| 4234 | |
| 4235 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4236 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4237 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | #endif |
| 4239 | |
| 4240 | return injectionResult; |
| 4241 | } |
| 4242 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4243 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4244 | std::array<uint8_t, 32> calculatedHmac; |
| 4245 | std::unique_ptr<VerifiedInputEvent> result; |
| 4246 | switch (event.getType()) { |
| 4247 | case AINPUT_EVENT_TYPE_KEY: { |
| 4248 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4249 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4250 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4251 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4252 | break; |
| 4253 | } |
| 4254 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4255 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4256 | VerifiedMotionEvent verifiedMotionEvent = |
| 4257 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4258 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4259 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4260 | break; |
| 4261 | } |
| 4262 | default: { |
| 4263 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4264 | return nullptr; |
| 4265 | } |
| 4266 | } |
| 4267 | if (calculatedHmac == INVALID_HMAC) { |
| 4268 | return nullptr; |
| 4269 | } |
| 4270 | if (calculatedHmac != event.getHmac()) { |
| 4271 | return nullptr; |
| 4272 | } |
| 4273 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4274 | } |
| 4275 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4276 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4277 | return injectorUid == 0 || |
| 4278 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4279 | } |
| 4280 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4281 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4282 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4283 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4284 | if (injectionState) { |
| 4285 | #if DEBUG_INJECTION |
| 4286 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4287 | "injectorPid=%d, injectorUid=%d", |
| 4288 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4289 | #endif |
| 4290 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4291 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4292 | // Log the outcome since the injector did not wait for the injection result. |
| 4293 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4294 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4295 | ALOGV("Asynchronous input event injection succeeded."); |
| 4296 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4297 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4298 | ALOGW("Asynchronous input event injection failed."); |
| 4299 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4300 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4301 | ALOGW("Asynchronous input event injection permission denied."); |
| 4302 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4303 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4304 | ALOGW("Asynchronous input event injection timed out."); |
| 4305 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4306 | case InputEventInjectionResult::PENDING: |
| 4307 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4308 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4313 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4314 | } |
| 4315 | } |
| 4316 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4317 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4318 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4319 | if (injectionState) { |
| 4320 | injectionState->pendingForegroundDispatches += 1; |
| 4321 | } |
| 4322 | } |
| 4323 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4324 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4325 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4326 | if (injectionState) { |
| 4327 | injectionState->pendingForegroundDispatches -= 1; |
| 4328 | |
| 4329 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4330 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 | } |
| 4332 | } |
| 4333 | } |
| 4334 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4335 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4336 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4337 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 4338 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4339 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4340 | } |
| 4341 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4342 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4343 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4344 | if (windowHandleToken == nullptr) { |
| 4345 | return nullptr; |
| 4346 | } |
| 4347 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4348 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4349 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4350 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4351 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4352 | return windowHandle; |
| 4353 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4354 | } |
| 4355 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4356 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | } |
| 4358 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4359 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4360 | int displayId) const { |
| 4361 | if (windowHandleToken == nullptr) { |
| 4362 | return nullptr; |
| 4363 | } |
| 4364 | |
| 4365 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 4366 | if (windowHandle->getToken() == windowHandleToken) { |
| 4367 | return windowHandle; |
| 4368 | } |
| 4369 | } |
| 4370 | return nullptr; |
| 4371 | } |
| 4372 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4373 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
| 4374 | const sp<InputWindowHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4375 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4376 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4377 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4378 | if (handle->getId() == windowHandle->getId() && |
| 4379 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4380 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4381 | ALOGE("Found window %s in display %" PRId32 |
| 4382 | ", but it should belong to display %" PRId32, |
| 4383 | windowHandle->getName().c_str(), it.first, |
| 4384 | windowHandle->getInfo()->displayId); |
| 4385 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4386 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4388 | } |
| 4389 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4390 | return nullptr; |
| 4391 | } |
| 4392 | |
| 4393 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 4394 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4395 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | } |
| 4397 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4398 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 4399 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4400 | const bool noInputChannel = |
| 4401 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4402 | if (connection != nullptr && noInputChannel) { |
| 4403 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4404 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4405 | return false; |
| 4406 | } |
| 4407 | |
| 4408 | if (connection == nullptr) { |
| 4409 | if (!noInputChannel) { |
| 4410 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4411 | } |
| 4412 | return false; |
| 4413 | } |
| 4414 | if (!connection->responsive) { |
| 4415 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4416 | return false; |
| 4417 | } |
| 4418 | return true; |
| 4419 | } |
| 4420 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4421 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4422 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4423 | auto connectionIt = mConnectionsByToken.find(token); |
| 4424 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4425 | return nullptr; |
| 4426 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4427 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4430 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 4431 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 4432 | if (inputWindowHandles.empty()) { |
| 4433 | // Remove all handles on a display if there are no windows left. |
| 4434 | mWindowHandlesByDisplay.erase(displayId); |
| 4435 | return; |
| 4436 | } |
| 4437 | |
| 4438 | // Since we compare the pointer of input window handles across window updates, we need |
| 4439 | // to make sure the handle object for the same window stays unchanged across updates. |
| 4440 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4441 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4442 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4443 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | std::vector<sp<InputWindowHandle>> newHandles; |
| 4447 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 4448 | if (!handle->updateInfo()) { |
| 4449 | // handle no longer valid |
| 4450 | continue; |
| 4451 | } |
| 4452 | |
| 4453 | const InputWindowInfo* info = handle->getInfo(); |
| 4454 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 4455 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 4456 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4457 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4458 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 4459 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4460 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4461 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4462 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4463 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4464 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | if (info->displayId != displayId) { |
| 4468 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4469 | handle->getName().c_str(), displayId, info->displayId); |
| 4470 | continue; |
| 4471 | } |
| 4472 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4473 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4474 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4475 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4476 | oldHandle->updateFrom(handle); |
| 4477 | newHandles.push_back(oldHandle); |
| 4478 | } else { |
| 4479 | newHandles.push_back(handle); |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | // Insert or replace |
| 4484 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4485 | } |
| 4486 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4487 | void InputDispatcher::setInputWindows( |
| 4488 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 4489 | { // acquire lock |
| 4490 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4491 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4492 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4496 | mLooper->wake(); |
| 4497 | } |
| 4498 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4499 | /** |
| 4500 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4501 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4502 | * If set an empty list, remove all handles from the specific display. |
| 4503 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4504 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4505 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4506 | void InputDispatcher::setInputWindowsLocked( |
| 4507 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4508 | if (DEBUG_FOCUS) { |
| 4509 | std::string windowList; |
| 4510 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 4511 | windowList += iwh->getName() + " "; |
| 4512 | } |
| 4513 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4514 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4515 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4516 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4517 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 4518 | const bool noInputWindow = |
| 4519 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4520 | if (noInputWindow && window->getToken() != nullptr) { |
| 4521 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4522 | window->getName().c_str()); |
| 4523 | window->releaseChannel(); |
| 4524 | } |
| 4525 | } |
| 4526 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4527 | // Copy old handles for release if they are no longer present. |
| 4528 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4529 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4530 | // Save the old windows' orientation by ID before it gets updated. |
| 4531 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
| 4532 | for (const sp<InputWindowHandle>& handle : oldWindowHandles) { |
| 4533 | oldWindowOrientations.emplace(handle->getId(), |
| 4534 | handle->getInfo()->transform.getOrientation()); |
| 4535 | } |
| 4536 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4537 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4538 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4539 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 4540 | if (mLastHoverWindowHandle && |
| 4541 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4542 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4543 | mLastHoverWindowHandle = nullptr; |
| 4544 | } |
| 4545 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4546 | std::optional<FocusResolver::FocusChanges> changes = |
| 4547 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4548 | if (changes) { |
| 4549 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4550 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4551 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4552 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4553 | mTouchStatesByDisplay.find(displayId); |
| 4554 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4555 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4556 | for (size_t i = 0; i < state.windows.size();) { |
| 4557 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4558 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4559 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4560 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4561 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4562 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4563 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4564 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4565 | if (touchedInputChannel != nullptr) { |
| 4566 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4567 | "touched window was removed"); |
| 4568 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4569 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4570 | state.windows.erase(state.windows.begin() + i); |
| 4571 | } else { |
| 4572 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4573 | } |
| 4574 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4575 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4576 | // 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] | 4577 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4578 | if (mDragState && |
| 4579 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4580 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4581 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4582 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4583 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4584 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4585 | if (isPerWindowInputRotationEnabled()) { |
| 4586 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4587 | // pointer events if necessary. |
| 4588 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
| 4589 | const sp<InputWindowHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4590 | if (newWindowHandle != nullptr && |
| 4591 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4592 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4593 | std::shared_ptr<InputChannel> inputChannel = |
| 4594 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4595 | if (inputChannel != nullptr) { |
| 4596 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4597 | "touched window's orientation changed"); |
| 4598 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4599 | } |
| 4600 | } |
| 4601 | } |
| 4602 | } |
| 4603 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4604 | // Release information for windows that are no longer present. |
| 4605 | // This ensures that unused input channels are released promptly. |
| 4606 | // Otherwise, they might stick around until the window handle is destroyed |
| 4607 | // which might not happen until the next GC. |
| 4608 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4609 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4610 | if (DEBUG_FOCUS) { |
| 4611 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4612 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4613 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4614 | // To avoid making too many calls into the compat framework, only |
| 4615 | // check for window flags when windows are going away. |
| 4616 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4617 | // in order to gather some data about the flag usage |
| 4618 | if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { |
| 4619 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4620 | oldWindowHandle->getName().c_str()); |
| 4621 | if (mCompatService != nullptr) { |
| 4622 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4623 | oldWindowHandle->getInfo()->ownerUid); |
| 4624 | } |
| 4625 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4626 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4627 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4631 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4632 | if (DEBUG_FOCUS) { |
| 4633 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4634 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4635 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4636 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4637 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4638 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4639 | } // release lock |
| 4640 | |
| 4641 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4642 | mLooper->wake(); |
| 4643 | } |
| 4644 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4645 | void InputDispatcher::setFocusedApplicationLocked( |
| 4646 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4647 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4648 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4649 | |
| 4650 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4651 | return; // This application is already focused. No need to wake up or change anything. |
| 4652 | } |
| 4653 | |
| 4654 | // Set the new application handle. |
| 4655 | if (inputApplicationHandle != nullptr) { |
| 4656 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4657 | } else { |
| 4658 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4659 | } |
| 4660 | |
| 4661 | // No matter what the old focused application was, stop waiting on it because it is |
| 4662 | // no longer focused. |
| 4663 | resetNoFocusedWindowTimeoutLocked(); |
| 4664 | } |
| 4665 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4666 | /** |
| 4667 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4668 | * the display not specified. |
| 4669 | * |
| 4670 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4671 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4672 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4673 | * display. The display-specified events won't be affected. |
| 4674 | */ |
| 4675 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4676 | if (DEBUG_FOCUS) { |
| 4677 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4678 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4679 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4680 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4681 | |
| 4682 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4683 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4684 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4685 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4686 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4687 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4688 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4689 | CancelationOptions |
| 4690 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4691 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4692 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4693 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4694 | } |
| 4695 | } |
| 4696 | mFocusedDisplayId = displayId; |
| 4697 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4698 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4699 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4700 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4701 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4702 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4703 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4704 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4705 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4706 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4707 | } |
| 4708 | } |
| 4709 | } |
| 4710 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4711 | if (DEBUG_FOCUS) { |
| 4712 | logDispatchStateLocked(); |
| 4713 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4714 | } // release lock |
| 4715 | |
| 4716 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4717 | mLooper->wake(); |
| 4718 | } |
| 4719 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4720 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4721 | if (DEBUG_FOCUS) { |
| 4722 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4723 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4724 | |
| 4725 | bool changed; |
| 4726 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4727 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4728 | |
| 4729 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4730 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4731 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | if (mDispatchEnabled && !enabled) { |
| 4735 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4736 | } |
| 4737 | |
| 4738 | mDispatchEnabled = enabled; |
| 4739 | mDispatchFrozen = frozen; |
| 4740 | changed = true; |
| 4741 | } else { |
| 4742 | changed = false; |
| 4743 | } |
| 4744 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4745 | if (DEBUG_FOCUS) { |
| 4746 | logDispatchStateLocked(); |
| 4747 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4748 | } // release lock |
| 4749 | |
| 4750 | if (changed) { |
| 4751 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4752 | mLooper->wake(); |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4757 | if (DEBUG_FOCUS) { |
| 4758 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4759 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4760 | |
| 4761 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4762 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4763 | |
| 4764 | if (mInputFilterEnabled == enabled) { |
| 4765 | return; |
| 4766 | } |
| 4767 | |
| 4768 | mInputFilterEnabled = enabled; |
| 4769 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4770 | } // release lock |
| 4771 | |
| 4772 | // Wake up poll loop since there might be work to do to drop everything. |
| 4773 | mLooper->wake(); |
| 4774 | } |
| 4775 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4776 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4777 | std::scoped_lock lock(mLock); |
| 4778 | mInTouchMode = inTouchMode; |
| 4779 | } |
| 4780 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4781 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4782 | if (opacity < 0 || opacity > 1) { |
| 4783 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4784 | return; |
| 4785 | } |
| 4786 | |
| 4787 | std::scoped_lock lock(mLock); |
| 4788 | mMaximumObscuringOpacityForTouch = opacity; |
| 4789 | } |
| 4790 | |
| 4791 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4792 | std::scoped_lock lock(mLock); |
| 4793 | mBlockUntrustedTouchesMode = mode; |
| 4794 | } |
| 4795 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4796 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4797 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4798 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4799 | if (DEBUG_FOCUS) { |
| 4800 | ALOGD("Trivial transfer to same window."); |
| 4801 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4802 | return true; |
| 4803 | } |
| 4804 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4805 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4806 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4807 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4808 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4809 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4810 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4811 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4812 | return false; |
| 4813 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4814 | if (DEBUG_FOCUS) { |
| 4815 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4816 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4817 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4818 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4819 | if (DEBUG_FOCUS) { |
| 4820 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4821 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4822 | return false; |
| 4823 | } |
| 4824 | |
| 4825 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4826 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4827 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4828 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4829 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4830 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4831 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4832 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4833 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4834 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4835 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4836 | int32_t newTargetFlags = oldTargetFlags & |
| 4837 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4838 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4839 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4840 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4841 | // Store the dragging window. |
| 4842 | if (isDragDrop) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4843 | mDragState = std::make_unique<DragState>(toWindowHandle); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4844 | } |
| 4845 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4846 | found = true; |
| 4847 | goto Found; |
| 4848 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4849 | } |
| 4850 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4851 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4852 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4853 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4854 | if (DEBUG_FOCUS) { |
| 4855 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4856 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4857 | return false; |
| 4858 | } |
| 4859 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4860 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4861 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4862 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4863 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4864 | CancelationOptions |
| 4865 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4866 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4868 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4869 | } |
| 4870 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4871 | if (DEBUG_FOCUS) { |
| 4872 | logDispatchStateLocked(); |
| 4873 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4874 | } // release lock |
| 4875 | |
| 4876 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4877 | mLooper->wake(); |
| 4878 | return true; |
| 4879 | } |
| 4880 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4881 | // Binder call |
| 4882 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 4883 | sp<IBinder> fromToken; |
| 4884 | { // acquire lock |
| 4885 | std::scoped_lock _l(mLock); |
| 4886 | |
| 4887 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); |
| 4888 | if (toWindowHandle == nullptr) { |
| 4889 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 4890 | return false; |
| 4891 | } |
| 4892 | |
| 4893 | const int32_t displayId = toWindowHandle->getInfo()->displayId; |
| 4894 | |
| 4895 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 4896 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 4897 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", |
| 4898 | displayId); |
| 4899 | return false; |
| 4900 | } |
| 4901 | |
| 4902 | TouchState& state = touchStateIt->second; |
| 4903 | if (state.windows.size() != 1) { |
| 4904 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", |
| 4905 | state.windows.size()); |
| 4906 | return false; |
| 4907 | } |
| 4908 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 4909 | fromToken = touchedWindow.windowHandle->getToken(); |
| 4910 | } // release lock |
| 4911 | |
| 4912 | return transferTouchFocus(fromToken, destChannelToken); |
| 4913 | } |
| 4914 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4915 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4916 | if (DEBUG_FOCUS) { |
| 4917 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4918 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4919 | |
| 4920 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4921 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4922 | |
| 4923 | resetKeyRepeatLocked(); |
| 4924 | releasePendingEventLocked(); |
| 4925 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4926 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4927 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4928 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4929 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4930 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4931 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4935 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4936 | dumpDispatchStateLocked(dump); |
| 4937 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4938 | std::istringstream stream(dump); |
| 4939 | std::string line; |
| 4940 | |
| 4941 | while (std::getline(stream, line, '\n')) { |
| 4942 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4943 | } |
| 4944 | } |
| 4945 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4946 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 4947 | std::string dump; |
| 4948 | |
| 4949 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", |
| 4950 | toString(mFocusedWindowRequestedPointerCapture)); |
| 4951 | |
| 4952 | std::string windowName = "None"; |
| 4953 | if (mWindowTokenWithPointerCapture) { |
| 4954 | const sp<InputWindowHandle> captureWindowHandle = |
| 4955 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 4956 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 4957 | : "token has capture without window"; |
| 4958 | } |
| 4959 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); |
| 4960 | |
| 4961 | return dump; |
| 4962 | } |
| 4963 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4964 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4965 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4966 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4967 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4968 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4969 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4970 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4971 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4972 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4973 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4974 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4975 | const std::chrono::duration timeout = |
| 4976 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4977 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4978 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4979 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4980 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4981 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4982 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4983 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4984 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4985 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4986 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4987 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4988 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4989 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4990 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4991 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4992 | 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] | 4993 | state.displayId, toString(state.down), toString(state.split), |
| 4994 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4995 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4996 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4997 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4998 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4999 | dump += StringPrintf(INDENT4 |
| 5000 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5001 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5002 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5003 | } |
| 5004 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5005 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5006 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5007 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 5008 | dump += INDENT3 "Portal windows:\n"; |
| 5009 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5010 | const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5011 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 5012 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 5013 | } |
| 5014 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5015 | } |
| 5016 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5017 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5018 | } |
| 5019 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5020 | if (mDragState) { |
| 5021 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5022 | mDragState->dump(dump, INDENT2); |
| 5023 | } |
| 5024 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5025 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5026 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5027 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5028 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5029 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5030 | dump += INDENT2 "Windows:\n"; |
| 5031 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5032 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5033 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5034 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5035 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5036 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5037 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5038 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5039 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5040 | "applicationInfo.name=%s, " |
| 5041 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5042 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5043 | i, windowInfo->name.c_str(), windowInfo->id, |
| 5044 | windowInfo->displayId, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5045 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5046 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5047 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5048 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5049 | windowInfo->flags.string().c_str(), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5050 | NamedEnum::string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5051 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5052 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5053 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5054 | windowInfo->applicationInfo.name.c_str(), |
| 5055 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5056 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5057 | dump += StringPrintf(", inputFeatures=%s", |
| 5058 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5059 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5060 | "ms, trustedOverlay=%s, hasToken=%s, " |
| 5061 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5062 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5063 | millis(windowInfo->dispatchingTimeout), |
| 5064 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5065 | toString(windowInfo->token != nullptr), |
| 5066 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5067 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5068 | } |
| 5069 | } else { |
| 5070 | dump += INDENT2 "Windows: <none>\n"; |
| 5071 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5072 | } |
| 5073 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5074 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5075 | } |
| 5076 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5077 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5078 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5079 | const std::vector<Monitor>& monitors = it.second; |
| 5080 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5081 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5082 | } |
| 5083 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5084 | const std::vector<Monitor>& monitors = it.second; |
| 5085 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5086 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5087 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5088 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5089 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | nsecs_t currentTime = now(); |
| 5093 | |
| 5094 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5095 | if (!mRecentQueue.empty()) { |
| 5096 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5097 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5098 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5099 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5100 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5101 | } |
| 5102 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5103 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | // Dump event currently being dispatched. |
| 5107 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5108 | dump += INDENT "PendingEvent:\n"; |
| 5109 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5110 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5111 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5112 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5113 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5114 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5118 | if (!mInboundQueue.empty()) { |
| 5119 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5120 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5121 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5122 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5123 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5124 | } |
| 5125 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5126 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5129 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5130 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5131 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5132 | const KeyReplacement& replacement = pair.first; |
| 5133 | int32_t newKeyCode = pair.second; |
| 5134 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5135 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5136 | } |
| 5137 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5138 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5141 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5142 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5143 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5144 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5145 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5146 | connection->inputChannel->getFd().get(), |
| 5147 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5148 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5149 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5150 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5151 | if (!connection->outboundQueue.empty()) { |
| 5152 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5153 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5154 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5155 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5156 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5157 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5158 | } |
| 5159 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5160 | if (!connection->waitQueue.empty()) { |
| 5161 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5162 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5163 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5164 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5165 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5169 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5173 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5174 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5175 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5176 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | } |
| 5178 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5179 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5180 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5181 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5182 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5183 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5184 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5185 | } |
| 5186 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5187 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5188 | const size_t numMonitors = monitors.size(); |
| 5189 | for (size_t i = 0; i < numMonitors; i++) { |
| 5190 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5191 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5192 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5193 | dump += "\n"; |
| 5194 | } |
| 5195 | } |
| 5196 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5197 | class LooperEventCallback : public LooperCallback { |
| 5198 | public: |
| 5199 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5200 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5201 | |
| 5202 | private: |
| 5203 | std::function<int(int events)> mCallback; |
| 5204 | }; |
| 5205 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5206 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5207 | #if DEBUG_CHANNEL_CREATION |
| 5208 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5209 | #endif |
| 5210 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5211 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5212 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5213 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5214 | |
| 5215 | if (result) { |
| 5216 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5217 | } |
| 5218 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5219 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5220 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5221 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5222 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5223 | sp<Connection> connection = |
| 5224 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5225 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5226 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5227 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5228 | } |
| 5229 | mConnectionsByToken.emplace(token, connection); |
| 5230 | |
| 5231 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5232 | this, std::placeholders::_1, token); |
| 5233 | |
| 5234 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5235 | } // release lock |
| 5236 | |
| 5237 | // Wake the looper because some connections have changed. |
| 5238 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5239 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5240 | } |
| 5241 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5242 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5243 | bool isGestureMonitor, |
| 5244 | const std::string& name, |
| 5245 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5246 | std::shared_ptr<InputChannel> serverChannel; |
| 5247 | std::unique_ptr<InputChannel> clientChannel; |
| 5248 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5249 | if (result) { |
| 5250 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5251 | } |
| 5252 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5253 | { // acquire lock |
| 5254 | std::scoped_lock _l(mLock); |
| 5255 | |
| 5256 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5257 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5258 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5261 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5262 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5263 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5264 | |
| 5265 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5266 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5267 | } |
| 5268 | mConnectionsByToken.emplace(token, connection); |
| 5269 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5270 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5271 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5272 | auto& monitorsByDisplay = |
| 5273 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5274 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5275 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5276 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5277 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5278 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5279 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5280 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5281 | // Wake the looper because some connections have changed. |
| 5282 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5283 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5286 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5287 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5288 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5289 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5290 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5291 | if (status) { |
| 5292 | return status; |
| 5293 | } |
| 5294 | } // release lock |
| 5295 | |
| 5296 | // Wake the poll loop because removing the connection may have changed the current |
| 5297 | // synchronization state. |
| 5298 | mLooper->wake(); |
| 5299 | return OK; |
| 5300 | } |
| 5301 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5302 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5303 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5304 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5305 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5306 | // 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] | 5307 | return BAD_VALUE; |
| 5308 | } |
| 5309 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5310 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5311 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5313 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5314 | } |
| 5315 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5316 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5317 | |
| 5318 | nsecs_t currentTime = now(); |
| 5319 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5320 | |
| 5321 | connection->status = Connection::STATUS_ZOMBIE; |
| 5322 | return OK; |
| 5323 | } |
| 5324 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5325 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5326 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5327 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5330 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5331 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5332 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5333 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5334 | std::vector<Monitor>& monitors = it->second; |
| 5335 | const size_t numMonitors = monitors.size(); |
| 5336 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5337 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5338 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5339 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5340 | monitors.erase(monitors.begin() + i); |
| 5341 | break; |
| 5342 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5343 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5344 | if (monitors.empty()) { |
| 5345 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5346 | } else { |
| 5347 | ++it; |
| 5348 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5349 | } |
| 5350 | } |
| 5351 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5352 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5353 | { // acquire lock |
| 5354 | std::scoped_lock _l(mLock); |
| 5355 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5356 | |
| 5357 | if (!foundDisplayId) { |
| 5358 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5359 | return BAD_VALUE; |
| 5360 | } |
| 5361 | int32_t displayId = foundDisplayId.value(); |
| 5362 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5363 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5364 | mTouchStatesByDisplay.find(displayId); |
| 5365 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5366 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5367 | return BAD_VALUE; |
| 5368 | } |
| 5369 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5370 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5371 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5372 | std::optional<int32_t> foundDeviceId; |
| 5373 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5374 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5375 | requestingChannel = touchedMonitor.monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5376 | foundDeviceId = state.deviceId; |
| 5377 | } |
| 5378 | } |
| 5379 | if (!foundDeviceId || !state.down) { |
| 5380 | 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] | 5381 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5382 | return BAD_VALUE; |
| 5383 | } |
| 5384 | int32_t deviceId = foundDeviceId.value(); |
| 5385 | |
| 5386 | // Send cancel events to all the input channels we're stealing from. |
| 5387 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5388 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5389 | options.deviceId = deviceId; |
| 5390 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5391 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5392 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5393 | std::shared_ptr<InputChannel> channel = |
| 5394 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5395 | if (channel != nullptr) { |
| 5396 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5397 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5398 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5399 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5400 | canceledWindows += "]"; |
| 5401 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5402 | canceledWindows.c_str()); |
| 5403 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5404 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5405 | state.filterNonMonitors(); |
| 5406 | } |
| 5407 | return OK; |
| 5408 | } |
| 5409 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5410 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5411 | { // acquire lock |
| 5412 | std::scoped_lock _l(mLock); |
| 5413 | if (DEBUG_FOCUS) { |
| 5414 | const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); |
| 5415 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5416 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5417 | : "token without window"); |
| 5418 | } |
| 5419 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5420 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5421 | if (focusedToken != windowToken) { |
| 5422 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5423 | enabled ? "enable" : "disable"); |
| 5424 | return; |
| 5425 | } |
| 5426 | |
| 5427 | if (enabled == mFocusedWindowRequestedPointerCapture) { |
| 5428 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5429 | "window has %s requested pointer capture.", |
| 5430 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5431 | return; |
| 5432 | } |
| 5433 | |
| 5434 | mFocusedWindowRequestedPointerCapture = enabled; |
| 5435 | setPointerCaptureLocked(enabled); |
| 5436 | } // release lock |
| 5437 | |
| 5438 | // Wake the thread to process command entries. |
| 5439 | mLooper->wake(); |
| 5440 | } |
| 5441 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5442 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5443 | const sp<IBinder>& token) { |
| 5444 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5445 | const std::vector<Monitor>& monitors = it.second; |
| 5446 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5447 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5448 | return it.first; |
| 5449 | } |
| 5450 | } |
| 5451 | } |
| 5452 | return std::nullopt; |
| 5453 | } |
| 5454 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5455 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5456 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5457 | if (gesturePid.has_value()) { |
| 5458 | return gesturePid; |
| 5459 | } |
| 5460 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5461 | } |
| 5462 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5463 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5464 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5465 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5466 | } |
| 5467 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5468 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5469 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5470 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5471 | } |
| 5472 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5473 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5474 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5475 | } |
| 5476 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5477 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5478 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5479 | if (connection == nullptr) { |
| 5480 | return "<nullptr>"; |
| 5481 | } |
| 5482 | return connection->getInputChannelName(); |
| 5483 | } |
| 5484 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5485 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5486 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5487 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5488 | } |
| 5489 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5490 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 5491 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5492 | bool handled, nsecs_t consumeTime) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5493 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5494 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5495 | commandEntry->connection = connection; |
| 5496 | commandEntry->eventTime = currentTime; |
| 5497 | commandEntry->seq = seq; |
| 5498 | commandEntry->handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5499 | commandEntry->consumeTime = consumeTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5500 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5501 | } |
| 5502 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5503 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 5504 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5505 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5506 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5507 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5508 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5509 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5510 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5511 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5512 | } |
| 5513 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5514 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 5515 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5516 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5517 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5518 | commandEntry->oldToken = oldToken; |
| 5519 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5520 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5521 | } |
| 5522 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5523 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { |
| 5524 | std::unique_ptr<CommandEntry> commandEntry = |
| 5525 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); |
| 5526 | commandEntry->newToken = token; |
| 5527 | commandEntry->x = x; |
| 5528 | commandEntry->y = y; |
| 5529 | postCommandLocked(std::move(commandEntry)); |
| 5530 | } |
| 5531 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5532 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5533 | if (connection == nullptr) { |
| 5534 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5535 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5536 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5537 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5538 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5539 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5540 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5541 | return; |
| 5542 | } |
| 5543 | /** |
| 5544 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5545 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5546 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5547 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5548 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5549 | * most useful. |
| 5550 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5551 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5552 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5553 | std::string reason = |
| 5554 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5555 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5556 | ns2ms(currentWait), |
| 5557 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5558 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5559 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5560 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5561 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5562 | |
| 5563 | // Stop waking up for events on this connection, it is already unresponsive |
| 5564 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5565 | } |
| 5566 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5567 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5568 | std::string reason = |
| 5569 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5570 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5571 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5572 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5573 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); |
| 5574 | commandEntry->inputApplicationHandle = std::move(application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5575 | postCommandLocked(std::move(commandEntry)); |
| 5576 | } |
| 5577 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5578 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 5579 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5580 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 5581 | commandEntry->obscuringPackage = obscuringPackage; |
| 5582 | postCommandLocked(std::move(commandEntry)); |
| 5583 | } |
| 5584 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5585 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 5586 | const std::string& reason) { |
| 5587 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5588 | updateLastAnrStateLocked(windowLabel, reason); |
| 5589 | } |
| 5590 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5591 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5592 | const std::string& reason) { |
| 5593 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5594 | updateLastAnrStateLocked(windowLabel, reason); |
| 5595 | } |
| 5596 | |
| 5597 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5598 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5599 | // 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] | 5600 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5601 | struct tm tm; |
| 5602 | localtime_r(&t, &tm); |
| 5603 | char timestr[64]; |
| 5604 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5605 | mLastAnrState.clear(); |
| 5606 | mLastAnrState += INDENT "ANR:\n"; |
| 5607 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5608 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5609 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5610 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5611 | } |
| 5612 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5613 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5614 | mLock.unlock(); |
| 5615 | |
| 5616 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 5617 | |
| 5618 | mLock.lock(); |
| 5619 | } |
| 5620 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5621 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5622 | sp<Connection> connection = commandEntry->connection; |
| 5623 | |
| 5624 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 5625 | mLock.unlock(); |
| 5626 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5627 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5628 | |
| 5629 | mLock.lock(); |
| 5630 | } |
| 5631 | } |
| 5632 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5633 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5634 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 5635 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5636 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5637 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5638 | mLock.lock(); |
| 5639 | } |
| 5640 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5641 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { |
| 5642 | sp<IBinder> newToken = commandEntry->newToken; |
| 5643 | mLock.unlock(); |
| 5644 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); |
| 5645 | mLock.lock(); |
| 5646 | } |
| 5647 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5648 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5649 | mLock.unlock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5650 | |
| 5651 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); |
| 5652 | |
| 5653 | mLock.lock(); |
| 5654 | } |
| 5655 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5656 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5657 | mLock.unlock(); |
| 5658 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5659 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5660 | |
| 5661 | mLock.lock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5662 | } |
| 5663 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5664 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5665 | mLock.unlock(); |
| 5666 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5667 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); |
| 5668 | |
| 5669 | mLock.lock(); |
| 5670 | } |
| 5671 | |
| 5672 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5673 | mLock.unlock(); |
| 5674 | |
| 5675 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); |
| 5676 | |
| 5677 | mLock.lock(); |
| 5678 | } |
| 5679 | |
| 5680 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5681 | mLock.unlock(); |
| 5682 | |
| 5683 | mPolicy->notifyMonitorResponsive(commandEntry->pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5684 | |
| 5685 | mLock.lock(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5686 | } |
| 5687 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5688 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 5689 | mLock.unlock(); |
| 5690 | |
| 5691 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 5692 | |
| 5693 | mLock.lock(); |
| 5694 | } |
| 5695 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5696 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5697 | CommandEntry* commandEntry) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5698 | KeyEntry& entry = *(commandEntry->keyEntry); |
| 5699 | KeyEvent event = createKeyEvent(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5700 | |
| 5701 | mLock.unlock(); |
| 5702 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5703 | android::base::Timer t; |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5704 | const sp<IBinder>& token = commandEntry->connectionToken; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5705 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5706 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5707 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5708 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5709 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5710 | |
| 5711 | mLock.lock(); |
| 5712 | |
| 5713 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5714 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5715 | } else if (!delay) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5716 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5717 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5718 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5719 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5720 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5721 | } |
| 5722 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5723 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5724 | mLock.unlock(); |
| 5725 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5726 | mLock.lock(); |
| 5727 | } |
| 5728 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5729 | /** |
| 5730 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5731 | * current time. |
| 5732 | */ |
| 5733 | static bool isConnectionResponsive(const Connection& connection) { |
| 5734 | const nsecs_t currentTime = now(); |
| 5735 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5736 | if (entry->timeoutTime < currentTime) { |
| 5737 | return false; |
| 5738 | } |
| 5739 | } |
| 5740 | return true; |
| 5741 | } |
| 5742 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5743 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5744 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5745 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5746 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5747 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5748 | |
| 5749 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5750 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5751 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5752 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5753 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5754 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5755 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5756 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5757 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5758 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5759 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5760 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5761 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5762 | connection->inputChannel->getConnectionToken(), |
| 5763 | dispatchEntry->deliveryTime, commandEntry->consumeTime, |
| 5764 | finishTime); |
| 5765 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5766 | |
| 5767 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5768 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5769 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5770 | restartEvent = |
| 5771 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5772 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5773 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5774 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5775 | handled); |
| 5776 | } else { |
| 5777 | restartEvent = false; |
| 5778 | } |
| 5779 | |
| 5780 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5781 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5782 | // contents of the wait queue to have been drained, so we need to double-check |
| 5783 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5784 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5785 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5786 | dispatchEntry = *dispatchEntryIt; |
| 5787 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5788 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5789 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5790 | if (!connection->responsive) { |
| 5791 | connection->responsive = isConnectionResponsive(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5792 | if (connection->responsive) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5793 | // The connection was unresponsive, and now it's responsive. |
| 5794 | processConnectionResponsiveLocked(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5795 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5796 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5797 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5798 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5799 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5800 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5801 | } else { |
| 5802 | releaseDispatchEntry(dispatchEntry); |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | // Start the next dispatch cycle for this connection. |
| 5807 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5808 | } |
| 5809 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5810 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
| 5811 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5812 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); |
| 5813 | monitorUnresponsiveCommand->pid = pid; |
| 5814 | monitorUnresponsiveCommand->reason = std::move(reason); |
| 5815 | postCommandLocked(std::move(monitorUnresponsiveCommand)); |
| 5816 | } |
| 5817 | |
| 5818 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, |
| 5819 | std::string reason) { |
| 5820 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5821 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); |
| 5822 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); |
| 5823 | windowUnresponsiveCommand->reason = std::move(reason); |
| 5824 | postCommandLocked(std::move(windowUnresponsiveCommand)); |
| 5825 | } |
| 5826 | |
| 5827 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
| 5828 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( |
| 5829 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); |
| 5830 | monitorResponsiveCommand->pid = pid; |
| 5831 | postCommandLocked(std::move(monitorResponsiveCommand)); |
| 5832 | } |
| 5833 | |
| 5834 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { |
| 5835 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( |
| 5836 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); |
| 5837 | windowResponsiveCommand->connectionToken = std::move(connectionToken); |
| 5838 | postCommandLocked(std::move(windowResponsiveCommand)); |
| 5839 | } |
| 5840 | |
| 5841 | /** |
| 5842 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5843 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5844 | * command entry to the command queue. |
| 5845 | */ |
| 5846 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5847 | std::string reason) { |
| 5848 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5849 | if (connection.monitor) { |
| 5850 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5851 | reason.c_str()); |
| 5852 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5853 | if (!pid.has_value()) { |
| 5854 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5855 | connection.inputChannel->getName().c_str()); |
| 5856 | return; |
| 5857 | } |
| 5858 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5859 | return; |
| 5860 | } |
| 5861 | // If not a monitor, must be a window |
| 5862 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5863 | reason.c_str()); |
| 5864 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5865 | } |
| 5866 | |
| 5867 | /** |
| 5868 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5869 | */ |
| 5870 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5871 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5872 | if (connection.monitor) { |
| 5873 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5874 | if (!pid.has_value()) { |
| 5875 | ALOGE("Could not find responsive monitor for connection %s", |
| 5876 | connection.inputChannel->getName().c_str()); |
| 5877 | return; |
| 5878 | } |
| 5879 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5880 | return; |
| 5881 | } |
| 5882 | // If not a monitor, must be a window |
| 5883 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5884 | } |
| 5885 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5886 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5887 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5888 | KeyEntry& keyEntry, bool handled) { |
| 5889 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5890 | if (!handled) { |
| 5891 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5892 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5893 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5894 | return false; |
| 5895 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5896 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5897 | // Get the fallback key state. |
| 5898 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5899 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5900 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5901 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5902 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5903 | } |
| 5904 | |
| 5905 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5906 | // If the application handles the original key for which we previously |
| 5907 | // generated a fallback or if the window is not a foreground window, |
| 5908 | // then cancel the associated fallback key, if any. |
| 5909 | if (fallbackKeyCode != -1) { |
| 5910 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5911 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5912 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5913 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5914 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5915 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5916 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5917 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5918 | |
| 5919 | mLock.unlock(); |
| 5920 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5921 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5922 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5923 | |
| 5924 | mLock.lock(); |
| 5925 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5926 | // Cancel the fallback key. |
| 5927 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5928 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5929 | "application handled the original non-fallback key " |
| 5930 | "or is no longer a foreground target, " |
| 5931 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5932 | options.keyCode = fallbackKeyCode; |
| 5933 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5934 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5935 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5936 | } |
| 5937 | } else { |
| 5938 | // If the application did not handle a non-fallback key, first check |
| 5939 | // that we are in a good state to perform unhandled key event processing |
| 5940 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5941 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5942 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5943 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5944 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5945 | "since this is not an initial down. " |
| 5946 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5947 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5948 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5949 | return false; |
| 5950 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5951 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5952 | // Dispatch the unhandled key to the policy. |
| 5953 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5954 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5955 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5956 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5957 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5958 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5959 | |
| 5960 | mLock.unlock(); |
| 5961 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5962 | bool fallback = |
| 5963 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5964 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5965 | |
| 5966 | mLock.lock(); |
| 5967 | |
| 5968 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5969 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5970 | return false; |
| 5971 | } |
| 5972 | |
| 5973 | // Latch the fallback keycode for this key on an initial down. |
| 5974 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5975 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5976 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5977 | fallbackKeyCode = event.getKeyCode(); |
| 5978 | } else { |
| 5979 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5980 | } |
| 5981 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5982 | } |
| 5983 | |
| 5984 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5985 | |
| 5986 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5987 | // We will continue to dispatch the key to the policy but we will no |
| 5988 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5989 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5990 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5991 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5992 | if (fallback) { |
| 5993 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5994 | "as a fallback for %d, but on the DOWN it had requested " |
| 5995 | "to send %d instead. Fallback canceled.", |
| 5996 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5997 | } else { |
| 5998 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5999 | "but on the DOWN it had requested to send %d. " |
| 6000 | "Fallback canceled.", |
| 6001 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6002 | } |
| 6003 | #endif |
| 6004 | |
| 6005 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6006 | "canceling fallback, policy no longer desires it"); |
| 6007 | options.keyCode = fallbackKeyCode; |
| 6008 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6009 | |
| 6010 | fallback = false; |
| 6011 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6012 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6013 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6014 | } |
| 6015 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6016 | |
| 6017 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6018 | { |
| 6019 | std::string msg; |
| 6020 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6021 | connection->inputState.getFallbackKeys(); |
| 6022 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6023 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6024 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6025 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6026 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6027 | } |
| 6028 | #endif |
| 6029 | |
| 6030 | if (fallback) { |
| 6031 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6032 | keyEntry.eventTime = event.getEventTime(); |
| 6033 | keyEntry.deviceId = event.getDeviceId(); |
| 6034 | keyEntry.source = event.getSource(); |
| 6035 | keyEntry.displayId = event.getDisplayId(); |
| 6036 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6037 | keyEntry.keyCode = fallbackKeyCode; |
| 6038 | keyEntry.scanCode = event.getScanCode(); |
| 6039 | keyEntry.metaState = event.getMetaState(); |
| 6040 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6041 | keyEntry.downTime = event.getDownTime(); |
| 6042 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6043 | |
| 6044 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 6045 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6046 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6047 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6048 | #endif |
| 6049 | return true; // restart the event |
| 6050 | } else { |
| 6051 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 6052 | ALOGD("Unhandled key event: No fallback key."); |
| 6053 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6054 | |
| 6055 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6056 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6057 | } |
| 6058 | } |
| 6059 | return false; |
| 6060 | } |
| 6061 | |
| 6062 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6063 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6064 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6065 | return false; |
| 6066 | } |
| 6067 | |
| 6068 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 6069 | mLock.unlock(); |
| 6070 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 6071 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, |
| 6072 | commandEntry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6073 | |
| 6074 | mLock.lock(); |
| 6075 | } |
| 6076 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6077 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6078 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6079 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6080 | } |
| 6081 | } |
| 6082 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6083 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6084 | if (ATRACE_ENABLED()) { |
| 6085 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6086 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6087 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6088 | } |
| 6089 | } |
| 6090 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6091 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6092 | if (ATRACE_ENABLED()) { |
| 6093 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6094 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6095 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6096 | } |
| 6097 | } |
| 6098 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6099 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6100 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6101 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6102 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6103 | dumpDispatchStateLocked(dump); |
| 6104 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6105 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6106 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6107 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6108 | } |
| 6109 | } |
| 6110 | |
| 6111 | void InputDispatcher::monitor() { |
| 6112 | // 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] | 6113 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6114 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6115 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6116 | } |
| 6117 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6118 | /** |
| 6119 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6120 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6121 | * this method can be safely called from any thread, as long as you've ensured that |
| 6122 | * the work you are interested in completing has already been queued. |
| 6123 | */ |
| 6124 | bool InputDispatcher::waitForIdle() { |
| 6125 | /** |
| 6126 | * Timeout should represent the longest possible time that a device might spend processing |
| 6127 | * events and commands. |
| 6128 | */ |
| 6129 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6130 | std::unique_lock lock(mLock); |
| 6131 | mLooper->wake(); |
| 6132 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6133 | return result == std::cv_status::no_timeout; |
| 6134 | } |
| 6135 | |
Vishnu Nair | 212dcf4 | 2022-01-27 22:44:01 +0000 | [diff] [blame^] | 6136 | bool InputDispatcher::shouldDropInput(const EventEntry& entry, |
| 6137 | const sp<InputWindowHandle>& windowHandle) const { |
| 6138 | if (windowHandle->getInfo()->inputFeatures.test(InputWindowInfo::Feature::DROP_INPUT)) { |
| 6139 | ALOGW("Dropping %s event targeting %s as requested by inputFeatures={%s} on display " |
| 6140 | "%" PRId32 ".", |
| 6141 | entry.getDescription().c_str(), windowHandle->getName().c_str(), |
| 6142 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6143 | windowHandle->getInfo()->displayId); |
| 6144 | return true; |
| 6145 | } |
| 6146 | return false; |
| 6147 | } |
| 6148 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6149 | /** |
| 6150 | * Sets focus to the window identified by the token. This must be called |
| 6151 | * after updating any input window handles. |
| 6152 | * |
| 6153 | * Params: |
| 6154 | * request.token - input channel token used to identify the window that should gain focus. |
| 6155 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6156 | * specified token does not match the currently focused window, this request will be dropped. |
| 6157 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6158 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6159 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6160 | * when requesting the focus change. This determines which request gets |
| 6161 | * precedence if there is a focus change request from another source such as pointer down. |
| 6162 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6163 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6164 | { // acquire lock |
| 6165 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6166 | std::optional<FocusResolver::FocusChanges> changes = |
| 6167 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6168 | if (changes) { |
| 6169 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6170 | } |
| 6171 | } // release lock |
| 6172 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6173 | mLooper->wake(); |
| 6174 | } |
| 6175 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6176 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6177 | if (changes.oldFocus) { |
| 6178 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6179 | if (focusedInputChannel) { |
| 6180 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6181 | "focus left window"); |
| 6182 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6183 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6184 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6185 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6186 | if (changes.newFocus) { |
| 6187 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6188 | } |
| 6189 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6190 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6191 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6192 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6193 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6194 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6195 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6196 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6197 | // front. |
| 6198 | disablePointerCaptureForcedLocked(); |
| 6199 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6200 | if (mFocusedDisplayId == changes.displayId) { |
| 6201 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6202 | } |
| 6203 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6204 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6205 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
| 6206 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { |
| 6207 | return; |
| 6208 | } |
| 6209 | |
| 6210 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6211 | |
| 6212 | if (mFocusedWindowRequestedPointerCapture) { |
| 6213 | mFocusedWindowRequestedPointerCapture = false; |
| 6214 | setPointerCaptureLocked(false); |
| 6215 | } |
| 6216 | |
| 6217 | if (!mWindowTokenWithPointerCapture) { |
| 6218 | // No need to send capture changes because no window has capture. |
| 6219 | return; |
| 6220 | } |
| 6221 | |
| 6222 | if (mPendingEvent != nullptr) { |
| 6223 | // Move the pending event to the front of the queue. This will give the chance |
| 6224 | // for the pending event to be dropped if it is a captured event. |
| 6225 | mInboundQueue.push_front(mPendingEvent); |
| 6226 | mPendingEvent = nullptr; |
| 6227 | } |
| 6228 | |
| 6229 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
| 6230 | false /* hasCapture */); |
| 6231 | mInboundQueue.push_front(std::move(entry)); |
| 6232 | } |
| 6233 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6234 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { |
| 6235 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 6236 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); |
| 6237 | commandEntry->enabled = enabled; |
| 6238 | postCommandLocked(std::move(commandEntry)); |
| 6239 | } |
| 6240 | |
| 6241 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( |
| 6242 | android::inputdispatcher::CommandEntry* commandEntry) { |
| 6243 | mLock.unlock(); |
| 6244 | |
| 6245 | mPolicy->setPointerCapture(commandEntry->enabled); |
| 6246 | |
| 6247 | mLock.lock(); |
| 6248 | } |
| 6249 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6250 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6251 | { // acquire lock |
| 6252 | std::scoped_lock _l(mLock); |
| 6253 | // Set an empty list to remove all handles from the specific display. |
| 6254 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6255 | setFocusedApplicationLocked(displayId, nullptr); |
| 6256 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6257 | // have been removed for the removed display. |
| 6258 | mFocusResolver.displayRemoved(displayId); |
| 6259 | } // release lock |
| 6260 | |
| 6261 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6262 | mLooper->wake(); |
| 6263 | } |
| 6264 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6265 | } // namespace android::inputdispatcher |