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 | |
Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 50 | #include <InputFlingerProperties.sysprop.h> |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 51 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 52 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 53 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 54 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 55 | #include <binder/Binder.h> |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 56 | #include <binder/IServiceManager.h> |
| 57 | #include <com/android/internal/compat/IPlatformCompatNative.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 58 | #include <input/InputDevice.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 59 | #include <input/InputWindow.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 60 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 61 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 62 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 63 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 64 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 65 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 66 | #include <cerrno> |
| 67 | #include <cinttypes> |
| 68 | #include <climits> |
| 69 | #include <cstddef> |
| 70 | #include <ctime> |
| 71 | #include <queue> |
| 72 | #include <sstream> |
| 73 | |
| 74 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 75 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 76 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | #define INDENT " " |
| 78 | #define INDENT2 " " |
| 79 | #define INDENT3 " " |
| 80 | #define INDENT4 " " |
| 81 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 82 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 83 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 84 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 85 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 86 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 87 | using android::os::InputEventInjectionResult; |
| 88 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 89 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 90 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 91 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 93 | // When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display |
| 94 | // coordinates and SurfaceFlinger includes the display rotation in the input window transforms. |
| 95 | static bool isPerWindowInputRotationEnabled() { |
| 96 | static const bool PER_WINDOW_INPUT_ROTATION = |
Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 97 | sysprop::InputFlingerProperties::per_window_input_rotation().value_or(false); |
| 98 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 99 | return PER_WINDOW_INPUT_ROTATION; |
| 100 | } |
| 101 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 102 | // Default input dispatching timeout if there is no focused application or paused window |
| 103 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 104 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 105 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 106 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | |
| 108 | // Amount of time to allow for all pending events to be processed when an app switch |
| 109 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 110 | // 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] | 111 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | |
| 113 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 114 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 115 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 116 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | // 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] | 118 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 119 | |
| 120 | // Log a warning when an interception call takes longer than this to process. |
| 121 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 122 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 123 | // Additional key latency in case a connection is still processing some motion events. |
| 124 | // This will help with the case when a user touched a button that opens a new window, |
| 125 | // and gives us the chance to dispatch the key to this new window. |
| 126 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 127 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 129 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 130 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 131 | // Event log tags. See EventLogTags.logtags for reference |
| 132 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 133 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 134 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | static inline nsecs_t now() { |
| 136 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 137 | } |
| 138 | |
| 139 | static inline const char* toString(bool value) { |
| 140 | return value ? "true" : "false"; |
| 141 | } |
| 142 | |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 143 | static inline const std::string toString(sp<IBinder> binder) { |
| 144 | if (binder == nullptr) { |
| 145 | return "<null>"; |
| 146 | } |
| 147 | return StringPrintf("%p", binder.get()); |
| 148 | } |
| 149 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 151 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 152 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static bool isValidKeyAction(int32_t action) { |
| 156 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 157 | case AKEY_EVENT_ACTION_DOWN: |
| 158 | case AKEY_EVENT_ACTION_UP: |
| 159 | return true; |
| 160 | default: |
| 161 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 166 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 167 | ALOGE("Key event has invalid action code 0x%x", action); |
| 168 | return false; |
| 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 173 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 174 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 175 | case AMOTION_EVENT_ACTION_DOWN: |
| 176 | case AMOTION_EVENT_ACTION_UP: |
| 177 | case AMOTION_EVENT_ACTION_CANCEL: |
| 178 | case AMOTION_EVENT_ACTION_MOVE: |
| 179 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 180 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 181 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 182 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 183 | case AMOTION_EVENT_ACTION_SCROLL: |
| 184 | return true; |
| 185 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 186 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 187 | int32_t index = getMotionEventActionPointerIndex(action); |
| 188 | return index >= 0 && index < pointerCount; |
| 189 | } |
| 190 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 191 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 192 | return actionButton != 0; |
| 193 | default: |
| 194 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 198 | static int64_t millis(std::chrono::nanoseconds t) { |
| 199 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 200 | } |
| 201 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 202 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 203 | const PointerProperties* pointerProperties) { |
| 204 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 206 | return false; |
| 207 | } |
| 208 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 209 | 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] | 210 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | BitSet32 pointerIdBits; |
| 214 | for (size_t i = 0; i < pointerCount; i++) { |
| 215 | int32_t id = pointerProperties[i].id; |
| 216 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 217 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 218 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | if (pointerIdBits.hasBit(id)) { |
| 222 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 223 | return false; |
| 224 | } |
| 225 | pointerIdBits.markBit(id); |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 230 | static std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 231 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 232 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 235 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 236 | bool first = true; |
| 237 | Region::const_iterator cur = region.begin(); |
| 238 | Region::const_iterator const tail = region.end(); |
| 239 | while (cur != tail) { |
| 240 | if (first) { |
| 241 | first = false; |
| 242 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 243 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 245 | 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] | 246 | cur++; |
| 247 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 248 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 251 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
| 252 | constexpr size_t maxEntries = 50; // max events to print |
| 253 | constexpr size_t skipBegin = maxEntries / 2; |
| 254 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 255 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 256 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 257 | |
| 258 | std::string dump; |
| 259 | for (size_t i = 0; i < queue.size(); i++) { |
| 260 | const DispatchEntry& entry = *queue[i]; |
| 261 | if (i >= skipBegin && i < skipEnd) { |
| 262 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 263 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 264 | continue; |
| 265 | } |
| 266 | dump.append(INDENT4); |
| 267 | dump += entry.eventEntry->getDescription(); |
| 268 | dump += StringPrintf(", seq=%" PRIu32 |
| 269 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 270 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 271 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 272 | if (entry.deliveryTime != 0) { |
| 273 | // This entry was delivered, so add information on how long we've been waiting |
| 274 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 275 | } |
| 276 | dump.append("\n"); |
| 277 | } |
| 278 | return dump; |
| 279 | } |
| 280 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Find the entry in std::unordered_map by key, and return it. |
| 283 | * If the entry is not found, return a default constructed entry. |
| 284 | * |
| 285 | * Useful when the entries are vectors, since an empty vector will be returned |
| 286 | * if the entry is not found. |
| 287 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 288 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 289 | template <typename K, typename V> |
| 290 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 291 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 292 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 293 | } |
| 294 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 295 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { |
| 296 | if (first == second) { |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | if (first == nullptr || second == nullptr) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | return first->getToken() == second->getToken(); |
| 305 | } |
| 306 | |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 307 | static bool haveSameApplicationToken(const InputWindowInfo* first, const InputWindowInfo* second) { |
| 308 | if (first == nullptr || second == nullptr) { |
| 309 | return false; |
| 310 | } |
| 311 | return first->applicationInfo.token != nullptr && |
| 312 | first->applicationInfo.token == second->applicationInfo.token; |
| 313 | } |
| 314 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 315 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 316 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 317 | } |
| 318 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 319 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 320 | std::shared_ptr<EventEntry> eventEntry, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 321 | int32_t inputTargetFlags) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 322 | if (eventEntry->type == EventEntry::Type::MOTION) { |
| 323 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 324 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_JOYSTICK) || |
| 325 | (motionEntry.source & AINPUT_SOURCE_CLASS_POSITION)) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 326 | const ui::Transform identityTransform; |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 327 | // Use identity transform for joystick and position-based (touchpad) events because they |
| 328 | // don't depend on the window transform. |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 329 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 330 | 1.0f /*globalScaleFactor*/, |
| 331 | inputTarget.displaySize); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 335 | if (inputTarget.useDefaultPointerTransform()) { |
| 336 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 337 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 338 | inputTarget.globalScaleFactor, |
| 339 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 343 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 344 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 345 | std::vector<PointerCoords> pointerCoords; |
| 346 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 347 | |
| 348 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 349 | // 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] | 350 | // uses the transform for the normalized pointer. |
| 351 | const ui::Transform& firstPointerTransform = |
| 352 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 353 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 354 | |
| 355 | // Iterate through all pointers in the event to normalize against the first. |
| 356 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 357 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 358 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 359 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 360 | |
| 361 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 362 | // First, apply the current pointer's transform to update the coordinates into |
| 363 | // window space. |
| 364 | pointerCoords[pointerIndex].transform(currTransform); |
| 365 | // Next, apply the inverse transform of the normalized coordinates so the |
| 366 | // current coordinates are transformed into the normalized coordinate space. |
| 367 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 370 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 371 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 372 | motionEntry.deviceId, motionEntry.source, |
| 373 | motionEntry.displayId, motionEntry.policyFlags, |
| 374 | motionEntry.action, motionEntry.actionButton, |
| 375 | motionEntry.flags, motionEntry.metaState, |
| 376 | motionEntry.buttonState, motionEntry.classification, |
| 377 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 378 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 379 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 380 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 381 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 382 | |
| 383 | if (motionEntry.injectionState) { |
| 384 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 385 | combinedMotionEntry->injectionState->refCount += 1; |
| 386 | } |
| 387 | |
| 388 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 389 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 390 | firstPointerTransform, inputTarget.globalScaleFactor, |
| 391 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 392 | return dispatchEntry; |
| 393 | } |
| 394 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 395 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 396 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 397 | float yOffset = 0) { |
| 398 | if (monitors.empty()) { |
| 399 | return; |
| 400 | } |
| 401 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 402 | for (const Monitor& monitor : monitors) { |
| 403 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 404 | } |
| 405 | } |
| 406 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 407 | static status_t openInputChannelPair(const std::string& name, |
| 408 | std::shared_ptr<InputChannel>& serverChannel, |
| 409 | std::unique_ptr<InputChannel>& clientChannel) { |
| 410 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 411 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 412 | |
| 413 | serverChannel = std::move(uniqueServerChannel); |
| 414 | return result; |
| 415 | } |
| 416 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 417 | template <typename T> |
| 418 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 419 | if (lhs == nullptr && rhs == nullptr) { |
| 420 | return true; |
| 421 | } |
| 422 | if (lhs == nullptr || rhs == nullptr) { |
| 423 | return false; |
| 424 | } |
| 425 | return *lhs == *rhs; |
| 426 | } |
| 427 | |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 428 | static sp<IPlatformCompatNative> getCompatService() { |
| 429 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 430 | if (service == nullptr) { |
| 431 | ALOGE("Failed to link to compat service"); |
| 432 | return nullptr; |
| 433 | } |
| 434 | return interface_cast<IPlatformCompatNative>(service); |
| 435 | } |
| 436 | |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 437 | static KeyEvent createKeyEvent(const KeyEntry& entry) { |
| 438 | KeyEvent event; |
| 439 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 440 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 441 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 442 | return event; |
| 443 | } |
| 444 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 445 | static std::optional<int32_t> findMonitorPidByToken( |
| 446 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 447 | const sp<IBinder>& token) { |
| 448 | for (const auto& it : monitorsByDisplay) { |
| 449 | const std::vector<Monitor>& monitors = it.second; |
| 450 | for (const Monitor& monitor : monitors) { |
| 451 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 452 | return monitor.pid; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | return std::nullopt; |
| 457 | } |
| 458 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 459 | static bool shouldReportMetricsForConnection(const Connection& connection) { |
| 460 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 461 | // affect the statistics. |
| 462 | if (connection.monitor) { |
| 463 | return false; |
| 464 | } |
| 465 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 466 | if (!connection.responsive) { |
| 467 | return false; |
| 468 | } |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | static bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, |
| 473 | const Connection& connection) { |
| 474 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 475 | const int32_t& inputEventId = eventEntry.id; |
| 476 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 477 | // Event was transmuted |
| 478 | return false; |
| 479 | } |
| 480 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 481 | return false; |
| 482 | } |
| 483 | // Only track latency for events that originated from hardware |
| 484 | if (eventEntry.isSynthesized()) { |
| 485 | return false; |
| 486 | } |
| 487 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 488 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 489 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 490 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 491 | return false; |
| 492 | } |
| 493 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 494 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 495 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 496 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 497 | return false; |
| 498 | } |
| 499 | } else { |
| 500 | // Not a key or a motion |
| 501 | return false; |
| 502 | } |
| 503 | if (!shouldReportMetricsForConnection(connection)) { |
| 504 | return false; |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 509 | // --- InputDispatcher --- |
| 510 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 511 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 512 | : mPolicy(policy), |
| 513 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 514 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 515 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 516 | mAppSwitchSawKeyDown(false), |
| 517 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 518 | mNextUnblockedEvent(nullptr), |
| 519 | mDispatchEnabled(false), |
| 520 | mDispatchFrozen(false), |
| 521 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 522 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 523 | // To avoid leaking stack in case that call never comes, and for tests, |
| 524 | // initialize it here anyways. |
| 525 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 526 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 527 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 528 | mFocusedWindowRequestedPointerCapture(false), |
| 529 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 530 | mLatencyAggregator(), |
| 531 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 532 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 534 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 535 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 536 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 | |
| 538 | policy->getDispatcherConfiguration(&mConfig); |
| 539 | } |
| 540 | |
| 541 | InputDispatcher::~InputDispatcher() { |
| 542 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 543 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 544 | |
| 545 | resetKeyRepeatLocked(); |
| 546 | releasePendingEventLocked(); |
| 547 | drainInboundQueueLocked(); |
| 548 | } |
| 549 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 550 | while (!mConnectionsByToken.empty()) { |
| 551 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 552 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 556 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 557 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 558 | return ALREADY_EXISTS; |
| 559 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 560 | mThread = std::make_unique<InputThread>( |
| 561 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 562 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 566 | if (mThread && mThread->isCallingThread()) { |
| 567 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 568 | return INVALID_OPERATION; |
| 569 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 570 | mThread.reset(); |
| 571 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | void InputDispatcher::dispatchOnce() { |
| 575 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 576 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 577 | std::scoped_lock _l(mLock); |
| 578 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | |
| 580 | // Run a dispatch loop if there are no pending commands. |
| 581 | // The dispatch loop might enqueue commands to run afterwards. |
| 582 | if (!haveCommandsLocked()) { |
| 583 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 584 | } |
| 585 | |
| 586 | // Run all pending commands if there are any. |
| 587 | // If any commands were run then force the next poll to wake up immediately. |
| 588 | if (runCommandsLockedInterruptible()) { |
| 589 | nextWakeupTime = LONG_LONG_MIN; |
| 590 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 591 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 592 | // If we are still waiting for ack on some events, |
| 593 | // we might have to wake up earlier to check if an app is anr'ing. |
| 594 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 595 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 596 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 597 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 598 | // pending or queued events |
| 599 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 600 | mDispatcherEnteredIdle.notify_all(); |
| 601 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | } // release lock |
| 603 | |
| 604 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 605 | nsecs_t currentTime = now(); |
| 606 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 607 | mLooper->pollOnce(timeoutMillis); |
| 608 | } |
| 609 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 610 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 611 | * Raise ANR if there is no focused window. |
| 612 | * Before the ANR is raised, do a final state check: |
| 613 | * 1. The currently focused application must be the same one we are waiting for. |
| 614 | * 2. Ensure we still don't have a focused window. |
| 615 | */ |
| 616 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 617 | // Check if the application that we are waiting for is still focused. |
| 618 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 619 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 620 | if (focusedApplication == nullptr || |
| 621 | focusedApplication->getApplicationToken() != |
| 622 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 623 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 624 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 625 | focusedApplication->getName().c_str()); |
| 626 | return; // The focused application has changed. |
| 627 | } |
| 628 | |
| 629 | const sp<InputWindowHandle>& focusedWindowHandle = |
| 630 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 631 | if (focusedWindowHandle != nullptr) { |
| 632 | return; // We now have a focused window. No need for ANR. |
| 633 | } |
| 634 | onAnrLocked(mAwaitedFocusedApplication); |
| 635 | } |
| 636 | |
| 637 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 638 | * Check if any of the connections' wait queues have events that are too old. |
| 639 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 640 | * Return the time at which we should wake up next. |
| 641 | */ |
| 642 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 643 | const nsecs_t currentTime = now(); |
| 644 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 645 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 646 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 647 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 648 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 649 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 650 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 651 | return LONG_LONG_MIN; |
| 652 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 653 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 654 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // Check if any connection ANRs are due |
| 659 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 660 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 661 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 662 | } |
| 663 | |
| 664 | // If we reached here, we have an unresponsive connection. |
| 665 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 666 | if (connection == nullptr) { |
| 667 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 668 | return nextAnrCheck; |
| 669 | } |
| 670 | connection->responsive = false; |
| 671 | // Stop waking up for this unresponsive connection |
| 672 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 673 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 674 | return LONG_LONG_MIN; |
| 675 | } |
| 676 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 677 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 678 | sp<InputWindowHandle> window = getWindowHandleLocked(token); |
| 679 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 680 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 681 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 682 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 686 | nsecs_t currentTime = now(); |
| 687 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 688 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 689 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 690 | // repeat if the device is just coming out of sleep. |
| 691 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | resetKeyRepeatLocked(); |
| 693 | } |
| 694 | |
| 695 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 696 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 697 | if (DEBUG_FOCUS) { |
| 698 | ALOGD("Dispatch frozen. Waiting some more."); |
| 699 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 700 | return; |
| 701 | } |
| 702 | |
| 703 | // Optimize latency of app switches. |
| 704 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 705 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 706 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 707 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 708 | *nextWakeupTime = mAppSwitchDueTime; |
| 709 | } |
| 710 | |
| 711 | // Ready to start a new event. |
| 712 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 713 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 714 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 715 | if (isAppSwitchDue) { |
| 716 | // The inbound queue is empty so the app switch key we were waiting |
| 717 | // for will never arrive. Stop waiting for it. |
| 718 | resetPendingAppSwitchLocked(false); |
| 719 | isAppSwitchDue = false; |
| 720 | } |
| 721 | |
| 722 | // Synthesize a key repeat if appropriate. |
| 723 | if (mKeyRepeatState.lastKeyEntry) { |
| 724 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 725 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 726 | } else { |
| 727 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 728 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Nothing to do if there is no pending event. |
| 734 | if (!mPendingEvent) { |
| 735 | return; |
| 736 | } |
| 737 | } else { |
| 738 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 739 | mPendingEvent = mInboundQueue.front(); |
| 740 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | traceInboundQueueLengthLocked(); |
| 742 | } |
| 743 | |
| 744 | // Poke user activity for this event. |
| 745 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 746 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // Now we have an event to dispatch. |
| 751 | // 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] | 752 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 754 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 756 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 757 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 758 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 762 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 766 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 767 | const ConfigurationChangedEntry& typedEntry = |
| 768 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 769 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 770 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 771 | break; |
| 772 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 774 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 775 | const DeviceResetEntry& typedEntry = |
| 776 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 777 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 778 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 779 | break; |
| 780 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 782 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 783 | std::shared_ptr<FocusEntry> typedEntry = |
| 784 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 785 | dispatchFocusLocked(currentTime, typedEntry); |
| 786 | done = true; |
| 787 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 788 | break; |
| 789 | } |
| 790 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 791 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 792 | const auto typedEntry = |
| 793 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 794 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 795 | done = true; |
| 796 | break; |
| 797 | } |
| 798 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 799 | case EventEntry::Type::DRAG: { |
| 800 | std::shared_ptr<DragEntry> typedEntry = |
| 801 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 802 | dispatchDragLocked(currentTime, typedEntry); |
| 803 | done = true; |
| 804 | break; |
| 805 | } |
| 806 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 807 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 808 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 810 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 811 | resetPendingAppSwitchLocked(true); |
| 812 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 813 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 814 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 817 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 818 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 819 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 820 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 821 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 822 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 823 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 824 | break; |
| 825 | } |
| 826 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 827 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 828 | std::shared_ptr<MotionEntry> motionEntry = |
| 829 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 830 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 831 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 832 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 833 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 834 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 835 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 836 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 837 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 838 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 839 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 840 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 841 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 842 | |
| 843 | case EventEntry::Type::SENSOR: { |
| 844 | std::shared_ptr<SensorEntry> sensorEntry = |
| 845 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 846 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 847 | dropReason = DropReason::APP_SWITCH; |
| 848 | } |
| 849 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 850 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 851 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 852 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 853 | dropReason = DropReason::STALE; |
| 854 | } |
| 855 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 856 | done = true; |
| 857 | break; |
| 858 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 862 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 863 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 864 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 865 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | |
| 867 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 868 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 872 | /** |
| 873 | * Return true if the events preceding this incoming motion event should be dropped |
| 874 | * Return false otherwise (the default behaviour) |
| 875 | */ |
| 876 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 877 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 878 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 879 | |
| 880 | // Optimize case where the current application is unresponsive and the user |
| 881 | // decides to touch a window in a different application. |
| 882 | // If the application takes too long to catch up then we drop all events preceding |
| 883 | // the touch into the other window. |
| 884 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 885 | int32_t displayId = motionEntry.displayId; |
| 886 | int32_t x = static_cast<int32_t>( |
| 887 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 888 | int32_t y = static_cast<int32_t>( |
| 889 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 890 | sp<InputWindowHandle> touchedWindowHandle = |
| 891 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 892 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 893 | touchedWindowHandle->getApplicationToken() != |
| 894 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 895 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 896 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 897 | "for %s", |
| 898 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 899 | return true; |
| 900 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 901 | |
| 902 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 903 | std::vector<TouchedMonitor> gestureMonitors = findTouchedGestureMonitorsLocked(displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 904 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 905 | sp<Connection> connection = |
| 906 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 907 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 908 | // This monitor could take more input. Drop all events preceding this |
| 909 | // event, so that gesture monitor could get a chance to receive the stream |
| 910 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 911 | "responsive gesture monitor that may handle the event", |
| 912 | mAwaitedFocusedApplication->getName().c_str()); |
| 913 | return true; |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 919 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 920 | // events to be processed before dispatching the key event. This is because these motion events |
| 921 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 922 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 923 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 924 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 925 | "just send the pending key event to the focused window."); |
| 926 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 927 | } |
| 928 | return false; |
| 929 | } |
| 930 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 931 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 932 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 933 | mInboundQueue.push_back(std::move(newEntry)); |
| 934 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 935 | traceInboundQueueLengthLocked(); |
| 936 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 937 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 938 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 939 | // Optimize app switch latency. |
| 940 | // If the application takes too long to catch up then we drop all events preceding |
| 941 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 942 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 943 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 944 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 945 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 946 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 947 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 948 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 949 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 951 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 952 | mAppSwitchSawKeyDown = false; |
| 953 | needWake = true; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | break; |
| 958 | } |
| 959 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 960 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 961 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 962 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 963 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 965 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 966 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 967 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 968 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 969 | break; |
| 970 | } |
| 971 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 972 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 973 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 974 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 975 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 976 | // nothing to do |
| 977 | break; |
| 978 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | return needWake; |
| 982 | } |
| 983 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 984 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 985 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 986 | if (entry->type != EventEntry::Type::SENSOR) { |
| 987 | mRecentQueue.push_back(entry); |
| 988 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 989 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 990 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 994 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 995 | int32_t y, TouchState* touchState, |
| 996 | bool addOutsideTargets, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 997 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 998 | if (addOutsideTargets && touchState == nullptr) { |
| 999 | LOG_ALWAYS_FATAL("Must provide a valid touch state if adding outside targets"); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1000 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1001 | // Traverse windows from front to back to find touched window. |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1002 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1003 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1004 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1005 | continue; |
| 1006 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1007 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 1008 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1009 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1010 | |
| 1011 | if (windowInfo->visible) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1012 | if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1013 | bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && |
| 1014 | !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 1016 | // Found window. |
| 1017 | return windowHandle; |
| 1018 | } |
| 1019 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1020 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1021 | if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1022 | touchState->addOrUpdateWindow(windowHandle, |
| 1023 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1024 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1025 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1029 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1032 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 1033 | int32_t displayId) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1034 | std::vector<TouchedMonitor> touchedMonitors; |
| 1035 | |
| 1036 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 1037 | addGestureMonitors(monitors, touchedMonitors); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1038 | return touchedMonitors; |
| 1039 | } |
| 1040 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1041 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1042 | const char* reason; |
| 1043 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1044 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1046 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1047 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1048 | reason = "inbound event was dropped because the policy consumed it"; |
| 1049 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1050 | case DropReason::DISABLED: |
| 1051 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1052 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1053 | } |
| 1054 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1055 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1056 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1057 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1058 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1059 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1060 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1061 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1062 | "has started interacting with a different application."); |
| 1063 | reason = "inbound event was dropped because the current application is not responding " |
| 1064 | "and the user has started interacting with a different application"; |
| 1065 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1066 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1067 | ALOGI("Dropped event because it is stale."); |
| 1068 | reason = "inbound event was dropped because it is stale"; |
| 1069 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1070 | case DropReason::NO_POINTER_CAPTURE: |
| 1071 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1072 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1073 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1074 | case DropReason::NOT_DROPPED: { |
| 1075 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1076 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1077 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1080 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1081 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1082 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1083 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1084 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1085 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1086 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1087 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1088 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1089 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1090 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1091 | } else { |
| 1092 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1093 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1094 | } |
| 1095 | break; |
| 1096 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1097 | case EventEntry::Type::SENSOR: { |
| 1098 | break; |
| 1099 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1100 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1101 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1102 | break; |
| 1103 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1104 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1105 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1106 | case EventEntry::Type::DEVICE_RESET: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1107 | 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] | 1108 | break; |
| 1109 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1113 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1115 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1118 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1119 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1120 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1121 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1125 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1126 | } |
| 1127 | |
| 1128 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1129 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1130 | |
| 1131 | #if DEBUG_APP_SWITCH |
| 1132 | if (handled) { |
| 1133 | ALOGD("App switch has arrived."); |
| 1134 | } else { |
| 1135 | ALOGD("App switch was abandoned."); |
| 1136 | } |
| 1137 | #endif |
| 1138 | } |
| 1139 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1140 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1141 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1145 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1146 | return false; |
| 1147 | } |
| 1148 | |
| 1149 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1150 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1151 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1152 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1153 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | |
| 1155 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1156 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
| 1159 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1160 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1161 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1165 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1166 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1167 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1168 | releaseInboundEventLocked(entry); |
| 1169 | } |
| 1170 | traceInboundQueueLengthLocked(); |
| 1171 | } |
| 1172 | |
| 1173 | void InputDispatcher::releasePendingEventLocked() { |
| 1174 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1176 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1180 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1182 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | #if DEBUG_DISPATCH_CYCLE |
| 1184 | ALOGD("Injected inbound event was dropped."); |
| 1185 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1186 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | } |
| 1188 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1189 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | } |
| 1191 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1195 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1196 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1200 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1201 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1203 | uint32_t policyFlags = entry->policyFlags & |
| 1204 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1206 | std::shared_ptr<KeyEntry> newEntry = |
| 1207 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1208 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1209 | entry->flags, entry->keyCode, entry->scanCode, |
| 1210 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1212 | newEntry->syntheticRepeat = true; |
| 1213 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1214 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1215 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1218 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1219 | const ConfigurationChangedEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1221 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | #endif |
| 1223 | |
| 1224 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1225 | resetKeyRepeatLocked(); |
| 1226 | |
| 1227 | // 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] | 1228 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1229 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1230 | commandEntry->eventTime = entry.eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1231 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | return true; |
| 1233 | } |
| 1234 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1235 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1236 | const DeviceResetEntry& 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("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1239 | entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1240 | #endif |
| 1241 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1242 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1243 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1244 | resetKeyRepeatLocked(); |
| 1245 | } |
| 1246 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1247 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1250 | return true; |
| 1251 | } |
| 1252 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1253 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1254 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1255 | if (mPendingEvent != nullptr) { |
| 1256 | // Move the pending event to the front of the queue. This will give the chance |
| 1257 | // for the pending event to get dispatched to the newly focused window |
| 1258 | mInboundQueue.push_front(mPendingEvent); |
| 1259 | mPendingEvent = nullptr; |
| 1260 | } |
| 1261 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1262 | std::unique_ptr<FocusEntry> focusEntry = |
| 1263 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1264 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1265 | |
| 1266 | // This event should go to the front of the queue, but behind all other focus events |
| 1267 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1269 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1270 | [](const std::shared_ptr<EventEntry>& event) { |
| 1271 | return event->type == EventEntry::Type::FOCUS; |
| 1272 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1273 | |
| 1274 | // 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] | 1275 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1278 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1279 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1280 | if (channel == nullptr) { |
| 1281 | return; // Window has gone away |
| 1282 | } |
| 1283 | InputTarget target; |
| 1284 | target.inputChannel = channel; |
| 1285 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1286 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1287 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1288 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1289 | std::string reason = std::string("reason=").append(entry->reason); |
| 1290 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1291 | dispatchEventLocked(currentTime, entry, {target}); |
| 1292 | } |
| 1293 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1294 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1295 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1296 | DropReason& dropReason) { |
| 1297 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1298 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { |
| 1299 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); |
| 1300 | } |
| 1301 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1302 | // Pointer capture was already forcefully disabled because of focus change. |
| 1303 | dropReason = DropReason::NOT_DROPPED; |
| 1304 | return; |
| 1305 | } |
| 1306 | |
| 1307 | // Set drop reason for early returns |
| 1308 | dropReason = DropReason::NO_POINTER_CAPTURE; |
| 1309 | |
| 1310 | sp<IBinder> token; |
| 1311 | if (entry->pointerCaptureEnabled) { |
| 1312 | // Enable Pointer Capture |
| 1313 | if (!mFocusedWindowRequestedPointerCapture) { |
| 1314 | // This can happen if a window requests capture and immediately releases capture. |
| 1315 | ALOGW("No window requested Pointer Capture."); |
| 1316 | return; |
| 1317 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1318 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1319 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1320 | mWindowTokenWithPointerCapture = token; |
| 1321 | } else { |
| 1322 | // Disable Pointer Capture |
| 1323 | token = mWindowTokenWithPointerCapture; |
| 1324 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1325 | if (mFocusedWindowRequestedPointerCapture) { |
| 1326 | mFocusedWindowRequestedPointerCapture = false; |
| 1327 | setPointerCaptureLocked(false); |
| 1328 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | auto channel = getInputChannelLocked(token); |
| 1332 | if (channel == nullptr) { |
| 1333 | // Window has gone away, clean up Pointer Capture state. |
| 1334 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1335 | if (mFocusedWindowRequestedPointerCapture) { |
| 1336 | mFocusedWindowRequestedPointerCapture = false; |
| 1337 | setPointerCaptureLocked(false); |
| 1338 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1339 | return; |
| 1340 | } |
| 1341 | InputTarget target; |
| 1342 | target.inputChannel = channel; |
| 1343 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1344 | entry->dispatchInProgress = true; |
| 1345 | dispatchEventLocked(currentTime, entry, {target}); |
| 1346 | |
| 1347 | dropReason = DropReason::NOT_DROPPED; |
| 1348 | } |
| 1349 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1350 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1351 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1353 | if (!entry->dispatchInProgress) { |
| 1354 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1355 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1356 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1357 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1358 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1359 | // We have seen two identical key downs in a row which indicates that the device |
| 1360 | // driver is automatically generating key repeats itself. We take note of the |
| 1361 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1362 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1363 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1364 | // Make sure we don't get key down from a different device. If a different |
| 1365 | // device Id has same key pressed down, the new device Id will replace the |
| 1366 | // current one to hold the key repeat with repeat count reset. |
| 1367 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1368 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1369 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1370 | resetKeyRepeatLocked(); |
| 1371 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1372 | } else { |
| 1373 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1374 | resetKeyRepeatLocked(); |
| 1375 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1376 | } |
| 1377 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1378 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1379 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1380 | // 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] | 1381 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1382 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1383 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1384 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1385 | resetKeyRepeatLocked(); |
| 1386 | } |
| 1387 | |
| 1388 | if (entry->repeatCount == 1) { |
| 1389 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1390 | } else { |
| 1391 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1392 | } |
| 1393 | |
| 1394 | entry->dispatchInProgress = true; |
| 1395 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1396 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | // Handle case where the policy asked us to try again later last time. |
| 1400 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1401 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1402 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1403 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1404 | } |
| 1405 | return false; // wait until next wakeup |
| 1406 | } |
| 1407 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1408 | entry->interceptKeyWakeupTime = 0; |
| 1409 | } |
| 1410 | |
| 1411 | // Give the policy a chance to intercept the key. |
| 1412 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1413 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1414 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1415 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1416 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1417 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1418 | commandEntry->connectionToken = focusedWindowToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1419 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1420 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1421 | return false; // wait for the command to run |
| 1422 | } else { |
| 1423 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1424 | } |
| 1425 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1426 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1427 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1432 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1433 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1434 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1435 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1436 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1437 | return true; |
| 1438 | } |
| 1439 | |
| 1440 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1441 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1442 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1443 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1444 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1445 | return false; |
| 1446 | } |
| 1447 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1448 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1449 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1450 | return true; |
| 1451 | } |
| 1452 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1453 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1454 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1455 | |
| 1456 | // Dispatch the key. |
| 1457 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1461 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1462 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1463 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1464 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1465 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1466 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1467 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1468 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1469 | #endif |
| 1470 | } |
| 1471 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1472 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { |
| 1473 | mLock.unlock(); |
| 1474 | |
| 1475 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; |
| 1476 | if (entry->accuracyChanged) { |
| 1477 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1478 | } |
| 1479 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1480 | entry->hwTimestamp, entry->values); |
| 1481 | mLock.lock(); |
| 1482 | } |
| 1483 | |
| 1484 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, |
| 1485 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 1486 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1487 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1488 | "source=0x%x, sensorType=%s", |
| 1489 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1490 | NamedEnum::string(entry->sensorType).c_str()); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1491 | #endif |
| 1492 | std::unique_ptr<CommandEntry> commandEntry = |
| 1493 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); |
| 1494 | commandEntry->sensorEntry = entry; |
| 1495 | postCommandLocked(std::move(commandEntry)); |
| 1496 | } |
| 1497 | |
| 1498 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
| 1499 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1500 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
| 1501 | NamedEnum::string(sensorType).c_str()); |
| 1502 | #endif |
| 1503 | { // acquire lock |
| 1504 | std::scoped_lock _l(mLock); |
| 1505 | |
| 1506 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1507 | std::shared_ptr<EventEntry> entry = *it; |
| 1508 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1509 | it = mInboundQueue.erase(it); |
| 1510 | releaseInboundEventLocked(entry); |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1517 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1518 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1519 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1520 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1521 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | entry->dispatchInProgress = true; |
| 1523 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1524 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1528 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1529 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1530 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1531 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | return true; |
| 1533 | } |
| 1534 | |
| 1535 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1536 | |
| 1537 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1538 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1539 | |
| 1540 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1541 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 | if (isPointerEvent) { |
| 1543 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1544 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1545 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1546 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1547 | } else { |
| 1548 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1549 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1550 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1551 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1552 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1553 | return false; |
| 1554 | } |
| 1555 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1556 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1557 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1558 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1559 | return true; |
| 1560 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1561 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1562 | CancelationOptions::Mode mode(isPointerEvent |
| 1563 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1564 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1565 | CancelationOptions options(mode, "input event injection failed"); |
| 1566 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | return true; |
| 1568 | } |
| 1569 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1570 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1571 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1572 | |
| 1573 | // Dispatch the motion. |
| 1574 | if (conflictingPointerActions) { |
| 1575 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1576 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1578 | } |
| 1579 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1583 | void InputDispatcher::enqueueDragEventLocked(const sp<InputWindowHandle>& windowHandle, |
| 1584 | bool isExiting, const MotionEntry& motionEntry) { |
| 1585 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1586 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1587 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1588 | PointerCoords pointerCoords; |
| 1589 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1590 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1591 | |
| 1592 | std::unique_ptr<DragEntry> dragEntry = |
| 1593 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1594 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1595 | pointerCoords.getY()); |
| 1596 | |
| 1597 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1598 | } |
| 1599 | |
| 1600 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1601 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1602 | if (channel == nullptr) { |
| 1603 | return; // Window has gone away |
| 1604 | } |
| 1605 | InputTarget target; |
| 1606 | target.inputChannel = channel; |
| 1607 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1608 | entry->dispatchInProgress = true; |
| 1609 | dispatchEventLocked(currentTime, entry, {target}); |
| 1610 | } |
| 1611 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1612 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1613 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1614 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1615 | ", policyFlags=0x%x, " |
| 1616 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1617 | "metaState=0x%x, buttonState=0x%x," |
| 1618 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1619 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1620 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1621 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1623 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1625 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1626 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1627 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1628 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1629 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1630 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1631 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1632 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1633 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1634 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1635 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1636 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1637 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | } |
| 1639 | #endif |
| 1640 | } |
| 1641 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1642 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1643 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1644 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1645 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1646 | #if DEBUG_DISPATCH_CYCLE |
| 1647 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1648 | #endif |
| 1649 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1650 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1651 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1652 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1653 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1654 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1656 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1657 | sp<Connection> connection = |
| 1658 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1659 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1660 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1661 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1662 | if (DEBUG_FOCUS) { |
| 1663 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1664 | "is no longer registered with the input dispatcher.", |
| 1665 | inputTarget.inputChannel->getName().c_str()); |
| 1666 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1671 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1672 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1673 | // If the policy decides to close the app, we will get a channel removal event via |
| 1674 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1675 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1676 | // pile up. |
| 1677 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1678 | connection->inputChannel->getName().c_str()); |
| 1679 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1680 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1681 | "application not responding"); |
| 1682 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1686 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1687 | if (DEBUG_FOCUS) { |
| 1688 | ALOGD("Resetting ANR timeouts."); |
| 1689 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1690 | |
| 1691 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1692 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1693 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1696 | /** |
| 1697 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1698 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1699 | * Focused display is the display that the user most recently interacted with. |
| 1700 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1701 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1702 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1703 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1704 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1705 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1706 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1707 | break; |
| 1708 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1709 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1710 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1711 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1712 | break; |
| 1713 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1714 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1715 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1716 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1717 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1718 | case EventEntry::Type::SENSOR: |
| 1719 | case EventEntry::Type::DRAG: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1720 | 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] | 1721 | return ADISPLAY_ID_NONE; |
| 1722 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1723 | } |
| 1724 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1725 | } |
| 1726 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1727 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1728 | const char* focusedWindowName) { |
| 1729 | if (mAnrTracker.empty()) { |
| 1730 | // already processed all events that we waited for |
| 1731 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1732 | return false; |
| 1733 | } |
| 1734 | |
| 1735 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1736 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1737 | // 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] | 1738 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1739 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1740 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1741 | return true; |
| 1742 | } |
| 1743 | |
| 1744 | // We still have pending events, and already started the timer |
| 1745 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1746 | return true; // Still waiting |
| 1747 | } |
| 1748 | |
| 1749 | // Waited too long, and some connection still hasn't processed all motions |
| 1750 | // Just send the key to the focused window |
| 1751 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1752 | focusedWindowName); |
| 1753 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1754 | return false; |
| 1755 | } |
| 1756 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1757 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1758 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1759 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1760 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1761 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1762 | int32_t displayId = getTargetDisplayId(entry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1763 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1764 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1765 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1766 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1767 | // If there is no currently focused window and no focused application |
| 1768 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1769 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1770 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1771 | "display %" PRId32 ".", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1772 | NamedEnum::string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1773 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1776 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1777 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1778 | // start interacting with another application via touch (app switch). This code can be removed |
| 1779 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1780 | // an app is expected to have a focused window. |
| 1781 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1782 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1783 | // 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] | 1784 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1785 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1786 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1787 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1788 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1789 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1790 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1791 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1792 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1793 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1794 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1795 | // Already raised ANR. Drop the event |
| 1796 | ALOGE("Dropping %s event because there is no focused window", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1797 | NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1798 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1799 | } else { |
| 1800 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1801 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | // we have a valid, non-null focused window |
| 1806 | resetNoFocusedWindowTimeoutLocked(); |
| 1807 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1808 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1809 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1810 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1813 | if (focusedWindowHandle->getInfo()->paused) { |
| 1814 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1815 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | // If the event is a key event, then we must wait for all previous events to |
| 1819 | // complete before delivering it because previous events may have the |
| 1820 | // side-effect of transferring focus to a different window and we want to |
| 1821 | // ensure that the following keys are sent to the new window. |
| 1822 | // |
| 1823 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1824 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1825 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1826 | // often anticipate pending UI changes when typing on a keyboard. |
| 1827 | // To obtain this behavior, we must serialize key events with respect to all |
| 1828 | // prior input events. |
| 1829 | if (entry.type == EventEntry::Type::KEY) { |
| 1830 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1831 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1832 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1837 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1838 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1839 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1840 | |
| 1841 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1842 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1845 | /** |
| 1846 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1847 | * that are currently unresponsive. |
| 1848 | */ |
| 1849 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1850 | const std::vector<TouchedMonitor>& monitors) const { |
| 1851 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1852 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1853 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1854 | sp<Connection> connection = getConnectionLocked( |
| 1855 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1856 | if (connection == nullptr) { |
| 1857 | ALOGE("Could not find connection for monitor %s", |
| 1858 | monitor.monitor.inputChannel->getName().c_str()); |
| 1859 | return false; |
| 1860 | } |
| 1861 | if (!connection->responsive) { |
| 1862 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1863 | connection->inputChannel->getName().c_str()); |
| 1864 | return false; |
| 1865 | } |
| 1866 | return true; |
| 1867 | }); |
| 1868 | return responsiveMonitors; |
| 1869 | } |
| 1870 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1871 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1872 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1873 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1874 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1875 | enum InjectionPermission { |
| 1876 | INJECTION_PERMISSION_UNKNOWN, |
| 1877 | INJECTION_PERMISSION_GRANTED, |
| 1878 | INJECTION_PERMISSION_DENIED |
| 1879 | }; |
| 1880 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1881 | // For security reasons, we defer updating the touch state until we are sure that |
| 1882 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1883 | int32_t displayId = entry.displayId; |
| 1884 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1885 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1886 | |
| 1887 | // 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] | 1888 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1889 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1890 | sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1891 | sp<InputWindowHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1892 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1893 | // Copy current touch state into tempTouchState. |
| 1894 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1895 | // 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] | 1896 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1897 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1898 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1899 | mTouchStatesByDisplay.find(displayId); |
| 1900 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1901 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1902 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1903 | } |
| 1904 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1905 | bool isSplit = tempTouchState.split; |
| 1906 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1907 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1908 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1909 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1910 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1911 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1912 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1913 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1914 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1915 | bool wrongDevice = false; |
| 1916 | if (newGesture) { |
| 1917 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1918 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1919 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1920 | "in display %" PRId32, |
| 1921 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1922 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1923 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1924 | switchedDevice = false; |
| 1925 | wrongDevice = true; |
| 1926 | goto Failed; |
| 1927 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1928 | tempTouchState.reset(); |
| 1929 | tempTouchState.down = down; |
| 1930 | tempTouchState.deviceId = entry.deviceId; |
| 1931 | tempTouchState.source = entry.source; |
| 1932 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1933 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1934 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1935 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1936 | "in display %" PRId32, |
| 1937 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1938 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1939 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1940 | switchedDevice = false; |
| 1941 | wrongDevice = true; |
| 1942 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1946 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1947 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1948 | int32_t x; |
| 1949 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1950 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1951 | // Always dispatch mouse events to cursor position. |
| 1952 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1953 | x = int32_t(entry.xCursorPosition); |
| 1954 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1955 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1956 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1957 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1958 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1959 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 1960 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1961 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1962 | |
| 1963 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 1964 | ? findTouchedGestureMonitorsLocked(displayId) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1965 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1966 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1967 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1968 | if (newTouchedWindowHandle != nullptr && |
| 1969 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1970 | // New window supports splitting, but we should never split mouse events. |
| 1971 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | } else if (isSplit) { |
| 1973 | // New window does not support splitting but we have already split events. |
| 1974 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1975 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1979 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1980 | // 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] | 1981 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1984 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 1985 | ALOGI("Not sending touch event to %s because it is paused", |
| 1986 | newTouchedWindowHandle->getName().c_str()); |
| 1987 | newTouchedWindowHandle = nullptr; |
| 1988 | } |
| 1989 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1990 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1991 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1992 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 1993 | if (!isResponsive) { |
| 1994 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1995 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 1996 | newTouchedWindowHandle = nullptr; |
| 1997 | } |
| 1998 | } |
| 1999 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2000 | // Drop events that can't be trusted due to occlusion |
| 2001 | if (newTouchedWindowHandle != nullptr && |
| 2002 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2003 | TouchOcclusionInfo occlusionInfo = |
| 2004 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2005 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2006 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2007 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2008 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2009 | ALOGD("%s", log.c_str()); |
| 2010 | } |
| 2011 | } |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2012 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 2013 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2014 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2015 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2016 | newTouchedWindowHandle = nullptr; |
| 2017 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2021 | // Also don't send the new touch event to unresponsive gesture monitors |
| 2022 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 2023 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2024 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2025 | 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] | 2026 | "(%d, %d) in display %" PRId32 ".", |
| 2027 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2028 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2029 | goto Failed; |
| 2030 | } |
| 2031 | |
| 2032 | if (newTouchedWindowHandle != nullptr) { |
| 2033 | // Set target flags. |
| 2034 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2035 | if (isSplit) { |
| 2036 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2037 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2038 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2039 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2040 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2041 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2042 | } |
| 2043 | |
| 2044 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2045 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2046 | newHoverWindowHandle = nullptr; |
| 2047 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2048 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | // Update the temporary touch state. |
| 2052 | BitSet32 pointerIds; |
| 2053 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2054 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2055 | pointerIds.markBit(pointerId); |
| 2056 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2057 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2060 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | } else { |
| 2062 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2063 | |
| 2064 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2065 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2066 | if (DEBUG_FOCUS) { |
| 2067 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2068 | "dropped the pointer down event in display %" PRId32, |
| 2069 | displayId); |
| 2070 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2071 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | goto Failed; |
| 2073 | } |
| 2074 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2075 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2076 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2078 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2079 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2080 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2081 | 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] | 2082 | |
| 2083 | sp<InputWindowHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2084 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2085 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2086 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2087 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2088 | if (DEBUG_FOCUS) { |
| 2089 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2090 | oldTouchedWindowHandle->getName().c_str(), |
| 2091 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2092 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2093 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2094 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2095 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2096 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2097 | |
| 2098 | // Make a slippery entrance into the new window. |
| 2099 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2100 | isSplit = true; |
| 2101 | } |
| 2102 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2103 | int32_t targetFlags = |
| 2104 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 | if (isSplit) { |
| 2106 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2107 | } |
| 2108 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2109 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2110 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2111 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | BitSet32 pointerIds; |
| 2115 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2116 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2117 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2118 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2124 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2125 | // when dispatching it as is to newTouchedWindowHandle. |
| 2126 | if (mLastHoverWindowHandle != nullptr && |
| 2127 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2128 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2129 | #if DEBUG_HOVER |
| 2130 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2131 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2132 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2133 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2134 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2137 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2138 | // when dispatching it as is to newTouchedWindowHandle. |
| 2139 | if (newHoverWindowHandle != nullptr && |
| 2140 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2141 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2142 | #if DEBUG_HOVER |
| 2143 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2144 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2145 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2146 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2147 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2148 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | // Check permission to inject into all touched foreground windows and ensure there |
| 2153 | // is at least one touched foreground window. |
| 2154 | { |
| 2155 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2156 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2157 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2158 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2159 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2160 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2161 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2162 | goto Failed; |
| 2163 | } |
| 2164 | } |
| 2165 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2166 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2167 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2168 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2169 | "%" PRId32 " or gesture monitor to receive it.", |
| 2170 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2171 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2172 | goto Failed; |
| 2173 | } |
| 2174 | |
| 2175 | // Permission granted to injection into all touched foreground windows. |
| 2176 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2177 | } |
| 2178 | |
| 2179 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2180 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2181 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2182 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2183 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2184 | if (foregroundWindowHandle) { |
| 2185 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2186 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2187 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2188 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
| 2189 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2190 | tempTouchState.addOrUpdateWindow(inputWindowHandle, |
| 2191 | InputTarget::FLAG_ZERO_COORDS, |
| 2192 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2200 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2201 | // of the touch gesture. |
| 2202 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2203 | // engine only supports touch events. We would need to add a mechanism similar |
| 2204 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2205 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2206 | sp<InputWindowHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2207 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2208 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2209 | const std::vector<sp<InputWindowHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2210 | getWindowHandlesLocked(displayId); |
| 2211 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2212 | const InputWindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2213 | if (info->displayId == displayId && |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2214 | windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2215 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2216 | .addOrUpdateWindow(windowHandle, |
| 2217 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2218 | InputTarget:: |
| 2219 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2220 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2221 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2228 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2230 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2231 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2232 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2233 | } |
| 2234 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2235 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2236 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2237 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 | // Drop the outside or hover touch windows since we will not care about them |
| 2241 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2242 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2243 | |
| 2244 | Failed: |
| 2245 | // Check injection permission once and for all. |
| 2246 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2247 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2248 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2249 | } else { |
| 2250 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2251 | } |
| 2252 | } |
| 2253 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2254 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2255 | return injectionResult; |
| 2256 | } |
| 2257 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2258 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2259 | if (!wrongDevice) { |
| 2260 | if (switchedDevice) { |
| 2261 | if (DEBUG_FOCUS) { |
| 2262 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2263 | } |
| 2264 | *outConflictingPointerActions = true; |
| 2265 | } |
| 2266 | |
| 2267 | if (isHoverAction) { |
| 2268 | // Started hovering, therefore no longer down. |
| 2269 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2270 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2271 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2272 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2273 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2274 | *outConflictingPointerActions = true; |
| 2275 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2276 | tempTouchState.reset(); |
| 2277 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2278 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2279 | tempTouchState.deviceId = entry.deviceId; |
| 2280 | tempTouchState.source = entry.source; |
| 2281 | tempTouchState.displayId = displayId; |
| 2282 | } |
| 2283 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2284 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2285 | // All pointers up or canceled. |
| 2286 | tempTouchState.reset(); |
| 2287 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2288 | // First pointer went down. |
| 2289 | if (oldState && oldState->down) { |
| 2290 | if (DEBUG_FOCUS) { |
| 2291 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2292 | } |
| 2293 | *outConflictingPointerActions = true; |
| 2294 | } |
| 2295 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2296 | // One pointer went up. |
| 2297 | if (isSplit) { |
| 2298 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2299 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2300 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2301 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2302 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2303 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2304 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2305 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2306 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2307 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2309 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2310 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2311 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2312 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2313 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2314 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2315 | // Save changes unless the action was scroll in which case the temporary touch |
| 2316 | // state was only valid for this one action. |
| 2317 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2318 | if (tempTouchState.displayId >= 0) { |
| 2319 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2320 | } else { |
| 2321 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2323 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2325 | // Update hover state. |
| 2326 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2329 | return injectionResult; |
| 2330 | } |
| 2331 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2332 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
| 2333 | const sp<InputWindowHandle> dropWindow = |
| 2334 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 2335 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2336 | if (dropWindow) { |
| 2337 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
| 2338 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2339 | } else { |
| 2340 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2341 | } |
| 2342 | mDragState.reset(); |
| 2343 | } |
| 2344 | |
| 2345 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2346 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2347 | return; |
| 2348 | } |
| 2349 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2350 | if (!mDragState->isStartDrag) { |
| 2351 | mDragState->isStartDrag = true; |
| 2352 | mDragState->isStylusButtonDownAtStart = |
| 2353 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2354 | } |
| 2355 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2356 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2357 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2358 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2359 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2360 | // Handle the special case : stylus button no longer pressed. |
| 2361 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2362 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2363 | finishDragAndDrop(entry.displayId, x, y); |
| 2364 | return; |
| 2365 | } |
| 2366 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2367 | const sp<InputWindowHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2368 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 2369 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2370 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2371 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2372 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2373 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2374 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2375 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2376 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2377 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2378 | } |
| 2379 | // enqueue drag location if needed. |
| 2380 | if (hoverWindowHandle != nullptr) { |
| 2381 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2382 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2383 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2384 | finishDragAndDrop(entry.displayId, x, y); |
| 2385 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2386 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2387 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2391 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2392 | int32_t targetFlags, BitSet32 pointerIds, |
| 2393 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2394 | std::vector<InputTarget>::iterator it = |
| 2395 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2396 | [&windowHandle](const InputTarget& inputTarget) { |
| 2397 | return inputTarget.inputChannel->getConnectionToken() == |
| 2398 | windowHandle->getToken(); |
| 2399 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2400 | |
Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2401 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2402 | |
| 2403 | if (it == inputTargets.end()) { |
| 2404 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2405 | std::shared_ptr<InputChannel> inputChannel = |
| 2406 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2407 | if (inputChannel == nullptr) { |
| 2408 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2409 | return; |
| 2410 | } |
| 2411 | inputTarget.inputChannel = inputChannel; |
| 2412 | inputTarget.flags = targetFlags; |
| 2413 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2414 | inputTarget.displaySize = |
Evan Rosky | 44edce9 | 2021-05-14 18:09:55 -0700 | [diff] [blame] | 2415 | int2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2416 | inputTargets.push_back(inputTarget); |
| 2417 | it = inputTargets.end() - 1; |
| 2418 | } |
| 2419 | |
| 2420 | ALOG_ASSERT(it->flags == targetFlags); |
| 2421 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2422 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2423 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2424 | } |
| 2425 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2426 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2427 | int32_t displayId, float xOffset, |
| 2428 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2429 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2430 | mGlobalMonitorsByDisplay.find(displayId); |
| 2431 | |
| 2432 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2433 | const std::vector<Monitor>& monitors = it->second; |
| 2434 | for (const Monitor& monitor : monitors) { |
| 2435 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2436 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2440 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2441 | float yOffset, |
| 2442 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2443 | InputTarget target; |
| 2444 | target.inputChannel = monitor.inputChannel; |
| 2445 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2446 | ui::Transform t; |
| 2447 | t.set(xOffset, yOffset); |
| 2448 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2449 | inputTargets.push_back(target); |
| 2450 | } |
| 2451 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2452 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2453 | const InjectionState* injectionState) { |
| 2454 | if (injectionState && |
| 2455 | (windowHandle == nullptr || |
| 2456 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2457 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2458 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2459 | 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] | 2460 | "owned by uid %d", |
| 2461 | injectionState->injectorPid, injectionState->injectorUid, |
| 2462 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2463 | } else { |
| 2464 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2465 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2466 | } |
| 2467 | return false; |
| 2468 | } |
| 2469 | return true; |
| 2470 | } |
| 2471 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2472 | /** |
| 2473 | * Indicate whether one window handle should be considered as obscuring |
| 2474 | * another window handle. We only check a few preconditions. Actually |
| 2475 | * checking the bounds is left to the caller. |
| 2476 | */ |
| 2477 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, |
| 2478 | const sp<InputWindowHandle>& otherHandle) { |
| 2479 | // Compare by token so cloned layers aren't counted |
| 2480 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2481 | return false; |
| 2482 | } |
| 2483 | auto info = windowHandle->getInfo(); |
| 2484 | auto otherInfo = otherHandle->getInfo(); |
| 2485 | if (!otherInfo->visible) { |
| 2486 | return false; |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2487 | } else if (otherInfo->alpha == 0 && |
| 2488 | otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { |
| 2489 | // Those act as if they were invisible, so we don't need to flag them. |
| 2490 | // We do want to potentially flag touchable windows even if they have 0 |
| 2491 | // opacity, since they can consume touches and alter the effects of the |
| 2492 | // user interaction (eg. apps that rely on |
| 2493 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2494 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2495 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2496 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2497 | // If ownerUid is the same we don't generate occlusion events as there |
| 2498 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2499 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2500 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2501 | return false; |
| 2502 | } else if (otherInfo->displayId != info->displayId) { |
| 2503 | return false; |
| 2504 | } |
| 2505 | return true; |
| 2506 | } |
| 2507 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2508 | /** |
| 2509 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2510 | * untrusted, one should check: |
| 2511 | * |
| 2512 | * 1. If result.hasBlockingOcclusion is true. |
| 2513 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2514 | * BLOCK_UNTRUSTED. |
| 2515 | * |
| 2516 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2517 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2518 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2519 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2520 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2521 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2522 | * |
| 2523 | * If neither of those is true, then it means the touch can be allowed. |
| 2524 | */ |
| 2525 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
| 2526 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2527 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2528 | int32_t displayId = windowInfo->displayId; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2529 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2530 | TouchOcclusionInfo info; |
| 2531 | info.hasBlockingOcclusion = false; |
| 2532 | info.obscuringOpacity = 0; |
| 2533 | info.obscuringUid = -1; |
| 2534 | std::map<int32_t, float> opacityByUid; |
| 2535 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
| 2536 | if (windowHandle == otherHandle) { |
| 2537 | break; // All future windows are below us. Exit early. |
| 2538 | } |
| 2539 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2540 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2541 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2542 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2543 | info.debugInfo.push_back( |
| 2544 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2545 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2546 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2547 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2548 | // window's touch occlusion mode |
| 2549 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2550 | info.hasBlockingOcclusion = true; |
| 2551 | info.obscuringUid = otherInfo->ownerUid; |
| 2552 | info.obscuringPackage = otherInfo->packageName; |
| 2553 | break; |
| 2554 | } |
| 2555 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2556 | uint32_t uid = otherInfo->ownerUid; |
| 2557 | float opacity = |
| 2558 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2559 | // Given windows A and B: |
| 2560 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2561 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2562 | opacityByUid[uid] = opacity; |
| 2563 | if (opacity > info.obscuringOpacity) { |
| 2564 | info.obscuringOpacity = opacity; |
| 2565 | info.obscuringUid = uid; |
| 2566 | info.obscuringPackage = otherInfo->packageName; |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2571 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2572 | info.debugInfo.push_back( |
| 2573 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2574 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2575 | return info; |
| 2576 | } |
| 2577 | |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2578 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, |
| 2579 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2580 | return StringPrintf(INDENT2 |
| 2581 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2582 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2583 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2584 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2585 | (isTouchedWindow) ? "[TOUCHED] " : "", |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2586 | NamedEnum::string(info->type, "%" PRId32).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2587 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2588 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2589 | info->frameTop, info->frameRight, info->frameBottom, |
| 2590 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2591 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2592 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2593 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2596 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2597 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2598 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2599 | occlusionInfo.obscuringUid); |
| 2600 | return false; |
| 2601 | } |
| 2602 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2603 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2604 | "%.2f, maximum allowed = %.2f)", |
| 2605 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2606 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2607 | return false; |
| 2608 | } |
| 2609 | return true; |
| 2610 | } |
| 2611 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2612 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, |
| 2613 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2614 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2615 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2616 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2617 | if (windowHandle == otherHandle) { |
| 2618 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2619 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2621 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2622 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2623 | return true; |
| 2624 | } |
| 2625 | } |
| 2626 | return false; |
| 2627 | } |
| 2628 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2629 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { |
| 2630 | int32_t displayId = windowHandle->getInfo()->displayId; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2631 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2632 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2633 | for (const sp<InputWindowHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2634 | if (windowHandle == otherHandle) { |
| 2635 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2636 | } |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2637 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2638 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2639 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2640 | return true; |
| 2641 | } |
| 2642 | } |
| 2643 | return false; |
| 2644 | } |
| 2645 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2646 | std::string InputDispatcher::getApplicationWindowLabel( |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2647 | const InputApplicationHandle* applicationHandle, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2648 | const sp<InputWindowHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2649 | if (applicationHandle != nullptr) { |
| 2650 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2651 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2652 | } else { |
| 2653 | return applicationHandle->getName(); |
| 2654 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2655 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2656 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2657 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2658 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2662 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2663 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2664 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2665 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2666 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2667 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2668 | return; |
| 2669 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2670 | int32_t displayId = getTargetDisplayId(eventEntry); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2671 | sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2672 | if (focusedWindowHandle != nullptr) { |
| 2673 | const InputWindowInfo* info = focusedWindowHandle->getInfo(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2674 | if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2675 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2676 | 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] | 2677 | #endif |
| 2678 | return; |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2683 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2684 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2685 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2686 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2687 | return; |
| 2688 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2689 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2690 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2691 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2692 | } |
| 2693 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2694 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2695 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2696 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2697 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2701 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2702 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2703 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2704 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2705 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2706 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2707 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2708 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2709 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2710 | NamedEnum::string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2711 | break; |
| 2712 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2713 | } |
| 2714 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2715 | std::unique_ptr<CommandEntry> commandEntry = |
| 2716 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2717 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2718 | commandEntry->userActivityEventType = eventType; |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2719 | commandEntry->displayId = displayId; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2720 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2724 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2725 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2726 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2727 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2728 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2729 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2730 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2731 | ATRACE_NAME(message.c_str()); |
| 2732 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2733 | #if DEBUG_DISPATCH_CYCLE |
| 2734 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2735 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2736 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2737 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2738 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2739 | #endif |
| 2740 | |
| 2741 | // Skip this event if the connection status is not normal. |
| 2742 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2743 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2744 | #if DEBUG_DISPATCH_CYCLE |
| 2745 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2746 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2747 | #endif |
| 2748 | return; |
| 2749 | } |
| 2750 | |
| 2751 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2752 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2753 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2754 | "Entry type %s should not have FLAG_SPLIT", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2755 | NamedEnum::string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2756 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2757 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2758 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2759 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2760 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2761 | if (!splitMotionEntry) { |
| 2762 | return; // split event was dropped |
| 2763 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2764 | if (DEBUG_FOCUS) { |
| 2765 | ALOGD("channel '%s' ~ Split motion event.", |
| 2766 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2767 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2768 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2769 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2770 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2771 | return; |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2776 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2777 | } |
| 2778 | |
| 2779 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2780 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2781 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2782 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2783 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2784 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2785 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2786 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2787 | ATRACE_NAME(message.c_str()); |
| 2788 | } |
| 2789 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2790 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2791 | |
| 2792 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2793 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2794 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2795 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2796 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2797 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2798 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2799 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2800 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2801 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2802 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2803 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2804 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2805 | |
| 2806 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2807 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2808 | startDispatchCycleLocked(currentTime, connection); |
| 2809 | } |
| 2810 | } |
| 2811 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2812 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2813 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2814 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2815 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2816 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2817 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2818 | connection->getInputChannelName().c_str(), |
| 2819 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2820 | ATRACE_NAME(message.c_str()); |
| 2821 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2822 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2823 | if (!(inputTargetFlags & dispatchMode)) { |
| 2824 | return; |
| 2825 | } |
| 2826 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2827 | |
| 2828 | // This is a new event. |
| 2829 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2830 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2831 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2832 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2833 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2834 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2835 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2836 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2837 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2838 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2839 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2840 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2841 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2842 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2843 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2844 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2845 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2846 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2847 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2848 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2849 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2850 | return; // skip the inconsistent event |
| 2851 | } |
| 2852 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2853 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2855 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2856 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2857 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2858 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2859 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2860 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2861 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2862 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2863 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2864 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2865 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2866 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2867 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2868 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2869 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2870 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2871 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2872 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2873 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2874 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2875 | } |
| 2876 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2877 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2878 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2879 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2880 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2881 | "event", |
| 2882 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2883 | #endif |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2884 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2885 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2886 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2888 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2889 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2890 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2891 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2892 | } |
| 2893 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2894 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2895 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2896 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2897 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2898 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2899 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2900 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2901 | "event", |
| 2902 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2903 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2904 | return; // skip the inconsistent event |
| 2905 | } |
| 2906 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2907 | dispatchEntry->resolvedEventId = |
| 2908 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2909 | ? mIdGenerator.nextId() |
| 2910 | : motionEntry.id; |
| 2911 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2912 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2913 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2914 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2915 | ATRACE_NAME(message.c_str()); |
| 2916 | } |
| 2917 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2918 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 2919 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 2920 | // Skip reporting pointer down outside focus to the policy. |
| 2921 | break; |
| 2922 | } |
| 2923 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2924 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2925 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2926 | |
| 2927 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2929 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2930 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2931 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2932 | break; |
| 2933 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2934 | case EventEntry::Type::SENSOR: { |
| 2935 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 2936 | break; |
| 2937 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2938 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2939 | case EventEntry::Type::DEVICE_RESET: { |
| 2940 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2941 | NamedEnum::string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | // Remember that we are waiting for this dispatch to complete. |
| 2947 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2948 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2952 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 2953 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2956 | /** |
| 2957 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2958 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2959 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2960 | * We will see both launcher and wallpaper in that list. |
| 2961 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 2962 | * until the set of interacted connections changes. |
| 2963 | * |
| 2964 | * The following items are skipped, to reduce the logspam: |
| 2965 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 2966 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 2967 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 2968 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 2969 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2970 | */ |
| 2971 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 2972 | const std::vector<InputTarget>& targets) { |
| 2973 | // Skip ACTION_UP events, and all events other than keys and motions |
| 2974 | if (entry.type == EventEntry::Type::KEY) { |
| 2975 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2976 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 2977 | return; |
| 2978 | } |
| 2979 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 2980 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2981 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 2982 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2983 | return; |
| 2984 | } |
| 2985 | } else { |
| 2986 | return; // Not a key or a motion |
| 2987 | } |
| 2988 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 2989 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2990 | std::vector<sp<Connection>> newConnections; |
| 2991 | for (const InputTarget& target : targets) { |
| 2992 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 2993 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2994 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 2995 | } |
| 2996 | |
| 2997 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 2998 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2999 | if (connection == nullptr) { |
| 3000 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3001 | } |
| 3002 | newConnectionTokens.insert(std::move(token)); |
| 3003 | newConnections.emplace_back(connection); |
| 3004 | } |
| 3005 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3006 | return; // no change |
| 3007 | } |
| 3008 | mInteractionConnectionTokens = newConnectionTokens; |
| 3009 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3010 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3011 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3012 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3013 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3014 | std::string message = "Interaction with: " + targetList; |
| 3015 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3016 | message += "<none>"; |
| 3017 | } |
| 3018 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3019 | } |
| 3020 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3021 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3022 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3023 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3024 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3025 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3026 | return; |
| 3027 | } |
| 3028 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3029 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3030 | if (focusedToken == token) { |
| 3031 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3032 | return; |
| 3033 | } |
| 3034 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3035 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 3036 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3037 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3038 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3042 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3043 | if (ATRACE_ENABLED()) { |
| 3044 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3045 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3046 | ATRACE_NAME(message.c_str()); |
| 3047 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3048 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3049 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3050 | #endif |
| 3051 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3052 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3053 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3054 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3055 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3056 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3057 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3058 | |
| 3059 | // Publish the event. |
| 3060 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3061 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3062 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3063 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3064 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3065 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3067 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3068 | status = connection->inputPublisher |
| 3069 | .publishKeyEvent(dispatchEntry->seq, |
| 3070 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3071 | keyEntry.source, keyEntry.displayId, |
| 3072 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3073 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3074 | keyEntry.scanCode, keyEntry.metaState, |
| 3075 | keyEntry.repeatCount, keyEntry.downTime, |
| 3076 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3077 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3080 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3081 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3082 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3083 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3084 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3085 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3086 | // 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] | 3087 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3088 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3089 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3090 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3091 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3092 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3093 | // Don't apply window scale here since we don't want scale to affect raw |
| 3094 | // coordinates. The scale will be sent back to the client and applied |
| 3095 | // later when requesting relative coordinates. |
| 3096 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3097 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3098 | } |
| 3099 | usingCoords = scaledCoords; |
| 3100 | } |
| 3101 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3102 | // We don't want the dispatch target to know. |
| 3103 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3104 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3105 | scaledCoords[i].clear(); |
| 3106 | } |
| 3107 | usingCoords = scaledCoords; |
| 3108 | } |
| 3109 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3110 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3111 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3112 | |
| 3113 | // Publish the motion event. |
| 3114 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3115 | .publishMotionEvent(dispatchEntry->seq, |
| 3116 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3117 | motionEntry.deviceId, motionEntry.source, |
| 3118 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3119 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3120 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3121 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3122 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3123 | motionEntry.buttonState, |
| 3124 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3125 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3126 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3127 | motionEntry.xCursorPosition, |
| 3128 | motionEntry.yCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3129 | dispatchEntry->displaySize.x, |
| 3130 | dispatchEntry->displaySize.y, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3131 | motionEntry.downTime, motionEntry.eventTime, |
| 3132 | motionEntry.pointerCount, |
| 3133 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3134 | break; |
| 3135 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3136 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3137 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3138 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3139 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3140 | focusEntry.id, |
| 3141 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3142 | mInTouchMode); |
| 3143 | break; |
| 3144 | } |
| 3145 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3146 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3147 | const auto& captureEntry = |
| 3148 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3149 | status = connection->inputPublisher |
| 3150 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
| 3151 | captureEntry.pointerCaptureEnabled); |
| 3152 | break; |
| 3153 | } |
| 3154 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3155 | case EventEntry::Type::DRAG: { |
| 3156 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3157 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3158 | dragEntry.id, dragEntry.x, |
| 3159 | dragEntry.y, |
| 3160 | dragEntry.isExiting); |
| 3161 | break; |
| 3162 | } |
| 3163 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3164 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3165 | case EventEntry::Type::DEVICE_RESET: |
| 3166 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3167 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3168 | NamedEnum::string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3169 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3170 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | // Check the result. |
| 3174 | if (status) { |
| 3175 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3176 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3177 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3178 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3179 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3180 | "event to it, status=%s(%d)", |
| 3181 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3182 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3183 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3184 | } else { |
| 3185 | // Pipe is full and we are waiting for the app to finish process some events |
| 3186 | // before sending more events to it. |
| 3187 | #if DEBUG_DISPATCH_CYCLE |
| 3188 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3189 | "waiting for the application to catch up", |
| 3190 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3191 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3192 | } |
| 3193 | } else { |
| 3194 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3195 | "status=%s(%d)", |
| 3196 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3197 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3198 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3199 | } |
| 3200 | return; |
| 3201 | } |
| 3202 | |
| 3203 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3204 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3205 | connection->outboundQueue.end(), |
| 3206 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3207 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3208 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3209 | if (connection->responsive) { |
| 3210 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3211 | connection->inputChannel->getConnectionToken()); |
| 3212 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3213 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3214 | } |
| 3215 | } |
| 3216 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3217 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3218 | size_t size; |
| 3219 | switch (event.type) { |
| 3220 | case VerifiedInputEvent::Type::KEY: { |
| 3221 | size = sizeof(VerifiedKeyEvent); |
| 3222 | break; |
| 3223 | } |
| 3224 | case VerifiedInputEvent::Type::MOTION: { |
| 3225 | size = sizeof(VerifiedMotionEvent); |
| 3226 | break; |
| 3227 | } |
| 3228 | } |
| 3229 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3230 | return mHmacKeyManager.sign(start, size); |
| 3231 | } |
| 3232 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3233 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3234 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3235 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3236 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3237 | // Only sign events up and down events as the purely move events |
| 3238 | // are tied to their up/down counterparts so signing would be redundant. |
| 3239 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3240 | verifiedEvent.actionMasked = actionMasked; |
| 3241 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3242 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3243 | } |
| 3244 | return INVALID_HMAC; |
| 3245 | } |
| 3246 | |
| 3247 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3248 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3249 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3250 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3251 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3252 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3253 | } |
| 3254 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3255 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3257 | bool handled, nsecs_t consumeTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3258 | #if DEBUG_DISPATCH_CYCLE |
| 3259 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3260 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3261 | #endif |
| 3262 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3263 | if (connection->status == Connection::STATUS_BROKEN || |
| 3264 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3265 | return; |
| 3266 | } |
| 3267 | |
| 3268 | // Notify other system components and prepare to start the next dispatch cycle. |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3269 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3273 | const sp<Connection>& connection, |
| 3274 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3275 | #if DEBUG_DISPATCH_CYCLE |
| 3276 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3277 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3278 | #endif |
| 3279 | |
| 3280 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3281 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3282 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3283 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3284 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3285 | |
| 3286 | // The connection appears to be unrecoverably broken. |
| 3287 | // Ignore already broken or zombie connections. |
| 3288 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3289 | connection->status = Connection::STATUS_BROKEN; |
| 3290 | |
| 3291 | if (notify) { |
| 3292 | // Notify other system components. |
| 3293 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3298 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3299 | while (!queue.empty()) { |
| 3300 | DispatchEntry* dispatchEntry = queue.front(); |
| 3301 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3302 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3306 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3307 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3308 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3309 | } |
| 3310 | delete dispatchEntry; |
| 3311 | } |
| 3312 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3313 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3314 | std::scoped_lock _l(mLock); |
| 3315 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3316 | if (connection == nullptr) { |
| 3317 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3318 | connectionToken.get(), events); |
| 3319 | return 0; // remove the callback |
| 3320 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3321 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3322 | bool notify; |
| 3323 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3324 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3325 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3326 | "events=0x%x", |
| 3327 | connection->getInputChannelName().c_str(), events); |
| 3328 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3329 | } |
| 3330 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3331 | nsecs_t currentTime = now(); |
| 3332 | bool gotOne = false; |
| 3333 | status_t status = OK; |
| 3334 | for (;;) { |
| 3335 | Result<InputPublisher::ConsumerResponse> result = |
| 3336 | connection->inputPublisher.receiveConsumerResponse(); |
| 3337 | if (!result.ok()) { |
| 3338 | status = result.error().code(); |
| 3339 | break; |
| 3340 | } |
| 3341 | |
| 3342 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3343 | const InputPublisher::Finished& finish = |
| 3344 | std::get<InputPublisher::Finished>(*result); |
| 3345 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3346 | finish.consumeTime); |
| 3347 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3348 | if (shouldReportMetricsForConnection(*connection)) { |
| 3349 | const InputPublisher::Timeline& timeline = |
| 3350 | std::get<InputPublisher::Timeline>(*result); |
| 3351 | mLatencyTracker |
| 3352 | .trackGraphicsLatency(timeline.inputEventId, |
| 3353 | connection->inputChannel->getConnectionToken(), |
| 3354 | std::move(timeline.graphicsTimeline)); |
| 3355 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3356 | } |
| 3357 | gotOne = true; |
| 3358 | } |
| 3359 | if (gotOne) { |
| 3360 | runCommandsLockedInterruptible(); |
| 3361 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | return 1; |
| 3363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3364 | } |
| 3365 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3366 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3367 | if (notify) { |
| 3368 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3369 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3370 | status); |
| 3371 | } |
| 3372 | } else { |
| 3373 | // Monitor channels are never explicitly unregistered. |
| 3374 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3375 | const bool stillHaveWindowHandle = |
| 3376 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3377 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3378 | if (notify) { |
| 3379 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3380 | connection->getInputChannelName().c_str(), events); |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | // Remove the channel. |
| 3385 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3386 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3387 | } |
| 3388 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3389 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3391 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3392 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3393 | } |
| 3394 | } |
| 3395 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3396 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3397 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3398 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3399 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3400 | } |
| 3401 | |
| 3402 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3403 | const CancelationOptions& options, |
| 3404 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3405 | for (const auto& it : monitorsByDisplay) { |
| 3406 | const std::vector<Monitor>& monitors = it.second; |
| 3407 | for (const Monitor& monitor : monitors) { |
| 3408 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3409 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3413 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3414 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3415 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3416 | if (connection == nullptr) { |
| 3417 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3418 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3419 | |
| 3420 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3424 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3425 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3426 | return; |
| 3427 | } |
| 3428 | |
| 3429 | nsecs_t currentTime = now(); |
| 3430 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3431 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3432 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3433 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3434 | if (cancelationEvents.empty()) { |
| 3435 | return; |
| 3436 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3437 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3438 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3439 | "with reality: %s, mode=%d.", |
| 3440 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3441 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3442 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3443 | |
| 3444 | InputTarget target; |
| 3445 | sp<InputWindowHandle> windowHandle = |
| 3446 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3447 | if (windowHandle != nullptr) { |
| 3448 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3449 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3450 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3451 | } |
| 3452 | target.inputChannel = connection->inputChannel; |
| 3453 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3454 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3455 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3456 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3457 | switch (cancelationEventEntry->type) { |
| 3458 | case EventEntry::Type::KEY: { |
| 3459 | logOutboundKeyDetails("cancel - ", |
| 3460 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3461 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3463 | case EventEntry::Type::MOTION: { |
| 3464 | logOutboundMotionDetails("cancel - ", |
| 3465 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3466 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3467 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3468 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3469 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3470 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3471 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3472 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3473 | break; |
| 3474 | } |
| 3475 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3476 | case EventEntry::Type::DEVICE_RESET: |
| 3477 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3478 | 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] | 3479 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3480 | break; |
| 3481 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 | } |
| 3483 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3484 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3485 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3486 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3487 | |
| 3488 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3489 | } |
| 3490 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3491 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3492 | const sp<Connection>& connection) { |
| 3493 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3494 | return; |
| 3495 | } |
| 3496 | |
| 3497 | nsecs_t currentTime = now(); |
| 3498 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3499 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3500 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3501 | |
| 3502 | if (downEvents.empty()) { |
| 3503 | return; |
| 3504 | } |
| 3505 | |
| 3506 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3507 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3508 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3509 | #endif |
| 3510 | |
| 3511 | InputTarget target; |
| 3512 | sp<InputWindowHandle> windowHandle = |
| 3513 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3514 | if (windowHandle != nullptr) { |
| 3515 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3516 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3517 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3518 | } |
| 3519 | target.inputChannel = connection->inputChannel; |
| 3520 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3521 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3522 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3523 | switch (downEventEntry->type) { |
| 3524 | case EventEntry::Type::MOTION: { |
| 3525 | logOutboundMotionDetails("down - ", |
| 3526 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3527 | break; |
| 3528 | } |
| 3529 | |
| 3530 | case EventEntry::Type::KEY: |
| 3531 | case EventEntry::Type::FOCUS: |
| 3532 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3533 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3534 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3535 | case EventEntry::Type::SENSOR: |
| 3536 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3537 | 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] | 3538 | NamedEnum::string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3539 | break; |
| 3540 | } |
| 3541 | } |
| 3542 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3543 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3544 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | startDispatchCycleLocked(currentTime, connection); |
| 3548 | } |
| 3549 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3550 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3551 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3552 | ALOG_ASSERT(pointerIds.value != 0); |
| 3553 | |
| 3554 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3555 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3556 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3557 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3558 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3559 | uint32_t splitPointerCount = 0; |
| 3560 | |
| 3561 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3562 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3563 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3564 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3565 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3566 | if (pointerIds.hasBit(pointerId)) { |
| 3567 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3568 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3569 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3570 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3571 | splitPointerCount += 1; |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | if (splitPointerCount != pointerIds.count()) { |
| 3576 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3577 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3578 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3579 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3580 | // in this way. |
| 3581 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3582 | "we expected there to be %d pointers. This probably means we received " |
| 3583 | "a broken sequence of pointer ids from the input device.", |
| 3584 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3585 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | } |
| 3587 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3588 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3589 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3590 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3591 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3593 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3594 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3595 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3596 | if (pointerIds.hasBit(pointerId)) { |
| 3597 | if (pointerIds.count() == 1) { |
| 3598 | // The first/last pointer went down/up. |
| 3599 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3600 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3601 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3602 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3603 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | } else { |
| 3605 | // A secondary pointer went down/up. |
| 3606 | uint32_t splitPointerIndex = 0; |
| 3607 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3608 | splitPointerIndex += 1; |
| 3609 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3610 | action = maskedAction | |
| 3611 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3612 | } |
| 3613 | } else { |
| 3614 | // An unrelated pointer changed. |
| 3615 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3616 | } |
| 3617 | } |
| 3618 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3619 | int32_t newId = mIdGenerator.nextId(); |
| 3620 | if (ATRACE_ENABLED()) { |
| 3621 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3622 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3623 | originalMotionEntry.id, newId); |
| 3624 | ATRACE_NAME(message.c_str()); |
| 3625 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3626 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3627 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3628 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3629 | originalMotionEntry.displayId, |
| 3630 | originalMotionEntry.policyFlags, action, |
| 3631 | originalMotionEntry.actionButton, |
| 3632 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3633 | originalMotionEntry.buttonState, |
| 3634 | originalMotionEntry.classification, |
| 3635 | originalMotionEntry.edgeFlags, |
| 3636 | originalMotionEntry.xPrecision, |
| 3637 | originalMotionEntry.yPrecision, |
| 3638 | originalMotionEntry.xCursorPosition, |
| 3639 | originalMotionEntry.yCursorPosition, |
| 3640 | originalMotionEntry.downTime, splitPointerCount, |
| 3641 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3643 | if (originalMotionEntry.injectionState) { |
| 3644 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3645 | splitMotionEntry->injectionState->refCount += 1; |
| 3646 | } |
| 3647 | |
| 3648 | return splitMotionEntry; |
| 3649 | } |
| 3650 | |
| 3651 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3652 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3653 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3654 | #endif |
| 3655 | |
| 3656 | bool needWake; |
| 3657 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3658 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3659 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3660 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3661 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3662 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3663 | } // release lock |
| 3664 | |
| 3665 | if (needWake) { |
| 3666 | mLooper->wake(); |
| 3667 | } |
| 3668 | } |
| 3669 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3670 | /** |
| 3671 | * If one of the meta shortcuts is detected, process them here: |
| 3672 | * Meta + Backspace -> generate BACK |
| 3673 | * Meta + Enter -> generate HOME |
| 3674 | * This will potentially overwrite keyCode and metaState. |
| 3675 | */ |
| 3676 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3677 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3678 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3679 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3680 | if (keyCode == AKEYCODE_DEL) { |
| 3681 | newKeyCode = AKEYCODE_BACK; |
| 3682 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3683 | newKeyCode = AKEYCODE_HOME; |
| 3684 | } |
| 3685 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3686 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3687 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3688 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3689 | keyCode = newKeyCode; |
| 3690 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3691 | } |
| 3692 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3693 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3694 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3695 | // 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] | 3696 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3697 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3698 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3699 | if (replacementIt != mReplacedKeys.end()) { |
| 3700 | keyCode = replacementIt->second; |
| 3701 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3702 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3703 | } |
| 3704 | } |
| 3705 | } |
| 3706 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3707 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3708 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3709 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3710 | "policyFlags=0x%x, action=0x%x, " |
| 3711 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3712 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3713 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3714 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3715 | #endif |
| 3716 | if (!validateKeyEvent(args->action)) { |
| 3717 | return; |
| 3718 | } |
| 3719 | |
| 3720 | uint32_t policyFlags = args->policyFlags; |
| 3721 | int32_t flags = args->flags; |
| 3722 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3723 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3724 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3725 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3726 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3727 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3728 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3731 | metaState |= AMETA_FUNCTION_ON; |
| 3732 | } |
| 3733 | |
| 3734 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3735 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3736 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3737 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3738 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3739 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3740 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3741 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3742 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3743 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3744 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3745 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3746 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3747 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3748 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3749 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3750 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3751 | bool needWake; |
| 3752 | { // acquire lock |
| 3753 | mLock.lock(); |
| 3754 | |
| 3755 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3756 | mLock.unlock(); |
| 3757 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3758 | policyFlags |= POLICY_FLAG_FILTERED | POLICY_FLAG_INPUTFILTER_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3759 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3760 | return; // event was consumed by the filter |
| 3761 | } |
| 3762 | |
| 3763 | mLock.lock(); |
| 3764 | } |
| 3765 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3766 | std::unique_ptr<KeyEntry> newEntry = |
| 3767 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3768 | args->displayId, policyFlags, args->action, flags, |
| 3769 | keyCode, args->scanCode, metaState, repeatCount, |
| 3770 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3771 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3772 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | mLock.unlock(); |
| 3774 | } // release lock |
| 3775 | |
| 3776 | if (needWake) { |
| 3777 | mLooper->wake(); |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3782 | return mInputFilterEnabled; |
| 3783 | } |
| 3784 | |
| 3785 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3786 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3787 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3788 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3789 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3790 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3791 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3792 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3793 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3794 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3795 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3797 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3798 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3799 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3800 | "orientation=%f", |
| 3801 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3802 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3803 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3804 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3805 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3806 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3807 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3808 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3809 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3810 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3811 | } |
| 3812 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3813 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3814 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3815 | return; |
| 3816 | } |
| 3817 | |
| 3818 | uint32_t policyFlags = args->policyFlags; |
| 3819 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3820 | |
| 3821 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3822 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3823 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3824 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3825 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3827 | |
| 3828 | bool needWake; |
| 3829 | { // acquire lock |
| 3830 | mLock.lock(); |
| 3831 | |
| 3832 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3833 | mLock.unlock(); |
| 3834 | |
| 3835 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3836 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3837 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3838 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3839 | args->metaState, args->buttonState, args->classification, transform, |
| 3840 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3841 | args->yCursorPosition, AMOTION_EVENT_INVALID_DISPLAY_SIZE, |
| 3842 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3843 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3844 | |
| 3845 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3846 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3847 | return; // event was consumed by the filter |
| 3848 | } |
| 3849 | |
| 3850 | mLock.lock(); |
| 3851 | } |
| 3852 | |
| 3853 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3854 | std::unique_ptr<MotionEntry> newEntry = |
| 3855 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3856 | args->source, args->displayId, policyFlags, |
| 3857 | args->action, args->actionButton, args->flags, |
| 3858 | args->metaState, args->buttonState, |
| 3859 | args->classification, args->edgeFlags, |
| 3860 | args->xPrecision, args->yPrecision, |
| 3861 | args->xCursorPosition, args->yCursorPosition, |
| 3862 | args->downTime, args->pointerCount, |
| 3863 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3865 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3866 | mLock.unlock(); |
| 3867 | } // release lock |
| 3868 | |
| 3869 | if (needWake) { |
| 3870 | mLooper->wake(); |
| 3871 | } |
| 3872 | } |
| 3873 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3874 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
| 3875 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3876 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3877 | " sensorType=%s", |
| 3878 | args->id, args->eventTime, args->deviceId, args->source, |
| 3879 | NamedEnum::string(args->sensorType).c_str()); |
| 3880 | #endif |
| 3881 | |
| 3882 | bool needWake; |
| 3883 | { // acquire lock |
| 3884 | mLock.lock(); |
| 3885 | |
| 3886 | // Just enqueue a new sensor event. |
| 3887 | std::unique_ptr<SensorEntry> newEntry = |
| 3888 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 3889 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 3890 | args->sensorType, args->accuracy, |
| 3891 | args->accuracyChanged, args->values); |
| 3892 | |
| 3893 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 3894 | mLock.unlock(); |
| 3895 | } // release lock |
| 3896 | |
| 3897 | if (needWake) { |
| 3898 | mLooper->wake(); |
| 3899 | } |
| 3900 | } |
| 3901 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3902 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
| 3903 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3904 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 3905 | args->deviceId, args->isOn); |
| 3906 | #endif |
| 3907 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 3908 | } |
| 3909 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3910 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3911 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3915 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3916 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3917 | "switchMask=0x%08x", |
| 3918 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3919 | #endif |
| 3920 | |
| 3921 | uint32_t policyFlags = args->policyFlags; |
| 3922 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3923 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3927 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3928 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3929 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3930 | #endif |
| 3931 | |
| 3932 | bool needWake; |
| 3933 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3934 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3935 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3936 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 3937 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 3938 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3939 | } // release lock |
| 3940 | |
| 3941 | if (needWake) { |
| 3942 | mLooper->wake(); |
| 3943 | } |
| 3944 | } |
| 3945 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3946 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
| 3947 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3948 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
| 3949 | args->enabled ? "true" : "false"); |
| 3950 | #endif |
| 3951 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3952 | bool needWake; |
| 3953 | { // acquire lock |
| 3954 | std::scoped_lock _l(mLock); |
| 3955 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
| 3956 | args->enabled); |
| 3957 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 3958 | } // release lock |
| 3959 | |
| 3960 | if (needWake) { |
| 3961 | mLooper->wake(); |
| 3962 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3963 | } |
| 3964 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3965 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 3966 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 3967 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3969 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3970 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 3971 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3973 | 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] | 3974 | |
| 3975 | policyFlags |= POLICY_FLAG_INJECTED; |
| 3976 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3977 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3978 | } |
| 3979 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3980 | // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events |
| 3981 | // that have gone through the InputFilter. If the event passed through the InputFilter, |
| 3982 | // but did not get modified, assign the provided device id. If the InputFilter modifies the |
| 3983 | // events in any way, it is responsible for removing this flag. |
| 3984 | // If the injected event originated from accessibility, assign the accessibility device id, |
| 3985 | // so that it can be distinguished from regular injected events. |
| 3986 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
| 3987 | if (policyFlags & POLICY_FLAG_INPUTFILTER_TRUSTED) { |
| 3988 | resolvedDeviceId = event->getDeviceId(); |
| 3989 | } else if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 3990 | resolvedDeviceId = ACCESSIBILITY_DEVICE_ID; |
| 3991 | } |
| 3992 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3993 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3994 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3995 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3996 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 3997 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3998 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3999 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4000 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4002 | int32_t flags = incomingKey.getFlags(); |
| 4003 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4004 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4005 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4006 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4007 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4008 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4009 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4010 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4011 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4013 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4014 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4015 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4016 | |
| 4017 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4018 | android::base::Timer t; |
| 4019 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4020 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4021 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4022 | std::to_string(t.duration().count()).c_str()); |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4027 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4028 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4029 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4030 | incomingKey.getDisplayId(), policyFlags, action, |
| 4031 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4032 | incomingKey.getRepeatCount(), |
| 4033 | incomingKey.getDownTime()); |
| 4034 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4035 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4038 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4039 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 4040 | int32_t action = motionEvent.getAction(); |
| 4041 | size_t pointerCount = motionEvent.getPointerCount(); |
| 4042 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
| 4043 | int32_t actionButton = motionEvent.getActionButton(); |
| 4044 | int32_t displayId = motionEvent.getDisplayId(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4045 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4046 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4050 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4051 | android::base::Timer t; |
| 4052 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4053 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4054 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4055 | std::to_string(t.duration().count()).c_str()); |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4060 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4061 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4062 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4063 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4064 | resolvedDeviceId, motionEvent.getSource(), |
| 4065 | motionEvent.getDisplayId(), policyFlags, action, |
| 4066 | actionButton, motionEvent.getFlags(), |
| 4067 | motionEvent.getMetaState(), |
| 4068 | motionEvent.getButtonState(), |
| 4069 | motionEvent.getClassification(), |
| 4070 | motionEvent.getEdgeFlags(), |
| 4071 | motionEvent.getXPrecision(), |
| 4072 | motionEvent.getYPrecision(), |
| 4073 | motionEvent.getRawXCursorPosition(), |
| 4074 | motionEvent.getRawYCursorPosition(), |
| 4075 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4076 | pointerProperties, samplePointerCoords, |
| 4077 | motionEvent.getXOffset(), |
| 4078 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4079 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4080 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4081 | sampleEventTimes += 1; |
| 4082 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4083 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4084 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4085 | resolvedDeviceId, motionEvent.getSource(), |
| 4086 | motionEvent.getDisplayId(), policyFlags, |
| 4087 | action, actionButton, motionEvent.getFlags(), |
| 4088 | motionEvent.getMetaState(), |
| 4089 | motionEvent.getButtonState(), |
| 4090 | motionEvent.getClassification(), |
| 4091 | motionEvent.getEdgeFlags(), |
| 4092 | motionEvent.getXPrecision(), |
| 4093 | motionEvent.getYPrecision(), |
| 4094 | motionEvent.getRawXCursorPosition(), |
| 4095 | motionEvent.getRawYCursorPosition(), |
| 4096 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4097 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4098 | samplePointerCoords, motionEvent.getXOffset(), |
| 4099 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4100 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4101 | } |
| 4102 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4103 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4104 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4105 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4106 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4107 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4111 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4112 | injectionState->injectionIsAsync = true; |
| 4113 | } |
| 4114 | |
| 4115 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4116 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4117 | |
| 4118 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4119 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4120 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4121 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | mLock.unlock(); |
| 4125 | |
| 4126 | if (needWake) { |
| 4127 | mLooper->wake(); |
| 4128 | } |
| 4129 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4130 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4131 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4132 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4133 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4134 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4135 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4136 | } else { |
| 4137 | for (;;) { |
| 4138 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4139 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4140 | break; |
| 4141 | } |
| 4142 | |
| 4143 | nsecs_t remainingTimeout = endTime - now(); |
| 4144 | if (remainingTimeout <= 0) { |
| 4145 | #if DEBUG_INJECTION |
| 4146 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4147 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4148 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4149 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4150 | break; |
| 4151 | } |
| 4152 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4153 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4154 | } |
| 4155 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4156 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4157 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4158 | while (injectionState->pendingForegroundDispatches != 0) { |
| 4159 | #if DEBUG_INJECTION |
| 4160 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4161 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | #endif |
| 4163 | nsecs_t remainingTimeout = endTime - now(); |
| 4164 | if (remainingTimeout <= 0) { |
| 4165 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4166 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4167 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4168 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4169 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4170 | break; |
| 4171 | } |
| 4172 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4173 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4174 | } |
| 4175 | } |
| 4176 | } |
| 4177 | |
| 4178 | injectionState->release(); |
| 4179 | } // release lock |
| 4180 | |
| 4181 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4182 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4183 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4184 | #endif |
| 4185 | |
| 4186 | return injectionResult; |
| 4187 | } |
| 4188 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4189 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4190 | std::array<uint8_t, 32> calculatedHmac; |
| 4191 | std::unique_ptr<VerifiedInputEvent> result; |
| 4192 | switch (event.getType()) { |
| 4193 | case AINPUT_EVENT_TYPE_KEY: { |
| 4194 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4195 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4196 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4197 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4198 | break; |
| 4199 | } |
| 4200 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4201 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4202 | VerifiedMotionEvent verifiedMotionEvent = |
| 4203 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4204 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4205 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4206 | break; |
| 4207 | } |
| 4208 | default: { |
| 4209 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4210 | return nullptr; |
| 4211 | } |
| 4212 | } |
| 4213 | if (calculatedHmac == INVALID_HMAC) { |
| 4214 | return nullptr; |
| 4215 | } |
| 4216 | if (calculatedHmac != event.getHmac()) { |
| 4217 | return nullptr; |
| 4218 | } |
| 4219 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4220 | } |
| 4221 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4222 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4223 | return injectorUid == 0 || |
| 4224 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4227 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4228 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4229 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4230 | if (injectionState) { |
| 4231 | #if DEBUG_INJECTION |
| 4232 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4233 | "injectorPid=%d, injectorUid=%d", |
| 4234 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4235 | #endif |
| 4236 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4237 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4238 | // Log the outcome since the injector did not wait for the injection result. |
| 4239 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4240 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4241 | ALOGV("Asynchronous input event injection succeeded."); |
| 4242 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4243 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4244 | ALOGW("Asynchronous input event injection failed."); |
| 4245 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4246 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4247 | ALOGW("Asynchronous input event injection permission denied."); |
| 4248 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4249 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4250 | ALOGW("Asynchronous input event injection timed out."); |
| 4251 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4252 | case InputEventInjectionResult::PENDING: |
| 4253 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4254 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4255 | } |
| 4256 | } |
| 4257 | |
| 4258 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4259 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4260 | } |
| 4261 | } |
| 4262 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4263 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4264 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | if (injectionState) { |
| 4266 | injectionState->pendingForegroundDispatches += 1; |
| 4267 | } |
| 4268 | } |
| 4269 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4270 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4271 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4272 | if (injectionState) { |
| 4273 | injectionState->pendingForegroundDispatches -= 1; |
| 4274 | |
| 4275 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4276 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | } |
| 4278 | } |
| 4279 | } |
| 4280 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4281 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4282 | int32_t displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4283 | static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; |
| 4284 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4285 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4286 | } |
| 4287 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4289 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4290 | if (windowHandleToken == nullptr) { |
| 4291 | return nullptr; |
| 4292 | } |
| 4293 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4294 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4295 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4296 | for (const sp<InputWindowHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4297 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4298 | return windowHandle; |
| 4299 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4300 | } |
| 4301 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4302 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | } |
| 4304 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4305 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4306 | int displayId) const { |
| 4307 | if (windowHandleToken == nullptr) { |
| 4308 | return nullptr; |
| 4309 | } |
| 4310 | |
| 4311 | for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
| 4312 | if (windowHandle->getToken() == windowHandleToken) { |
| 4313 | return windowHandle; |
| 4314 | } |
| 4315 | } |
| 4316 | return nullptr; |
| 4317 | } |
| 4318 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4319 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
| 4320 | const sp<InputWindowHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4321 | for (auto& it : mWindowHandlesByDisplay) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4322 | const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4323 | for (const sp<InputWindowHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4324 | if (handle->getId() == windowHandle->getId() && |
| 4325 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4326 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4327 | ALOGE("Found window %s in display %" PRId32 |
| 4328 | ", but it should belong to display %" PRId32, |
| 4329 | windowHandle->getName().c_str(), it.first, |
| 4330 | windowHandle->getInfo()->displayId); |
| 4331 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4332 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4333 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | } |
| 4335 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4336 | return nullptr; |
| 4337 | } |
| 4338 | |
| 4339 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
| 4340 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4341 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4342 | } |
| 4343 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4344 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { |
| 4345 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4346 | const bool noInputChannel = |
| 4347 | windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4348 | if (connection != nullptr && noInputChannel) { |
| 4349 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4350 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4351 | return false; |
| 4352 | } |
| 4353 | |
| 4354 | if (connection == nullptr) { |
| 4355 | if (!noInputChannel) { |
| 4356 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4357 | } |
| 4358 | return false; |
| 4359 | } |
| 4360 | if (!connection->responsive) { |
| 4361 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4362 | return false; |
| 4363 | } |
| 4364 | return true; |
| 4365 | } |
| 4366 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4367 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4368 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4369 | auto connectionIt = mConnectionsByToken.find(token); |
| 4370 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4371 | return nullptr; |
| 4372 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4373 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4374 | } |
| 4375 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4376 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
| 4377 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
| 4378 | if (inputWindowHandles.empty()) { |
| 4379 | // Remove all handles on a display if there are no windows left. |
| 4380 | mWindowHandlesByDisplay.erase(displayId); |
| 4381 | return; |
| 4382 | } |
| 4383 | |
| 4384 | // Since we compare the pointer of input window handles across window updates, we need |
| 4385 | // to make sure the handle object for the same window stays unchanged across updates. |
| 4386 | const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4387 | std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4388 | for (const sp<InputWindowHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4389 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | std::vector<sp<InputWindowHandle>> newHandles; |
| 4393 | for (const sp<InputWindowHandle>& handle : inputWindowHandles) { |
| 4394 | if (!handle->updateInfo()) { |
| 4395 | // handle no longer valid |
| 4396 | continue; |
| 4397 | } |
| 4398 | |
| 4399 | const InputWindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 4400 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4401 | const bool noInputChannel = |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4402 | info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4403 | const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || |
| 4404 | !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4405 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4406 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4407 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4408 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4409 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | if (info->displayId != displayId) { |
| 4413 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4414 | handle->getName().c_str(), displayId, info->displayId); |
| 4415 | continue; |
| 4416 | } |
| 4417 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4418 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4419 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4420 | const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4421 | oldHandle->updateFrom(handle); |
| 4422 | newHandles.push_back(oldHandle); |
| 4423 | } else { |
| 4424 | newHandles.push_back(handle); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | // Insert or replace |
| 4429 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4430 | } |
| 4431 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4432 | void InputDispatcher::setInputWindows( |
| 4433 | const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { |
| 4434 | { // acquire lock |
| 4435 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4436 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4437 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4438 | } |
| 4439 | } |
| 4440 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4441 | mLooper->wake(); |
| 4442 | } |
| 4443 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4444 | /** |
| 4445 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4446 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4447 | * If set an empty list, remove all handles from the specific display. |
| 4448 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4449 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4450 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4451 | void InputDispatcher::setInputWindowsLocked( |
| 4452 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4453 | if (DEBUG_FOCUS) { |
| 4454 | std::string windowList; |
| 4455 | for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { |
| 4456 | windowList += iwh->getName() + " "; |
| 4457 | } |
| 4458 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4459 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4460 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4461 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
| 4462 | for (const sp<InputWindowHandle>& window : inputWindowHandles) { |
| 4463 | const bool noInputWindow = |
| 4464 | window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4465 | if (noInputWindow && window->getToken() != nullptr) { |
| 4466 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4467 | window->getName().c_str()); |
| 4468 | window->releaseChannel(); |
| 4469 | } |
| 4470 | } |
| 4471 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4472 | // Copy old handles for release if they are no longer present. |
| 4473 | const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4474 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4475 | // Save the old windows' orientation by ID before it gets updated. |
| 4476 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
| 4477 | for (const sp<InputWindowHandle>& handle : oldWindowHandles) { |
| 4478 | oldWindowOrientations.emplace(handle->getId(), |
| 4479 | handle->getInfo()->transform.getOrientation()); |
| 4480 | } |
| 4481 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4482 | updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4483 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4484 | const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 4485 | if (mLastHoverWindowHandle && |
| 4486 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4487 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4488 | mLastHoverWindowHandle = nullptr; |
| 4489 | } |
| 4490 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4491 | std::optional<FocusResolver::FocusChanges> changes = |
| 4492 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4493 | if (changes) { |
| 4494 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4495 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4496 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4497 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4498 | mTouchStatesByDisplay.find(displayId); |
| 4499 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4500 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4501 | for (size_t i = 0; i < state.windows.size();) { |
| 4502 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4503 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4504 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4505 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4506 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4507 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4508 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4509 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4510 | if (touchedInputChannel != nullptr) { |
| 4511 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4512 | "touched window was removed"); |
| 4513 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4515 | state.windows.erase(state.windows.begin() + i); |
| 4516 | } else { |
| 4517 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4518 | } |
| 4519 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4520 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4521 | // 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] | 4522 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4523 | if (mDragState && |
| 4524 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4525 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4526 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4527 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4528 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4529 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4530 | if (isPerWindowInputRotationEnabled()) { |
| 4531 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4532 | // pointer events if necessary. |
| 4533 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
| 4534 | const sp<InputWindowHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4535 | if (newWindowHandle != nullptr && |
| 4536 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4537 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4538 | std::shared_ptr<InputChannel> inputChannel = |
| 4539 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4540 | if (inputChannel != nullptr) { |
| 4541 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4542 | "touched window's orientation changed"); |
| 4543 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4544 | } |
| 4545 | } |
| 4546 | } |
| 4547 | } |
| 4548 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4549 | // Release information for windows that are no longer present. |
| 4550 | // This ensures that unused input channels are released promptly. |
| 4551 | // Otherwise, they might stick around until the window handle is destroyed |
| 4552 | // which might not happen until the next GC. |
| 4553 | for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4554 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4555 | if (DEBUG_FOCUS) { |
| 4556 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4557 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4558 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4559 | // To avoid making too many calls into the compat framework, only |
| 4560 | // check for window flags when windows are going away. |
| 4561 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4562 | // in order to gather some data about the flag usage |
| 4563 | if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { |
| 4564 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4565 | oldWindowHandle->getName().c_str()); |
| 4566 | if (mCompatService != nullptr) { |
| 4567 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4568 | oldWindowHandle->getInfo()->ownerUid); |
| 4569 | } |
| 4570 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4571 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4572 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4576 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4577 | if (DEBUG_FOCUS) { |
| 4578 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4579 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4580 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4581 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4582 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4583 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4584 | } // release lock |
| 4585 | |
| 4586 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4587 | mLooper->wake(); |
| 4588 | } |
| 4589 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4590 | void InputDispatcher::setFocusedApplicationLocked( |
| 4591 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4592 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4593 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4594 | |
| 4595 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4596 | return; // This application is already focused. No need to wake up or change anything. |
| 4597 | } |
| 4598 | |
| 4599 | // Set the new application handle. |
| 4600 | if (inputApplicationHandle != nullptr) { |
| 4601 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4602 | } else { |
| 4603 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4604 | } |
| 4605 | |
| 4606 | // No matter what the old focused application was, stop waiting on it because it is |
| 4607 | // no longer focused. |
| 4608 | resetNoFocusedWindowTimeoutLocked(); |
| 4609 | } |
| 4610 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4611 | /** |
| 4612 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4613 | * the display not specified. |
| 4614 | * |
| 4615 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4616 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4617 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4618 | * display. The display-specified events won't be affected. |
| 4619 | */ |
| 4620 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4621 | if (DEBUG_FOCUS) { |
| 4622 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4623 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4624 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4625 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4626 | |
| 4627 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4628 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4629 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4630 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4631 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4632 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4633 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4634 | CancelationOptions |
| 4635 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4636 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4637 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4638 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4639 | } |
| 4640 | } |
| 4641 | mFocusedDisplayId = displayId; |
| 4642 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4643 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4644 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4645 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4646 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4647 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4648 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4649 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4650 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4651 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4652 | } |
| 4653 | } |
| 4654 | } |
| 4655 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4656 | if (DEBUG_FOCUS) { |
| 4657 | logDispatchStateLocked(); |
| 4658 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4659 | } // release lock |
| 4660 | |
| 4661 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4662 | mLooper->wake(); |
| 4663 | } |
| 4664 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4665 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4666 | if (DEBUG_FOCUS) { |
| 4667 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4668 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4669 | |
| 4670 | bool changed; |
| 4671 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4672 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4673 | |
| 4674 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4675 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4676 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | if (mDispatchEnabled && !enabled) { |
| 4680 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4681 | } |
| 4682 | |
| 4683 | mDispatchEnabled = enabled; |
| 4684 | mDispatchFrozen = frozen; |
| 4685 | changed = true; |
| 4686 | } else { |
| 4687 | changed = false; |
| 4688 | } |
| 4689 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4690 | if (DEBUG_FOCUS) { |
| 4691 | logDispatchStateLocked(); |
| 4692 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4693 | } // release lock |
| 4694 | |
| 4695 | if (changed) { |
| 4696 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4697 | mLooper->wake(); |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4702 | if (DEBUG_FOCUS) { |
| 4703 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4704 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4705 | |
| 4706 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4707 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4708 | |
| 4709 | if (mInputFilterEnabled == enabled) { |
| 4710 | return; |
| 4711 | } |
| 4712 | |
| 4713 | mInputFilterEnabled = enabled; |
| 4714 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4715 | } // release lock |
| 4716 | |
| 4717 | // Wake up poll loop since there might be work to do to drop everything. |
| 4718 | mLooper->wake(); |
| 4719 | } |
| 4720 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4721 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4722 | std::scoped_lock lock(mLock); |
| 4723 | mInTouchMode = inTouchMode; |
| 4724 | } |
| 4725 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4726 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4727 | if (opacity < 0 || opacity > 1) { |
| 4728 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4729 | return; |
| 4730 | } |
| 4731 | |
| 4732 | std::scoped_lock lock(mLock); |
| 4733 | mMaximumObscuringOpacityForTouch = opacity; |
| 4734 | } |
| 4735 | |
| 4736 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4737 | std::scoped_lock lock(mLock); |
| 4738 | mBlockUntrustedTouchesMode = mode; |
| 4739 | } |
| 4740 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4741 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4742 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4743 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4744 | if (DEBUG_FOCUS) { |
| 4745 | ALOGD("Trivial transfer to same window."); |
| 4746 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4747 | return true; |
| 4748 | } |
| 4749 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4750 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4751 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4752 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4753 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4754 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4755 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4756 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4757 | return false; |
| 4758 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4759 | if (DEBUG_FOCUS) { |
| 4760 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4761 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4762 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4763 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4764 | if (DEBUG_FOCUS) { |
| 4765 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4766 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4767 | return false; |
| 4768 | } |
| 4769 | |
| 4770 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4771 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4772 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4773 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4774 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4775 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4776 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4777 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4778 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4779 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4780 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4781 | int32_t newTargetFlags = oldTargetFlags & |
| 4782 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4783 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4784 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4785 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4786 | // Store the dragging window. |
| 4787 | if (isDragDrop) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4788 | mDragState = std::make_unique<DragState>(toWindowHandle); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4789 | } |
| 4790 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4791 | found = true; |
| 4792 | goto Found; |
| 4793 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4794 | } |
| 4795 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4796 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4797 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4798 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4799 | if (DEBUG_FOCUS) { |
| 4800 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4801 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4802 | return false; |
| 4803 | } |
| 4804 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4805 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4806 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4807 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4808 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4809 | CancelationOptions |
| 4810 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4811 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4812 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4813 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4814 | } |
| 4815 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4816 | if (DEBUG_FOCUS) { |
| 4817 | logDispatchStateLocked(); |
| 4818 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4819 | } // release lock |
| 4820 | |
| 4821 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4822 | mLooper->wake(); |
| 4823 | return true; |
| 4824 | } |
| 4825 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4826 | // Binder call |
| 4827 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 4828 | sp<IBinder> fromToken; |
| 4829 | { // acquire lock |
| 4830 | std::scoped_lock _l(mLock); |
| 4831 | |
| 4832 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); |
| 4833 | if (toWindowHandle == nullptr) { |
| 4834 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 4835 | return false; |
| 4836 | } |
| 4837 | |
| 4838 | const int32_t displayId = toWindowHandle->getInfo()->displayId; |
| 4839 | |
| 4840 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 4841 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 4842 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", |
| 4843 | displayId); |
| 4844 | return false; |
| 4845 | } |
| 4846 | |
| 4847 | TouchState& state = touchStateIt->second; |
| 4848 | if (state.windows.size() != 1) { |
| 4849 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", |
| 4850 | state.windows.size()); |
| 4851 | return false; |
| 4852 | } |
| 4853 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 4854 | fromToken = touchedWindow.windowHandle->getToken(); |
| 4855 | } // release lock |
| 4856 | |
| 4857 | return transferTouchFocus(fromToken, destChannelToken); |
| 4858 | } |
| 4859 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4861 | if (DEBUG_FOCUS) { |
| 4862 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4863 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4864 | |
| 4865 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4866 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4867 | |
| 4868 | resetKeyRepeatLocked(); |
| 4869 | releasePendingEventLocked(); |
| 4870 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4871 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4872 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4873 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4874 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4875 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4876 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4880 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4881 | dumpDispatchStateLocked(dump); |
| 4882 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4883 | std::istringstream stream(dump); |
| 4884 | std::string line; |
| 4885 | |
| 4886 | while (std::getline(stream, line, '\n')) { |
| 4887 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4891 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 4892 | std::string dump; |
| 4893 | |
| 4894 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", |
| 4895 | toString(mFocusedWindowRequestedPointerCapture)); |
| 4896 | |
| 4897 | std::string windowName = "None"; |
| 4898 | if (mWindowTokenWithPointerCapture) { |
| 4899 | const sp<InputWindowHandle> captureWindowHandle = |
| 4900 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 4901 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 4902 | : "token has capture without window"; |
| 4903 | } |
| 4904 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); |
| 4905 | |
| 4906 | return dump; |
| 4907 | } |
| 4908 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4909 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4910 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4911 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4912 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4913 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4914 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4915 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4916 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4917 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4918 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4919 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4920 | const std::chrono::duration timeout = |
| 4921 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4922 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4923 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4924 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4925 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4926 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4927 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4928 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4929 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4930 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4931 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4932 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4933 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4934 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4935 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4936 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4937 | 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] | 4938 | state.displayId, toString(state.down), toString(state.split), |
| 4939 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4940 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4941 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4942 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4943 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4944 | dump += StringPrintf(INDENT4 |
| 4945 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4946 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4947 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4948 | } |
| 4949 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4950 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4951 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4952 | } |
| 4953 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4954 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | } |
| 4956 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4957 | if (mDragState) { |
| 4958 | dump += StringPrintf(INDENT "DragState:\n"); |
| 4959 | mDragState->dump(dump, INDENT2); |
| 4960 | } |
| 4961 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4962 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4963 | for (auto& it : mWindowHandlesByDisplay) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4964 | const std::vector<sp<InputWindowHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4965 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4966 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4967 | dump += INDENT2 "Windows:\n"; |
| 4968 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4969 | const sp<InputWindowHandle>& windowHandle = windowHandles[i]; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4970 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4971 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4972 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 4973 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4974 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4975 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4976 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4977 | "applicationInfo.name=%s, " |
| 4978 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4979 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4980 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame^] | 4981 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4982 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4983 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4984 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4985 | windowInfo->flags.string().c_str(), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4986 | NamedEnum::string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4987 | windowInfo->frameLeft, windowInfo->frameTop, |
| 4988 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4989 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 4990 | windowInfo->applicationInfo.name.c_str(), |
| 4991 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4992 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4993 | dump += StringPrintf(", inputFeatures=%s", |
| 4994 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4995 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4996 | "ms, trustedOverlay=%s, hasToken=%s, " |
| 4997 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4998 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4999 | millis(windowInfo->dispatchingTimeout), |
| 5000 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5001 | toString(windowInfo->token != nullptr), |
| 5002 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5003 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5004 | } |
| 5005 | } else { |
| 5006 | dump += INDENT2 "Windows: <none>\n"; |
| 5007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | } |
| 5009 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5010 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5011 | } |
| 5012 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5013 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5014 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5015 | const std::vector<Monitor>& monitors = it.second; |
| 5016 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5017 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5018 | } |
| 5019 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5020 | const std::vector<Monitor>& monitors = it.second; |
| 5021 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5022 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5023 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5024 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5025 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | nsecs_t currentTime = now(); |
| 5029 | |
| 5030 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5031 | if (!mRecentQueue.empty()) { |
| 5032 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5033 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5034 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5035 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5036 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5037 | } |
| 5038 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5039 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5040 | } |
| 5041 | |
| 5042 | // Dump event currently being dispatched. |
| 5043 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5044 | dump += INDENT "PendingEvent:\n"; |
| 5045 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5046 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5047 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5048 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5049 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5050 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5054 | if (!mInboundQueue.empty()) { |
| 5055 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5056 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5057 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5058 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5059 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5060 | } |
| 5061 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5062 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5063 | } |
| 5064 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5065 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5066 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5067 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5068 | const KeyReplacement& replacement = pair.first; |
| 5069 | int32_t newKeyCode = pair.second; |
| 5070 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5071 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5072 | } |
| 5073 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5074 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5075 | } |
| 5076 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5077 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5078 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5079 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5080 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5081 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5082 | connection->inputChannel->getFd().get(), |
| 5083 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5084 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5085 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5086 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5087 | if (!connection->outboundQueue.empty()) { |
| 5088 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5089 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5090 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5091 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5092 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5093 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5094 | } |
| 5095 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5096 | if (!connection->waitQueue.empty()) { |
| 5097 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5098 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5099 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5100 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5101 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5102 | } |
| 5103 | } |
| 5104 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5105 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5109 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5110 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5111 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5112 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5113 | } |
| 5114 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5115 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5116 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5117 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5118 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5119 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5120 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5121 | } |
| 5122 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5123 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5124 | const size_t numMonitors = monitors.size(); |
| 5125 | for (size_t i = 0; i < numMonitors; i++) { |
| 5126 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5127 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5128 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5129 | dump += "\n"; |
| 5130 | } |
| 5131 | } |
| 5132 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5133 | class LooperEventCallback : public LooperCallback { |
| 5134 | public: |
| 5135 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5136 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5137 | |
| 5138 | private: |
| 5139 | std::function<int(int events)> mCallback; |
| 5140 | }; |
| 5141 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5142 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5143 | #if DEBUG_CHANNEL_CREATION |
| 5144 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5145 | #endif |
| 5146 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5147 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5148 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5149 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5150 | |
| 5151 | if (result) { |
| 5152 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5153 | } |
| 5154 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5155 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5156 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5157 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5158 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5159 | sp<Connection> connection = |
| 5160 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5161 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5162 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5163 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5164 | } |
| 5165 | mConnectionsByToken.emplace(token, connection); |
| 5166 | |
| 5167 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5168 | this, std::placeholders::_1, token); |
| 5169 | |
| 5170 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5171 | } // release lock |
| 5172 | |
| 5173 | // Wake the looper because some connections have changed. |
| 5174 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5175 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | } |
| 5177 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5178 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5179 | bool isGestureMonitor, |
| 5180 | const std::string& name, |
| 5181 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5182 | std::shared_ptr<InputChannel> serverChannel; |
| 5183 | std::unique_ptr<InputChannel> clientChannel; |
| 5184 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5185 | if (result) { |
| 5186 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5187 | } |
| 5188 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5189 | { // acquire lock |
| 5190 | std::scoped_lock _l(mLock); |
| 5191 | |
| 5192 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5193 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5194 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5197 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5198 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5199 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5200 | |
| 5201 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5202 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5203 | } |
| 5204 | mConnectionsByToken.emplace(token, connection); |
| 5205 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5206 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5207 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5208 | auto& monitorsByDisplay = |
| 5209 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5210 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5211 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5212 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5213 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5214 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5215 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5216 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5217 | // Wake the looper because some connections have changed. |
| 5218 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5219 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5222 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5223 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5224 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5225 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5226 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5227 | if (status) { |
| 5228 | return status; |
| 5229 | } |
| 5230 | } // release lock |
| 5231 | |
| 5232 | // Wake the poll loop because removing the connection may have changed the current |
| 5233 | // synchronization state. |
| 5234 | mLooper->wake(); |
| 5235 | return OK; |
| 5236 | } |
| 5237 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5238 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5239 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5240 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5241 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5242 | // 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] | 5243 | return BAD_VALUE; |
| 5244 | } |
| 5245 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5246 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5247 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5248 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5249 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5250 | } |
| 5251 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5252 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5253 | |
| 5254 | nsecs_t currentTime = now(); |
| 5255 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5256 | |
| 5257 | connection->status = Connection::STATUS_ZOMBIE; |
| 5258 | return OK; |
| 5259 | } |
| 5260 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5261 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5262 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5263 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5264 | } |
| 5265 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5266 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5267 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5268 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5269 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5270 | std::vector<Monitor>& monitors = it->second; |
| 5271 | const size_t numMonitors = monitors.size(); |
| 5272 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5273 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5274 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5275 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5276 | monitors.erase(monitors.begin() + i); |
| 5277 | break; |
| 5278 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5279 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5280 | if (monitors.empty()) { |
| 5281 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5282 | } else { |
| 5283 | ++it; |
| 5284 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5288 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5289 | { // acquire lock |
| 5290 | std::scoped_lock _l(mLock); |
| 5291 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5292 | |
| 5293 | if (!foundDisplayId) { |
| 5294 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5295 | return BAD_VALUE; |
| 5296 | } |
| 5297 | int32_t displayId = foundDisplayId.value(); |
| 5298 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5299 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5300 | mTouchStatesByDisplay.find(displayId); |
| 5301 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5302 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5303 | return BAD_VALUE; |
| 5304 | } |
| 5305 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5306 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5307 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5308 | std::optional<int32_t> foundDeviceId; |
| 5309 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5310 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5311 | requestingChannel = touchedMonitor.monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5312 | foundDeviceId = state.deviceId; |
| 5313 | } |
| 5314 | } |
| 5315 | if (!foundDeviceId || !state.down) { |
| 5316 | 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] | 5317 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5318 | return BAD_VALUE; |
| 5319 | } |
| 5320 | int32_t deviceId = foundDeviceId.value(); |
| 5321 | |
| 5322 | // Send cancel events to all the input channels we're stealing from. |
| 5323 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5324 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5325 | options.deviceId = deviceId; |
| 5326 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5327 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5328 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5329 | std::shared_ptr<InputChannel> channel = |
| 5330 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5331 | if (channel != nullptr) { |
| 5332 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5333 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5334 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5335 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5336 | canceledWindows += "]"; |
| 5337 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5338 | canceledWindows.c_str()); |
| 5339 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5340 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5341 | state.filterNonMonitors(); |
| 5342 | } |
| 5343 | return OK; |
| 5344 | } |
| 5345 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5346 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5347 | { // acquire lock |
| 5348 | std::scoped_lock _l(mLock); |
| 5349 | if (DEBUG_FOCUS) { |
| 5350 | const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); |
| 5351 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5352 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5353 | : "token without window"); |
| 5354 | } |
| 5355 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5356 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5357 | if (focusedToken != windowToken) { |
| 5358 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5359 | enabled ? "enable" : "disable"); |
| 5360 | return; |
| 5361 | } |
| 5362 | |
| 5363 | if (enabled == mFocusedWindowRequestedPointerCapture) { |
| 5364 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5365 | "window has %s requested pointer capture.", |
| 5366 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5367 | return; |
| 5368 | } |
| 5369 | |
| 5370 | mFocusedWindowRequestedPointerCapture = enabled; |
| 5371 | setPointerCaptureLocked(enabled); |
| 5372 | } // release lock |
| 5373 | |
| 5374 | // Wake the thread to process command entries. |
| 5375 | mLooper->wake(); |
| 5376 | } |
| 5377 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5378 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5379 | const sp<IBinder>& token) { |
| 5380 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5381 | const std::vector<Monitor>& monitors = it.second; |
| 5382 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5383 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5384 | return it.first; |
| 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | return std::nullopt; |
| 5389 | } |
| 5390 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5391 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5392 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5393 | if (gesturePid.has_value()) { |
| 5394 | return gesturePid; |
| 5395 | } |
| 5396 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5397 | } |
| 5398 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5399 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5400 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5401 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5402 | } |
| 5403 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5404 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5405 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5406 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5407 | } |
| 5408 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5409 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5410 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5411 | } |
| 5412 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5413 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5414 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5415 | if (connection == nullptr) { |
| 5416 | return "<nullptr>"; |
| 5417 | } |
| 5418 | return connection->getInputChannelName(); |
| 5419 | } |
| 5420 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5421 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5422 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5423 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5424 | } |
| 5425 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5426 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 5427 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5428 | bool handled, nsecs_t consumeTime) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5429 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5430 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5431 | commandEntry->connection = connection; |
| 5432 | commandEntry->eventTime = currentTime; |
| 5433 | commandEntry->seq = seq; |
| 5434 | commandEntry->handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5435 | commandEntry->consumeTime = consumeTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5436 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5437 | } |
| 5438 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5439 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 5440 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5441 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5442 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5443 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5444 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5445 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5446 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5447 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5450 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 5451 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5452 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5453 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5454 | commandEntry->oldToken = oldToken; |
| 5455 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5456 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5457 | } |
| 5458 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5459 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { |
| 5460 | std::unique_ptr<CommandEntry> commandEntry = |
| 5461 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); |
| 5462 | commandEntry->newToken = token; |
| 5463 | commandEntry->x = x; |
| 5464 | commandEntry->y = y; |
| 5465 | postCommandLocked(std::move(commandEntry)); |
| 5466 | } |
| 5467 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5468 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5469 | if (connection == nullptr) { |
| 5470 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5471 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5472 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5473 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5474 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5475 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5476 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5477 | return; |
| 5478 | } |
| 5479 | /** |
| 5480 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5481 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5482 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5483 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5484 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5485 | * most useful. |
| 5486 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5487 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5488 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5489 | std::string reason = |
| 5490 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5491 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5492 | ns2ms(currentWait), |
| 5493 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5494 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5495 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5496 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5497 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5498 | |
| 5499 | // Stop waking up for events on this connection, it is already unresponsive |
| 5500 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5501 | } |
| 5502 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5503 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5504 | std::string reason = |
| 5505 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5506 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5507 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5508 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5509 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); |
| 5510 | commandEntry->inputApplicationHandle = std::move(application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5511 | postCommandLocked(std::move(commandEntry)); |
| 5512 | } |
| 5513 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5514 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 5515 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5516 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 5517 | commandEntry->obscuringPackage = obscuringPackage; |
| 5518 | postCommandLocked(std::move(commandEntry)); |
| 5519 | } |
| 5520 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5521 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, |
| 5522 | const std::string& reason) { |
| 5523 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5524 | updateLastAnrStateLocked(windowLabel, reason); |
| 5525 | } |
| 5526 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5527 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5528 | const std::string& reason) { |
| 5529 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5530 | updateLastAnrStateLocked(windowLabel, reason); |
| 5531 | } |
| 5532 | |
| 5533 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5534 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5535 | // 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] | 5536 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5537 | struct tm tm; |
| 5538 | localtime_r(&t, &tm); |
| 5539 | char timestr[64]; |
| 5540 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5541 | mLastAnrState.clear(); |
| 5542 | mLastAnrState += INDENT "ANR:\n"; |
| 5543 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5544 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5545 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5546 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5547 | } |
| 5548 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5549 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5550 | mLock.unlock(); |
| 5551 | |
| 5552 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 5553 | |
| 5554 | mLock.lock(); |
| 5555 | } |
| 5556 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5557 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5558 | sp<Connection> connection = commandEntry->connection; |
| 5559 | |
| 5560 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 5561 | mLock.unlock(); |
| 5562 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5563 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5564 | |
| 5565 | mLock.lock(); |
| 5566 | } |
| 5567 | } |
| 5568 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5569 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5570 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 5571 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5572 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5573 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5574 | mLock.lock(); |
| 5575 | } |
| 5576 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5577 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { |
| 5578 | sp<IBinder> newToken = commandEntry->newToken; |
| 5579 | mLock.unlock(); |
| 5580 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); |
| 5581 | mLock.lock(); |
| 5582 | } |
| 5583 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5584 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5585 | mLock.unlock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5586 | |
| 5587 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); |
| 5588 | |
| 5589 | mLock.lock(); |
| 5590 | } |
| 5591 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5592 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5593 | mLock.unlock(); |
| 5594 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5595 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5596 | |
| 5597 | mLock.lock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5598 | } |
| 5599 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5600 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5601 | mLock.unlock(); |
| 5602 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5603 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); |
| 5604 | |
| 5605 | mLock.lock(); |
| 5606 | } |
| 5607 | |
| 5608 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5609 | mLock.unlock(); |
| 5610 | |
| 5611 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); |
| 5612 | |
| 5613 | mLock.lock(); |
| 5614 | } |
| 5615 | |
| 5616 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5617 | mLock.unlock(); |
| 5618 | |
| 5619 | mPolicy->notifyMonitorResponsive(commandEntry->pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5620 | |
| 5621 | mLock.lock(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5624 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 5625 | mLock.unlock(); |
| 5626 | |
| 5627 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 5628 | |
| 5629 | mLock.lock(); |
| 5630 | } |
| 5631 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5632 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5633 | CommandEntry* commandEntry) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5634 | KeyEntry& entry = *(commandEntry->keyEntry); |
| 5635 | KeyEvent event = createKeyEvent(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5636 | |
| 5637 | mLock.unlock(); |
| 5638 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5639 | android::base::Timer t; |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5640 | const sp<IBinder>& token = commandEntry->connectionToken; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5641 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5642 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5643 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5644 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5645 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5646 | |
| 5647 | mLock.lock(); |
| 5648 | |
| 5649 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5650 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5651 | } else if (!delay) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5652 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5653 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5654 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5655 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5656 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5657 | } |
| 5658 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5659 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5660 | mLock.unlock(); |
| 5661 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5662 | mLock.lock(); |
| 5663 | } |
| 5664 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5665 | /** |
| 5666 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5667 | * current time. |
| 5668 | */ |
| 5669 | static bool isConnectionResponsive(const Connection& connection) { |
| 5670 | const nsecs_t currentTime = now(); |
| 5671 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5672 | if (entry->timeoutTime < currentTime) { |
| 5673 | return false; |
| 5674 | } |
| 5675 | } |
| 5676 | return true; |
| 5677 | } |
| 5678 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5679 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5680 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5681 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5682 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5683 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5684 | |
| 5685 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5686 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5687 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5688 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5689 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5690 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5691 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5692 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5693 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5694 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5695 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5696 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5697 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5698 | connection->inputChannel->getConnectionToken(), |
| 5699 | dispatchEntry->deliveryTime, commandEntry->consumeTime, |
| 5700 | finishTime); |
| 5701 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5702 | |
| 5703 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5704 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5705 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5706 | restartEvent = |
| 5707 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5708 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5709 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5710 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5711 | handled); |
| 5712 | } else { |
| 5713 | restartEvent = false; |
| 5714 | } |
| 5715 | |
| 5716 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5717 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5718 | // contents of the wait queue to have been drained, so we need to double-check |
| 5719 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5720 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5721 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5722 | dispatchEntry = *dispatchEntryIt; |
| 5723 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5724 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5725 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5726 | if (!connection->responsive) { |
| 5727 | connection->responsive = isConnectionResponsive(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5728 | if (connection->responsive) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5729 | // The connection was unresponsive, and now it's responsive. |
| 5730 | processConnectionResponsiveLocked(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5731 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5732 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5733 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5734 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5735 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5736 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5737 | } else { |
| 5738 | releaseDispatchEntry(dispatchEntry); |
| 5739 | } |
| 5740 | } |
| 5741 | |
| 5742 | // Start the next dispatch cycle for this connection. |
| 5743 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5744 | } |
| 5745 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5746 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
| 5747 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5748 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); |
| 5749 | monitorUnresponsiveCommand->pid = pid; |
| 5750 | monitorUnresponsiveCommand->reason = std::move(reason); |
| 5751 | postCommandLocked(std::move(monitorUnresponsiveCommand)); |
| 5752 | } |
| 5753 | |
| 5754 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, |
| 5755 | std::string reason) { |
| 5756 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5757 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); |
| 5758 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); |
| 5759 | windowUnresponsiveCommand->reason = std::move(reason); |
| 5760 | postCommandLocked(std::move(windowUnresponsiveCommand)); |
| 5761 | } |
| 5762 | |
| 5763 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
| 5764 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( |
| 5765 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); |
| 5766 | monitorResponsiveCommand->pid = pid; |
| 5767 | postCommandLocked(std::move(monitorResponsiveCommand)); |
| 5768 | } |
| 5769 | |
| 5770 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { |
| 5771 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( |
| 5772 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); |
| 5773 | windowResponsiveCommand->connectionToken = std::move(connectionToken); |
| 5774 | postCommandLocked(std::move(windowResponsiveCommand)); |
| 5775 | } |
| 5776 | |
| 5777 | /** |
| 5778 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5779 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5780 | * command entry to the command queue. |
| 5781 | */ |
| 5782 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5783 | std::string reason) { |
| 5784 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5785 | if (connection.monitor) { |
| 5786 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5787 | reason.c_str()); |
| 5788 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5789 | if (!pid.has_value()) { |
| 5790 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5791 | connection.inputChannel->getName().c_str()); |
| 5792 | return; |
| 5793 | } |
| 5794 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5795 | return; |
| 5796 | } |
| 5797 | // If not a monitor, must be a window |
| 5798 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5799 | reason.c_str()); |
| 5800 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5801 | } |
| 5802 | |
| 5803 | /** |
| 5804 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5805 | */ |
| 5806 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5807 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5808 | if (connection.monitor) { |
| 5809 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5810 | if (!pid.has_value()) { |
| 5811 | ALOGE("Could not find responsive monitor for connection %s", |
| 5812 | connection.inputChannel->getName().c_str()); |
| 5813 | return; |
| 5814 | } |
| 5815 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5816 | return; |
| 5817 | } |
| 5818 | // If not a monitor, must be a window |
| 5819 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5820 | } |
| 5821 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5822 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5823 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5824 | KeyEntry& keyEntry, bool handled) { |
| 5825 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5826 | if (!handled) { |
| 5827 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5828 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5829 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5830 | return false; |
| 5831 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5832 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5833 | // Get the fallback key state. |
| 5834 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5835 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5836 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5837 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5838 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5839 | } |
| 5840 | |
| 5841 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5842 | // If the application handles the original key for which we previously |
| 5843 | // generated a fallback or if the window is not a foreground window, |
| 5844 | // then cancel the associated fallback key, if any. |
| 5845 | if (fallbackKeyCode != -1) { |
| 5846 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5847 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5848 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5849 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5850 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5851 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5852 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5853 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5854 | |
| 5855 | mLock.unlock(); |
| 5856 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5857 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5858 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5859 | |
| 5860 | mLock.lock(); |
| 5861 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5862 | // Cancel the fallback key. |
| 5863 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5864 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5865 | "application handled the original non-fallback key " |
| 5866 | "or is no longer a foreground target, " |
| 5867 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5868 | options.keyCode = fallbackKeyCode; |
| 5869 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5870 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5871 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5872 | } |
| 5873 | } else { |
| 5874 | // If the application did not handle a non-fallback key, first check |
| 5875 | // that we are in a good state to perform unhandled key event processing |
| 5876 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5877 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5878 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5879 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5880 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5881 | "since this is not an initial down. " |
| 5882 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5883 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5884 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5885 | return false; |
| 5886 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5887 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5888 | // Dispatch the unhandled key to the policy. |
| 5889 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5890 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5891 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5892 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5893 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5894 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5895 | |
| 5896 | mLock.unlock(); |
| 5897 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5898 | bool fallback = |
| 5899 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5900 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5901 | |
| 5902 | mLock.lock(); |
| 5903 | |
| 5904 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5905 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5906 | return false; |
| 5907 | } |
| 5908 | |
| 5909 | // Latch the fallback keycode for this key on an initial down. |
| 5910 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5911 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5912 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5913 | fallbackKeyCode = event.getKeyCode(); |
| 5914 | } else { |
| 5915 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5916 | } |
| 5917 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5918 | } |
| 5919 | |
| 5920 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5921 | |
| 5922 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5923 | // We will continue to dispatch the key to the policy but we will no |
| 5924 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5925 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5926 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5927 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5928 | if (fallback) { |
| 5929 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5930 | "as a fallback for %d, but on the DOWN it had requested " |
| 5931 | "to send %d instead. Fallback canceled.", |
| 5932 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5933 | } else { |
| 5934 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5935 | "but on the DOWN it had requested to send %d. " |
| 5936 | "Fallback canceled.", |
| 5937 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5938 | } |
| 5939 | #endif |
| 5940 | |
| 5941 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5942 | "canceling fallback, policy no longer desires it"); |
| 5943 | options.keyCode = fallbackKeyCode; |
| 5944 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5945 | |
| 5946 | fallback = false; |
| 5947 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5948 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5949 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5950 | } |
| 5951 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5952 | |
| 5953 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5954 | { |
| 5955 | std::string msg; |
| 5956 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5957 | connection->inputState.getFallbackKeys(); |
| 5958 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5959 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5960 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5961 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5962 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5963 | } |
| 5964 | #endif |
| 5965 | |
| 5966 | if (fallback) { |
| 5967 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5968 | keyEntry.eventTime = event.getEventTime(); |
| 5969 | keyEntry.deviceId = event.getDeviceId(); |
| 5970 | keyEntry.source = event.getSource(); |
| 5971 | keyEntry.displayId = event.getDisplayId(); |
| 5972 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 5973 | keyEntry.keyCode = fallbackKeyCode; |
| 5974 | keyEntry.scanCode = event.getScanCode(); |
| 5975 | keyEntry.metaState = event.getMetaState(); |
| 5976 | keyEntry.repeatCount = event.getRepeatCount(); |
| 5977 | keyEntry.downTime = event.getDownTime(); |
| 5978 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5979 | |
| 5980 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5981 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5982 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5983 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5984 | #endif |
| 5985 | return true; // restart the event |
| 5986 | } else { |
| 5987 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5988 | ALOGD("Unhandled key event: No fallback key."); |
| 5989 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5990 | |
| 5991 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5992 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5993 | } |
| 5994 | } |
| 5995 | return false; |
| 5996 | } |
| 5997 | |
| 5998 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5999 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6000 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6001 | return false; |
| 6002 | } |
| 6003 | |
| 6004 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 6005 | mLock.unlock(); |
| 6006 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 6007 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, |
| 6008 | commandEntry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6009 | |
| 6010 | mLock.lock(); |
| 6011 | } |
| 6012 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6013 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6014 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6015 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6016 | } |
| 6017 | } |
| 6018 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6019 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6020 | if (ATRACE_ENABLED()) { |
| 6021 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6022 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6023 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6024 | } |
| 6025 | } |
| 6026 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6027 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6028 | if (ATRACE_ENABLED()) { |
| 6029 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6030 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6031 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6032 | } |
| 6033 | } |
| 6034 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6035 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6036 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6037 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6038 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6039 | dumpDispatchStateLocked(dump); |
| 6040 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6041 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6042 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6043 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6044 | } |
| 6045 | } |
| 6046 | |
| 6047 | void InputDispatcher::monitor() { |
| 6048 | // 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] | 6049 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6050 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6051 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6052 | } |
| 6053 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6054 | /** |
| 6055 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6056 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6057 | * this method can be safely called from any thread, as long as you've ensured that |
| 6058 | * the work you are interested in completing has already been queued. |
| 6059 | */ |
| 6060 | bool InputDispatcher::waitForIdle() { |
| 6061 | /** |
| 6062 | * Timeout should represent the longest possible time that a device might spend processing |
| 6063 | * events and commands. |
| 6064 | */ |
| 6065 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6066 | std::unique_lock lock(mLock); |
| 6067 | mLooper->wake(); |
| 6068 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6069 | return result == std::cv_status::no_timeout; |
| 6070 | } |
| 6071 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6072 | /** |
| 6073 | * Sets focus to the window identified by the token. This must be called |
| 6074 | * after updating any input window handles. |
| 6075 | * |
| 6076 | * Params: |
| 6077 | * request.token - input channel token used to identify the window that should gain focus. |
| 6078 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6079 | * specified token does not match the currently focused window, this request will be dropped. |
| 6080 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6081 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6082 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6083 | * when requesting the focus change. This determines which request gets |
| 6084 | * precedence if there is a focus change request from another source such as pointer down. |
| 6085 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6086 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6087 | { // acquire lock |
| 6088 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6089 | std::optional<FocusResolver::FocusChanges> changes = |
| 6090 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6091 | if (changes) { |
| 6092 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6093 | } |
| 6094 | } // release lock |
| 6095 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6096 | mLooper->wake(); |
| 6097 | } |
| 6098 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6099 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6100 | if (changes.oldFocus) { |
| 6101 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6102 | if (focusedInputChannel) { |
| 6103 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6104 | "focus left window"); |
| 6105 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6106 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6107 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6108 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6109 | if (changes.newFocus) { |
| 6110 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6111 | } |
| 6112 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6113 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6114 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6115 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6116 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6117 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6118 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6119 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6120 | // front. |
| 6121 | disablePointerCaptureForcedLocked(); |
| 6122 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6123 | if (mFocusedDisplayId == changes.displayId) { |
| 6124 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6125 | } |
| 6126 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6127 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6128 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
| 6129 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { |
| 6130 | return; |
| 6131 | } |
| 6132 | |
| 6133 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6134 | |
| 6135 | if (mFocusedWindowRequestedPointerCapture) { |
| 6136 | mFocusedWindowRequestedPointerCapture = false; |
| 6137 | setPointerCaptureLocked(false); |
| 6138 | } |
| 6139 | |
| 6140 | if (!mWindowTokenWithPointerCapture) { |
| 6141 | // No need to send capture changes because no window has capture. |
| 6142 | return; |
| 6143 | } |
| 6144 | |
| 6145 | if (mPendingEvent != nullptr) { |
| 6146 | // Move the pending event to the front of the queue. This will give the chance |
| 6147 | // for the pending event to be dropped if it is a captured event. |
| 6148 | mInboundQueue.push_front(mPendingEvent); |
| 6149 | mPendingEvent = nullptr; |
| 6150 | } |
| 6151 | |
| 6152 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
| 6153 | false /* hasCapture */); |
| 6154 | mInboundQueue.push_front(std::move(entry)); |
| 6155 | } |
| 6156 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6157 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { |
| 6158 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 6159 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); |
| 6160 | commandEntry->enabled = enabled; |
| 6161 | postCommandLocked(std::move(commandEntry)); |
| 6162 | } |
| 6163 | |
| 6164 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( |
| 6165 | android::inputdispatcher::CommandEntry* commandEntry) { |
| 6166 | mLock.unlock(); |
| 6167 | |
| 6168 | mPolicy->setPointerCapture(commandEntry->enabled); |
| 6169 | |
| 6170 | mLock.lock(); |
| 6171 | } |
| 6172 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6173 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6174 | { // acquire lock |
| 6175 | std::scoped_lock _l(mLock); |
| 6176 | // Set an empty list to remove all handles from the specific display. |
| 6177 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6178 | setFocusedApplicationLocked(displayId, nullptr); |
| 6179 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6180 | // have been removed for the removed display. |
| 6181 | mFocusResolver.displayRemoved(displayId); |
| 6182 | } // release lock |
| 6183 | |
| 6184 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6185 | mLooper->wake(); |
| 6186 | } |
| 6187 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6188 | } // namespace android::inputdispatcher |