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> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 59 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 60 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 61 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 62 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 63 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 65 | #include <cerrno> |
| 66 | #include <cinttypes> |
| 67 | #include <climits> |
| 68 | #include <cstddef> |
| 69 | #include <ctime> |
| 70 | #include <queue> |
| 71 | #include <sstream> |
| 72 | |
| 73 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 74 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 75 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | #define INDENT " " |
| 77 | #define INDENT2 " " |
| 78 | #define INDENT3 " " |
| 79 | #define INDENT4 " " |
| 80 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 81 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 82 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 83 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 84 | using android::gui::FocusRequest; |
| 85 | using android::gui::TouchOcclusionMode; |
| 86 | using android::gui::WindowInfo; |
| 87 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 88 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 89 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 90 | using android::os::InputEventInjectionResult; |
| 91 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 92 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 93 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 94 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 96 | // When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display |
| 97 | // coordinates and SurfaceFlinger includes the display rotation in the input window transforms. |
| 98 | static bool isPerWindowInputRotationEnabled() { |
| 99 | static const bool PER_WINDOW_INPUT_ROTATION = |
Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 100 | sysprop::InputFlingerProperties::per_window_input_rotation().value_or(false); |
| 101 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 102 | return PER_WINDOW_INPUT_ROTATION; |
| 103 | } |
| 104 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | // Default input dispatching timeout if there is no focused application or paused window |
| 106 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 107 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 108 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 109 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | |
| 111 | // Amount of time to allow for all pending events to be processed when an app switch |
| 112 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 113 | // 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] | 114 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | |
| 116 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 117 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 118 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | // 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] | 121 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 122 | |
| 123 | // Log a warning when an interception call takes longer than this to process. |
| 124 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 125 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 126 | // Additional key latency in case a connection is still processing some motion events. |
| 127 | // This will help with the case when a user touched a button that opens a new window, |
| 128 | // and gives us the chance to dispatch the key to this new window. |
| 129 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 130 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 132 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 133 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 134 | // Event log tags. See EventLogTags.logtags for reference |
| 135 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 136 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 137 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 138 | static inline nsecs_t now() { |
| 139 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 140 | } |
| 141 | |
| 142 | static inline const char* toString(bool value) { |
| 143 | return value ? "true" : "false"; |
| 144 | } |
| 145 | |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 146 | static inline const std::string toString(sp<IBinder> binder) { |
| 147 | if (binder == nullptr) { |
| 148 | return "<null>"; |
| 149 | } |
| 150 | return StringPrintf("%p", binder.get()); |
| 151 | } |
| 152 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 154 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 155 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static bool isValidKeyAction(int32_t action) { |
| 159 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 160 | case AKEY_EVENT_ACTION_DOWN: |
| 161 | case AKEY_EVENT_ACTION_UP: |
| 162 | return true; |
| 163 | default: |
| 164 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | static bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 169 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | ALOGE("Key event has invalid action code 0x%x", action); |
| 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 176 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 177 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 178 | case AMOTION_EVENT_ACTION_DOWN: |
| 179 | case AMOTION_EVENT_ACTION_UP: |
| 180 | case AMOTION_EVENT_ACTION_CANCEL: |
| 181 | case AMOTION_EVENT_ACTION_MOVE: |
| 182 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 183 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 184 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 185 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 186 | case AMOTION_EVENT_ACTION_SCROLL: |
| 187 | return true; |
| 188 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 189 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 190 | int32_t index = getMotionEventActionPointerIndex(action); |
| 191 | return index >= 0 && index < pointerCount; |
| 192 | } |
| 193 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 194 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 195 | return actionButton != 0; |
| 196 | default: |
| 197 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 201 | static int64_t millis(std::chrono::nanoseconds t) { |
| 202 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 203 | } |
| 204 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 205 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 206 | const PointerProperties* pointerProperties) { |
| 207 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 209 | return false; |
| 210 | } |
| 211 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 212 | 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] | 213 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | BitSet32 pointerIdBits; |
| 217 | for (size_t i = 0; i < pointerCount; i++) { |
| 218 | int32_t id = pointerProperties[i].id; |
| 219 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 220 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 221 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | if (pointerIdBits.hasBit(id)) { |
| 225 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 226 | return false; |
| 227 | } |
| 228 | pointerIdBits.markBit(id); |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 233 | static std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 235 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 238 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 239 | bool first = true; |
| 240 | Region::const_iterator cur = region.begin(); |
| 241 | Region::const_iterator const tail = region.end(); |
| 242 | while (cur != tail) { |
| 243 | if (first) { |
| 244 | first = false; |
| 245 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 246 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 248 | 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] | 249 | cur++; |
| 250 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 251 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 254 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
| 255 | constexpr size_t maxEntries = 50; // max events to print |
| 256 | constexpr size_t skipBegin = maxEntries / 2; |
| 257 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 258 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 259 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 260 | |
| 261 | std::string dump; |
| 262 | for (size_t i = 0; i < queue.size(); i++) { |
| 263 | const DispatchEntry& entry = *queue[i]; |
| 264 | if (i >= skipBegin && i < skipEnd) { |
| 265 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 266 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 267 | continue; |
| 268 | } |
| 269 | dump.append(INDENT4); |
| 270 | dump += entry.eventEntry->getDescription(); |
| 271 | dump += StringPrintf(", seq=%" PRIu32 |
| 272 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 273 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 274 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 275 | if (entry.deliveryTime != 0) { |
| 276 | // This entry was delivered, so add information on how long we've been waiting |
| 277 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 278 | } |
| 279 | dump.append("\n"); |
| 280 | } |
| 281 | return dump; |
| 282 | } |
| 283 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 284 | /** |
| 285 | * Find the entry in std::unordered_map by key, and return it. |
| 286 | * If the entry is not found, return a default constructed entry. |
| 287 | * |
| 288 | * Useful when the entries are vectors, since an empty vector will be returned |
| 289 | * if the entry is not found. |
| 290 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 291 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 292 | template <typename K, typename V> |
| 293 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 294 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 295 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 296 | } |
| 297 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 298 | static bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 299 | if (first == second) { |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | if (first == nullptr || second == nullptr) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | return first->getToken() == second->getToken(); |
| 308 | } |
| 309 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 310 | static bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 311 | if (first == nullptr || second == nullptr) { |
| 312 | return false; |
| 313 | } |
| 314 | return first->applicationInfo.token != nullptr && |
| 315 | first->applicationInfo.token == second->applicationInfo.token; |
| 316 | } |
| 317 | |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 318 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 319 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 320 | } |
| 321 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 322 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 323 | std::shared_ptr<EventEntry> eventEntry, |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 324 | int32_t inputTargetFlags) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 325 | if (eventEntry->type == EventEntry::Type::MOTION) { |
| 326 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 327 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_JOYSTICK) || |
| 328 | (motionEntry.source & AINPUT_SOURCE_CLASS_POSITION)) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 329 | const ui::Transform identityTransform; |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 330 | // Use identity transform for joystick and position-based (touchpad) events because they |
| 331 | // don't depend on the window transform. |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 332 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 333 | 1.0f /*globalScaleFactor*/, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 334 | inputTarget.displayOrientation, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 335 | inputTarget.displaySize); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 339 | if (inputTarget.useDefaultPointerTransform()) { |
| 340 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 341 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 342 | inputTarget.globalScaleFactor, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 343 | inputTarget.displayOrientation, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 344 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 348 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 349 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 350 | std::vector<PointerCoords> pointerCoords; |
| 351 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 352 | |
| 353 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 354 | // 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] | 355 | // uses the transform for the normalized pointer. |
| 356 | const ui::Transform& firstPointerTransform = |
| 357 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 358 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 359 | |
| 360 | // Iterate through all pointers in the event to normalize against the first. |
| 361 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 362 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 363 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 364 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 365 | |
| 366 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 367 | // First, apply the current pointer's transform to update the coordinates into |
| 368 | // window space. |
| 369 | pointerCoords[pointerIndex].transform(currTransform); |
| 370 | // Next, apply the inverse transform of the normalized coordinates so the |
| 371 | // current coordinates are transformed into the normalized coordinate space. |
| 372 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 375 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 376 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 377 | motionEntry.deviceId, motionEntry.source, |
| 378 | motionEntry.displayId, motionEntry.policyFlags, |
| 379 | motionEntry.action, motionEntry.actionButton, |
| 380 | motionEntry.flags, motionEntry.metaState, |
| 381 | motionEntry.buttonState, motionEntry.classification, |
| 382 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 383 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 384 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 385 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 386 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 387 | |
| 388 | if (motionEntry.injectionState) { |
| 389 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 390 | combinedMotionEntry->injectionState->refCount += 1; |
| 391 | } |
| 392 | |
| 393 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 394 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 395 | firstPointerTransform, inputTarget.globalScaleFactor, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 396 | inputTarget.displayOrientation, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 397 | inputTarget.displaySize); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 398 | return dispatchEntry; |
| 399 | } |
| 400 | |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 401 | static void addGestureMonitors(const std::vector<Monitor>& monitors, |
| 402 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 403 | float yOffset = 0) { |
| 404 | if (monitors.empty()) { |
| 405 | return; |
| 406 | } |
| 407 | outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); |
| 408 | for (const Monitor& monitor : monitors) { |
| 409 | outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); |
| 410 | } |
| 411 | } |
| 412 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 413 | static status_t openInputChannelPair(const std::string& name, |
| 414 | std::shared_ptr<InputChannel>& serverChannel, |
| 415 | std::unique_ptr<InputChannel>& clientChannel) { |
| 416 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 417 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 418 | |
| 419 | serverChannel = std::move(uniqueServerChannel); |
| 420 | return result; |
| 421 | } |
| 422 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 423 | template <typename T> |
| 424 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
| 425 | if (lhs == nullptr && rhs == nullptr) { |
| 426 | return true; |
| 427 | } |
| 428 | if (lhs == nullptr || rhs == nullptr) { |
| 429 | return false; |
| 430 | } |
| 431 | return *lhs == *rhs; |
| 432 | } |
| 433 | |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 434 | static sp<IPlatformCompatNative> getCompatService() { |
| 435 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 436 | if (service == nullptr) { |
| 437 | ALOGE("Failed to link to compat service"); |
| 438 | return nullptr; |
| 439 | } |
| 440 | return interface_cast<IPlatformCompatNative>(service); |
| 441 | } |
| 442 | |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 443 | static KeyEvent createKeyEvent(const KeyEntry& entry) { |
| 444 | KeyEvent event; |
| 445 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 446 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 447 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 448 | return event; |
| 449 | } |
| 450 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 451 | static std::optional<int32_t> findMonitorPidByToken( |
| 452 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 453 | const sp<IBinder>& token) { |
| 454 | for (const auto& it : monitorsByDisplay) { |
| 455 | const std::vector<Monitor>& monitors = it.second; |
| 456 | for (const Monitor& monitor : monitors) { |
| 457 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 458 | return monitor.pid; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | return std::nullopt; |
| 463 | } |
| 464 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 465 | static bool shouldReportMetricsForConnection(const Connection& connection) { |
| 466 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 467 | // affect the statistics. |
| 468 | if (connection.monitor) { |
| 469 | return false; |
| 470 | } |
| 471 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 472 | if (!connection.responsive) { |
| 473 | return false; |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | static bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, |
| 479 | const Connection& connection) { |
| 480 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 481 | const int32_t& inputEventId = eventEntry.id; |
| 482 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 483 | // Event was transmuted |
| 484 | return false; |
| 485 | } |
| 486 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 487 | return false; |
| 488 | } |
| 489 | // Only track latency for events that originated from hardware |
| 490 | if (eventEntry.isSynthesized()) { |
| 491 | return false; |
| 492 | } |
| 493 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 494 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 495 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 496 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 497 | return false; |
| 498 | } |
| 499 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 500 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 501 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 502 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 503 | return false; |
| 504 | } |
| 505 | } else { |
| 506 | // Not a key or a motion |
| 507 | return false; |
| 508 | } |
| 509 | if (!shouldReportMetricsForConnection(connection)) { |
| 510 | return false; |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 515 | // --- InputDispatcher --- |
| 516 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 517 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 518 | : mPolicy(policy), |
| 519 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 520 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 521 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 522 | mAppSwitchSawKeyDown(false), |
| 523 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 524 | mNextUnblockedEvent(nullptr), |
| 525 | mDispatchEnabled(false), |
| 526 | mDispatchFrozen(false), |
| 527 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 528 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 529 | // To avoid leaking stack in case that call never comes, and for tests, |
| 530 | // initialize it here anyways. |
| 531 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 532 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 533 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 534 | mFocusedWindowRequestedPointerCapture(false), |
| 535 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 536 | mLatencyAggregator(), |
| 537 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 538 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 540 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 542 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | |
| 544 | policy->getDispatcherConfiguration(&mConfig); |
| 545 | } |
| 546 | |
| 547 | InputDispatcher::~InputDispatcher() { |
| 548 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 549 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | |
| 551 | resetKeyRepeatLocked(); |
| 552 | releasePendingEventLocked(); |
| 553 | drainInboundQueueLocked(); |
| 554 | } |
| 555 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 556 | while (!mConnectionsByToken.empty()) { |
| 557 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 558 | removeInputChannel(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 562 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 563 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 564 | return ALREADY_EXISTS; |
| 565 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 566 | mThread = std::make_unique<InputThread>( |
| 567 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 568 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 572 | if (mThread && mThread->isCallingThread()) { |
| 573 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 574 | return INVALID_OPERATION; |
| 575 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 576 | mThread.reset(); |
| 577 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | void InputDispatcher::dispatchOnce() { |
| 581 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 582 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 583 | std::scoped_lock _l(mLock); |
| 584 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | |
| 586 | // Run a dispatch loop if there are no pending commands. |
| 587 | // The dispatch loop might enqueue commands to run afterwards. |
| 588 | if (!haveCommandsLocked()) { |
| 589 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 590 | } |
| 591 | |
| 592 | // Run all pending commands if there are any. |
| 593 | // If any commands were run then force the next poll to wake up immediately. |
| 594 | if (runCommandsLockedInterruptible()) { |
| 595 | nextWakeupTime = LONG_LONG_MIN; |
| 596 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 597 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 598 | // If we are still waiting for ack on some events, |
| 599 | // we might have to wake up earlier to check if an app is anr'ing. |
| 600 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 601 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 602 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 603 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 604 | // pending or queued events |
| 605 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 606 | mDispatcherEnteredIdle.notify_all(); |
| 607 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | } // release lock |
| 609 | |
| 610 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 611 | nsecs_t currentTime = now(); |
| 612 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 613 | mLooper->pollOnce(timeoutMillis); |
| 614 | } |
| 615 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 616 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 617 | * Raise ANR if there is no focused window. |
| 618 | * Before the ANR is raised, do a final state check: |
| 619 | * 1. The currently focused application must be the same one we are waiting for. |
| 620 | * 2. Ensure we still don't have a focused window. |
| 621 | */ |
| 622 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 623 | // Check if the application that we are waiting for is still focused. |
| 624 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 625 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 626 | if (focusedApplication == nullptr || |
| 627 | focusedApplication->getApplicationToken() != |
| 628 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 629 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 630 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 631 | focusedApplication->getName().c_str()); |
| 632 | return; // The focused application has changed. |
| 633 | } |
| 634 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 635 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 636 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 637 | if (focusedWindowHandle != nullptr) { |
| 638 | return; // We now have a focused window. No need for ANR. |
| 639 | } |
| 640 | onAnrLocked(mAwaitedFocusedApplication); |
| 641 | } |
| 642 | |
| 643 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 644 | * Check if any of the connections' wait queues have events that are too old. |
| 645 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 646 | * Return the time at which we should wake up next. |
| 647 | */ |
| 648 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 649 | const nsecs_t currentTime = now(); |
| 650 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 651 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 652 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 653 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 654 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 655 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 656 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 657 | return LONG_LONG_MIN; |
| 658 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 659 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 660 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Check if any connection ANRs are due |
| 665 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 666 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 667 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 668 | } |
| 669 | |
| 670 | // If we reached here, we have an unresponsive connection. |
| 671 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 672 | if (connection == nullptr) { |
| 673 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 674 | return nextAnrCheck; |
| 675 | } |
| 676 | connection->responsive = false; |
| 677 | // Stop waking up for this unresponsive connection |
| 678 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 679 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 680 | return LONG_LONG_MIN; |
| 681 | } |
| 682 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 683 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 684 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 685 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 686 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 687 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 688 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 692 | nsecs_t currentTime = now(); |
| 693 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 694 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 695 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 696 | // repeat if the device is just coming out of sleep. |
| 697 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 698 | resetKeyRepeatLocked(); |
| 699 | } |
| 700 | |
| 701 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 702 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 703 | if (DEBUG_FOCUS) { |
| 704 | ALOGD("Dispatch frozen. Waiting some more."); |
| 705 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 | return; |
| 707 | } |
| 708 | |
| 709 | // Optimize latency of app switches. |
| 710 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 711 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 712 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 713 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 714 | *nextWakeupTime = mAppSwitchDueTime; |
| 715 | } |
| 716 | |
| 717 | // Ready to start a new event. |
| 718 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 719 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 720 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 721 | if (isAppSwitchDue) { |
| 722 | // The inbound queue is empty so the app switch key we were waiting |
| 723 | // for will never arrive. Stop waiting for it. |
| 724 | resetPendingAppSwitchLocked(false); |
| 725 | isAppSwitchDue = false; |
| 726 | } |
| 727 | |
| 728 | // Synthesize a key repeat if appropriate. |
| 729 | if (mKeyRepeatState.lastKeyEntry) { |
| 730 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 731 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 732 | } else { |
| 733 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 734 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | // Nothing to do if there is no pending event. |
| 740 | if (!mPendingEvent) { |
| 741 | return; |
| 742 | } |
| 743 | } else { |
| 744 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 745 | mPendingEvent = mInboundQueue.front(); |
| 746 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | traceInboundQueueLengthLocked(); |
| 748 | } |
| 749 | |
| 750 | // Poke user activity for this event. |
| 751 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 752 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Now we have an event to dispatch. |
| 757 | // 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] | 758 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 760 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 761 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 762 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 763 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 764 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 768 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 772 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 773 | const ConfigurationChangedEntry& typedEntry = |
| 774 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 775 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 776 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 777 | break; |
| 778 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 780 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 781 | const DeviceResetEntry& typedEntry = |
| 782 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 783 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 784 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 785 | break; |
| 786 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 788 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 789 | std::shared_ptr<FocusEntry> typedEntry = |
| 790 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 791 | dispatchFocusLocked(currentTime, typedEntry); |
| 792 | done = true; |
| 793 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 794 | break; |
| 795 | } |
| 796 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 797 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 798 | const auto typedEntry = |
| 799 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 800 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 801 | done = true; |
| 802 | break; |
| 803 | } |
| 804 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 805 | case EventEntry::Type::DRAG: { |
| 806 | std::shared_ptr<DragEntry> typedEntry = |
| 807 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 808 | dispatchDragLocked(currentTime, typedEntry); |
| 809 | done = true; |
| 810 | break; |
| 811 | } |
| 812 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 813 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 814 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 815 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 816 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 817 | resetPendingAppSwitchLocked(true); |
| 818 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 819 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 820 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 823 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 824 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 825 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 826 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 827 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 828 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 829 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 830 | break; |
| 831 | } |
| 832 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 833 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 834 | std::shared_ptr<MotionEntry> motionEntry = |
| 835 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 836 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 837 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 838 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 839 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 840 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 841 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 842 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 843 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 844 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 845 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 846 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 847 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 848 | |
| 849 | case EventEntry::Type::SENSOR: { |
| 850 | std::shared_ptr<SensorEntry> sensorEntry = |
| 851 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 852 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 853 | dropReason = DropReason::APP_SWITCH; |
| 854 | } |
| 855 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 856 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 857 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 858 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 859 | dropReason = DropReason::STALE; |
| 860 | } |
| 861 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 862 | done = true; |
| 863 | break; |
| 864 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 868 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 869 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 871 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | |
| 873 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 874 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 878 | /** |
| 879 | * Return true if the events preceding this incoming motion event should be dropped |
| 880 | * Return false otherwise (the default behaviour) |
| 881 | */ |
| 882 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 883 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 884 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 885 | |
| 886 | // Optimize case where the current application is unresponsive and the user |
| 887 | // decides to touch a window in a different application. |
| 888 | // If the application takes too long to catch up then we drop all events preceding |
| 889 | // the touch into the other window. |
| 890 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 891 | int32_t displayId = motionEntry.displayId; |
| 892 | int32_t x = static_cast<int32_t>( |
| 893 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 894 | int32_t y = static_cast<int32_t>( |
| 895 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 896 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 897 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 898 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 899 | touchedWindowHandle->getApplicationToken() != |
| 900 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 901 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 902 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 903 | "for %s", |
| 904 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 905 | return true; |
| 906 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 907 | |
| 908 | // Alternatively, maybe there's a gesture monitor that could handle this event |
| 909 | std::vector<TouchedMonitor> gestureMonitors = |
| 910 | findTouchedGestureMonitorsLocked(displayId, {}); |
| 911 | for (TouchedMonitor& gestureMonitor : gestureMonitors) { |
| 912 | sp<Connection> connection = |
| 913 | getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 914 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 915 | // This monitor could take more input. Drop all events preceding this |
| 916 | // event, so that gesture monitor could get a chance to receive the stream |
| 917 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 918 | "responsive gesture monitor that may handle the event", |
| 919 | mAwaitedFocusedApplication->getName().c_str()); |
| 920 | return true; |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 926 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 927 | // events to be processed before dispatching the key event. This is because these motion events |
| 928 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 929 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 930 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 931 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 932 | "just send the pending key event to the focused window."); |
| 933 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 934 | } |
| 935 | return false; |
| 936 | } |
| 937 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 938 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 939 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 940 | mInboundQueue.push_back(std::move(newEntry)); |
| 941 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 | traceInboundQueueLengthLocked(); |
| 943 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 944 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 945 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 946 | // Optimize app switch latency. |
| 947 | // If the application takes too long to catch up then we drop all events preceding |
| 948 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 949 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 950 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 951 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 952 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 953 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 954 | if (mAppSwitchSawKeyDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 955 | #if DEBUG_APP_SWITCH |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 956 | ALOGD("App switch is pending!"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | #endif |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 958 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 959 | mAppSwitchSawKeyDown = false; |
| 960 | needWake = true; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | break; |
| 965 | } |
| 966 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 967 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 968 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 969 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 970 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 971 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 972 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 973 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 974 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 975 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 976 | break; |
| 977 | } |
| 978 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 979 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 980 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 981 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 982 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 983 | // nothing to do |
| 984 | break; |
| 985 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | return needWake; |
| 989 | } |
| 990 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 991 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 992 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 993 | if (entry->type != EventEntry::Type::SENSOR) { |
| 994 | mRecentQueue.push_back(entry); |
| 995 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 996 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 997 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1001 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1002 | int32_t y, TouchState* touchState, |
| 1003 | bool addOutsideTargets, |
| 1004 | bool addPortalWindows, |
| 1005 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1006 | if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { |
| 1007 | LOG_ALWAYS_FATAL( |
| 1008 | "Must provide a valid touch state if adding portal windows or outside targets"); |
| 1009 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1010 | // Traverse windows from front to back to find touched window. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1011 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 1012 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1013 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1014 | continue; |
| 1015 | } |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1016 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1017 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1018 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1019 | |
| 1020 | if (windowInfo->visible) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1021 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1022 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 1023 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1024 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1025 | int32_t portalToDisplayId = windowInfo->portalToDisplayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1026 | if (portalToDisplayId != ADISPLAY_ID_NONE && |
| 1027 | portalToDisplayId != displayId) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1028 | if (addPortalWindows) { |
| 1029 | // For the monitoring channels of the display. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1030 | touchState->addPortalWindow(windowHandle); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1031 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1032 | return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1033 | addOutsideTargets, addPortalWindows); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1034 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1035 | // Found window. |
| 1036 | return windowHandle; |
| 1037 | } |
| 1038 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1039 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1040 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1041 | touchState->addOrUpdateWindow(windowHandle, |
| 1042 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1043 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1048 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1051 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1052 | int32_t displayId, const std::vector<sp<WindowInfoHandle>>& portalWindows) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1053 | std::vector<TouchedMonitor> touchedMonitors; |
| 1054 | |
| 1055 | std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); |
| 1056 | addGestureMonitors(monitors, touchedMonitors); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1057 | for (const sp<WindowInfoHandle>& portalWindow : portalWindows) { |
| 1058 | const WindowInfo* windowInfo = portalWindow->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1059 | monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1060 | addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, |
| 1061 | -windowInfo->frameTop); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1062 | } |
| 1063 | return touchedMonitors; |
| 1064 | } |
| 1065 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1066 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | const char* reason; |
| 1068 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1069 | case DropReason::POLICY: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1070 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1071 | ALOGD("Dropped event because policy consumed it."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1072 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1073 | reason = "inbound event was dropped because the policy consumed it"; |
| 1074 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1075 | case DropReason::DISABLED: |
| 1076 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1077 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1078 | } |
| 1079 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1080 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1081 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1082 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1083 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1084 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1085 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1086 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1087 | "has started interacting with a different application."); |
| 1088 | reason = "inbound event was dropped because the current application is not responding " |
| 1089 | "and the user has started interacting with a different application"; |
| 1090 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1091 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1092 | ALOGI("Dropped event because it is stale."); |
| 1093 | reason = "inbound event was dropped because it is stale"; |
| 1094 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1095 | case DropReason::NO_POINTER_CAPTURE: |
| 1096 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1097 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1098 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1099 | case DropReason::NOT_DROPPED: { |
| 1100 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1101 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1102 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1105 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1106 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1108 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1109 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1111 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1112 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1113 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1115 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1116 | } else { |
| 1117 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1118 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1119 | } |
| 1120 | break; |
| 1121 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1122 | case EventEntry::Type::SENSOR: { |
| 1123 | break; |
| 1124 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1125 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1126 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1127 | break; |
| 1128 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1129 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1130 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1131 | case EventEntry::Type::DEVICE_RESET: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1132 | 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] | 1133 | break; |
| 1134 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1138 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1139 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1140 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1143 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1144 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1145 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1146 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1150 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1151 | } |
| 1152 | |
| 1153 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1154 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1155 | |
| 1156 | #if DEBUG_APP_SWITCH |
| 1157 | if (handled) { |
| 1158 | ALOGD("App switch has arrived."); |
| 1159 | } else { |
| 1160 | ALOGD("App switch was abandoned."); |
| 1161 | } |
| 1162 | #endif |
| 1163 | } |
| 1164 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1166 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | bool InputDispatcher::runCommandsLockedInterruptible() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1170 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | do { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1175 | std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1176 | mCommandQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | Command command = commandEntry->command; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1178 | command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1179 | |
| 1180 | commandEntry->connection.clear(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1181 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1182 | return true; |
| 1183 | } |
| 1184 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1185 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { |
| 1186 | mCommandQueue.push_back(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1190 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1191 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1192 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | releaseInboundEventLocked(entry); |
| 1194 | } |
| 1195 | traceInboundQueueLengthLocked(); |
| 1196 | } |
| 1197 | |
| 1198 | void InputDispatcher::releasePendingEventLocked() { |
| 1199 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1201 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1205 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1206 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1207 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | #if DEBUG_DISPATCH_CYCLE |
| 1209 | ALOGD("Injected inbound event was dropped."); |
| 1210 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1211 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } |
| 1213 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1214 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1220 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1221 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1225 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1226 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1228 | uint32_t policyFlags = entry->policyFlags & |
| 1229 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1231 | std::shared_ptr<KeyEntry> newEntry = |
| 1232 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1233 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1234 | entry->flags, entry->keyCode, entry->scanCode, |
| 1235 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1237 | newEntry->syntheticRepeat = true; |
| 1238 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1239 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1240 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1243 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1244 | const ConfigurationChangedEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1246 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | #endif |
| 1248 | |
| 1249 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1250 | resetKeyRepeatLocked(); |
| 1251 | |
| 1252 | // 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] | 1253 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 1254 | &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | commandEntry->eventTime = entry.eventTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1256 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | return true; |
| 1258 | } |
| 1259 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1260 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1261 | const DeviceResetEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1262 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1263 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1264 | entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | #endif |
| 1266 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1267 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1269 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1273 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1274 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1275 | if (mPendingEvent != nullptr) { |
| 1276 | // Move the pending event to the front of the queue. This will give the chance |
| 1277 | // for the pending event to get dispatched to the newly focused window |
| 1278 | mInboundQueue.push_front(mPendingEvent); |
| 1279 | mPendingEvent = nullptr; |
| 1280 | } |
| 1281 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1282 | std::unique_ptr<FocusEntry> focusEntry = |
| 1283 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1284 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1285 | |
| 1286 | // This event should go to the front of the queue, but behind all other focus events |
| 1287 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1288 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1289 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | [](const std::shared_ptr<EventEntry>& event) { |
| 1291 | return event->type == EventEntry::Type::FOCUS; |
| 1292 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1293 | |
| 1294 | // 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] | 1295 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1298 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1299 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1300 | if (channel == nullptr) { |
| 1301 | return; // Window has gone away |
| 1302 | } |
| 1303 | InputTarget target; |
| 1304 | target.inputChannel = channel; |
| 1305 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1306 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1307 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1308 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1309 | std::string reason = std::string("reason=").append(entry->reason); |
| 1310 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1311 | dispatchEventLocked(currentTime, entry, {target}); |
| 1312 | } |
| 1313 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1314 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1315 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1316 | DropReason& dropReason) { |
| 1317 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 1318 | if (entry->pointerCaptureEnabled && haveWindowWithPointerCapture) { |
| 1319 | LOG_ALWAYS_FATAL("Pointer Capture has already been enabled for the window."); |
| 1320 | } |
| 1321 | if (!entry->pointerCaptureEnabled && !haveWindowWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1322 | // Pointer capture was already forcefully disabled because of focus change. |
| 1323 | dropReason = DropReason::NOT_DROPPED; |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | // Set drop reason for early returns |
| 1328 | dropReason = DropReason::NO_POINTER_CAPTURE; |
| 1329 | |
| 1330 | sp<IBinder> token; |
| 1331 | if (entry->pointerCaptureEnabled) { |
| 1332 | // Enable Pointer Capture |
| 1333 | if (!mFocusedWindowRequestedPointerCapture) { |
| 1334 | // This can happen if a window requests capture and immediately releases capture. |
| 1335 | ALOGW("No window requested Pointer Capture."); |
| 1336 | return; |
| 1337 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1338 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1339 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1340 | mWindowTokenWithPointerCapture = token; |
| 1341 | } else { |
| 1342 | // Disable Pointer Capture |
| 1343 | token = mWindowTokenWithPointerCapture; |
| 1344 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1345 | if (mFocusedWindowRequestedPointerCapture) { |
| 1346 | mFocusedWindowRequestedPointerCapture = false; |
| 1347 | setPointerCaptureLocked(false); |
| 1348 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | auto channel = getInputChannelLocked(token); |
| 1352 | if (channel == nullptr) { |
| 1353 | // Window has gone away, clean up Pointer Capture state. |
| 1354 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1355 | if (mFocusedWindowRequestedPointerCapture) { |
| 1356 | mFocusedWindowRequestedPointerCapture = false; |
| 1357 | setPointerCaptureLocked(false); |
| 1358 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1359 | return; |
| 1360 | } |
| 1361 | InputTarget target; |
| 1362 | target.inputChannel = channel; |
| 1363 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1364 | entry->dispatchInProgress = true; |
| 1365 | dispatchEventLocked(currentTime, entry, {target}); |
| 1366 | |
| 1367 | dropReason = DropReason::NOT_DROPPED; |
| 1368 | } |
| 1369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1370 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1371 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1372 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1373 | if (!entry->dispatchInProgress) { |
| 1374 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1375 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1376 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1377 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1378 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1379 | // We have seen two identical key downs in a row which indicates that the device |
| 1380 | // driver is automatically generating key repeats itself. We take note of the |
| 1381 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1382 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1383 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1384 | // Make sure we don't get key down from a different device. If a different |
| 1385 | // device Id has same key pressed down, the new device Id will replace the |
| 1386 | // current one to hold the key repeat with repeat count reset. |
| 1387 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1388 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1390 | resetKeyRepeatLocked(); |
| 1391 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1392 | } else { |
| 1393 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1394 | resetKeyRepeatLocked(); |
| 1395 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1396 | } |
| 1397 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1398 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1399 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1400 | // 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] | 1401 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1402 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1403 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1404 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1405 | resetKeyRepeatLocked(); |
| 1406 | } |
| 1407 | |
| 1408 | if (entry->repeatCount == 1) { |
| 1409 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1410 | } else { |
| 1411 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1412 | } |
| 1413 | |
| 1414 | entry->dispatchInProgress = true; |
| 1415 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1416 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | // Handle case where the policy asked us to try again later last time. |
| 1420 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1421 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1422 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1423 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1424 | } |
| 1425 | return false; // wait until next wakeup |
| 1426 | } |
| 1427 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1428 | entry->interceptKeyWakeupTime = 0; |
| 1429 | } |
| 1430 | |
| 1431 | // Give the policy a chance to intercept the key. |
| 1432 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1433 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1434 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1435 | &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1436 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1437 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1438 | commandEntry->connectionToken = focusedWindowToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1439 | commandEntry->keyEntry = entry; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1440 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1441 | return false; // wait for the command to run |
| 1442 | } else { |
| 1443 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1444 | } |
| 1445 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1446 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1447 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1452 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1453 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1454 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1455 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1456 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1457 | return true; |
| 1458 | } |
| 1459 | |
| 1460 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1461 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1462 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1463 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1464 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | return false; |
| 1466 | } |
| 1467 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1468 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1469 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
| 1472 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1473 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1474 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1475 | |
| 1476 | // Dispatch the key. |
| 1477 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1478 | return true; |
| 1479 | } |
| 1480 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1481 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1482 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1483 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1484 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1485 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1486 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1487 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 1488 | entry.repeatCount, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1489 | #endif |
| 1490 | } |
| 1491 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1492 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { |
| 1493 | mLock.unlock(); |
| 1494 | |
| 1495 | const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; |
| 1496 | if (entry->accuracyChanged) { |
| 1497 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1498 | } |
| 1499 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1500 | entry->hwTimestamp, entry->values); |
| 1501 | mLock.lock(); |
| 1502 | } |
| 1503 | |
| 1504 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, |
| 1505 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
| 1506 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1507 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1508 | "source=0x%x, sensorType=%s", |
| 1509 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Prabir Pradhan | be05b5b | 2021-02-24 16:39:43 -0800 | [diff] [blame] | 1510 | NamedEnum::string(entry->sensorType).c_str()); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1511 | #endif |
| 1512 | std::unique_ptr<CommandEntry> commandEntry = |
| 1513 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); |
| 1514 | commandEntry->sensorEntry = entry; |
| 1515 | postCommandLocked(std::move(commandEntry)); |
| 1516 | } |
| 1517 | |
| 1518 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
| 1519 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 1520 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
| 1521 | NamedEnum::string(sensorType).c_str()); |
| 1522 | #endif |
| 1523 | { // acquire lock |
| 1524 | std::scoped_lock _l(mLock); |
| 1525 | |
| 1526 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1527 | std::shared_ptr<EventEntry> entry = *it; |
| 1528 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1529 | it = mInboundQueue.erase(it); |
| 1530 | releaseInboundEventLocked(entry); |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | return true; |
| 1535 | } |
| 1536 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1537 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1538 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1539 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1541 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 | entry->dispatchInProgress = true; |
| 1543 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1544 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1548 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1549 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1550 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1551 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1552 | return true; |
| 1553 | } |
| 1554 | |
| 1555 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1556 | |
| 1557 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1558 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1559 | |
| 1560 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1561 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1562 | if (isPointerEvent) { |
| 1563 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1564 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1565 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1566 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | } else { |
| 1568 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1569 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1570 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1571 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1572 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1573 | return false; |
| 1574 | } |
| 1575 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1576 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1577 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1578 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1579 | return true; |
| 1580 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1581 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1582 | CancelationOptions::Mode mode(isPointerEvent |
| 1583 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1584 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1585 | CancelationOptions options(mode, "input event injection failed"); |
| 1586 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | return true; |
| 1588 | } |
| 1589 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1590 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1591 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1593 | if (isPointerEvent) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1594 | std::unordered_map<int32_t, TouchState>::iterator it = |
| 1595 | mTouchStatesByDisplay.find(entry->displayId); |
| 1596 | if (it != mTouchStatesByDisplay.end()) { |
| 1597 | const TouchState& state = it->second; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1598 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1599 | // The event has gone through these portal windows, so we add monitoring targets of |
| 1600 | // the corresponding displays as well. |
| 1601 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1602 | const WindowInfo* windowInfo = state.portalWindows[i]->getInfo(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1603 | addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1604 | -windowInfo->frameLeft, -windowInfo->frameTop); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1610 | // Dispatch the motion. |
| 1611 | if (conflictingPointerActions) { |
| 1612 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1613 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1614 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1615 | } |
| 1616 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1617 | return true; |
| 1618 | } |
| 1619 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1620 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1621 | bool isExiting, const MotionEntry& motionEntry) { |
| 1622 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1623 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1624 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1625 | PointerCoords pointerCoords; |
| 1626 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1627 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1628 | |
| 1629 | std::unique_ptr<DragEntry> dragEntry = |
| 1630 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1631 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1632 | pointerCoords.getY()); |
| 1633 | |
| 1634 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1635 | } |
| 1636 | |
| 1637 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1638 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1639 | if (channel == nullptr) { |
| 1640 | return; // Window has gone away |
| 1641 | } |
| 1642 | InputTarget target; |
| 1643 | target.inputChannel = channel; |
| 1644 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1645 | entry->dispatchInProgress = true; |
| 1646 | dispatchEventLocked(currentTime, entry, {target}); |
| 1647 | } |
| 1648 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1649 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1650 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1651 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1652 | ", policyFlags=0x%x, " |
| 1653 | "action=0x%x, actionButton=0x%x, flags=0x%x, " |
| 1654 | "metaState=0x%x, buttonState=0x%x," |
| 1655 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1656 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, |
| 1657 | entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, |
| 1658 | entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1660 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1661 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1662 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1663 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1664 | "orientation=%f", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1665 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1666 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1667 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1668 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1669 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1670 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1671 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1672 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1673 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1674 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | } |
| 1676 | #endif |
| 1677 | } |
| 1678 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1679 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1680 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1681 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1682 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1683 | #if DEBUG_DISPATCH_CYCLE |
| 1684 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1685 | #endif |
| 1686 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1687 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1688 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1689 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1690 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1691 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1692 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1693 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1694 | sp<Connection> connection = |
| 1695 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1696 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1697 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1698 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1699 | if (DEBUG_FOCUS) { |
| 1700 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1701 | "is no longer registered with the input dispatcher.", |
| 1702 | inputTarget.inputChannel->getName().c_str()); |
| 1703 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1708 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1709 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1710 | // If the policy decides to close the app, we will get a channel removal event via |
| 1711 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1712 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1713 | // pile up. |
| 1714 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1715 | connection->inputChannel->getName().c_str()); |
| 1716 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1717 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1718 | "application not responding"); |
| 1719 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1723 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1724 | if (DEBUG_FOCUS) { |
| 1725 | ALOGD("Resetting ANR timeouts."); |
| 1726 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1727 | |
| 1728 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1729 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1730 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1733 | /** |
| 1734 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1735 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1736 | * Focused display is the display that the user most recently interacted with. |
| 1737 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1738 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1739 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1740 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1741 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1742 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1743 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1744 | break; |
| 1745 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1746 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1747 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1748 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1749 | break; |
| 1750 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1751 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1752 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1753 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1754 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1755 | case EventEntry::Type::SENSOR: |
| 1756 | case EventEntry::Type::DRAG: { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1757 | 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] | 1758 | return ADISPLAY_ID_NONE; |
| 1759 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1760 | } |
| 1761 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1762 | } |
| 1763 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1764 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1765 | const char* focusedWindowName) { |
| 1766 | if (mAnrTracker.empty()) { |
| 1767 | // already processed all events that we waited for |
| 1768 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1769 | return false; |
| 1770 | } |
| 1771 | |
| 1772 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1773 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1774 | // 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] | 1775 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1776 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1777 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1778 | return true; |
| 1779 | } |
| 1780 | |
| 1781 | // We still have pending events, and already started the timer |
| 1782 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1783 | return true; // Still waiting |
| 1784 | } |
| 1785 | |
| 1786 | // Waited too long, and some connection still hasn't processed all motions |
| 1787 | // Just send the key to the focused window |
| 1788 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1789 | focusedWindowName); |
| 1790 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1791 | return false; |
| 1792 | } |
| 1793 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1794 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1795 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1796 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1797 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1798 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1799 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1800 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1801 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1802 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1803 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1804 | // If there is no currently focused window and no focused application |
| 1805 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1806 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1807 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1808 | "display %" PRId32 ".", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1809 | NamedEnum::string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1810 | return InputEventInjectionResult::FAILED; |
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 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1814 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1815 | // start interacting with another application via touch (app switch). This code can be removed |
| 1816 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1817 | // an app is expected to have a focused window. |
| 1818 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1819 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1820 | // 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] | 1821 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1822 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1823 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1824 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1825 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1826 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1827 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1828 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1829 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1830 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1831 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1832 | // Already raised ANR. Drop the event |
| 1833 | ALOGE("Dropping %s event because there is no focused window", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1834 | NamedEnum::string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1835 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1836 | } else { |
| 1837 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1838 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | // we have a valid, non-null focused window |
| 1843 | resetNoFocusedWindowTimeoutLocked(); |
| 1844 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1846 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1847 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1850 | if (focusedWindowHandle->getInfo()->paused) { |
| 1851 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1852 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | // If the event is a key event, then we must wait for all previous events to |
| 1856 | // complete before delivering it because previous events may have the |
| 1857 | // side-effect of transferring focus to a different window and we want to |
| 1858 | // ensure that the following keys are sent to the new window. |
| 1859 | // |
| 1860 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1861 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1862 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1863 | // often anticipate pending UI changes when typing on a keyboard. |
| 1864 | // To obtain this behavior, we must serialize key events with respect to all |
| 1865 | // prior input events. |
| 1866 | if (entry.type == EventEntry::Type::KEY) { |
| 1867 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1868 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1869 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1870 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1874 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1875 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1876 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1877 | |
| 1878 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1879 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1882 | /** |
| 1883 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1884 | * that are currently unresponsive. |
| 1885 | */ |
| 1886 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1887 | const std::vector<TouchedMonitor>& monitors) const { |
| 1888 | std::vector<TouchedMonitor> responsiveMonitors; |
| 1889 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
| 1890 | [this](const TouchedMonitor& monitor) REQUIRES(mLock) { |
| 1891 | sp<Connection> connection = getConnectionLocked( |
| 1892 | monitor.monitor.inputChannel->getConnectionToken()); |
| 1893 | if (connection == nullptr) { |
| 1894 | ALOGE("Could not find connection for monitor %s", |
| 1895 | monitor.monitor.inputChannel->getName().c_str()); |
| 1896 | return false; |
| 1897 | } |
| 1898 | if (!connection->responsive) { |
| 1899 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1900 | connection->inputChannel->getName().c_str()); |
| 1901 | return false; |
| 1902 | } |
| 1903 | return true; |
| 1904 | }); |
| 1905 | return responsiveMonitors; |
| 1906 | } |
| 1907 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1908 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1909 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1910 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1911 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1912 | enum InjectionPermission { |
| 1913 | INJECTION_PERMISSION_UNKNOWN, |
| 1914 | INJECTION_PERMISSION_GRANTED, |
| 1915 | INJECTION_PERMISSION_DENIED |
| 1916 | }; |
| 1917 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 | // For security reasons, we defer updating the touch state until we are sure that |
| 1919 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1920 | int32_t displayId = entry.displayId; |
| 1921 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1923 | |
| 1924 | // 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] | 1925 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1927 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1928 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1930 | // Copy current touch state into tempTouchState. |
| 1931 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1932 | // 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] | 1933 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1934 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1935 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1936 | mTouchStatesByDisplay.find(displayId); |
| 1937 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1938 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1939 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1942 | bool isSplit = tempTouchState.split; |
| 1943 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1944 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1945 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1946 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1947 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1948 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1949 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1950 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1951 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1952 | bool wrongDevice = false; |
| 1953 | if (newGesture) { |
| 1954 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1955 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1956 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1957 | "in display %" PRId32, |
| 1958 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1959 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1960 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1961 | switchedDevice = false; |
| 1962 | wrongDevice = true; |
| 1963 | goto Failed; |
| 1964 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1965 | tempTouchState.reset(); |
| 1966 | tempTouchState.down = down; |
| 1967 | tempTouchState.deviceId = entry.deviceId; |
| 1968 | tempTouchState.source = entry.source; |
| 1969 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1970 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1971 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1972 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 1973 | "in display %" PRId32, |
| 1974 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1975 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1976 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1977 | switchedDevice = false; |
| 1978 | wrongDevice = true; |
| 1979 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 1983 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 1984 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1985 | int32_t x; |
| 1986 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1988 | // Always dispatch mouse events to cursor position. |
| 1989 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1990 | x = int32_t(entry.xCursorPosition); |
| 1991 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1992 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1993 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1994 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1995 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1996 | bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1997 | newTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1998 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 1999 | isDown /*addOutsideTargets*/, true /*addPortalWindows*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2000 | |
| 2001 | std::vector<TouchedMonitor> newGestureMonitors = isDown |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2002 | ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2003 | : std::vector<TouchedMonitor>{}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2006 | if (newTouchedWindowHandle != nullptr && |
| 2007 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2008 | // New window supports splitting, but we should never split mouse events. |
| 2009 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2010 | } else if (isSplit) { |
| 2011 | // New window does not support splitting but we have already split events. |
| 2012 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2013 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2017 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2018 | // 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] | 2019 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2022 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2023 | ALOGI("Not sending touch event to %s because it is paused", |
| 2024 | newTouchedWindowHandle->getName().c_str()); |
| 2025 | newTouchedWindowHandle = nullptr; |
| 2026 | } |
| 2027 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2028 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2029 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2030 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2031 | if (!isResponsive) { |
| 2032 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2033 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2034 | newTouchedWindowHandle = nullptr; |
| 2035 | } |
| 2036 | } |
| 2037 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2038 | // Drop events that can't be trusted due to occlusion |
| 2039 | if (newTouchedWindowHandle != nullptr && |
| 2040 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2041 | TouchOcclusionInfo occlusionInfo = |
| 2042 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2043 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2044 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2045 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2046 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2047 | ALOGD("%s", log.c_str()); |
| 2048 | } |
| 2049 | } |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2050 | onUntrustedTouchLocked(occlusionInfo.obscuringPackage); |
| 2051 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2052 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2053 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2054 | newTouchedWindowHandle = nullptr; |
| 2055 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2059 | // Also don't send the new touch event to unresponsive gesture monitors |
| 2060 | newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); |
| 2061 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2062 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2063 | 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] | 2064 | "(%d, %d) in display %" PRId32 ".", |
| 2065 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2066 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2067 | goto Failed; |
| 2068 | } |
| 2069 | |
| 2070 | if (newTouchedWindowHandle != nullptr) { |
| 2071 | // Set target flags. |
| 2072 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2073 | if (isSplit) { |
| 2074 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2075 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2076 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2077 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2078 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2079 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2080 | } |
| 2081 | |
| 2082 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2083 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2084 | newHoverWindowHandle = nullptr; |
| 2085 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2086 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | // Update the temporary touch state. |
| 2090 | BitSet32 pointerIds; |
| 2091 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2092 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2093 | pointerIds.markBit(pointerId); |
| 2094 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2095 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2096 | } |
| 2097 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2098 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2099 | } else { |
| 2100 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2101 | |
| 2102 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2103 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2104 | if (DEBUG_FOCUS) { |
| 2105 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2106 | "dropped the pointer down event in display %" PRId32, |
| 2107 | displayId); |
| 2108 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2109 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2110 | goto Failed; |
| 2111 | } |
| 2112 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2113 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2114 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2115 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2116 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2117 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2118 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2119 | 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] | 2120 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2121 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2122 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2123 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2124 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2125 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2126 | if (DEBUG_FOCUS) { |
| 2127 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2128 | oldTouchedWindowHandle->getName().c_str(), |
| 2129 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2130 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2131 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2132 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2133 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2134 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2135 | |
| 2136 | // Make a slippery entrance into the new window. |
| 2137 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2138 | isSplit = true; |
| 2139 | } |
| 2140 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2141 | int32_t targetFlags = |
| 2142 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2143 | if (isSplit) { |
| 2144 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2145 | } |
| 2146 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2147 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2148 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2149 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | BitSet32 pointerIds; |
| 2153 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2154 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2155 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2156 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2162 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2163 | // when dispatching it as is to newTouchedWindowHandle. |
| 2164 | if (mLastHoverWindowHandle != nullptr && |
| 2165 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2166 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2167 | #if DEBUG_HOVER |
| 2168 | ALOGD("Sending hover exit event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2169 | mLastHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2170 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2171 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2172 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2175 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2176 | // when dispatching it as is to newTouchedWindowHandle. |
| 2177 | if (newHoverWindowHandle != nullptr && |
| 2178 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2179 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2180 | #if DEBUG_HOVER |
| 2181 | ALOGD("Sending hover enter event to window %s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2182 | newHoverWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 | #endif |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2184 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2185 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2186 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | // Check permission to inject into all touched foreground windows and ensure there |
| 2191 | // is at least one touched foreground window. |
| 2192 | { |
| 2193 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2194 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2195 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2196 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2197 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2198 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2200 | goto Failed; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2204 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2205 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2206 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2207 | "%" PRId32 " or gesture monitor to receive it.", |
| 2208 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2209 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | goto Failed; |
| 2211 | } |
| 2212 | |
| 2213 | // Permission granted to injection into all touched foreground windows. |
| 2214 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2215 | } |
| 2216 | |
| 2217 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2218 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2219 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2220 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2221 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2222 | if (foregroundWindowHandle) { |
| 2223 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2224 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2225 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2226 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2227 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2228 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2229 | InputTarget::FLAG_ZERO_COORDS, |
| 2230 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2231 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2237 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2238 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2239 | // of the touch gesture. |
| 2240 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2241 | // engine only supports touch events. We would need to add a mechanism similar |
| 2242 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2243 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2244 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2245 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2246 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2247 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2248 | getWindowHandlesLocked(displayId); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2249 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2250 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2251 | if (info->displayId == displayId && |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2252 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2253 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2254 | .addOrUpdateWindow(windowHandle, |
| 2255 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2256 | InputTarget:: |
| 2257 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2258 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2259 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2266 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2267 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2268 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2270 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2271 | } |
| 2272 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2273 | for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2274 | addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2275 | touchedMonitor.yOffset, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2278 | // Drop the outside or hover touch windows since we will not care about them |
| 2279 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2280 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2281 | |
| 2282 | Failed: |
| 2283 | // Check injection permission once and for all. |
| 2284 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2285 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2286 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2287 | } else { |
| 2288 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2292 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2293 | return injectionResult; |
| 2294 | } |
| 2295 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2296 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2297 | if (!wrongDevice) { |
| 2298 | if (switchedDevice) { |
| 2299 | if (DEBUG_FOCUS) { |
| 2300 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2301 | } |
| 2302 | *outConflictingPointerActions = true; |
| 2303 | } |
| 2304 | |
| 2305 | if (isHoverAction) { |
| 2306 | // Started hovering, therefore no longer down. |
| 2307 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2308 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2309 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2310 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2311 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2312 | *outConflictingPointerActions = true; |
| 2313 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2314 | tempTouchState.reset(); |
| 2315 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2316 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2317 | tempTouchState.deviceId = entry.deviceId; |
| 2318 | tempTouchState.source = entry.source; |
| 2319 | tempTouchState.displayId = displayId; |
| 2320 | } |
| 2321 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2322 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2323 | // All pointers up or canceled. |
| 2324 | tempTouchState.reset(); |
| 2325 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2326 | // First pointer went down. |
| 2327 | if (oldState && oldState->down) { |
| 2328 | if (DEBUG_FOCUS) { |
| 2329 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2330 | } |
| 2331 | *outConflictingPointerActions = true; |
| 2332 | } |
| 2333 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2334 | // One pointer went up. |
| 2335 | if (isSplit) { |
| 2336 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2337 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2339 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2340 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2341 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2342 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2343 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2344 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2345 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2347 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2348 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2349 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2350 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2351 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2352 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2353 | // Save changes unless the action was scroll in which case the temporary touch |
| 2354 | // state was only valid for this one action. |
| 2355 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2356 | if (tempTouchState.displayId >= 0) { |
| 2357 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2358 | } else { |
| 2359 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2360 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2361 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2362 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2363 | // Update hover state. |
| 2364 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2367 | return injectionResult; |
| 2368 | } |
| 2369 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2370 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2371 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2372 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
| 2373 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2374 | true /*ignoreDragWindow*/); |
| 2375 | if (dropWindow) { |
| 2376 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
| 2377 | notifyDropWindowLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2378 | } else { |
| 2379 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2380 | } |
| 2381 | mDragState.reset(); |
| 2382 | } |
| 2383 | |
| 2384 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2385 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2386 | return; |
| 2387 | } |
| 2388 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2389 | if (!mDragState->isStartDrag) { |
| 2390 | mDragState->isStartDrag = true; |
| 2391 | mDragState->isStylusButtonDownAtStart = |
| 2392 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2393 | } |
| 2394 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2395 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2396 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2397 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2398 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2399 | // Handle the special case : stylus button no longer pressed. |
| 2400 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2401 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2402 | finishDragAndDrop(entry.displayId, x, y); |
| 2403 | return; |
| 2404 | } |
| 2405 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2406 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2407 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2408 | false /*addOutsideTargets*/, false /*addPortalWindows*/, |
| 2409 | true /*ignoreDragWindow*/); |
| 2410 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2411 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2412 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2413 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2414 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2415 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2416 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2417 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2418 | } |
| 2419 | // enqueue drag location if needed. |
| 2420 | if (hoverWindowHandle != nullptr) { |
| 2421 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2422 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2423 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2424 | finishDragAndDrop(entry.displayId, x, y); |
| 2425 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2426 | notifyDropWindowLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2427 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2431 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2432 | int32_t targetFlags, BitSet32 pointerIds, |
| 2433 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2434 | std::vector<InputTarget>::iterator it = |
| 2435 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2436 | [&windowHandle](const InputTarget& inputTarget) { |
| 2437 | return inputTarget.inputChannel->getConnectionToken() == |
| 2438 | windowHandle->getToken(); |
| 2439 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2440 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2441 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2442 | |
| 2443 | if (it == inputTargets.end()) { |
| 2444 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2445 | std::shared_ptr<InputChannel> inputChannel = |
| 2446 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2447 | if (inputChannel == nullptr) { |
| 2448 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2449 | return; |
| 2450 | } |
| 2451 | inputTarget.inputChannel = inputChannel; |
| 2452 | inputTarget.flags = targetFlags; |
| 2453 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 2454 | inputTarget.displayOrientation = windowInfo->displayOrientation; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2455 | inputTarget.displaySize = |
Evan Rosky | 44edce9 | 2021-05-14 18:09:55 -0700 | [diff] [blame] | 2456 | int2(windowHandle->getInfo()->displayWidth, windowHandle->getInfo()->displayHeight); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2457 | inputTargets.push_back(inputTarget); |
| 2458 | it = inputTargets.end() - 1; |
| 2459 | } |
| 2460 | |
| 2461 | ALOG_ASSERT(it->flags == targetFlags); |
| 2462 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2463 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2464 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2467 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2468 | int32_t displayId, float xOffset, |
| 2469 | float yOffset) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2470 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2471 | mGlobalMonitorsByDisplay.find(displayId); |
| 2472 | |
| 2473 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2474 | const std::vector<Monitor>& monitors = it->second; |
| 2475 | for (const Monitor& monitor : monitors) { |
| 2476 | addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2481 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, |
| 2482 | float yOffset, |
| 2483 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2484 | InputTarget target; |
| 2485 | target.inputChannel = monitor.inputChannel; |
| 2486 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2487 | ui::Transform t; |
| 2488 | t.set(xOffset, yOffset); |
| 2489 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2490 | inputTargets.push_back(target); |
| 2491 | } |
| 2492 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2493 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2494 | const InjectionState* injectionState) { |
| 2495 | if (injectionState && |
| 2496 | (windowHandle == nullptr || |
| 2497 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2498 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2499 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2500 | 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] | 2501 | "owned by uid %d", |
| 2502 | injectionState->injectorPid, injectionState->injectorUid, |
| 2503 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2504 | } else { |
| 2505 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2506 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2507 | } |
| 2508 | return false; |
| 2509 | } |
| 2510 | return true; |
| 2511 | } |
| 2512 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2513 | /** |
| 2514 | * Indicate whether one window handle should be considered as obscuring |
| 2515 | * another window handle. We only check a few preconditions. Actually |
| 2516 | * checking the bounds is left to the caller. |
| 2517 | */ |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2518 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2519 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2520 | // Compare by token so cloned layers aren't counted |
| 2521 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2522 | return false; |
| 2523 | } |
| 2524 | auto info = windowHandle->getInfo(); |
| 2525 | auto otherInfo = otherHandle->getInfo(); |
| 2526 | if (!otherInfo->visible) { |
| 2527 | return false; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2528 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2529 | // Those act as if they were invisible, so we don't need to flag them. |
| 2530 | // We do want to potentially flag touchable windows even if they have 0 |
| 2531 | // opacity, since they can consume touches and alter the effects of the |
| 2532 | // user interaction (eg. apps that rely on |
| 2533 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2534 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2535 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2536 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2537 | // If ownerUid is the same we don't generate occlusion events as there |
| 2538 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2539 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2540 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2541 | return false; |
| 2542 | } else if (otherInfo->displayId != info->displayId) { |
| 2543 | return false; |
| 2544 | } |
| 2545 | return true; |
| 2546 | } |
| 2547 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2548 | /** |
| 2549 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2550 | * untrusted, one should check: |
| 2551 | * |
| 2552 | * 1. If result.hasBlockingOcclusion is true. |
| 2553 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2554 | * BLOCK_UNTRUSTED. |
| 2555 | * |
| 2556 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2557 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2558 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2559 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2560 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2561 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2562 | * |
| 2563 | * If neither of those is true, then it means the touch can be allowed. |
| 2564 | */ |
| 2565 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2566 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2567 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2568 | int32_t displayId = windowInfo->displayId; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2569 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2570 | TouchOcclusionInfo info; |
| 2571 | info.hasBlockingOcclusion = false; |
| 2572 | info.obscuringOpacity = 0; |
| 2573 | info.obscuringUid = -1; |
| 2574 | std::map<int32_t, float> opacityByUid; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2575 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2576 | if (windowHandle == otherHandle) { |
| 2577 | break; // All future windows are below us. Exit early. |
| 2578 | } |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2579 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2580 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2581 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2582 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2583 | info.debugInfo.push_back( |
| 2584 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2585 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2586 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2587 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2588 | // window's touch occlusion mode |
| 2589 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2590 | info.hasBlockingOcclusion = true; |
| 2591 | info.obscuringUid = otherInfo->ownerUid; |
| 2592 | info.obscuringPackage = otherInfo->packageName; |
| 2593 | break; |
| 2594 | } |
| 2595 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2596 | uint32_t uid = otherInfo->ownerUid; |
| 2597 | float opacity = |
| 2598 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2599 | // Given windows A and B: |
| 2600 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2601 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2602 | opacityByUid[uid] = opacity; |
| 2603 | if (opacity > info.obscuringOpacity) { |
| 2604 | info.obscuringOpacity = opacity; |
| 2605 | info.obscuringUid = uid; |
| 2606 | info.obscuringPackage = otherInfo->packageName; |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2611 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2612 | info.debugInfo.push_back( |
| 2613 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2614 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2615 | return info; |
| 2616 | } |
| 2617 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2618 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2619 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2620 | return StringPrintf(INDENT2 |
| 2621 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2622 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2623 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2624 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2625 | (isTouchedWindow) ? "[TOUCHED] " : "", |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2626 | NamedEnum::string(info->type, "%" PRId32).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2627 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2628 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2629 | info->frameTop, info->frameRight, info->frameBottom, |
| 2630 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2631 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2632 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2633 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2636 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2637 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2638 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2639 | occlusionInfo.obscuringUid); |
| 2640 | return false; |
| 2641 | } |
| 2642 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2643 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2644 | "%.2f, maximum allowed = %.2f)", |
| 2645 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2646 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2647 | return false; |
| 2648 | } |
| 2649 | return true; |
| 2650 | } |
| 2651 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2652 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2653 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2654 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2655 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2656 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2657 | if (windowHandle == otherHandle) { |
| 2658 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2659 | } |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2660 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2661 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2662 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2663 | return true; |
| 2664 | } |
| 2665 | } |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2669 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2670 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2671 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2672 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2673 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2674 | if (windowHandle == otherHandle) { |
| 2675 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2676 | } |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2677 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2678 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2679 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2680 | return true; |
| 2681 | } |
| 2682 | } |
| 2683 | return false; |
| 2684 | } |
| 2685 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2686 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2687 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2688 | if (applicationHandle != nullptr) { |
| 2689 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2690 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2691 | } else { |
| 2692 | return applicationHandle->getName(); |
| 2693 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2694 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2695 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2696 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2697 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2698 | } |
| 2699 | } |
| 2700 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2701 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2702 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2703 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2704 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2705 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2706 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2707 | return; |
| 2708 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2709 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2710 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2711 | if (focusedWindowHandle != nullptr) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2712 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2713 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2714 | #if DEBUG_DISPATCH_CYCLE |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2715 | 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] | 2716 | #endif |
| 2717 | return; |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2722 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2723 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2724 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2725 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2726 | return; |
| 2727 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2728 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2729 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2730 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2731 | } |
| 2732 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2733 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2734 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2735 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2736 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2737 | return; |
| 2738 | } |
| 2739 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2740 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2741 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2742 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2743 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2744 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2745 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2746 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2747 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2748 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2749 | NamedEnum::string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2750 | break; |
| 2751 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2752 | } |
| 2753 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2754 | std::unique_ptr<CommandEntry> commandEntry = |
| 2755 | std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2756 | commandEntry->eventTime = eventEntry.eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2757 | commandEntry->userActivityEventType = eventType; |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 2758 | commandEntry->displayId = displayId; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2759 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2763 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2764 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2765 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2766 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2767 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2768 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2769 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2770 | ATRACE_NAME(message.c_str()); |
| 2771 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | #if DEBUG_DISPATCH_CYCLE |
| 2773 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2774 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2775 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2776 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2777 | inputTarget.getPointerInfoString().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2778 | #endif |
| 2779 | |
| 2780 | // Skip this event if the connection status is not normal. |
| 2781 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2782 | if (connection->status != Connection::STATUS_NORMAL) { |
| 2783 | #if DEBUG_DISPATCH_CYCLE |
| 2784 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2785 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2786 | #endif |
| 2787 | return; |
| 2788 | } |
| 2789 | |
| 2790 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2791 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2792 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2793 | "Entry type %s should not have FLAG_SPLIT", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2794 | NamedEnum::string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2795 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2796 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2797 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2798 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2799 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2800 | if (!splitMotionEntry) { |
| 2801 | return; // split event was dropped |
| 2802 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2803 | if (DEBUG_FOCUS) { |
| 2804 | ALOGD("channel '%s' ~ Split motion event.", |
| 2805 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2806 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2807 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2808 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2809 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 | return; |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2815 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2816 | } |
| 2817 | |
| 2818 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2819 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2820 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2821 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2822 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2823 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2824 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2825 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2826 | ATRACE_NAME(message.c_str()); |
| 2827 | } |
| 2828 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2829 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2830 | |
| 2831 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2832 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2833 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2834 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2835 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2836 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2837 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2838 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2839 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2840 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2841 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2842 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2843 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2844 | |
| 2845 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2846 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2847 | startDispatchCycleLocked(currentTime, connection); |
| 2848 | } |
| 2849 | } |
| 2850 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2851 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2852 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2853 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2854 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2855 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2856 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2857 | connection->getInputChannelName().c_str(), |
| 2858 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2859 | ATRACE_NAME(message.c_str()); |
| 2860 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2861 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2862 | if (!(inputTargetFlags & dispatchMode)) { |
| 2863 | return; |
| 2864 | } |
| 2865 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2866 | |
| 2867 | // This is a new event. |
| 2868 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2869 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2870 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2871 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2872 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2873 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2874 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2875 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2876 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2877 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2878 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2879 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2880 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2881 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2882 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2883 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2884 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2885 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2886 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2887 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2888 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2889 | return; // skip the inconsistent event |
| 2890 | } |
| 2891 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2893 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2894 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2895 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2896 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2897 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2898 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2899 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2900 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2901 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2902 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2903 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2904 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2905 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2906 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2907 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2908 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2909 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2910 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2911 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2912 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2913 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2914 | } |
| 2915 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2916 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2917 | motionEntry.displayId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2918 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2919 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " |
| 2920 | "event", |
| 2921 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2922 | #endif |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2923 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2924 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2925 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2926 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2927 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2928 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2929 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2930 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2931 | } |
| 2932 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 2933 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2934 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2935 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2936 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 2937 | dispatchEntry->resolvedFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2938 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2939 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 2940 | "event", |
| 2941 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2942 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2943 | return; // skip the inconsistent event |
| 2944 | } |
| 2945 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2946 | dispatchEntry->resolvedEventId = |
| 2947 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 2948 | ? mIdGenerator.nextId() |
| 2949 | : motionEntry.id; |
| 2950 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 2951 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 2952 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 2953 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 2954 | ATRACE_NAME(message.c_str()); |
| 2955 | } |
| 2956 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 2957 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 2958 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 2959 | // Skip reporting pointer down outside focus to the policy. |
| 2960 | break; |
| 2961 | } |
| 2962 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2963 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2964 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2965 | |
| 2966 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2967 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2968 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2969 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2970 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2971 | break; |
| 2972 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2973 | case EventEntry::Type::SENSOR: { |
| 2974 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 2975 | break; |
| 2976 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2977 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 2978 | case EventEntry::Type::DEVICE_RESET: { |
| 2979 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2980 | NamedEnum::string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2981 | break; |
| 2982 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | // Remember that we are waiting for this dispatch to complete. |
| 2986 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2987 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2991 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 2992 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2995 | /** |
| 2996 | * This function is purely for debugging. It helps us understand where the user interaction |
| 2997 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 2998 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 2999 | * We will see both launcher and wallpaper in that list. |
| 3000 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3001 | * until the set of interacted connections changes. |
| 3002 | * |
| 3003 | * The following items are skipped, to reduce the logspam: |
| 3004 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3005 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3006 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3007 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3008 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3009 | */ |
| 3010 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3011 | const std::vector<InputTarget>& targets) { |
| 3012 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3013 | if (entry.type == EventEntry::Type::KEY) { |
| 3014 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3015 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3016 | return; |
| 3017 | } |
| 3018 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3019 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3020 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3021 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3022 | return; |
| 3023 | } |
| 3024 | } else { |
| 3025 | return; // Not a key or a motion |
| 3026 | } |
| 3027 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3028 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3029 | std::vector<sp<Connection>> newConnections; |
| 3030 | for (const InputTarget& target : targets) { |
| 3031 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3032 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3033 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3034 | } |
| 3035 | |
| 3036 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3037 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3038 | if (connection == nullptr) { |
| 3039 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3040 | } |
| 3041 | newConnectionTokens.insert(std::move(token)); |
| 3042 | newConnections.emplace_back(connection); |
| 3043 | } |
| 3044 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3045 | return; // no change |
| 3046 | } |
| 3047 | mInteractionConnectionTokens = newConnectionTokens; |
| 3048 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3049 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3050 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3051 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3052 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3053 | std::string message = "Interaction with: " + targetList; |
| 3054 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3055 | message += "<none>"; |
| 3056 | } |
| 3057 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3058 | } |
| 3059 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3060 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3061 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3062 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3063 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3064 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3065 | return; |
| 3066 | } |
| 3067 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3068 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3069 | if (focusedToken == token) { |
| 3070 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3071 | return; |
| 3072 | } |
| 3073 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3074 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 3075 | &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3076 | commandEntry->newToken = token; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 3077 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3081 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3082 | if (ATRACE_ENABLED()) { |
| 3083 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3084 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3085 | ATRACE_NAME(message.c_str()); |
| 3086 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3087 | #if DEBUG_DISPATCH_CYCLE |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3088 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3089 | #endif |
| 3090 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3091 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3092 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3093 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3094 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3095 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3096 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3097 | |
| 3098 | // Publish the event. |
| 3099 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3100 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3101 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3102 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3103 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3104 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3105 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3106 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3107 | status = connection->inputPublisher |
| 3108 | .publishKeyEvent(dispatchEntry->seq, |
| 3109 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3110 | keyEntry.source, keyEntry.displayId, |
| 3111 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3112 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3113 | keyEntry.scanCode, keyEntry.metaState, |
| 3114 | keyEntry.repeatCount, keyEntry.downTime, |
| 3115 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3116 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3117 | } |
| 3118 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3119 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3120 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3121 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3122 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3123 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3124 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3125 | // 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] | 3126 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3127 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3128 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3129 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3130 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3131 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3132 | // Don't apply window scale here since we don't want scale to affect raw |
| 3133 | // coordinates. The scale will be sent back to the client and applied |
| 3134 | // later when requesting relative coordinates. |
| 3135 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3136 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3137 | } |
| 3138 | usingCoords = scaledCoords; |
| 3139 | } |
| 3140 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3141 | // We don't want the dispatch target to know. |
| 3142 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3143 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3144 | scaledCoords[i].clear(); |
| 3145 | } |
| 3146 | usingCoords = scaledCoords; |
| 3147 | } |
| 3148 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3149 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3150 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3151 | |
| 3152 | // Publish the motion event. |
| 3153 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3154 | .publishMotionEvent(dispatchEntry->seq, |
| 3155 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3156 | motionEntry.deviceId, motionEntry.source, |
| 3157 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3158 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3159 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3160 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3161 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3162 | motionEntry.buttonState, |
| 3163 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3164 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3165 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3166 | motionEntry.xCursorPosition, |
| 3167 | motionEntry.yCursorPosition, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3168 | dispatchEntry->displayOrientation, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3169 | dispatchEntry->displaySize.x, |
| 3170 | dispatchEntry->displaySize.y, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3171 | motionEntry.downTime, motionEntry.eventTime, |
| 3172 | motionEntry.pointerCount, |
| 3173 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3174 | break; |
| 3175 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3176 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3177 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3178 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3179 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3180 | focusEntry.id, |
| 3181 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3182 | mInTouchMode); |
| 3183 | break; |
| 3184 | } |
| 3185 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3186 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3187 | const auto& captureEntry = |
| 3188 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3189 | status = connection->inputPublisher |
| 3190 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
| 3191 | captureEntry.pointerCaptureEnabled); |
| 3192 | break; |
| 3193 | } |
| 3194 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3195 | case EventEntry::Type::DRAG: { |
| 3196 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3197 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3198 | dragEntry.id, dragEntry.x, |
| 3199 | dragEntry.y, |
| 3200 | dragEntry.isExiting); |
| 3201 | break; |
| 3202 | } |
| 3203 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3204 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3205 | case EventEntry::Type::DEVICE_RESET: |
| 3206 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3207 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3208 | NamedEnum::string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3209 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3210 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | // Check the result. |
| 3214 | if (status) { |
| 3215 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3216 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3217 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3218 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3219 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3220 | "event to it, status=%s(%d)", |
| 3221 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3222 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3223 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3224 | } else { |
| 3225 | // Pipe is full and we are waiting for the app to finish process some events |
| 3226 | // before sending more events to it. |
| 3227 | #if DEBUG_DISPATCH_CYCLE |
| 3228 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3229 | "waiting for the application to catch up", |
| 3230 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3231 | #endif |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3232 | } |
| 3233 | } else { |
| 3234 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3235 | "status=%s(%d)", |
| 3236 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3237 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3238 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3239 | } |
| 3240 | return; |
| 3241 | } |
| 3242 | |
| 3243 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3244 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3245 | connection->outboundQueue.end(), |
| 3246 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3247 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3248 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3249 | if (connection->responsive) { |
| 3250 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3251 | connection->inputChannel->getConnectionToken()); |
| 3252 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3253 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3254 | } |
| 3255 | } |
| 3256 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3257 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3258 | size_t size; |
| 3259 | switch (event.type) { |
| 3260 | case VerifiedInputEvent::Type::KEY: { |
| 3261 | size = sizeof(VerifiedKeyEvent); |
| 3262 | break; |
| 3263 | } |
| 3264 | case VerifiedInputEvent::Type::MOTION: { |
| 3265 | size = sizeof(VerifiedMotionEvent); |
| 3266 | break; |
| 3267 | } |
| 3268 | } |
| 3269 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3270 | return mHmacKeyManager.sign(start, size); |
| 3271 | } |
| 3272 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3273 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3274 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3275 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3276 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3277 | // Only sign events up and down events as the purely move events |
| 3278 | // are tied to their up/down counterparts so signing would be redundant. |
| 3279 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3280 | verifiedEvent.actionMasked = actionMasked; |
| 3281 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3282 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3283 | } |
| 3284 | return INVALID_HMAC; |
| 3285 | } |
| 3286 | |
| 3287 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3288 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3289 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3290 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3291 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3292 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3295 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3296 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3297 | bool handled, nsecs_t consumeTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | #if DEBUG_DISPATCH_CYCLE |
| 3299 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3300 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3301 | #endif |
| 3302 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3303 | if (connection->status == Connection::STATUS_BROKEN || |
| 3304 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | return; |
| 3306 | } |
| 3307 | |
| 3308 | // Notify other system components and prepare to start the next dispatch cycle. |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3309 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled, consumeTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3313 | const sp<Connection>& connection, |
| 3314 | bool notify) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3315 | #if DEBUG_DISPATCH_CYCLE |
| 3316 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3317 | connection->getInputChannelName().c_str(), toString(notify)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3318 | #endif |
| 3319 | |
| 3320 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3321 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3322 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3323 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3324 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3325 | |
| 3326 | // The connection appears to be unrecoverably broken. |
| 3327 | // Ignore already broken or zombie connections. |
| 3328 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3329 | connection->status = Connection::STATUS_BROKEN; |
| 3330 | |
| 3331 | if (notify) { |
| 3332 | // Notify other system components. |
| 3333 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 3334 | } |
| 3335 | } |
| 3336 | } |
| 3337 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3338 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3339 | while (!queue.empty()) { |
| 3340 | DispatchEntry* dispatchEntry = queue.front(); |
| 3341 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3342 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | } |
| 3344 | } |
| 3345 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3346 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3348 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3349 | } |
| 3350 | delete dispatchEntry; |
| 3351 | } |
| 3352 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3353 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3354 | std::scoped_lock _l(mLock); |
| 3355 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3356 | if (connection == nullptr) { |
| 3357 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3358 | connectionToken.get(), events); |
| 3359 | return 0; // remove the callback |
| 3360 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3361 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3362 | bool notify; |
| 3363 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3364 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3365 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3366 | "events=0x%x", |
| 3367 | connection->getInputChannelName().c_str(), events); |
| 3368 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3369 | } |
| 3370 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3371 | nsecs_t currentTime = now(); |
| 3372 | bool gotOne = false; |
| 3373 | status_t status = OK; |
| 3374 | for (;;) { |
| 3375 | Result<InputPublisher::ConsumerResponse> result = |
| 3376 | connection->inputPublisher.receiveConsumerResponse(); |
| 3377 | if (!result.ok()) { |
| 3378 | status = result.error().code(); |
| 3379 | break; |
| 3380 | } |
| 3381 | |
| 3382 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3383 | const InputPublisher::Finished& finish = |
| 3384 | std::get<InputPublisher::Finished>(*result); |
| 3385 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3386 | finish.consumeTime); |
| 3387 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3388 | if (shouldReportMetricsForConnection(*connection)) { |
| 3389 | const InputPublisher::Timeline& timeline = |
| 3390 | std::get<InputPublisher::Timeline>(*result); |
| 3391 | mLatencyTracker |
| 3392 | .trackGraphicsLatency(timeline.inputEventId, |
| 3393 | connection->inputChannel->getConnectionToken(), |
| 3394 | std::move(timeline.graphicsTimeline)); |
| 3395 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3396 | } |
| 3397 | gotOne = true; |
| 3398 | } |
| 3399 | if (gotOne) { |
| 3400 | runCommandsLockedInterruptible(); |
| 3401 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3402 | return 1; |
| 3403 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3404 | } |
| 3405 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3406 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3407 | if (notify) { |
| 3408 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3409 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3410 | status); |
| 3411 | } |
| 3412 | } else { |
| 3413 | // Monitor channels are never explicitly unregistered. |
| 3414 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3415 | const bool stillHaveWindowHandle = |
| 3416 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3417 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3418 | if (notify) { |
| 3419 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3420 | connection->getInputChannelName().c_str(), events); |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | // Remove the channel. |
| 3425 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3426 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3427 | } |
| 3428 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3429 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3430 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3431 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3432 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3436 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3437 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3438 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3439 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3440 | } |
| 3441 | |
| 3442 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3443 | const CancelationOptions& options, |
| 3444 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3445 | for (const auto& it : monitorsByDisplay) { |
| 3446 | const std::vector<Monitor>& monitors = it.second; |
| 3447 | for (const Monitor& monitor : monitors) { |
| 3448 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3449 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3453 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3454 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3455 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3456 | if (connection == nullptr) { |
| 3457 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3458 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3459 | |
| 3460 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3464 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3465 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3466 | return; |
| 3467 | } |
| 3468 | |
| 3469 | nsecs_t currentTime = now(); |
| 3470 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3471 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3472 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3473 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3474 | if (cancelationEvents.empty()) { |
| 3475 | return; |
| 3476 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3477 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3478 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3479 | "with reality: %s, mode=%d.", |
| 3480 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3481 | options.mode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 | #endif |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3483 | |
| 3484 | InputTarget target; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3485 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3486 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3487 | if (windowHandle != nullptr) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3488 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3489 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3490 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3491 | } |
| 3492 | target.inputChannel = connection->inputChannel; |
| 3493 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3494 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3495 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3496 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3497 | switch (cancelationEventEntry->type) { |
| 3498 | case EventEntry::Type::KEY: { |
| 3499 | logOutboundKeyDetails("cancel - ", |
| 3500 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3501 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3503 | case EventEntry::Type::MOTION: { |
| 3504 | logOutboundMotionDetails("cancel - ", |
| 3505 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3506 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3507 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3508 | case EventEntry::Type::FOCUS: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3509 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3510 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3511 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3512 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3513 | break; |
| 3514 | } |
| 3515 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3516 | case EventEntry::Type::DEVICE_RESET: |
| 3517 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3518 | 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] | 3519 | NamedEnum::string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3520 | break; |
| 3521 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | } |
| 3523 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3524 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3525 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3527 | |
| 3528 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3531 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3532 | const sp<Connection>& connection) { |
| 3533 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3534 | return; |
| 3535 | } |
| 3536 | |
| 3537 | nsecs_t currentTime = now(); |
| 3538 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3539 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3540 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3541 | |
| 3542 | if (downEvents.empty()) { |
| 3543 | return; |
| 3544 | } |
| 3545 | |
| 3546 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3547 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3548 | connection->getInputChannelName().c_str(), downEvents.size()); |
| 3549 | #endif |
| 3550 | |
| 3551 | InputTarget target; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3552 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3553 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3554 | if (windowHandle != nullptr) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3555 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3556 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3557 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3558 | } |
| 3559 | target.inputChannel = connection->inputChannel; |
| 3560 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3561 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3562 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3563 | switch (downEventEntry->type) { |
| 3564 | case EventEntry::Type::MOTION: { |
| 3565 | logOutboundMotionDetails("down - ", |
| 3566 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3567 | break; |
| 3568 | } |
| 3569 | |
| 3570 | case EventEntry::Type::KEY: |
| 3571 | case EventEntry::Type::FOCUS: |
| 3572 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3573 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3574 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3575 | case EventEntry::Type::SENSOR: |
| 3576 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3577 | 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] | 3578 | NamedEnum::string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | } |
| 3582 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3583 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3584 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | startDispatchCycleLocked(currentTime, connection); |
| 3588 | } |
| 3589 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3590 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3591 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 | ALOG_ASSERT(pointerIds.value != 0); |
| 3593 | |
| 3594 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3595 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3596 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3597 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3598 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | uint32_t splitPointerCount = 0; |
| 3600 | |
| 3601 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3602 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3603 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3604 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3605 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3606 | if (pointerIds.hasBit(pointerId)) { |
| 3607 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3608 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3609 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3610 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3611 | splitPointerCount += 1; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | if (splitPointerCount != pointerIds.count()) { |
| 3616 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3617 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3618 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3619 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3620 | // in this way. |
| 3621 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3622 | "we expected there to be %d pointers. This probably means we received " |
| 3623 | "a broken sequence of pointer ids from the input device.", |
| 3624 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3625 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | } |
| 3627 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3628 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3630 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3631 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3633 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3634 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3636 | if (pointerIds.hasBit(pointerId)) { |
| 3637 | if (pointerIds.count() == 1) { |
| 3638 | // The first/last pointer went down/up. |
| 3639 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3640 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3641 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3642 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3643 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3644 | } else { |
| 3645 | // A secondary pointer went down/up. |
| 3646 | uint32_t splitPointerIndex = 0; |
| 3647 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3648 | splitPointerIndex += 1; |
| 3649 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3650 | action = maskedAction | |
| 3651 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3652 | } |
| 3653 | } else { |
| 3654 | // An unrelated pointer changed. |
| 3655 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3656 | } |
| 3657 | } |
| 3658 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3659 | int32_t newId = mIdGenerator.nextId(); |
| 3660 | if (ATRACE_ENABLED()) { |
| 3661 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3662 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3663 | originalMotionEntry.id, newId); |
| 3664 | ATRACE_NAME(message.c_str()); |
| 3665 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3666 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3667 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3668 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3669 | originalMotionEntry.displayId, |
| 3670 | originalMotionEntry.policyFlags, action, |
| 3671 | originalMotionEntry.actionButton, |
| 3672 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3673 | originalMotionEntry.buttonState, |
| 3674 | originalMotionEntry.classification, |
| 3675 | originalMotionEntry.edgeFlags, |
| 3676 | originalMotionEntry.xPrecision, |
| 3677 | originalMotionEntry.yPrecision, |
| 3678 | originalMotionEntry.xCursorPosition, |
| 3679 | originalMotionEntry.yCursorPosition, |
| 3680 | originalMotionEntry.downTime, splitPointerCount, |
| 3681 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3682 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3683 | if (originalMotionEntry.injectionState) { |
| 3684 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3685 | splitMotionEntry->injectionState->refCount += 1; |
| 3686 | } |
| 3687 | |
| 3688 | return splitMotionEntry; |
| 3689 | } |
| 3690 | |
| 3691 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 3692 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3693 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3694 | #endif |
| 3695 | |
| 3696 | bool needWake; |
| 3697 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3698 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3700 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3701 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3702 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3703 | } // release lock |
| 3704 | |
| 3705 | if (needWake) { |
| 3706 | mLooper->wake(); |
| 3707 | } |
| 3708 | } |
| 3709 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3710 | /** |
| 3711 | * If one of the meta shortcuts is detected, process them here: |
| 3712 | * Meta + Backspace -> generate BACK |
| 3713 | * Meta + Enter -> generate HOME |
| 3714 | * This will potentially overwrite keyCode and metaState. |
| 3715 | */ |
| 3716 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3717 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3718 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3719 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3720 | if (keyCode == AKEYCODE_DEL) { |
| 3721 | newKeyCode = AKEYCODE_BACK; |
| 3722 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3723 | newKeyCode = AKEYCODE_HOME; |
| 3724 | } |
| 3725 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3726 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3727 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3728 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3729 | keyCode = newKeyCode; |
| 3730 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3731 | } |
| 3732 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3733 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3734 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3735 | // 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] | 3736 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3737 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3738 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3739 | if (replacementIt != mReplacedKeys.end()) { |
| 3740 | keyCode = replacementIt->second; |
| 3741 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3742 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3743 | } |
| 3744 | } |
| 3745 | } |
| 3746 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3747 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
| 3748 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3749 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3750 | "policyFlags=0x%x, action=0x%x, " |
| 3751 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3752 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3753 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3754 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3755 | #endif |
| 3756 | if (!validateKeyEvent(args->action)) { |
| 3757 | return; |
| 3758 | } |
| 3759 | |
| 3760 | uint32_t policyFlags = args->policyFlags; |
| 3761 | int32_t flags = args->flags; |
| 3762 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3763 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3764 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3765 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3766 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3767 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3768 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3769 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3770 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3771 | metaState |= AMETA_FUNCTION_ON; |
| 3772 | } |
| 3773 | |
| 3774 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3775 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3776 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3777 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3778 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3779 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3780 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3781 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3782 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3783 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3784 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3785 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3786 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3787 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3790 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3791 | bool needWake; |
| 3792 | { // acquire lock |
| 3793 | mLock.lock(); |
| 3794 | |
| 3795 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3796 | mLock.unlock(); |
| 3797 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3798 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3799 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3800 | return; // event was consumed by the filter |
| 3801 | } |
| 3802 | |
| 3803 | mLock.lock(); |
| 3804 | } |
| 3805 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3806 | std::unique_ptr<KeyEntry> newEntry = |
| 3807 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3808 | args->displayId, policyFlags, args->action, flags, |
| 3809 | keyCode, args->scanCode, metaState, repeatCount, |
| 3810 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3811 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3812 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3813 | mLock.unlock(); |
| 3814 | } // release lock |
| 3815 | |
| 3816 | if (needWake) { |
| 3817 | mLooper->wake(); |
| 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3822 | return mInputFilterEnabled; |
| 3823 | } |
| 3824 | |
| 3825 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
| 3826 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3827 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3828 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3829 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3830 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3831 | "yCursorPosition=%f, downTime=%" PRId64, |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3832 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3833 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3834 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3835 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3836 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3837 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3838 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3839 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3840 | "orientation=%f", |
| 3841 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3842 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3843 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3844 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3845 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3846 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3847 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3848 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3849 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3850 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3851 | } |
| 3852 | #endif |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3853 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3854 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3855 | return; |
| 3856 | } |
| 3857 | |
| 3858 | uint32_t policyFlags = args->policyFlags; |
| 3859 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3860 | |
| 3861 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3862 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3863 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3864 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3865 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3866 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3867 | |
| 3868 | bool needWake; |
| 3869 | { // acquire lock |
| 3870 | mLock.lock(); |
| 3871 | |
| 3872 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3873 | mLock.unlock(); |
| 3874 | |
| 3875 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3876 | ui::Transform transform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3877 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3878 | args->action, args->actionButton, args->flags, args->edgeFlags, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3879 | args->metaState, args->buttonState, args->classification, transform, |
| 3880 | args->xPrecision, args->yPrecision, args->xCursorPosition, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3881 | args->yCursorPosition, ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, |
| 3882 | INVALID_DISPLAY_SIZE, args->downTime, args->eventTime, |
| 3883 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3884 | |
| 3885 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3886 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3887 | return; // event was consumed by the filter |
| 3888 | } |
| 3889 | |
| 3890 | mLock.lock(); |
| 3891 | } |
| 3892 | |
| 3893 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3894 | std::unique_ptr<MotionEntry> newEntry = |
| 3895 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3896 | args->source, args->displayId, policyFlags, |
| 3897 | args->action, args->actionButton, args->flags, |
| 3898 | args->metaState, args->buttonState, |
| 3899 | args->classification, args->edgeFlags, |
| 3900 | args->xPrecision, args->yPrecision, |
| 3901 | args->xCursorPosition, args->yCursorPosition, |
| 3902 | args->downTime, args->pointerCount, |
| 3903 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3905 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3906 | mLock.unlock(); |
| 3907 | } // release lock |
| 3908 | |
| 3909 | if (needWake) { |
| 3910 | mLooper->wake(); |
| 3911 | } |
| 3912 | } |
| 3913 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3914 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
| 3915 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3916 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3917 | " sensorType=%s", |
| 3918 | args->id, args->eventTime, args->deviceId, args->source, |
| 3919 | NamedEnum::string(args->sensorType).c_str()); |
| 3920 | #endif |
| 3921 | |
| 3922 | bool needWake; |
| 3923 | { // acquire lock |
| 3924 | mLock.lock(); |
| 3925 | |
| 3926 | // Just enqueue a new sensor event. |
| 3927 | std::unique_ptr<SensorEntry> newEntry = |
| 3928 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 3929 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 3930 | args->sensorType, args->accuracy, |
| 3931 | args->accuracyChanged, args->values); |
| 3932 | |
| 3933 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 3934 | mLock.unlock(); |
| 3935 | } // release lock |
| 3936 | |
| 3937 | if (needWake) { |
| 3938 | mLooper->wake(); |
| 3939 | } |
| 3940 | } |
| 3941 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 3942 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
| 3943 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3944 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 3945 | args->deviceId, args->isOn); |
| 3946 | #endif |
| 3947 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 3948 | } |
| 3949 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3951 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
| 3955 | #if DEBUG_INBOUND_EVENT_DETAILS |
Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3956 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3957 | "switchMask=0x%08x", |
| 3958 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | #endif |
| 3960 | |
| 3961 | uint32_t policyFlags = args->policyFlags; |
| 3962 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3963 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3967 | #if DEBUG_INBOUND_EVENT_DETAILS |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3968 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 3969 | args->deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3970 | #endif |
| 3971 | |
| 3972 | bool needWake; |
| 3973 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3974 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3975 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3976 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 3977 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 3978 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3979 | } // release lock |
| 3980 | |
| 3981 | if (needWake) { |
| 3982 | mLooper->wake(); |
| 3983 | } |
| 3984 | } |
| 3985 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3986 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
| 3987 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3988 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
| 3989 | args->enabled ? "true" : "false"); |
| 3990 | #endif |
| 3991 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3992 | bool needWake; |
| 3993 | { // acquire lock |
| 3994 | std::scoped_lock _l(mLock); |
| 3995 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
| 3996 | args->enabled); |
| 3997 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 3998 | } // release lock |
| 3999 | |
| 4000 | if (needWake) { |
| 4001 | mLooper->wake(); |
| 4002 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4003 | } |
| 4004 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4005 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4006 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4007 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4008 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 4009 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4010 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4011 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | #endif |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4013 | 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] | 4014 | |
| 4015 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4016 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4017 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4018 | } |
| 4019 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4020 | // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4021 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4022 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4023 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4024 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4025 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4026 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4027 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4028 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4031 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4032 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4033 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4034 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4035 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4036 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4037 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4038 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4039 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4040 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4041 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4042 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4043 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4044 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4045 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4046 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4047 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4048 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4049 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4050 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4051 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4052 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4053 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4054 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4055 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4056 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4057 | |
| 4058 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4059 | android::base::Timer t; |
| 4060 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4061 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4062 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4063 | std::to_string(t.duration().count()).c_str()); |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4068 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4069 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4070 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4071 | incomingKey.getDisplayId(), policyFlags, action, |
| 4072 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4073 | incomingKey.getRepeatCount(), |
| 4074 | incomingKey.getDownTime()); |
| 4075 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4076 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4077 | } |
| 4078 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4079 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4080 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 4081 | int32_t action = motionEvent.getAction(); |
| 4082 | size_t pointerCount = motionEvent.getPointerCount(); |
| 4083 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
| 4084 | int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4085 | int32_t flags = motionEvent.getFlags(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4086 | int32_t displayId = motionEvent.getDisplayId(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4087 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4088 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4092 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4093 | android::base::Timer t; |
| 4094 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4095 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4096 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4097 | std::to_string(t.duration().count()).c_str()); |
| 4098 | } |
| 4099 | } |
| 4100 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4101 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4102 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4103 | } |
| 4104 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4105 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4106 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4107 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4108 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4109 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4110 | resolvedDeviceId, motionEvent.getSource(), |
| 4111 | motionEvent.getDisplayId(), policyFlags, action, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4112 | actionButton, flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4113 | motionEvent.getButtonState(), |
| 4114 | motionEvent.getClassification(), |
| 4115 | motionEvent.getEdgeFlags(), |
| 4116 | motionEvent.getXPrecision(), |
| 4117 | motionEvent.getYPrecision(), |
| 4118 | motionEvent.getRawXCursorPosition(), |
| 4119 | motionEvent.getRawYCursorPosition(), |
| 4120 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4121 | pointerProperties, samplePointerCoords, |
| 4122 | motionEvent.getXOffset(), |
| 4123 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4124 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4125 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4126 | sampleEventTimes += 1; |
| 4127 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4128 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4129 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4130 | resolvedDeviceId, motionEvent.getSource(), |
| 4131 | motionEvent.getDisplayId(), policyFlags, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4132 | action, actionButton, flags, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4133 | motionEvent.getMetaState(), |
| 4134 | motionEvent.getButtonState(), |
| 4135 | motionEvent.getClassification(), |
| 4136 | motionEvent.getEdgeFlags(), |
| 4137 | motionEvent.getXPrecision(), |
| 4138 | motionEvent.getYPrecision(), |
| 4139 | motionEvent.getRawXCursorPosition(), |
| 4140 | motionEvent.getRawYCursorPosition(), |
| 4141 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4142 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4143 | samplePointerCoords, motionEvent.getXOffset(), |
| 4144 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4145 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4146 | } |
| 4147 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4148 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4149 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4150 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4151 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4152 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4156 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | injectionState->injectionIsAsync = true; |
| 4158 | } |
| 4159 | |
| 4160 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4161 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | |
| 4163 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4164 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4165 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4166 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | mLock.unlock(); |
| 4170 | |
| 4171 | if (needWake) { |
| 4172 | mLooper->wake(); |
| 4173 | } |
| 4174 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4175 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4177 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4178 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4179 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4180 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4181 | } else { |
| 4182 | for (;;) { |
| 4183 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4184 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4185 | break; |
| 4186 | } |
| 4187 | |
| 4188 | nsecs_t remainingTimeout = endTime - now(); |
| 4189 | if (remainingTimeout <= 0) { |
| 4190 | #if DEBUG_INJECTION |
| 4191 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4192 | "to become available."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4193 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4194 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4195 | break; |
| 4196 | } |
| 4197 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4198 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4199 | } |
| 4200 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4201 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4202 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4203 | while (injectionState->pendingForegroundDispatches != 0) { |
| 4204 | #if DEBUG_INJECTION |
| 4205 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4206 | injectionState->pendingForegroundDispatches); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4207 | #endif |
| 4208 | nsecs_t remainingTimeout = endTime - now(); |
| 4209 | if (remainingTimeout <= 0) { |
| 4210 | #if DEBUG_INJECTION |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4211 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4212 | "dispatches to finish."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4213 | #endif |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4214 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4215 | break; |
| 4216 | } |
| 4217 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4218 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | injectionState->release(); |
| 4224 | } // release lock |
| 4225 | |
| 4226 | #if DEBUG_INJECTION |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4227 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4228 | injectionResult, injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4229 | #endif |
| 4230 | |
| 4231 | return injectionResult; |
| 4232 | } |
| 4233 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4234 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4235 | std::array<uint8_t, 32> calculatedHmac; |
| 4236 | std::unique_ptr<VerifiedInputEvent> result; |
| 4237 | switch (event.getType()) { |
| 4238 | case AINPUT_EVENT_TYPE_KEY: { |
| 4239 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4240 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4241 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4242 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4243 | break; |
| 4244 | } |
| 4245 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4246 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4247 | VerifiedMotionEvent verifiedMotionEvent = |
| 4248 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4249 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4250 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4251 | break; |
| 4252 | } |
| 4253 | default: { |
| 4254 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4255 | return nullptr; |
| 4256 | } |
| 4257 | } |
| 4258 | if (calculatedHmac == INVALID_HMAC) { |
| 4259 | return nullptr; |
| 4260 | } |
| 4261 | if (calculatedHmac != event.getHmac()) { |
| 4262 | return nullptr; |
| 4263 | } |
| 4264 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4265 | } |
| 4266 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4267 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4268 | return injectorUid == 0 || |
| 4269 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4270 | } |
| 4271 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4272 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4273 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4274 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4275 | if (injectionState) { |
| 4276 | #if DEBUG_INJECTION |
| 4277 | ALOGD("Setting input event injection result to %d. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4278 | "injectorPid=%d, injectorUid=%d", |
| 4279 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | #endif |
| 4281 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4282 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | // Log the outcome since the injector did not wait for the injection result. |
| 4284 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4285 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4286 | ALOGV("Asynchronous input event injection succeeded."); |
| 4287 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4288 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4289 | ALOGW("Asynchronous input event injection failed."); |
| 4290 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4291 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4292 | ALOGW("Asynchronous input event injection permission denied."); |
| 4293 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4294 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4295 | ALOGW("Asynchronous input event injection timed out."); |
| 4296 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4297 | case InputEventInjectionResult::PENDING: |
| 4298 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4299 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4300 | } |
| 4301 | } |
| 4302 | |
| 4303 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4304 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4305 | } |
| 4306 | } |
| 4307 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4308 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4309 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | if (injectionState) { |
| 4311 | injectionState->pendingForegroundDispatches += 1; |
| 4312 | } |
| 4313 | } |
| 4314 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4315 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4316 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | if (injectionState) { |
| 4318 | injectionState->pendingForegroundDispatches -= 1; |
| 4319 | |
| 4320 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4321 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4322 | } |
| 4323 | } |
| 4324 | } |
| 4325 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4326 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4327 | int32_t displayId) const { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4328 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4329 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4330 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4331 | } |
| 4332 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4333 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4334 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4335 | if (windowHandleToken == nullptr) { |
| 4336 | return nullptr; |
| 4337 | } |
| 4338 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4339 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4340 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4341 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4342 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4343 | return windowHandle; |
| 4344 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4345 | } |
| 4346 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4347 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | } |
| 4349 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4350 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4351 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4352 | if (windowHandleToken == nullptr) { |
| 4353 | return nullptr; |
| 4354 | } |
| 4355 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4356 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4357 | if (windowHandle->getToken() == windowHandleToken) { |
| 4358 | return windowHandle; |
| 4359 | } |
| 4360 | } |
| 4361 | return nullptr; |
| 4362 | } |
| 4363 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4364 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4365 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4366 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4367 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4368 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4369 | if (handle->getId() == windowHandle->getId() && |
| 4370 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4371 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4372 | ALOGE("Found window %s in display %" PRId32 |
| 4373 | ", but it should belong to display %" PRId32, |
| 4374 | windowHandle->getName().c_str(), it.first, |
| 4375 | windowHandle->getInfo()->displayId); |
| 4376 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4377 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4378 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4379 | } |
| 4380 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4381 | return nullptr; |
| 4382 | } |
| 4383 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4384 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4385 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4386 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | } |
| 4388 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4389 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4390 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4391 | const bool noInputChannel = |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4392 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4393 | if (connection != nullptr && noInputChannel) { |
| 4394 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4395 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4396 | return false; |
| 4397 | } |
| 4398 | |
| 4399 | if (connection == nullptr) { |
| 4400 | if (!noInputChannel) { |
| 4401 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4402 | } |
| 4403 | return false; |
| 4404 | } |
| 4405 | if (!connection->responsive) { |
| 4406 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4407 | return false; |
| 4408 | } |
| 4409 | return true; |
| 4410 | } |
| 4411 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4412 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4413 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4414 | auto connectionIt = mConnectionsByToken.find(token); |
| 4415 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4416 | return nullptr; |
| 4417 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4418 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4419 | } |
| 4420 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4421 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4422 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4423 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4424 | // Remove all handles on a display if there are no windows left. |
| 4425 | mWindowHandlesByDisplay.erase(displayId); |
| 4426 | return; |
| 4427 | } |
| 4428 | |
| 4429 | // Since we compare the pointer of input window handles across window updates, we need |
| 4430 | // to make sure the handle object for the same window stays unchanged across updates. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4431 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4432 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4433 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4434 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4435 | } |
| 4436 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4437 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4438 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4439 | if (!handle->updateInfo()) { |
| 4440 | // handle no longer valid |
| 4441 | continue; |
| 4442 | } |
| 4443 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4444 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4445 | if ((getInputChannelLocked(handle->getToken()) == nullptr && |
| 4446 | info->portalToDisplayId == ADISPLAY_ID_NONE)) { |
| 4447 | const bool noInputChannel = |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4448 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4449 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4450 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4451 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4452 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4453 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4454 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4455 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | if (info->displayId != displayId) { |
| 4459 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4460 | handle->getName().c_str(), displayId, info->displayId); |
| 4461 | continue; |
| 4462 | } |
| 4463 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4464 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4465 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4466 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4467 | oldHandle->updateFrom(handle); |
| 4468 | newHandles.push_back(oldHandle); |
| 4469 | } else { |
| 4470 | newHandles.push_back(handle); |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | // Insert or replace |
| 4475 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4476 | } |
| 4477 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4478 | void InputDispatcher::setInputWindows( |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4479 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4480 | { // acquire lock |
| 4481 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4482 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4483 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4484 | } |
| 4485 | } |
| 4486 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4487 | mLooper->wake(); |
| 4488 | } |
| 4489 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4490 | /** |
| 4491 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4492 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4493 | * If set an empty list, remove all handles from the specific display. |
| 4494 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4495 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4496 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4497 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4498 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4499 | if (DEBUG_FOCUS) { |
| 4500 | std::string windowList; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4501 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4502 | windowList += iwh->getName() + " "; |
| 4503 | } |
| 4504 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4505 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4506 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4507 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4508 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4509 | const bool noInputWindow = |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4510 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4511 | if (noInputWindow && window->getToken() != nullptr) { |
| 4512 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4513 | window->getName().c_str()); |
| 4514 | window->releaseChannel(); |
| 4515 | } |
| 4516 | } |
| 4517 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4518 | // Copy old handles for release if they are no longer present. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4519 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4520 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4521 | // Save the old windows' orientation by ID before it gets updated. |
| 4522 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4523 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4524 | oldWindowOrientations.emplace(handle->getId(), |
| 4525 | handle->getInfo()->transform.getOrientation()); |
| 4526 | } |
| 4527 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4528 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4529 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4530 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4531 | if (mLastHoverWindowHandle && |
| 4532 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4533 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4534 | mLastHoverWindowHandle = nullptr; |
| 4535 | } |
| 4536 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4537 | std::optional<FocusResolver::FocusChanges> changes = |
| 4538 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4539 | if (changes) { |
| 4540 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4542 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4543 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4544 | mTouchStatesByDisplay.find(displayId); |
| 4545 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4546 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4547 | for (size_t i = 0; i < state.windows.size();) { |
| 4548 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4549 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4550 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4551 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4552 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4553 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4554 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4555 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4556 | if (touchedInputChannel != nullptr) { |
| 4557 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4558 | "touched window was removed"); |
| 4559 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4560 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4561 | state.windows.erase(state.windows.begin() + i); |
| 4562 | } else { |
| 4563 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | } |
| 4565 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4566 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4567 | // 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] | 4568 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4569 | if (mDragState && |
| 4570 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4571 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4572 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4573 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4574 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4575 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4576 | if (isPerWindowInputRotationEnabled()) { |
| 4577 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4578 | // pointer events if necessary. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4579 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4580 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4581 | if (newWindowHandle != nullptr && |
| 4582 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4583 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4584 | std::shared_ptr<InputChannel> inputChannel = |
| 4585 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4586 | if (inputChannel != nullptr) { |
| 4587 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4588 | "touched window's orientation changed"); |
| 4589 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4590 | } |
| 4591 | } |
| 4592 | } |
| 4593 | } |
| 4594 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4595 | // Release information for windows that are no longer present. |
| 4596 | // This ensures that unused input channels are released promptly. |
| 4597 | // Otherwise, they might stick around until the window handle is destroyed |
| 4598 | // which might not happen until the next GC. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4599 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4600 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4601 | if (DEBUG_FOCUS) { |
| 4602 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4603 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4604 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4605 | // To avoid making too many calls into the compat framework, only |
| 4606 | // check for window flags when windows are going away. |
| 4607 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4608 | // in order to gather some data about the flag usage |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4609 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4610 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4611 | oldWindowHandle->getName().c_str()); |
| 4612 | if (mCompatService != nullptr) { |
| 4613 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4614 | oldWindowHandle->getInfo()->ownerUid); |
| 4615 | } |
| 4616 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4617 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4618 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4622 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4623 | if (DEBUG_FOCUS) { |
| 4624 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4625 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4626 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4627 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4628 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4629 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4630 | } // release lock |
| 4631 | |
| 4632 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4633 | mLooper->wake(); |
| 4634 | } |
| 4635 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4636 | void InputDispatcher::setFocusedApplicationLocked( |
| 4637 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4638 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4639 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4640 | |
| 4641 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4642 | return; // This application is already focused. No need to wake up or change anything. |
| 4643 | } |
| 4644 | |
| 4645 | // Set the new application handle. |
| 4646 | if (inputApplicationHandle != nullptr) { |
| 4647 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4648 | } else { |
| 4649 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4650 | } |
| 4651 | |
| 4652 | // No matter what the old focused application was, stop waiting on it because it is |
| 4653 | // no longer focused. |
| 4654 | resetNoFocusedWindowTimeoutLocked(); |
| 4655 | } |
| 4656 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4657 | /** |
| 4658 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4659 | * the display not specified. |
| 4660 | * |
| 4661 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4662 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4663 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4664 | * display. The display-specified events won't be affected. |
| 4665 | */ |
| 4666 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4667 | if (DEBUG_FOCUS) { |
| 4668 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4669 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4670 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4671 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4672 | |
| 4673 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4674 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4675 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4676 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4677 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4678 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4679 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4680 | CancelationOptions |
| 4681 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4682 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4683 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4684 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4685 | } |
| 4686 | } |
| 4687 | mFocusedDisplayId = displayId; |
| 4688 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4689 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4690 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4691 | notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4692 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4693 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4694 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4695 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4696 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4697 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | } |
| 4701 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4702 | if (DEBUG_FOCUS) { |
| 4703 | logDispatchStateLocked(); |
| 4704 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4705 | } // release lock |
| 4706 | |
| 4707 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4708 | mLooper->wake(); |
| 4709 | } |
| 4710 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4711 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4712 | if (DEBUG_FOCUS) { |
| 4713 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4714 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4715 | |
| 4716 | bool changed; |
| 4717 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4718 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4719 | |
| 4720 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4721 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4722 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | if (mDispatchEnabled && !enabled) { |
| 4726 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4727 | } |
| 4728 | |
| 4729 | mDispatchEnabled = enabled; |
| 4730 | mDispatchFrozen = frozen; |
| 4731 | changed = true; |
| 4732 | } else { |
| 4733 | changed = false; |
| 4734 | } |
| 4735 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4736 | if (DEBUG_FOCUS) { |
| 4737 | logDispatchStateLocked(); |
| 4738 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4739 | } // release lock |
| 4740 | |
| 4741 | if (changed) { |
| 4742 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4743 | mLooper->wake(); |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4748 | if (DEBUG_FOCUS) { |
| 4749 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4750 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4751 | |
| 4752 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4753 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4754 | |
| 4755 | if (mInputFilterEnabled == enabled) { |
| 4756 | return; |
| 4757 | } |
| 4758 | |
| 4759 | mInputFilterEnabled = enabled; |
| 4760 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4761 | } // release lock |
| 4762 | |
| 4763 | // Wake up poll loop since there might be work to do to drop everything. |
| 4764 | mLooper->wake(); |
| 4765 | } |
| 4766 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4767 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4768 | std::scoped_lock lock(mLock); |
| 4769 | mInTouchMode = inTouchMode; |
| 4770 | } |
| 4771 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4772 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4773 | if (opacity < 0 || opacity > 1) { |
| 4774 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4775 | return; |
| 4776 | } |
| 4777 | |
| 4778 | std::scoped_lock lock(mLock); |
| 4779 | mMaximumObscuringOpacityForTouch = opacity; |
| 4780 | } |
| 4781 | |
| 4782 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4783 | std::scoped_lock lock(mLock); |
| 4784 | mBlockUntrustedTouchesMode = mode; |
| 4785 | } |
| 4786 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4787 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4788 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4789 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4790 | if (DEBUG_FOCUS) { |
| 4791 | ALOGD("Trivial transfer to same window."); |
| 4792 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4793 | return true; |
| 4794 | } |
| 4795 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4796 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4797 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4798 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4799 | sp<WindowInfoHandle> fromWindowHandle = getWindowHandleLocked(fromToken); |
| 4800 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4801 | if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4802 | ALOGW("Cannot transfer focus because from or to window not found."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4803 | return false; |
| 4804 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4805 | if (DEBUG_FOCUS) { |
| 4806 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
| 4807 | fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); |
| 4808 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4809 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4810 | if (DEBUG_FOCUS) { |
| 4811 | ALOGD("Cannot transfer focus because windows are on different displays."); |
| 4812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4813 | return false; |
| 4814 | } |
| 4815 | |
| 4816 | bool found = false; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4817 | for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4818 | TouchState& state = pair.second; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4819 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4820 | const TouchedWindow& touchedWindow = state.windows[i]; |
| 4821 | if (touchedWindow.windowHandle == fromWindowHandle) { |
| 4822 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 4823 | BitSet32 pointerIds = touchedWindow.pointerIds; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4824 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4825 | state.windows.erase(state.windows.begin() + i); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4826 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4827 | int32_t newTargetFlags = oldTargetFlags & |
| 4828 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4829 | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4830 | state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4831 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4832 | // Store the dragging window. |
| 4833 | if (isDragDrop) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4834 | mDragState = std::make_unique<DragState>(toWindowHandle); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4835 | } |
| 4836 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4837 | found = true; |
| 4838 | goto Found; |
| 4839 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4840 | } |
| 4841 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4842 | Found: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4843 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4844 | if (!found) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4845 | if (DEBUG_FOCUS) { |
| 4846 | ALOGD("Focus transfer failed because from window did not have focus."); |
| 4847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 | return false; |
| 4849 | } |
| 4850 | |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4851 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4852 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4853 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4854 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4855 | CancelationOptions |
| 4856 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4857 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4859 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 | } |
| 4861 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4862 | if (DEBUG_FOCUS) { |
| 4863 | logDispatchStateLocked(); |
| 4864 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4865 | } // release lock |
| 4866 | |
| 4867 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4868 | mLooper->wake(); |
| 4869 | return true; |
| 4870 | } |
| 4871 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4872 | // Binder call |
| 4873 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 4874 | sp<IBinder> fromToken; |
| 4875 | { // acquire lock |
| 4876 | std::scoped_lock _l(mLock); |
| 4877 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4878 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4879 | if (toWindowHandle == nullptr) { |
| 4880 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 4881 | return false; |
| 4882 | } |
| 4883 | |
| 4884 | const int32_t displayId = toWindowHandle->getInfo()->displayId; |
| 4885 | |
| 4886 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 4887 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 4888 | ALOGD("Could not transfer touch because the display %" PRId32 " is not being touched", |
| 4889 | displayId); |
| 4890 | return false; |
| 4891 | } |
| 4892 | |
| 4893 | TouchState& state = touchStateIt->second; |
| 4894 | if (state.windows.size() != 1) { |
| 4895 | ALOGW("Cannot transfer touch state because there are %zu windows being touched", |
| 4896 | state.windows.size()); |
| 4897 | return false; |
| 4898 | } |
| 4899 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 4900 | fromToken = touchedWindow.windowHandle->getToken(); |
| 4901 | } // release lock |
| 4902 | |
| 4903 | return transferTouchFocus(fromToken, destChannelToken); |
| 4904 | } |
| 4905 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4906 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4907 | if (DEBUG_FOCUS) { |
| 4908 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 4909 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 | |
| 4911 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 4912 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 4913 | |
| 4914 | resetKeyRepeatLocked(); |
| 4915 | releasePendingEventLocked(); |
| 4916 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4917 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4918 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4919 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4920 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4921 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4922 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4926 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4927 | dumpDispatchStateLocked(dump); |
| 4928 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4929 | std::istringstream stream(dump); |
| 4930 | std::string line; |
| 4931 | |
| 4932 | while (std::getline(stream, line, '\n')) { |
| 4933 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4934 | } |
| 4935 | } |
| 4936 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4937 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 4938 | std::string dump; |
| 4939 | |
| 4940 | dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", |
| 4941 | toString(mFocusedWindowRequestedPointerCapture)); |
| 4942 | |
| 4943 | std::string windowName = "None"; |
| 4944 | if (mWindowTokenWithPointerCapture) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4945 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4946 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 4947 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 4948 | : "token has capture without window"; |
| 4949 | } |
| 4950 | dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); |
| 4951 | |
| 4952 | return dump; |
| 4953 | } |
| 4954 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4955 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4956 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 4957 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 4958 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4959 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4960 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4961 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 4962 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 4963 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 4964 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4965 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4966 | const std::chrono::duration timeout = |
| 4967 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4968 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4969 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4970 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4971 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4972 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4973 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4974 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4975 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4976 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4977 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4978 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4979 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4980 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4981 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 4982 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4983 | 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] | 4984 | state.displayId, toString(state.down), toString(state.split), |
| 4985 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4986 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4987 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4988 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 4989 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4990 | dump += StringPrintf(INDENT4 |
| 4991 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 4992 | i, touchedWindow.windowHandle->getName().c_str(), |
| 4993 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4994 | } |
| 4995 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4996 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4997 | } |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4998 | if (!state.portalWindows.empty()) { |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4999 | dump += INDENT3 "Portal windows:\n"; |
| 5000 | for (size_t i = 0; i < state.portalWindows.size(); i++) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5001 | const sp<WindowInfoHandle> portalWindowHandle = state.portalWindows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5002 | dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, |
| 5003 | portalWindowHandle->getName().c_str()); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 5004 | } |
| 5005 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5006 | } |
| 5007 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5008 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5009 | } |
| 5010 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5011 | if (mDragState) { |
| 5012 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5013 | mDragState->dump(dump, INDENT2); |
| 5014 | } |
| 5015 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5016 | if (!mWindowHandlesByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5017 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5018 | const std::vector<sp<WindowInfoHandle>> windowHandles = it.second; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5019 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5020 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5021 | dump += INDENT2 "Windows:\n"; |
| 5022 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5023 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5024 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5025 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5026 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5027 | "portalToDisplayId=%d, paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5028 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5029 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5030 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5031 | "applicationInfo.name=%s, " |
| 5032 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5033 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5034 | i, windowInfo->name.c_str(), windowInfo->id, |
| 5035 | windowInfo->displayId, windowInfo->portalToDisplayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5036 | toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5037 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5038 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5039 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5040 | windowInfo->flags.string().c_str(), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5041 | NamedEnum::string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5042 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5043 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5044 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5045 | windowInfo->applicationInfo.name.c_str(), |
| 5046 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5047 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5048 | dump += StringPrintf(", inputFeatures=%s", |
| 5049 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5050 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5051 | "ms, trustedOverlay=%s, hasToken=%s, " |
| 5052 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5053 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5054 | millis(windowInfo->dispatchingTimeout), |
| 5055 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5056 | toString(windowInfo->token != nullptr), |
| 5057 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5058 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5059 | } |
| 5060 | } else { |
| 5061 | dump += INDENT2 "Windows: <none>\n"; |
| 5062 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5063 | } |
| 5064 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5065 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5066 | } |
| 5067 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5068 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5069 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5070 | const std::vector<Monitor>& monitors = it.second; |
| 5071 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5072 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5073 | } |
| 5074 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5075 | const std::vector<Monitor>& monitors = it.second; |
| 5076 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5077 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5078 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5079 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5080 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | nsecs_t currentTime = now(); |
| 5084 | |
| 5085 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5086 | if (!mRecentQueue.empty()) { |
| 5087 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5088 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5089 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5090 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5091 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5092 | } |
| 5093 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5094 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | // Dump event currently being dispatched. |
| 5098 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5099 | dump += INDENT "PendingEvent:\n"; |
| 5100 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5101 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5102 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5103 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5104 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5105 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5109 | if (!mInboundQueue.empty()) { |
| 5110 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5111 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5112 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5113 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5114 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5115 | } |
| 5116 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5117 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5118 | } |
| 5119 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5120 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5121 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5122 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5123 | const KeyReplacement& replacement = pair.first; |
| 5124 | int32_t newKeyCode = pair.second; |
| 5125 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5126 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5127 | } |
| 5128 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5129 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5132 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5133 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5134 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5135 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5136 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5137 | connection->inputChannel->getFd().get(), |
| 5138 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5139 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5140 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5141 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5142 | if (!connection->outboundQueue.empty()) { |
| 5143 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5144 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5145 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5146 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5147 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5148 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5149 | } |
| 5150 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5151 | if (!connection->waitQueue.empty()) { |
| 5152 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5153 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5154 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5155 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5156 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | } |
| 5158 | } |
| 5159 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5160 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5164 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5165 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5166 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5167 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5170 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5171 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5172 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5173 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5174 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5175 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | } |
| 5177 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5178 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5179 | const size_t numMonitors = monitors.size(); |
| 5180 | for (size_t i = 0; i < numMonitors; i++) { |
| 5181 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5182 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5183 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5184 | dump += "\n"; |
| 5185 | } |
| 5186 | } |
| 5187 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5188 | class LooperEventCallback : public LooperCallback { |
| 5189 | public: |
| 5190 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5191 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5192 | |
| 5193 | private: |
| 5194 | std::function<int(int events)> mCallback; |
| 5195 | }; |
| 5196 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5197 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5198 | #if DEBUG_CHANNEL_CREATION |
| 5199 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | #endif |
| 5201 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5202 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5203 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5204 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5205 | |
| 5206 | if (result) { |
| 5207 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5208 | } |
| 5209 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5210 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5211 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5212 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5213 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5214 | sp<Connection> connection = |
| 5215 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5216 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5217 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5218 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5219 | } |
| 5220 | mConnectionsByToken.emplace(token, connection); |
| 5221 | |
| 5222 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5223 | this, std::placeholders::_1, token); |
| 5224 | |
| 5225 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5226 | } // release lock |
| 5227 | |
| 5228 | // Wake the looper because some connections have changed. |
| 5229 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5230 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5231 | } |
| 5232 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5233 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5234 | bool isGestureMonitor, |
| 5235 | const std::string& name, |
| 5236 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5237 | std::shared_ptr<InputChannel> serverChannel; |
| 5238 | std::unique_ptr<InputChannel> clientChannel; |
| 5239 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5240 | if (result) { |
| 5241 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5242 | } |
| 5243 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5244 | { // acquire lock |
| 5245 | std::scoped_lock _l(mLock); |
| 5246 | |
| 5247 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5248 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5249 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5252 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5253 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5254 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5255 | |
| 5256 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5257 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5258 | } |
| 5259 | mConnectionsByToken.emplace(token, connection); |
| 5260 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5261 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5262 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5263 | auto& monitorsByDisplay = |
| 5264 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5265 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5266 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5267 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5268 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5269 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5270 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5271 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5272 | // Wake the looper because some connections have changed. |
| 5273 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5274 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5277 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5278 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5279 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5280 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5281 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | if (status) { |
| 5283 | return status; |
| 5284 | } |
| 5285 | } // release lock |
| 5286 | |
| 5287 | // Wake the poll loop because removing the connection may have changed the current |
| 5288 | // synchronization state. |
| 5289 | mLooper->wake(); |
| 5290 | return OK; |
| 5291 | } |
| 5292 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5293 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5294 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5295 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5296 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5297 | // 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] | 5298 | return BAD_VALUE; |
| 5299 | } |
| 5300 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5301 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5302 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5303 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5304 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5305 | } |
| 5306 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5307 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | |
| 5309 | nsecs_t currentTime = now(); |
| 5310 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5311 | |
| 5312 | connection->status = Connection::STATUS_ZOMBIE; |
| 5313 | return OK; |
| 5314 | } |
| 5315 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5316 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5317 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5318 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5321 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5322 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5323 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5324 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5325 | std::vector<Monitor>& monitors = it->second; |
| 5326 | const size_t numMonitors = monitors.size(); |
| 5327 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5328 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5329 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5330 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5331 | monitors.erase(monitors.begin() + i); |
| 5332 | break; |
| 5333 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5334 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5335 | if (monitors.empty()) { |
| 5336 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5337 | } else { |
| 5338 | ++it; |
| 5339 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5340 | } |
| 5341 | } |
| 5342 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5343 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5344 | { // acquire lock |
| 5345 | std::scoped_lock _l(mLock); |
| 5346 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5347 | |
| 5348 | if (!foundDisplayId) { |
| 5349 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5350 | return BAD_VALUE; |
| 5351 | } |
| 5352 | int32_t displayId = foundDisplayId.value(); |
| 5353 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5354 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5355 | mTouchStatesByDisplay.find(displayId); |
| 5356 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5357 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5358 | return BAD_VALUE; |
| 5359 | } |
| 5360 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5361 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5362 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5363 | std::optional<int32_t> foundDeviceId; |
| 5364 | for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5365 | if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5366 | requestingChannel = touchedMonitor.monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5367 | foundDeviceId = state.deviceId; |
| 5368 | } |
| 5369 | } |
| 5370 | if (!foundDeviceId || !state.down) { |
| 5371 | 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] | 5372 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5373 | return BAD_VALUE; |
| 5374 | } |
| 5375 | int32_t deviceId = foundDeviceId.value(); |
| 5376 | |
| 5377 | // Send cancel events to all the input channels we're stealing from. |
| 5378 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5379 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5380 | options.deviceId = deviceId; |
| 5381 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5382 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5383 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5384 | std::shared_ptr<InputChannel> channel = |
| 5385 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5386 | if (channel != nullptr) { |
| 5387 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5388 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5389 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5390 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5391 | canceledWindows += "]"; |
| 5392 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5393 | canceledWindows.c_str()); |
| 5394 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5395 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5396 | state.filterNonMonitors(); |
| 5397 | } |
| 5398 | return OK; |
| 5399 | } |
| 5400 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5401 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5402 | { // acquire lock |
| 5403 | std::scoped_lock _l(mLock); |
| 5404 | if (DEBUG_FOCUS) { |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5405 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5406 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5407 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5408 | : "token without window"); |
| 5409 | } |
| 5410 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5411 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5412 | if (focusedToken != windowToken) { |
| 5413 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5414 | enabled ? "enable" : "disable"); |
| 5415 | return; |
| 5416 | } |
| 5417 | |
| 5418 | if (enabled == mFocusedWindowRequestedPointerCapture) { |
| 5419 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5420 | "window has %s requested pointer capture.", |
| 5421 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5422 | return; |
| 5423 | } |
| 5424 | |
| 5425 | mFocusedWindowRequestedPointerCapture = enabled; |
| 5426 | setPointerCaptureLocked(enabled); |
| 5427 | } // release lock |
| 5428 | |
| 5429 | // Wake the thread to process command entries. |
| 5430 | mLooper->wake(); |
| 5431 | } |
| 5432 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5433 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5434 | const sp<IBinder>& token) { |
| 5435 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5436 | const std::vector<Monitor>& monitors = it.second; |
| 5437 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5438 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5439 | return it.first; |
| 5440 | } |
| 5441 | } |
| 5442 | } |
| 5443 | return std::nullopt; |
| 5444 | } |
| 5445 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5446 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5447 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5448 | if (gesturePid.has_value()) { |
| 5449 | return gesturePid; |
| 5450 | } |
| 5451 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5452 | } |
| 5453 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5454 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5455 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5456 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5457 | } |
| 5458 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5459 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5460 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5461 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5462 | } |
| 5463 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5464 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5465 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5466 | } |
| 5467 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5468 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5469 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5470 | if (connection == nullptr) { |
| 5471 | return "<nullptr>"; |
| 5472 | } |
| 5473 | return connection->getInputChannelName(); |
| 5474 | } |
| 5475 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5476 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5477 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5478 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5479 | } |
| 5480 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5481 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, |
| 5482 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5483 | bool handled, nsecs_t consumeTime) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5484 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5485 | &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5486 | commandEntry->connection = connection; |
| 5487 | commandEntry->eventTime = currentTime; |
| 5488 | commandEntry->seq = seq; |
| 5489 | commandEntry->handled = handled; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 5490 | commandEntry->consumeTime = consumeTime; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5491 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5492 | } |
| 5493 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5494 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, |
| 5495 | const sp<Connection>& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5496 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5497 | connection->getInputChannelName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5498 | |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5499 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5500 | &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5501 | commandEntry->connection = connection; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5502 | postCommandLocked(std::move(commandEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5503 | } |
| 5504 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5505 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, |
| 5506 | const sp<IBinder>& newToken) { |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5507 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5508 | &InputDispatcher::doNotifyFocusChangedLockedInterruptible); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5509 | commandEntry->oldToken = oldToken; |
| 5510 | commandEntry->newToken = newToken; |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5511 | postCommandLocked(std::move(commandEntry)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5512 | } |
| 5513 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5514 | void InputDispatcher::notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) { |
| 5515 | std::unique_ptr<CommandEntry> commandEntry = |
| 5516 | std::make_unique<CommandEntry>(&InputDispatcher::doNotifyDropWindowLockedInterruptible); |
| 5517 | commandEntry->newToken = token; |
| 5518 | commandEntry->x = x; |
| 5519 | commandEntry->y = y; |
| 5520 | postCommandLocked(std::move(commandEntry)); |
| 5521 | } |
| 5522 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5523 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5524 | if (connection == nullptr) { |
| 5525 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5526 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5527 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5528 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5529 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5530 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5531 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5532 | return; |
| 5533 | } |
| 5534 | /** |
| 5535 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5536 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5537 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5538 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5539 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5540 | * most useful. |
| 5541 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5542 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5543 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5544 | std::string reason = |
| 5545 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5546 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5547 | ns2ms(currentWait), |
| 5548 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5549 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5550 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5551 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5552 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5553 | |
| 5554 | // Stop waking up for events on this connection, it is already unresponsive |
| 5555 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5556 | } |
| 5557 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5558 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5559 | std::string reason = |
| 5560 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5561 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5562 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5563 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5564 | &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); |
| 5565 | commandEntry->inputApplicationHandle = std::move(application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5566 | postCommandLocked(std::move(commandEntry)); |
| 5567 | } |
| 5568 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5569 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { |
| 5570 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 5571 | &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); |
| 5572 | commandEntry->obscuringPackage = obscuringPackage; |
| 5573 | postCommandLocked(std::move(commandEntry)); |
| 5574 | } |
| 5575 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5576 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5577 | const std::string& reason) { |
| 5578 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5579 | updateLastAnrStateLocked(windowLabel, reason); |
| 5580 | } |
| 5581 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5582 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5583 | const std::string& reason) { |
| 5584 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5585 | updateLastAnrStateLocked(windowLabel, reason); |
| 5586 | } |
| 5587 | |
| 5588 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5589 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5590 | // 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] | 5591 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5592 | struct tm tm; |
| 5593 | localtime_r(&t, &tm); |
| 5594 | char timestr[64]; |
| 5595 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5596 | mLastAnrState.clear(); |
| 5597 | mLastAnrState += INDENT "ANR:\n"; |
| 5598 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5599 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5600 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5601 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5602 | } |
| 5603 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5604 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5605 | mLock.unlock(); |
| 5606 | |
| 5607 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 5608 | |
| 5609 | mLock.lock(); |
| 5610 | } |
| 5611 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5612 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5613 | sp<Connection> connection = commandEntry->connection; |
| 5614 | |
| 5615 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 5616 | mLock.unlock(); |
| 5617 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5618 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5619 | |
| 5620 | mLock.lock(); |
| 5621 | } |
| 5622 | } |
| 5623 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5624 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5625 | sp<IBinder> oldToken = commandEntry->oldToken; |
| 5626 | sp<IBinder> newToken = commandEntry->newToken; |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5627 | mLock.unlock(); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5628 | mPolicy->notifyFocusChanged(oldToken, newToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5629 | mLock.lock(); |
| 5630 | } |
| 5631 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5632 | void InputDispatcher::doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) { |
| 5633 | sp<IBinder> newToken = commandEntry->newToken; |
| 5634 | mLock.unlock(); |
| 5635 | mPolicy->notifyDropWindow(newToken, commandEntry->x, commandEntry->y); |
| 5636 | mLock.lock(); |
| 5637 | } |
| 5638 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5639 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5640 | mLock.unlock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5641 | |
| 5642 | mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); |
| 5643 | |
| 5644 | mLock.lock(); |
| 5645 | } |
| 5646 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5647 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5648 | mLock.unlock(); |
| 5649 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5650 | mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5651 | |
| 5652 | mLock.lock(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5653 | } |
| 5654 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5655 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5656 | mLock.unlock(); |
| 5657 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5658 | mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); |
| 5659 | |
| 5660 | mLock.lock(); |
| 5661 | } |
| 5662 | |
| 5663 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5664 | mLock.unlock(); |
| 5665 | |
| 5666 | mPolicy->notifyWindowResponsive(commandEntry->connectionToken); |
| 5667 | |
| 5668 | mLock.lock(); |
| 5669 | } |
| 5670 | |
| 5671 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { |
| 5672 | mLock.unlock(); |
| 5673 | |
| 5674 | mPolicy->notifyMonitorResponsive(commandEntry->pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5675 | |
| 5676 | mLock.lock(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5677 | } |
| 5678 | |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5679 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { |
| 5680 | mLock.unlock(); |
| 5681 | |
| 5682 | mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); |
| 5683 | |
| 5684 | mLock.lock(); |
| 5685 | } |
| 5686 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5687 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 5688 | CommandEntry* commandEntry) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5689 | KeyEntry& entry = *(commandEntry->keyEntry); |
| 5690 | KeyEvent event = createKeyEvent(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5691 | |
| 5692 | mLock.unlock(); |
| 5693 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5694 | android::base::Timer t; |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5695 | const sp<IBinder>& token = commandEntry->connectionToken; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5696 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5697 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5698 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5699 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5700 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5701 | |
| 5702 | mLock.lock(); |
| 5703 | |
| 5704 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5705 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5706 | } else if (!delay) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5707 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5708 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5709 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5710 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5711 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5712 | } |
| 5713 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5714 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { |
| 5715 | mLock.unlock(); |
| 5716 | mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); |
| 5717 | mLock.lock(); |
| 5718 | } |
| 5719 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5720 | /** |
| 5721 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 5722 | * current time. |
| 5723 | */ |
| 5724 | static bool isConnectionResponsive(const Connection& connection) { |
| 5725 | const nsecs_t currentTime = now(); |
| 5726 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 5727 | if (entry->timeoutTime < currentTime) { |
| 5728 | return false; |
| 5729 | } |
| 5730 | } |
| 5731 | return true; |
| 5732 | } |
| 5733 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5734 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5735 | sp<Connection> connection = commandEntry->connection; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5736 | const nsecs_t finishTime = commandEntry->eventTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5737 | uint32_t seq = commandEntry->seq; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5738 | const bool handled = commandEntry->handled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5739 | |
| 5740 | // Handle post-event policy actions. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5741 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5742 | if (dispatchEntryIt == connection->waitQueue.end()) { |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5743 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5744 | } |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5745 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5746 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5747 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5748 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5749 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5750 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5751 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5752 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5753 | connection->inputChannel->getConnectionToken(), |
| 5754 | dispatchEntry->deliveryTime, commandEntry->consumeTime, |
| 5755 | finishTime); |
| 5756 | } |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5757 | |
| 5758 | bool restartEvent; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5759 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5760 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5761 | restartEvent = |
| 5762 | afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5763 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5764 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5765 | restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, |
| 5766 | handled); |
| 5767 | } else { |
| 5768 | restartEvent = false; |
| 5769 | } |
| 5770 | |
| 5771 | // Dequeue the event and start the next cycle. |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5772 | // Because the lock might have been released, it is possible that the |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5773 | // contents of the wait queue to have been drained, so we need to double-check |
| 5774 | // a few things. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5775 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5776 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5777 | dispatchEntry = *dispatchEntryIt; |
| 5778 | connection->waitQueue.erase(dispatchEntryIt); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5779 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5780 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5781 | if (!connection->responsive) { |
| 5782 | connection->responsive = isConnectionResponsive(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5783 | if (connection->responsive) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5784 | // The connection was unresponsive, and now it's responsive. |
| 5785 | processConnectionResponsiveLocked(*connection); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5786 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5787 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5788 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5789 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5790 | connection->outboundQueue.push_front(dispatchEntry); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 5791 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5792 | } else { |
| 5793 | releaseDispatchEntry(dispatchEntry); |
| 5794 | } |
| 5795 | } |
| 5796 | |
| 5797 | // Start the next dispatch cycle for this connection. |
| 5798 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5799 | } |
| 5800 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5801 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
| 5802 | std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5803 | &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); |
| 5804 | monitorUnresponsiveCommand->pid = pid; |
| 5805 | monitorUnresponsiveCommand->reason = std::move(reason); |
| 5806 | postCommandLocked(std::move(monitorUnresponsiveCommand)); |
| 5807 | } |
| 5808 | |
| 5809 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, |
| 5810 | std::string reason) { |
| 5811 | std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( |
| 5812 | &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); |
| 5813 | windowUnresponsiveCommand->connectionToken = std::move(connectionToken); |
| 5814 | windowUnresponsiveCommand->reason = std::move(reason); |
| 5815 | postCommandLocked(std::move(windowUnresponsiveCommand)); |
| 5816 | } |
| 5817 | |
| 5818 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
| 5819 | std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( |
| 5820 | &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); |
| 5821 | monitorResponsiveCommand->pid = pid; |
| 5822 | postCommandLocked(std::move(monitorResponsiveCommand)); |
| 5823 | } |
| 5824 | |
| 5825 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { |
| 5826 | std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( |
| 5827 | &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); |
| 5828 | windowResponsiveCommand->connectionToken = std::move(connectionToken); |
| 5829 | postCommandLocked(std::move(windowResponsiveCommand)); |
| 5830 | } |
| 5831 | |
| 5832 | /** |
| 5833 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5834 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5835 | * command entry to the command queue. |
| 5836 | */ |
| 5837 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5838 | std::string reason) { |
| 5839 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5840 | if (connection.monitor) { |
| 5841 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5842 | reason.c_str()); |
| 5843 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5844 | if (!pid.has_value()) { |
| 5845 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5846 | connection.inputChannel->getName().c_str()); |
| 5847 | return; |
| 5848 | } |
| 5849 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5850 | return; |
| 5851 | } |
| 5852 | // If not a monitor, must be a window |
| 5853 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5854 | reason.c_str()); |
| 5855 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5856 | } |
| 5857 | |
| 5858 | /** |
| 5859 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5860 | */ |
| 5861 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5862 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5863 | if (connection.monitor) { |
| 5864 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5865 | if (!pid.has_value()) { |
| 5866 | ALOGE("Could not find responsive monitor for connection %s", |
| 5867 | connection.inputChannel->getName().c_str()); |
| 5868 | return; |
| 5869 | } |
| 5870 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5871 | return; |
| 5872 | } |
| 5873 | // If not a monitor, must be a window |
| 5874 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5875 | } |
| 5876 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5877 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5878 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5879 | KeyEntry& keyEntry, bool handled) { |
| 5880 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5881 | if (!handled) { |
| 5882 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5883 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5884 | } |
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 | // Get the fallback key state. |
| 5889 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5890 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5891 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5892 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5893 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5894 | } |
| 5895 | |
| 5896 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5897 | // If the application handles the original key for which we previously |
| 5898 | // generated a fallback or if the window is not a foreground window, |
| 5899 | // then cancel the associated fallback key, if any. |
| 5900 | if (fallbackKeyCode != -1) { |
| 5901 | // Dispatch the unhandled key to the policy with the cancel flag. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5902 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5903 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5904 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5905 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5906 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5907 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5908 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5909 | |
| 5910 | mLock.unlock(); |
| 5911 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5912 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5913 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5914 | |
| 5915 | mLock.lock(); |
| 5916 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5917 | // Cancel the fallback key. |
| 5918 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5919 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5920 | "application handled the original non-fallback key " |
| 5921 | "or is no longer a foreground target, " |
| 5922 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5923 | options.keyCode = fallbackKeyCode; |
| 5924 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5925 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5926 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5927 | } |
| 5928 | } else { |
| 5929 | // If the application did not handle a non-fallback key, first check |
| 5930 | // that we are in a good state to perform unhandled key event processing |
| 5931 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5932 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5933 | if (fallbackKeyCode == -1 && !initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5934 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5935 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5936 | "since this is not an initial down. " |
| 5937 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5938 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5939 | #endif |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5940 | return false; |
| 5941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5942 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5943 | // Dispatch the unhandled key to the policy. |
| 5944 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5945 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5946 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5947 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5948 | #endif |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5949 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5950 | |
| 5951 | mLock.unlock(); |
| 5952 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5953 | bool fallback = |
| 5954 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5955 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5956 | |
| 5957 | mLock.lock(); |
| 5958 | |
| 5959 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5960 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5961 | return false; |
| 5962 | } |
| 5963 | |
| 5964 | // Latch the fallback keycode for this key on an initial down. |
| 5965 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5966 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5967 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5968 | fallbackKeyCode = event.getKeyCode(); |
| 5969 | } else { |
| 5970 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5971 | } |
| 5972 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5973 | } |
| 5974 | |
| 5975 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5976 | |
| 5977 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5978 | // We will continue to dispatch the key to the policy but we will no |
| 5979 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5980 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5981 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5982 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 5983 | if (fallback) { |
| 5984 | ALOGD("Unhandled key event: Policy requested to send key %d" |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5985 | "as a fallback for %d, but on the DOWN it had requested " |
| 5986 | "to send %d instead. Fallback canceled.", |
| 5987 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5988 | } else { |
| 5989 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5990 | "but on the DOWN it had requested to send %d. " |
| 5991 | "Fallback canceled.", |
| 5992 | originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5993 | } |
| 5994 | #endif |
| 5995 | |
| 5996 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5997 | "canceling fallback, policy no longer desires it"); |
| 5998 | options.keyCode = fallbackKeyCode; |
| 5999 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6000 | |
| 6001 | fallback = false; |
| 6002 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6003 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6004 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6005 | } |
| 6006 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6007 | |
| 6008 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6009 | { |
| 6010 | std::string msg; |
| 6011 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6012 | connection->inputState.getFallbackKeys(); |
| 6013 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6014 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6015 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6016 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6017 | fallbackKeys.size(), msg.c_str()); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6018 | } |
| 6019 | #endif |
| 6020 | |
| 6021 | if (fallback) { |
| 6022 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6023 | keyEntry.eventTime = event.getEventTime(); |
| 6024 | keyEntry.deviceId = event.getDeviceId(); |
| 6025 | keyEntry.source = event.getSource(); |
| 6026 | keyEntry.displayId = event.getDisplayId(); |
| 6027 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6028 | keyEntry.keyCode = fallbackKeyCode; |
| 6029 | keyEntry.scanCode = event.getScanCode(); |
| 6030 | keyEntry.metaState = event.getMetaState(); |
| 6031 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6032 | keyEntry.downTime = event.getDownTime(); |
| 6033 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6034 | |
| 6035 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 6036 | ALOGD("Unhandled key event: Dispatching fallback key. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6037 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6038 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6039 | #endif |
| 6040 | return true; // restart the event |
| 6041 | } else { |
| 6042 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 6043 | ALOGD("Unhandled key event: No fallback key."); |
| 6044 | #endif |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6045 | |
| 6046 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6047 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6048 | } |
| 6049 | } |
| 6050 | return false; |
| 6051 | } |
| 6052 | |
| 6053 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6054 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6055 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6056 | return false; |
| 6057 | } |
| 6058 | |
| 6059 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 6060 | mLock.unlock(); |
| 6061 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 6062 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType, |
| 6063 | commandEntry->displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6064 | |
| 6065 | mLock.lock(); |
| 6066 | } |
| 6067 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6068 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6069 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6070 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6071 | } |
| 6072 | } |
| 6073 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6074 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6075 | if (ATRACE_ENABLED()) { |
| 6076 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6077 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6078 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6079 | } |
| 6080 | } |
| 6081 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6082 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6083 | if (ATRACE_ENABLED()) { |
| 6084 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6085 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6086 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6087 | } |
| 6088 | } |
| 6089 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6090 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6091 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6092 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6093 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6094 | dumpDispatchStateLocked(dump); |
| 6095 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6096 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6097 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6098 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6099 | } |
| 6100 | } |
| 6101 | |
| 6102 | void InputDispatcher::monitor() { |
| 6103 | // 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] | 6104 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6105 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6106 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6107 | } |
| 6108 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6109 | /** |
| 6110 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6111 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6112 | * this method can be safely called from any thread, as long as you've ensured that |
| 6113 | * the work you are interested in completing has already been queued. |
| 6114 | */ |
| 6115 | bool InputDispatcher::waitForIdle() { |
| 6116 | /** |
| 6117 | * Timeout should represent the longest possible time that a device might spend processing |
| 6118 | * events and commands. |
| 6119 | */ |
| 6120 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6121 | std::unique_lock lock(mLock); |
| 6122 | mLooper->wake(); |
| 6123 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6124 | return result == std::cv_status::no_timeout; |
| 6125 | } |
| 6126 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6127 | /** |
| 6128 | * Sets focus to the window identified by the token. This must be called |
| 6129 | * after updating any input window handles. |
| 6130 | * |
| 6131 | * Params: |
| 6132 | * request.token - input channel token used to identify the window that should gain focus. |
| 6133 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6134 | * specified token does not match the currently focused window, this request will be dropped. |
| 6135 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6136 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6137 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6138 | * when requesting the focus change. This determines which request gets |
| 6139 | * precedence if there is a focus change request from another source such as pointer down. |
| 6140 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6141 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6142 | { // acquire lock |
| 6143 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6144 | std::optional<FocusResolver::FocusChanges> changes = |
| 6145 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6146 | if (changes) { |
| 6147 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6148 | } |
| 6149 | } // release lock |
| 6150 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6151 | mLooper->wake(); |
| 6152 | } |
| 6153 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6154 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6155 | if (changes.oldFocus) { |
| 6156 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6157 | if (focusedInputChannel) { |
| 6158 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6159 | "focus left window"); |
| 6160 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6161 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6162 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6163 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6164 | if (changes.newFocus) { |
| 6165 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6166 | } |
| 6167 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6168 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6169 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6170 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6171 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6172 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6173 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6174 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6175 | // front. |
| 6176 | disablePointerCaptureForcedLocked(); |
| 6177 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6178 | if (mFocusedDisplayId == changes.displayId) { |
| 6179 | notifyFocusChangedLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6180 | } |
| 6181 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6182 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6183 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
| 6184 | if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { |
| 6185 | return; |
| 6186 | } |
| 6187 | |
| 6188 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6189 | |
| 6190 | if (mFocusedWindowRequestedPointerCapture) { |
| 6191 | mFocusedWindowRequestedPointerCapture = false; |
| 6192 | setPointerCaptureLocked(false); |
| 6193 | } |
| 6194 | |
| 6195 | if (!mWindowTokenWithPointerCapture) { |
| 6196 | // No need to send capture changes because no window has capture. |
| 6197 | return; |
| 6198 | } |
| 6199 | |
| 6200 | if (mPendingEvent != nullptr) { |
| 6201 | // Move the pending event to the front of the queue. This will give the chance |
| 6202 | // for the pending event to be dropped if it is a captured event. |
| 6203 | mInboundQueue.push_front(mPendingEvent); |
| 6204 | mPendingEvent = nullptr; |
| 6205 | } |
| 6206 | |
| 6207 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
| 6208 | false /* hasCapture */); |
| 6209 | mInboundQueue.push_front(std::move(entry)); |
| 6210 | } |
| 6211 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6212 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { |
| 6213 | std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( |
| 6214 | &InputDispatcher::doSetPointerCaptureLockedInterruptible); |
| 6215 | commandEntry->enabled = enabled; |
| 6216 | postCommandLocked(std::move(commandEntry)); |
| 6217 | } |
| 6218 | |
| 6219 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( |
| 6220 | android::inputdispatcher::CommandEntry* commandEntry) { |
| 6221 | mLock.unlock(); |
| 6222 | |
| 6223 | mPolicy->setPointerCapture(commandEntry->enabled); |
| 6224 | |
| 6225 | mLock.lock(); |
| 6226 | } |
| 6227 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6228 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6229 | { // acquire lock |
| 6230 | std::scoped_lock _l(mLock); |
| 6231 | // Set an empty list to remove all handles from the specific display. |
| 6232 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6233 | setFocusedApplicationLocked(displayId, nullptr); |
| 6234 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6235 | // have been removed for the removed display. |
| 6236 | mFocusResolver.displayRemoved(displayId); |
| 6237 | } // release lock |
| 6238 | |
| 6239 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6240 | mLooper->wake(); |
| 6241 | } |
| 6242 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6243 | } // namespace android::inputdispatcher |